Repository navigation
[precompile] Stop the dynamo tracer accumulating into the example's gradients - #194426
Closed
bobrenjc93 wants to merge 2 commits into
Closed
bobrenjc93 wants to merge 2 commits into
bobrenjc93 wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194426
Note: Links to docs will display an error until the docs builds have been completed. ❌ 13 New Failures, 1 Unrelated FailureAs of commit 8852fba with merge base 1b4e490 ( NEW FAILURES - The following jobs have failed:
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. |
This was referenced Aug 22, 2026
Closed
Closed
This was referenced Aug 22, 2026
Closed
This was referenced Aug 28, 2026
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
torch._precompilestates of its capture path that "precompile does notmutate the user's example .grad (params/buffers AND user inputs)", and the
make_fx tracer implements it: snapshot the ORIGINAL grad object, clear, restore
the same object in a finally, so both value and identity survive. The dynamo
tracer runs its examples directly and never did any of that.
A training example runs a real backward, which ACCUMULATES. So capturing a model
that had already taken a step doubled its gradients:
That is the documented warmup-step-then-capture flow, and the damage does not
stop at the capture: the doubled gradient goes through the optimizer, so every
subsequent step of the run is computed from perturbed parameters. On a
capture-then-train loop with SGD+momentum at lr=1e-4 the batch after the capture
comes out 1.7e-4 relative from an uncaptured baseline -- small enough to read as
inductor reassociation, which is exactly how it was reported to us.
Snapshot before the examples run and restore in the finally, the same shape
make_fx uses, over every tensor an example could accumulate into: the tensor
arguments, the parameters and buffers of any nn.Module argument, and the bound
receiver when the captured callable is a method. The same grad OBJECT goes back,
not a copy, because optimizer state can be keyed on its identity.
Only the automatic
example_inputsare covered. Calls the caller makes insidethe
withblock of the session API are the caller's own and run in theambient grad mode by documented design.
Test Plan:
The test that covered this is rewritten. It asserted that a capture accumulates
ONCE rather than twice, which blessed the mutation instead of forbidding it; it
now asserts the gradients are unchanged and the grad object is identical, and it
is parametrized over both tracers -- the three existing tests that pin this
contract all call precompile() without
tracer=, so they only ever exercisedthe path that already honoured it.
Authored with an AI assistant.
Differential Revision: D117030053