SIGN IN SIGN UP

[Bug] Fix scan Autograd (#193787)

This PR adds a regression test for the `scan` Autograd correctness bug reported in [#153679](https://github.com/pytorch/pytorch/issues/153679#issuecomment-5243398461).

The reported problem was ultimately an Inductor-only issue that surfaced through
`scan`: backward donated-buffer indices are computed in the top-level graph's
placeholder order, but a `SubgraphLowering` (used to lower control-flow HOP
bodies) inherited those indices and applied them to its own, unrelated
placeholder order. This could mark a subgraph input as donatable, so a reused
region could overwrite a saved input and corrupt gradients. With `scan`, the HOP
lowers to a `while_loop` whose carried inputs are mutated in place, so donating
such a carry corrupted the backward.

The underlying fix has **already landed upstream** in #193960.
Therefore, this PR merely add a test to pin the correct behavior for the future.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/193787
Approved by: https://github.com/aorenste
T
Thomas Bohnstingl committed
4287565497d8424c0aab536cafa39ddda763d249
Parent: 02b3fd9
Committed by PyTorch MergeBot <pytorchmergebot@users.noreply.github.com> on 8/23/2026, 1:34:56 PM