Repository navigation
[precompile] Let the example call keep the gradients it produced - #195014
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/195014
Note: Links to docs will display an error until the docs builds have been completed. ❌ 9 New Failures, 3 Unclassified FailuresAs of commit 6e40d09 with merge base 1b4e490 ( 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. |
This was referenced Aug 27, 2026
Closed
Closed
This was referenced Aug 27, 2026
Closed
This was referenced Aug 28, 2026
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):
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:
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.