Repository navigation
[aot_autograd] Replay an input mutation the way it actually happened - #194534
Closed
bobrenjc93 wants to merge 3 commits into
Closed
bobrenjc93 wants to merge 3 commits into
bobrenjc93 wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194534
Note: Links to docs will display an error until the docs builds have been completed. ❌ 22 New FailuresAs of commit acd3d05 with merge base 1b4e490 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Aug 23, 2026
Closed
Closed
This was referenced Aug 23, 2026
Closed
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This was referenced Aug 23, 2026
This was referenced Aug 26, 2026
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This was referenced Aug 28, 2026
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
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):
A model whose compiled region mutates an input that a custom autograd.Function
returned as-is dies in the mutation epilogue:
Two things have to line up. Returning an input as-is makes autograd substitute
an identity view and stamp it CreationMeta::IN_CUSTOM_FUNCTION
(custom_function.cpp), which refuses any tracked in-place edit. The write is
then done by an op that declares
Tensor(a!)but goes through raw pointers, sono version counter moves and eager never runs check_inplace -- and whose meta
kernel writes nothing, so the fake trace does not either. FBGEMM's
fused-optimizer TBE kernels are exactly this shape.
AOTAutograd reads
Tensor(a!)off the schema, functionalizes the write out ofthe graph, and replays it at the region boundary as an ordinary TRACKED
copy_. Because the meta kernel never performed the write,mutations_hidden_from_autograd is recorded False and the mutation cannot stay
in the graph, so the epilogue is the only place left. That replay is the first
autograd-visible in-place op the view has ever seen, and it raises -- for a
mutation that was legal where it originally happened.
Replay it the way it happened instead: under no_grad, preserving the version
counter. That is the only sound option. Clearing CreationMeta to force a
tracked copy through reroutes the base's history and silently drops the custom
Function's backward -- measured, not assumed. detach() alone and no_grad alone
do not fix it at all. Asking the caller to clone the custom Function's output
is not available to them either: the region boundary, and therefore the
replay, is AOTAutograd's choice, and clone() is not semantics-preserving here.
Dispatched per call rather than baked into the epilogue, because nothing
guards whether the caller's tensor is such a view: a graph traced against an
ordinary tensor can be handed one later, which for a serialized artifact means
in a different process. Forcing the decision back to compile time reproduces
the error on the second call.
Note the neighbouring hazard this deliberately does NOT change: the epilogue
also specializes on is_leaf, which is likewise unguarded, so a function warmed
on a leaf input and then called with a non-leaf silently takes the leaf
branch's detach().copy_() and drops the mutation's contribution to autograd.
That is a separate bug and wants its own change.
Not covered: a plain view of a leaf, whose CreationMeta is DEFAULT, still fails
a different autograd check for the same underlying reason. Widening the
predicate to "any tracked in-place would be rejected here" is likely correct
but risks silently dropping a mutation that autograd should see, so it is left
alone.
Test Plan:
One new test builds both ingredients and asserts the gradients -- the base's,
the other input's, and the mutated tensor -- match uncompiled eager on
aot_eager and inductor, rather than merely that nothing raised. It then warms
on an ordinary tensor and calls again with the restricted view, which is the
case the per-call dispatch exists for. It fails without this change with the
verbatim error above.
Narrowed to exactly CreationMeta::IN_CUSTOM_FUNCTION rather than "not DEFAULT",
which is what the rationale above actually describes. A view made under no_grad
or inference mode, or a multi-output-node view, still takes a tracked copy_, and
writing those invisibly would skip the version bump autograd relies on to catch
a genuinely stale use.
test_codegen_mutation_epilogue asserted on the literal copy_ this epilogue used
to emit. The write-back is spelled replay_input_mutation now, so the three
tests that reach the general path assert on that instead; the leaf-under-no_grad
case still emits detach().copy() directly and is unchanged.
Authored with an AI assistant.
Differential Revision: D117133992