8000 [3.13] deal with exec() semantic change in test_cond_no_dynamo_cache_… · pytorch/pytorch@72943ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 72943ba

Browse files
williamwen42pytorchmergebot
authored andcommitted
[3.13] deal with exec() semantic change in test_cond_no_dynamo_cache_limit (#140401)
https://peps.python.org/pep-0667/ changed the semantics of `eval/exec` in 3.13 so that changes to locals no longer propagate (but globals do). This is to make the behavior predictable since in the past, the locals may or may not update based on various mysterious conditions. Other test sites may need updating too. Pull Request resolved: #140401 Approved by: https://github.com/ydwu4, https://github.com/zou3519
1 parent e445239 commit 72943ba

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

test/functorch/test_control_flow.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5334,16 +5334,19 @@ def foo(x, true_fn, false_fn):
53345334
exp_out = inp.sin()
53355335
iter_n = torch._dynamo.config.cache_size_limit + 1
53365336

5337-
# Need this because Dynamo checks lambda code ID not object itself.
5338-
def make_dummy_fn(op):
5339-
exec(f"temp = lambda x: x.{op}()")
5340-
return locals()["temp"]
5337+
# Need functions that cause recompilations
5338+
def get_dummy_fns(str):
5339+
def dummy_cos(x):
5340+
return x.cos() + len(str) - len(str)
53415341

5342-
for _ in range(iter_n):
5343-
# each lambda has a different object id thus fails the guard
5344-
self.assertEqual(
5345-
foo(inp, make_dummy_fn("cos"), make_dummy_fn("sin")), exp_out
5346-
)
5342+
def dummy_sin(x):
5343+
return x.sin() + len(str) - len(str)
5344+
5345+
return dummy_cos, dummy_sin
5346+
5347+
for i in range(iter_n):
5348+
# we fail guards each iter because `str(i)` is different
5349+
self.assertEqual(foo(inp, *get_dummy_fns(str(i))), exp_out)
53475350

53485351
# each iteration captures a cond and a getitem from the tuple output
53495352
self.assertEqual(counters["stats"]["calls_captured"], iter_n * 2)

0 commit comments

Comments
 (0)
0