Dynamo resume functions pass restored stack values and locals as normal
positional arguments. CPython keeps those argument references alive for the
whole resume call, so a DELETE_FAST in the resumed bytecode can remove the
local name without actually releasing the tensor. That makes compiled graph
breaks hold deleted intermediates longer than eager execution.
Detect resume functions that may execute DELETE_FAST after the resume target
and use a boxed frame-values argument for those calls. The resume prologue now
loads each saved stack/local value from the list, stores locals where needed,
and clears each list slot immediately. Resume functions without a reachable
DELETE_FAST keep the previous positional calling convention.
Nested resume calls need to preserve the child's calling convention, so nested
boxed callees receive the frame-values list as one argument while non-boxed
callees keep the existing list extension behavior.
This fixes the actionable resume-frame/intermediate lifetime issue and the
issue-shaped list-cleared input path. Bare direct temporary inputs remain owned
by the outer torch.compile wrapper's *args tuple until the compiled call
returns; a pure Python def wrapper(*args): return fn(*args) has the same
lifetime behavior, so this patch does not try to make del x release such
bare inputs before return.
Fixes #153701
Generated by my agent
Test Plan:
- python test/dynamo/test_subgraphs.py SubGraphTests.test_nested_resume_del_releases_tensor SubGraphTests.test_resume_del_releases_tensor SubGraphTests.test_issue_shape_list_clear_and_intermediate_del_release_tensors SubGraphTests.test_del_compiled_only_local_before_graph_break
- python test/dynamo/test_subgraphs.py
- python test/dynamo/test_nested_graph_breaks.py -k test_step_graph_break_frame_values_not_corrupted
- python test/dynamo/test_repros.py ReproTests.test_weakref_reconstruct ReproTests.test_weakref_del ReproTests.test_weakref_callback
- lintrunner -a
Benchmark Results:
Tiny CPU graph-break runtime microbenchmark with DELETE_FAST, backend="eager",
7 samples of 3000 cached calls each:
- Before: samples_us [26.349, 26.217, 26.076, 26.089, 26.544, 26.578, 26.099], median 26.217 us
- After: samples_us [28.897, 29.262, 30.618, 29.686, 31.57, 32.353, 33.83], median 30.618 us (+16.8%)
Pull Request resolved: #185561
Approved by: https://github.com/IvanKobzarev
SOCIAL SHARE CARD GENERATOR