Skip to content

[aot_autograd] Don't let a non-differentiable output mark its aliases - #194479

Closed
bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/1049/basefrom
gh/bobrenjc93/1049/head
Closed

bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/1049/basefrom
gh/bobrenjc93/1049/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

torch.compile(backend="inductor") silently returns no gradient for

def f(x):
    y = torch.sin(x)
    return y[0:4], y[4:8], y.detach()

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 only
    differentiable 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:

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

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

[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/194479

Note: Links to docs will display an error until the docs builds have been completed.

❌ 23 New Failures

As of commit a77ca0a 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.

This was referenced Aug 23, 2026
[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 #195357. 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/1049/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