Skip to content

[aot_autograd] Replay an input mutation the way it actually happened - #194534

Closed
bobrenjc93 wants to merge 3 commits into
gh/bobrenjc93/1057/basefrom
gh/bobrenjc93/1057/head
Closed

bobrenjc93 wants to merge 3 commits into
gh/bobrenjc93/1057/basefrom
gh/bobrenjc93/1057/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

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:

codegen.py:codegen(mutation_epilogue):2  _apply_mutations
RuntimeError: Output 0 of CommOpGradientScaling is a view and is being
modified inplace. This view was created inside a custom Function (or
because an input was returned as-is) ...

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, so
no 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 of
the 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:

python test/functorch/test_aotdispatch.py
python test/dynamo/test_aot_autograd_cache.py
python test/test_precompile.py
python test/dynamo/test_package.py

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

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

pytorch-bot Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

🔗 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 Failures

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

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
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the canonical precompile stack #195353-#195362; this change lives in #195359. 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/1057/head branch November 11, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant