Skip to content

[precompile] Let the example call keep the gradients it produced - #195014

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

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

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

precompile snapshots every example model's .grad, clears it, runs the example
calls, and restores the snapshot. That is right for the documented flow -- a
warmup step and then a capture -- where not clearing would double the gradients
the capture finds, and not restoring would leave the model changed by having
been used as an example.

It is wrong for the other flow, and silently. A caller whose example call IS
their live training step gets an artifact and no gradients: the restore writes
the pre-step values back over everything the backward just computed. On a real
model that means the dense optimizer steps on nothing for one batch while
fused sparse optimizers, which apply inside the backward and never touch .grad,
have already updated -- one batch of dense/sparse desync, with the job
reporting success.

keep_example_grads=True turns the whole snapshot/clear/restore off, so
precompile does not touch .grad at all and a gradient already present
accumulates exactly as it would in eager. Default False, because the flow that
needs the restore cannot detect that it needed it either.

Not folded into training=True, though that was tempting and would have needed
no new argument. training=True says "fn runs a backward", which the
warmup-then-capture flow also says; the two are distinguishable only by whether
the caller intends to consume these particular gradients, and that is not
something the flag already means.

Threaded through precompile_capture as well. The two entry points differed here
only by accident -- a session driven with no example_inputs has nothing to
snapshot, so it preserved gradients by doing nothing -- and that is the kind of
divergence that gets discovered the expensive way.

Two smaller things while in here, both reported alongside the above:

The on-disk form with tracer='make_fx' returned an empty list AFTER fn had
already run. The return value of that form IS the example calls' results, and a
make_fx trace has none to give, so the caller is left holding "no results" for a
region that already executed and whose obvious recovery -- run it again -- is a
second backward over storages the first one freed. Refused up front instead.

_PrecompileApi's docstring, torch/compiler/init.py and one comment named a
capture member, and two of them a load_package; neither exists. Removed rather
than added: the manual session form is a real design question and advertising it
as though it had been settled is worse than not mentioning it.

Test Plan:

python test/test_precompile.py
python test/dynamo/test_package.py
python test/functorch/test_compile_to_python.py

Three new tests: the default restores and the flag keeps; a gradient present
before capture accumulates to the same value two eager steps produce; and the
flag is refused for tracer='make_fx' alongside the other multi-variant knobs.
The make_fx on-disk test now asserts the region did NOT run.

Authored with the assistance of an AI coding agent.

[ghstack-poisoned]
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Aug 27, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195014

Note: Links to docs will display an error until the docs builds have been completed.

❌ 9 New Failures, 3 Unclassified Failures

As of commit 6e40d09 with merge base 1b4e490 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

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 27, 2026
This was referenced Aug 27, 2026
This was referenced Aug 27, 2026
[ghstack-poisoned]
@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/1077/head branch November 11, 2026 01:47
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