Skip to content

[precompile] Stop the dynamo tracer accumulating into the example's gradients - #194426

Closed
bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/1033/basefrom
gh/bobrenjc93/1033/head
Closed

bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/1033/basefrom
gh/bobrenjc93/1033/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

torch._precompile states of its capture path that "precompile does not
mutate 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:

one eager step   |w.grad| = 14.351556
after precompile |w.grad| = 28.703112     ratio 2.0

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_inputs are covered. Calls the caller makes inside
the with block of the session API are the caller's own and run in the
ambient grad mode by documented design.

Test Plan:

python test/test_precompile.py
python test/dynamo/test_package.py
python test/dynamo/test_guard_serialization.py

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 exercised
the path that already honoured it.

Authored with an AI assistant.

Differential Revision: D117030053

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

🔗 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 Failure

As of commit 8852fba with merge base 1b4e490 (image):

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.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Aug 22, 2026
This was referenced Aug 22, 2026
This was referenced Aug 22, 2026
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the canonical precompile stack #195353-#195362; this change lives in #195362 (with the guard-drop policy half in #195360). Closing to consolidate review there. The branch is kept on GitHub.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1033/head branch November 11, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant