Skip to content

[aot_autograd] Dealias non-differentiable outputs before mark_non_differentiable - #195688

Closed
bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1107/basefrom
gh/bobrenjc93/1107/head
Closed

bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1107/basefrom
gh/bobrenjc93/1107/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

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 slot
non-differentiable also marks the differentiable one, whose backward then fails
with "does not require grad". Likewise return y[:2], y[2:], y.detach() marks
y'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:

python test/functorch/test_aotdispatch.py -k non_differentiable_output_aliasing_differentiable_output
python test/functorch/test_aotdispatch.py

Authored with an AI assistant.

Observed pre-fix behavior, for the record: return h * 1, h.detach() fails
loudly 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 None
because 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]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

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

As of commit 9e1d999 with merge base 3d3d3f4 (image):
💚 Looks good so far! There are no failures yet. 💚

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]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: aot autograd"

@pytorch-bot pytorch-bot Bot added the release notes: aot autograd release notes category label Sep 3, 2026
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by #195357 in the canonical stack #195353-#195362 (only two end-to-end tests are not carried over). Closing.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1107/head branch November 15, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant