Repository navigation
[aot_autograd] Replay an input mutation the way it actually happened - #195359
bobrenjc93 wants to merge 67 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195359
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. |
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
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: `_replay_input_mutation` writes a target
whose CreationMeta is exactly IN_CUSTOM_FUNCTION under no_grad with
`_unsafe_preserve_version_counter`, and emits a plain tracked `copy_` for
everything else. 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.
This is a DELIBERATE divergence from eager, and the trade-off should be stated
plainly. Eager raises the error above on an autograd-VISIBLE in-place write to
such a view; compile cannot tell a `.data`-style write (the motivating case)
from a visible one at the region boundary, because the graph does not guard on
the view's provenance and the op that did the write is gone by the time the
epilogue runs. Replaying invisibly therefore also accepts the write eager would
have refused, and a later use of the view differentiates through the view's
PRE-mutation history: the mutation's contribution to autograd is dropped rather
than reported. Refusing instead would break the case this replay exists for, so
the ambiguous configuration -- grad enabled and the target requiring grad --
emits a warning naming the wrong-gradient risk, once per process. The
once-per-process gate is an `lru_cache`'d helper rather than a module global:
the callers are codegen'd epilogues, each with a fresh fabricated frame, so
warnings' own registry would re-warn per compiled function, and a module flag
is mutable state a test has to save and restore by hand.
The decision is 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. The predicate is exactly
IN_CUSTOM_FUNCTION rather than "not DEFAULT": 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.
`_replay_input_mutation` is part of the standalone-source surface -- exported
from standalone_runtime and registered in to_standalone_python's
`_known_helper_table` -- so a rendered artifact whose epilogue writes an input
back resolves it like the other runtime helpers. The documented reference
implementation (`_RuntimeForwardEpilogue._apply_input_mutations`) calls the
same helper in its non-leaf data-mutation branch, keeping the file's "keep both
in sync" contract with the codegen; the leaf `detach().copy_()` branch was
already in sync and is unchanged.
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/functorch/test_codegen_mutation_epilogue.py
python test/dynamo/test_aot_autograd_cache.py
python test/test_precompile.py
python test/dynamo/test_package.py
```
The restricted-view test builds both ingredients -- an opaque `Tensor(a!)` op
that writes through `.data` with a no-op meta kernel, and a target that a
custom Function returned as-is -- 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. A second test pins the divergence: a visible in-place write to
such a view raises in eager and is replayed invisibly under compile, with the
warning emitted exactly once across repeated replays and not at all in the
unambiguous no-grad serving configuration.
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.
ghstack-source-id: ea9da22
Pull-Request: #195359
[ghstack-poisoned]
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: `_replay_input_mutation` writes a target
whose CreationMeta is exactly IN_CUSTOM_FUNCTION under no_grad with
`_unsafe_preserve_version_counter`, and emits a plain tracked `copy_` for
everything else. 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.
This is a DELIBERATE divergence from eager, and the trade-off should be stated
plainly. Eager raises the error above on an autograd-VISIBLE in-place write to
such a view; compile cannot tell a `.data`-style write (the motivating case)
from a visible one at the region boundary, because the graph does not guard on
the view's provenance and the op that did the write is gone by the time the
epilogue runs. Replaying invisibly therefore also accepts the write eager would
have refused, and a later use of the view differentiates through the view's
PRE-mutation history: the mutation's contribution to autograd is dropped rather
than reported. Refusing instead would break the case this replay exists for, so
the ambiguous configuration -- grad enabled and the target requiring grad --
emits a warning naming the wrong-gradient risk, once per process per mutated
input index (the index is the input's position among the compiled function's
inputs, and the message names it). The gate is a `functools.cache`'d helper
keyed on that index rather than a module global: the callers are codegen'd
epilogues, each with a fresh fabricated frame, so warnings' own registry would
re-warn per compiled function, and a module flag is mutable state a test has
to save and restore by hand. The reference epilogue and the codegen'd one
must hand the helper the same index; a test pins that the emitted call is
`_replay_input_mutation(orig_inputs[i], updated_inputs[k], i)` for a case
where the mutated slot k and the runtime input index i differ.
The decision is 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. The predicate is exactly
IN_CUSTOM_FUNCTION rather than "not DEFAULT": 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.
`_replay_input_mutation` is part of the standalone-source surface -- exported
from standalone_runtime and registered in to_standalone_python's
`_known_helper_table` -- so a rendered artifact whose epilogue writes an input
back resolves it like the other runtime helpers. The documented reference
implementation (`_RuntimeForwardEpilogue._apply_input_mutations`) calls the
same helper in its non-leaf data-mutation branch, keeping the file's "keep both
in sync" contract with the codegen; the leaf `detach().copy_()` branch was
already in sync and is unchanged.
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/functorch/test_codegen_mutation_epilogue.py
python test/dynamo/test_aot_autograd_cache.py
python test/test_precompile.py
python test/dynamo/test_package.py
```
The restricted-view test builds both ingredients -- an opaque `Tensor(a!)` op
that writes through `.data` with a no-op meta kernel, and a target that a
custom Function returned as-is -- 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. A second test pins the divergence: a visible in-place write to
such a view raises in eager and is replayed invisibly under compile, with the
warning emitted exactly once across repeated replays and not at all in the
unambiguous no-grad serving configuration.
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.
ghstack-source-id: f031ada
Pull-Request: #195359
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: `_replay_input_mutation` writes a target
whose CreationMeta is exactly IN_CUSTOM_FUNCTION under no_grad with
`_unsafe_preserve_version_counter`, and emits a plain tracked `copy_` for
everything else. 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.
This is a DELIBERATE divergence from eager, and the trade-off should be stated
plainly. Eager raises the error above on an autograd-VISIBLE in-place write to
such a view; compile cannot tell a `.data`-style write (the motivating case)
from a visible one at the region boundary, because the graph does not guard on
the view's provenance and the op that did the write is gone by the time the
epilogue runs. Replaying invisibly therefore also accepts the write eager would
have refused, and a later use of the view differentiates through the view's
PRE-mutation history: the mutation's contribution to autograd is dropped rather
than reported. Refusing instead would break the case this replay exists for, so
the ambiguous configuration -- grad enabled and the target requiring grad --
emits a warning naming the wrong-gradient risk, once per process per mutated
input index (the index is the input's position among the compiled function's
inputs, and the message names it). The gate is a `functools.cache`'d helper
keyed on that index rather than a module global: the callers are codegen'd
epilogues, each with a fresh fabricated frame, so warnings' own registry would
re-warn per compiled function, and a module flag is mutable state a test has
to save and restore by hand. The reference epilogue and the codegen'd one
must hand the helper the same index; a test pins that the emitted call is
`_replay_input_mutation(orig_inputs[i], updated_inputs[k], i)` for a case
where the mutated slot k and the runtime input index i differ.
The decision is 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. The predicate is exactly
IN_CUSTOM_FUNCTION rather than "not DEFAULT": 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.
`_replay_input_mutation` is part of the standalone-source surface -- exported
from standalone_runtime and registered in to_standalone_python's
`_known_helper_table` -- so a rendered artifact whose epilogue writes an input
back resolves it like the other runtime helpers. The documented reference
implementation (`_RuntimeForwardEpilogue._apply_input_mutations`) calls the
same helper in its non-leaf data-mutation branch, keeping the file's "keep both
in sync" contract with the codegen; the leaf `detach().copy_()` branch was
already in sync and is unchanged.
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/functorch/test_codegen_mutation_epilogue.py
python test/dynamo/test_aot_autograd_cache.py
python test/test_precompile.py
python test/dynamo/test_package.py
```
The restricted-view test builds both ingredients -- an opaque `Tensor(a!)` op
that writes through `.data` with a no-op meta kernel, and a target that a
custom Function returned as-is -- 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. A second test pins the divergence: a visible in-place write to
such a view raises in eager and is replayed invisibly under compile, with the
warning emitted exactly once across repeated replays and not at all in the
unambiguous no-grad serving configuration.
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.
ghstack-source-id: f031ada
Pull-Request: #195359
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: `_replay_input_mutation` writes a target
whose CreationMeta is exactly IN_CUSTOM_FUNCTION under no_grad with
`_unsafe_preserve_version_counter`, and emits a plain tracked `copy_` for
everything else. 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.
This is a DELIBERATE divergence from eager, and the trade-off should be stated
plainly. Eager raises the error above on an autograd-VISIBLE in-place write to
such a view; compile cannot tell a `.data`-style write (the motivating case)
from a visible one at the region boundary, because the graph does not guard on
the view's provenance and the op that did the write is gone by the time the
epilogue runs. Replaying invisibly therefore also accepts the write eager would
have refused, and a later use of the view differentiates through the view's
PRE-mutation history: the mutation's contribution to autograd is dropped rather
than reported. Refusing instead would break the case this replay exists for, so
the ambiguous configuration -- grad enabled and the target requiring grad --
emits a warning naming the wrong-gradient risk, once per process per mutated
input index (the index is the input's position among the compiled function's
inputs, and the message names it). The gate is a `functools.cache`'d helper
keyed on that index rather than a module global: the callers are codegen'd
epilogues, each with a fresh fabricated frame, so warnings' own registry would
re-warn per compiled function, and a module flag is mutable state a test has
to save and restore by hand. The reference epilogue and the codegen'd one
must hand the helper the same index; a test pins that the emitted call is
`_replay_input_mutation(orig_inputs[i], updated_inputs[k], i)` for a case
where the mutated slot k and the runtime input index i differ.
The decision is 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. The predicate is exactly
IN_CUSTOM_FUNCTION rather than "not DEFAULT": 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.
`_replay_input_mutation` is part of the standalone-source surface -- exported
from standalone_runtime and registered in to_standalone_python's
`_known_helper_table` -- so a rendered artifact whose epilogue writes an input
back resolves it like the other runtime helpers. The documented reference
implementation (`_RuntimeForwardEpilogue._apply_input_mutations`) calls the
same helper in its non-leaf data-mutation branch, keeping the file's "keep both
in sync" contract with the codegen; the leaf `detach().copy_()` branch was
already in sync and is unchanged.
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/functorch/test_codegen_mutation_epilogue.py
python test/dynamo/test_aot_autograd_cache.py
python test/test_precompile.py
python test/dynamo/test_package.py
```
The restricted-view test builds both ingredients -- an opaque `Tensor(a!)` op
that writes through `.data` with a no-op meta kernel, and a target that a
custom Function returned as-is -- 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. A second test pins the divergence: a visible in-place write to
such a view raises in eager and is replayed invisibly under compile, with the
warning emitted exactly once across repeated replays and not at all in the
unambiguous no-grad serving configuration.
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.
ghstack-source-id: f031ada
Pull-Request: #195359
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
Thanks for the thorough review -- addressed the full set. Summary of resolutions: Correctness (the CreationMeta gate). Fixed.
Cache-entry compile-id stamping. Fixed. BC (error -> warning). Fixed. Added
Docstring. Trimmed to the non-obvious "why" and corrected: the version counter Export round-trip ( Deferred to FAQ (added to the PR description): the per-call branch chain on the @claude please re-review. |
|
Claude finished @bobrenjc93's task in 18m 32s —— View job Claude PR re-reviewThe five substantive findings from the first pass are genuinely fixed — the gate now mirrors Full re-review — verified fixes + 7 remaining findingsVerified fixed
Also verified safe: the short-circuit at Remaining findingsF1 — Both tests compare the expected = f"orig_inputs[1], updated_inputs[0], 1, {cid!r}, _warned_inputs, False, False" # :215
expected = "orig_inputs[0], updated_inputs[0], 0, None, _warned_inputs, False, False" # :263The regexes still match, so these fail on F2 — This is the same reachability gap as the first pass. To exercise the epilogue from F3 — removing the write-back introduced a reference/codegen skew on F4 — the export fix is keyed off a bare global name. F5 — the escape hatch covers the warning but not the semantics change the inversion brings. I think both changes are correct — a F6 — F7 (nit) — misplaced comment. Test coverageGood additions: Still uncovered: RecommendationRequest Changes, but narrowly: F1 and F2 are test bugs (F1 certain, F2 high-confidence) that mean the parity and export claims aren't actually being checked, and F3 is a small regression introduced by the compile-id fix. The core logic change is correct and now genuinely mirrors the in-graph epilogue. F4–F7 are polish. |
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
Thanks for the re-review. Addressed at HEAD:
Deferred with rationale in new FAQ entries in the PR description:
|
[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]
Stack from ghstack (oldest at bottom):
Mutating an input a custom autograd.Function returned as-is dies in the
mutation epilogue:
Autograd gives such an input an identity view stamped
CreationMeta::IN_CUSTOM_FUNCTION, which refuses any tracked in-place edit. The
mutating op declares
Tensor(a!)but writes through raw pointers, so eagernever checks the view. AOTAutograd functionalizes the write out of the graph
and replays it at the region boundary as a tracked
copy_, which raises._replay_input_mutationreplays it the way it happened: under grad mode, ontoa requires-grad view stamped IN_CUSTOM_FUNCTION, the write runs under no_grad;
the version counter is preserved only when the traced write was itself hidden
from autograd (a
.data-style write), and bumped, as eager's no_grad write did,when it ran under no_grad or inference mode, so a stale saved tensor is still
caught; everything else gets the tracked
copy_. With grad mode off eager writes such a view tracked, and that versionbump later catches a stale saved tensor, so the plain copy_ stands. The
decision is per call, since nothing guards the view's provenance.
Release notes (torch.compile, BC): an autograd-visible in-place write to a view
created inside a custom autograd.Function (or an input it returned as-is),
which eager rejects with the error above, is now replayed without autograd
tracking at the compiled region boundary when grad mode is on and emits a
UserWarning; gradients that later flow through that input see its pre-mutation
history. It warns once per (compiled graph, mutated input index), and not when
the traced mutation was itself hidden from autograd or under no_grad/inference
mode.
The codegen'd leaf branch now raises the unsupported-stream error for a
stream-annotated input, matching the reference.
Test Plan:
Tests pin eager-matching gradients on aot_eager and inductor, the eager divergence and single warning, the
no_grad version bump, warn-once across
torch._dynamo.reset(), and codegen'dversus reference epilogue parity.
Authored with an AI assistant.
[no ci]