Skip to content

a.new_tensor([num]) should not introduce a graph break #176067

@haoliu3

Description

@haoliu3

🐛 Describe the bug

For this simple case, f2 has a break while f1 doesn't:

import torch

def f1(a):
    num = a.nonzero().squeeze(-1).numel()
    return torch.tensor([num]) # no break

def f2(a):
    num = a.nonzero().squeeze(-1).numel()
    return a.new_tensor([num]) # break

a = torch.tensor([1, 0])
torch.compile(f1, fullgraph=True)(a)
torch.compile(f2, fullgraph=True)(a)

Error logs

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    torch.compile(f2, fullgraph=True)(a)
  File "pytorch/torch/_dynamo/eval_frame.py", line 1030, in compile_wrapper
    raise e.with_traceback(None) from e.__cause__  # User compiler error
torch._dynamo.exc.UserError: Could not extract specialized integer from data-dependent expression u0 (unhinted: u0).  (Size-like symbols: u0)

Caused by: return a.new_tensor([num]) # break  # test.py:9 in f2 (_dynamo/utils.py:3906 in run_node)
For more information, run with TORCH_LOGS="dynamic"
For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u0"
If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing

User Stack (most recent call last):
  (snipped, see stack below for prefix)
  File "test.py", line 9, in f2
    return a.new_tensor([num]) # break

For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#constrain-as-size-example

from user code:
   File "test.py", line 9, in f2
    return a.new_tensor([num]) # break

versions.log

Versions

To my understanding, if torch.tensor([num]) could be compiled without break, a.new_tensor([num]) should also pass?

cc @chauhang @penguinwu @ezyang @bobrenjc93 @aditvenk @laithsakka @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @amjames @Lucaskabela @jataylo

Metadata

Metadata

Labels

PT2-Bug-BashActionable issues for PT2-Bug-Bashbot-triagedThis is a label only to be used by the auto triage botmodule: dynamic shapesmodule: dynamooncall: pt2triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions