Repository navigation
[aot_autograd] Don't let a non-differentiable output mark its aliases - #194479
Closed
bobrenjc93 wants to merge 2 commits into
Closed
bobrenjc93 wants to merge 2 commits into
bobrenjc93 wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194479
Note: Links to docs will display an error until the docs builds have been completed. ❌ 23 New FailuresAs of commit a77ca0a 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 22, 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 23, 2026
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):
torch.compile(backend="inductor") silently returns no gradient for
Eager gives grad_fn [True, True, False] and a populated x.grad; inductor gives
[False, False, False] and x.grad is None. Adding a fourth, independently
differentiable output turns the silent wrong answer into a crash in the
compiled backward -- "TypeError: expected Tensor() for op: input" out of
inductor's assert_size_stride_grouped, or "Expected tensors only, but got
NoneType" out of copy_misaligned_inputs when the backward has no kernels for
the asserts to attach to.
ctx.mark_non_differentiable is keyed on TensorImpl, so marking one returned
slot marks EVERY returned slot holding that same object, and the forward
epilogue pairs it with ctx._materialize_non_diff_grads = False, which makes a
marked slot's grad arrive as None rather than zeros. Backends are free to
return one object in two slots: inductor lowers aten.detach to a no-op, and
h * 1 / h + 0 fold away. So
return y[:2], y[2:], y.detach()marks y's intermediate base -- a slot_codegen_backward_prologue keeps unconditionally -- and the base's tangent
arrives None, which process_runtime_tangent forwards verbatim; and
return h * 1, h.detach()marks the sibling output, which is the onlydifferentiable thing in the model.
The metadata is not at fault: the backward's predicate,
requires_grad_for_backward, is a strict subset of the forward's requires_grad
(collect_metadata_analysis.py), so no slot is dropped by one and kept by the
other. Only the runtime identity collision is.
Give each slot about to be marked its own TensorImpl instead. That is sound
precisely because we are about to declare the slot non-differentiable, so an
alias of it is as good as the original; slots that stay differentiable keep
their identity. The rejected alternative was to skip marking any slot that
aliases a surviving one, which spares the base but also spares the detach
output -- it comes back with requires_grad=True and a grad_fn, and backward
flows through a detached tensor.
Fixing this in inductor instead would be the wrong layer twice over: detach is
not the culprit (h * 1 reproduces it with no detach anywhere), and AOTAutograd
is the component making the identity-keyed call.
The dedup indexes the marked slots, usually one or two, and probes the rest
against them, so it costs ~0.8us per forward of a compiled autograd function
that has any non-differentiable output -- about 0.3% of a small MLP forward
and below the noise floor of an end-to-end measurement. A follow-up worth
having is an index-based mark_non_differentiable, which would make this free
and also cover the same hazard in mark_dirty.
Test Plan:
Two new tests, one per collision shape: an output aliasing an intermediate
base, and an output aliasing a sibling output. Each asserts requires_grad,
grad_fn and the gradient itself against eager, and the first also asserts the
detach output stays a leaf so the rejected fix above cannot pass. Both fail
without this change -- the intermediate-base one errors, the sibling one
returns the wrong gradient.
Authored with an AI assistant.
Differential Revision: D117095337