Repository navigation
[aot_autograd] Dealias non-differentiable outputs before mark_non_differentiable - #195688
Closed
bobrenjc93 wants to merge 9 commits into
Closed
bobrenjc93 wants to merge 9 commits into
bobrenjc93 wants to merge 9 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195688
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 32 PendingAs of commit 9e1d999 with merge base 3d3d3f4 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Sep 2, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
This was referenced Sep 3, 2026
Closed
Contributor
Author
|
@pytorchbot label "release notes: aot autograd" |
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):
Root cause: Function.mark_non_differentiable is keyed on TensorImpl, not on the
output slot. When two returned slots hold the same tensor object, marking one
marks both. AOTAutograd sets ctx._materialize_non_diff_grads = False, so a slot
marked this way receives None instead of a zero tangent in the backward.
Backends legitimately produce aliased slots. Inductor lowers aten.detach to a
no-op and folds arithmetic identities (h * 1, h + 0), so
return h * 1, h.detach()hands back one object in two slots; marking the detached slotnon-differentiable also marks the differentiable one, whose backward then fails
with "does not require grad". Likewise
return y[:2], y[2:], y.detach()marksy's intermediate base, and the regenerated view outputs come back disconnected
from the graph: the backward runs and x.grad stays None.
Fix: before calling mark_non_differentiable, give every slot that is about to be
marked its own TensorImpl via detach() whenever that object is shared with
another returned slot. Substituting an alias is safe precisely because the slot
is one we are about to declare non-differentiable; slots that stay
differentiable keep their identity. The check is written to run cheaply on the
common no-collision path: it indexes only the (usually one or two) marked slots
and probes the other returns against them. The dealiasing is emitted into the
generated compiled-function wrapper, which is the live forward epilogue
(_AutogradForwardEpilogue.finalize is its reference implementation and is
replaced by the generated one at build time; it carries the same call for
parity).
Test Plan:
Authored with an AI assistant.
Observed pre-fix behavior, for the record:
return h * 1, h.detach()failsloudly at backward ("element 0 of tensors does not require grad"), and
return y[:2], y[2:], y.detach()completes its backward with x.grad left Nonebecause marking y's base makes the regenerated view outputs leaves disconnected
from x. The tests assert data_ptr identity between the aliased slots so they
fail visibly if inductor stops folding these ops rather than passing vacuously;
the two that depend on the h * 1 fold pin joint_graph_constant_folding=True.
Unmarked TensorAlias slots are not probed: autograd.Function treats them as
non-tensor outputs and never consults the non-differentiable set for them.
Co-Authored-By: Claude Fable 5.1 [email protected]