Skip to content

[precompile] Capture the caller's calls and keep their gradients - #195927

Closed
bobrenjc93 wants to merge 58 commits into
gh/bobrenjc93/1129/basefrom
gh/bobrenjc93/1129/head
Closed

bobrenjc93 wants to merge 58 commits into
gh/bobrenjc93/1129/basefrom
gh/bobrenjc93/1129/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

A training example runs a real backward, which accumulates into the caller's
tensors. The dynamo session now snapshots and clears the gradients every
example could accumulate into before the calls and restores the SAME grad
objects afterwards, as the make_fx path already did, so a capture cannot
double the gradients of the documented warmup-step-then-capture flow;
keep_example_grads=True turns that off for a caller whose example IS their
live training step, and the make_fx tracer refuses it. The snapshot reads
.grad only off leaves and retained-grad tensors, so a non-leaf example input no
longer warns twice per capture. A graph the compiler short-circuited to
noop_graph_call is recorded as an eager artifact rather than reported as a
compiled graph that lost its code, and capture pins bypass_autograd_cache_key
so AOTAutogradCache no longer has to KEY a graph before it can be recorded.

The session also grows what the accumulating capture above this commit needs:
the enter exception path drains in-flight calls BEFORE any teardown and
runs the region cleanup in a finally; re-entering an active session raises
PackageError; a resumable session keeps its compiled region across cycles,
gates only on the errors the current cycle raised, and is torn down by
retire(); snapshot_artifact() renders everything captured so far from
policy-filtered copies of the code entries; collect_results/example_results
retain what each example call returned for the on-disk form.

Test Plan:

python test/test_precompile.py -k does_not_touch_the_example_gradients -k short_circuited_noop -k cache_will_not_key -k worker_thread_inherits -k exit_waits_for_worker
python test/test_precompile.py

Authored with an AI assistant.

[no ci]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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 Sep 3, 2026
This was referenced Sep 3, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93 bobrenjc93 closed this Oct 16, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1129/head branch November 17, 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 release notes: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant