Repository navigation
[precompile] Snapshot only tensor leaves in _trace_without_side_effects - #198530
bobrenjc93 wants to merge 5 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/198530
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 4 Unrelated FailuresAs of commit f920c6b with merge base 12a7c44 ( 🟢 GREEN LIGHT - PR approved to be merged without human review:
reason: NEW FAILURE - The following job has failed:
NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following job failed, but the AI CI Advisor judged the failure not to be evidence against this change:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review this PR. Earlier bot reviews and my replies are in this thread; please verify the fixes and dispositions. |
|
Claude finished @bobrenjc93's task in 30s —— View job No issues found. This PR is a behavior-preserving cleanup of PR Review: #198530SummaryThe change filters I checked that nothing else changes:
Earlier bot reviews and replies: this thread has no earlier bot review comments or replies from you, so there were no fixes or dispositions to check. If they're on #198243, that PR is where they apply. The only thing this PR takes from it is the cleanup, and that cleanup is correct. CI: the red jobs all fail with RecommendationApprove. This is a pure refactor, and the existing tests ( |
[ghstack-poisoned]
|
@claude review this PR. Earlier bot reviews and my replies are in this thread; please verify the fixes and dispositions. |
|
Claude finished @bobrenjc93's task in 26s —— View job No issues found. This PR only cleans up PR Review: #198530SummaryThe PR filters I checked three things:
Earlier bot reviews and replies: #198530 has no inline review comments, no earlier bot review threads and no replies from you, so there are no fixes or dispositions to check here. If you meant the threads on #198243, this PR does carry out its cleanup correctly. CI: every red job fails with RecommendationApprove. This is a pure refactor. |
[ghstack-poisoned]
|
@claude review this PR. Earlier bot reviews and my replies are in this thread; please verify the fixes and dispositions. |
|
Claude finished @bobrenjc93's task in 27s —— View job No issues found. This PR only cleans up PR Review: #198530SummaryThe PR filters
Earlier bot reviews and your replies: the two earlier bot reviews in this thread both recommended approving and raised no findings. There are no inline review comments and no replies from you here, so there are no fixes or dispositions to check. Both earlier reviews blamed the red CI on the ghstack depth guard, but Dr. CI now reports a different cause. CI: all 5 red jobs now fail with RecommendationApprove. This is a pure refactor. |
[ghstack-poisoned]
|
@claude review this PR. Earlier bot reviews and my replies are in this thread; please verify the fixes and dispositions. |
|
Claude finished @bobrenjc93's task in 27s —— View job No issues found. This PR is a cleanup of PR Review: #198530SummaryThe PR filters
Earlier bot reviews and your replies: the three earlier bot reviews in this thread all recommended approving and raised no findings. There are no inline review comments and no replies from you on this PR, so there are no fixes or dispositions to check. CI: CI is running again now. The earlier RecommendationApprove. This is a pure refactor. |
[ghstack-poisoned]
|
@claude review |
|
Claude finished @bobrenjc93's task in 31s —— View job No issues found. This PR is a behavior-preserving cleanup of PR Review: #198530SummaryThe PR filters
Earlier bot reviews and your replies: the four earlier bot reviews in this thread all recommended approving and raised no findings. There are no inline review comments and no replies from you on this PR, so there are no fixes or dispositions to check. RecommendationApprove. This is a pure refactor. |
|
@pytorchbot merge -i |
**Problem.** Each tracer's capture ran its own `with`-block lifecycle from its own flags, so a Dynamo capture misused after its block gave different errors than a make_fx one, and a spent make_fx capture kept the pair its docstring says it drops. **Fix.** `Capture` owns the lifecycle as one state field under one lock, and the tracers implement only the hooks it calls. **Summary.** `_MakeFxCapture` tracked its block with two booleans, `_entered` and `_exited`, which are really three states, and `_DynamoCapture` kept its own `_exited` flag and its own copies of `__enter__`, `__exit__`, `__call__` and `save()`. The two front-ends therefore drifted: a Dynamo capture called after its block said "not active" where make_fx says "already exited", and make_fx's `__exit__` never cleared the pair that `Capture`'s docstring says a spent capture does not keep. `Capture` now holds the lifecycle as one field, `new`/`active`/`spent`, owns the four methods and their not-active, already-entered and spent messages, and a tracer implements only `_start`, `_run`, `_save` and `_finish`, where `_finish` drops the pair on every exit. The Dynamo capture's `RLock`, which serializes the block's calls, saves, exit and `summary()` because every call feeds one `PrecompileSession`, moves into `Capture` with the lifecycle. The messages a make_fx capture gives do not change, and a Dynamo capture now gives the same ones. These are review cleanups on `capture()` from #198135; that PR and #198243 are approved, so both are left as they are. The `Generic` typing of `Capture` and the tensor-only leaves snapshot that earlier revisions of this PR carried now live in #198529 and #198530. ## What a Dynamo capture says now ```python cap = capture(fn, ...) # a DynamoTracer capture cap(x) # "before calling it" (unchanged) with cap: with cap: ... # "already been entered" (unchanged) cap(x) # after the block: a call, save() or `with cap:` # parent: "not active" / "already been entered" # now: "already exited", the same as make_fx ``` The `save()` checkpoint contract moves from `_DynamoCapture`'s override onto the public `Capture.save` docstring, since it holds for both tracers and `help(cap.save)` shows only the base docstring: each call rewrites both files, a refusal or failed write raises but writes nothing partial and the capture stays open, and it raises `PrecompileError` outside the `with` block. `test_calls_from_several_threads_are_serialized` pins the lock. Test Plan: `test_misuse_is_refused` in `TestPrecompileDynamoCapture` moves to the shared messages, and `test_save_checkpoints_mid_block` now expects "already exited" for a save after exit. The make_fx lifecycle tests pass without changes (`test_a_capture_must_be_entered_before_it_is_called`, `test_a_capture_is_over_once_its_block_exits`, `test_save_after_a_raising_block_is_refused`, `test_a_capture_is_single_use`), which pins that its messages did not move. ``` python test/test_precompile.py python test/dynamo/test_precompile_package.py lintrunner -a torch/_precompile.py test/test_precompile.py ``` Authored with an AI assistant. Co-Authored-By: Claude Opus 5.5 <[email protected]> Pull Request resolved: #198423 Approved by: https://github.com/MengjiaoZhou ghstack dependencies: #198211, #198223, #198283, #198284, #197993, #198213, #198135, #198243, #198214, #198136, #198215, #198137, #198216, #198528, #199883, #198138, #198227, #198217
**Problem.** Each tracer's capture ran its own `with`-block lifecycle from its own flags, so a Dynamo capture misused after its block gave different errors than a make_fx one, and a spent make_fx capture kept the pair its docstring says it drops. **Fix.** `Capture` owns the lifecycle as one state field under one lock, and the tracers implement only the hooks it calls. **Summary.** `_MakeFxCapture` tracked its block with two booleans, `_entered` and `_exited`, which are really three states, and `_DynamoCapture` kept its own `_exited` flag and its own copies of `__enter__`, `__exit__`, `__call__` and `save()`. The two front-ends therefore drifted: a Dynamo capture called after its block said "not active" where make_fx says "already exited", and make_fx's `__exit__` never cleared the pair that `Capture`'s docstring says a spent capture does not keep. `Capture` now holds the lifecycle as one field, `new`/`active`/`spent`, owns the four methods and their not-active, already-entered and spent messages, and a tracer implements only `_start`, `_run`, `_save` and `_finish`, where `_finish` drops the pair on every exit. The Dynamo capture's `RLock`, which serializes the block's calls, saves, exit and `summary()` because every call feeds one `PrecompileSession`, moves into `Capture` with the lifecycle. The messages a make_fx capture gives do not change, and a Dynamo capture now gives the same ones. These are review cleanups on `capture()` from pytorch#198135; that PR and pytorch#198243 are approved, so both are left as they are. The `Generic` typing of `Capture` and the tensor-only leaves snapshot that earlier revisions of this PR carried now live in pytorch#198529 and pytorch#198530. ## What a Dynamo capture says now ```python cap = capture(fn, ...) # a DynamoTracer capture cap(x) # "before calling it" (unchanged) with cap: with cap: ... # "already been entered" (unchanged) cap(x) # after the block: a call, save() or `with cap:` # parent: "not active" / "already been entered" # now: "already exited", the same as make_fx ``` The `save()` checkpoint contract moves from `_DynamoCapture`'s override onto the public `Capture.save` docstring, since it holds for both tracers and `help(cap.save)` shows only the base docstring: each call rewrites both files, a refusal or failed write raises but writes nothing partial and the capture stays open, and it raises `PrecompileError` outside the `with` block. `test_calls_from_several_threads_are_serialized` pins the lock. Test Plan: `test_misuse_is_refused` in `TestPrecompileDynamoCapture` moves to the shared messages, and `test_save_checkpoints_mid_block` now expects "already exited" for a save after exit. The make_fx lifecycle tests pass without changes (`test_a_capture_must_be_entered_before_it_is_called`, `test_a_capture_is_over_once_its_block_exits`, `test_save_after_a_raising_block_is_refused`, `test_a_capture_is_single_use`), which pins that its messages did not move. ``` python test/test_precompile.py python test/dynamo/test_precompile_package.py lintrunner -a torch/_precompile.py test/test_precompile.py ``` Authored with an AI assistant. Co-Authored-By: Claude Opus 5.5 <[email protected]> Pull Request resolved: pytorch#198423 Approved by: https://github.com/MengjiaoZhou ghstack dependencies: pytorch#198211, pytorch#198223, pytorch#198283, pytorch#198284, pytorch#197993, pytorch#198213, pytorch#198135, pytorch#198243, pytorch#198214, pytorch#198136, pytorch#198215, pytorch#198137, pytorch#198216, pytorch#198528, pytorch#199883, pytorch#198138, pytorch#198227, pytorch#198217
…ts (pytorch#198530) **Summary.** `_trace_without_side_effects` snapshots the buffers and tensor inputs a static trace could mutate, so the serve that follows is the only execution the caller sees. It built `tensors` from the module buffers plus every pytree leaf of `args`, the modules themselves and any non-tensor leaves included, and applied `isinstance(t, torch.Tensor)` only inside the snapshot comprehension. The leaves are now filtered to tensors up front, so `tensors` holds only tensors and the snapshot needs no type check. The parameter-exclusion set loses the non-tensor ids too, which never matched anything because a live non-tensor object cannot share an id with a live parameter. What gets snapshotted is unchanged; this is review cleanup from pytorch#198243, which is approved, so it lands here at the top of the stack instead of in that commit. ```python # parent leaves = pytree.tree_leaves(args) saved = {id(t): (t, t.detach().clone()) for t in tensors if isinstance(t, torch.Tensor) and id(t) not in params} # now leaves = [t for t in pytree.tree_leaves(args) if isinstance(t, torch.Tensor)] saved = {id(t): (t, t.detach().clone()) for t in tensors if id(t) not in params} ``` Test Plan: `test_a_capture_applies_in_place_updates_once`, `test_a_capture_refuses_an_in_place_parameter_update` and `test_a_trace_that_raises_leaves_buffers_and_inputs_as_they_were` in test/test_precompile.py cover this path and pass unchanged. ``` python test/test_precompile.py lintrunner -a torch/_precompile.py ``` Authored with an AI assistant. Co-Authored-By: Claude Opus 5.5 <[email protected]> Pull Request resolved: pytorch#198530 Approved by: https://github.com/pytorchgreenlight ghstack dependencies: pytorch#198211, pytorch#198223, pytorch#198283, pytorch#198284, pytorch#197993, pytorch#198213, pytorch#198135, pytorch#198243, pytorch#198214, pytorch#198136, pytorch#198215, pytorch#198137, pytorch#198216, pytorch#198528, pytorch#199883, pytorch#198138, pytorch#198227, pytorch#198217, pytorch#198423, pytorch#198529
Stack from ghstack (oldest at bottom):
Summary.
_trace_without_side_effectssnapshots the buffers and tensor inputs a static trace could mutate, so the serve that follows is the only execution the caller sees. It builttensorsfrom the module buffers plus every pytree leaf ofargs, the modules themselves and any non-tensor leaves included, and appliedisinstance(t, torch.Tensor)only inside the snapshot comprehension. The leaves are now filtered to tensors up front, sotensorsholds only tensors and the snapshot needs no type check. The parameter-exclusion set loses the non-tensor ids too, which never matched anything because a live non-tensor object cannot share an id with a live parameter. What gets snapshotted is unchanged; this is review cleanup from #198243, which is approved, so it lands here at the top of the stack instead of in that commit.Test Plan:
test_a_capture_applies_in_place_updates_once,test_a_capture_refuses_an_in_place_parameter_updateandtest_a_trace_that_raises_leaves_buffers_and_inputs_as_they_werein test/test_precompile.py cover this path and pass unchanged.Authored with an AI assistant.
Co-Authored-By: Claude Opus 5.5 [email protected]