Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't specialize when indexing by SymInt #99123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/dynamo/test_subgraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ def fn(a, b):
# just one graph now rather than 10
self.assertEqual(cnt_dynamic.frame_count, 1)

@patch("torch._dynamo.config.dynamic_shapes", True)
@patch("torch._dynamo.config.assume_static_by_default", False)
def test_dynamic_getitem(self):
def fn(a, b):
return a[b.size(0) - 1]

cnt = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnt)(fn)
for i in range(3, 12):
opt_fn(torch.randn(i), torch.randn(i))
# just one graph
self.assertEqual(cnt.frame_count, 1)

def test_dynamic_kwarg(self):
def fn(a, b):
return a - b * 10
Expand Down
10 changes: 10 additions & 0 deletions torch/_dynamo/variables/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ def call_function(
# Work around weird bug in hf_T5
fn, args = operator.add, [args[1], args[0]]

if self.fn is operator.getitem and isinstance(args[1], SymNodeVariable):
# Standard indexing will force specialization due to
# __index__. Rewrite as a regular torch op which will
# trace fine
fn, args = torch.select, [
args[0],
variables.ConstantVariable(0),
args[1],
]

proxy = tx.output.create_proxy(
"call_function",
fn,
Expand Down