Skip to content

cuDNN SDPA grad layout fix: let compiled code modify the SDPA output in place - #15721

Merged
danielhanchen merged 2 commits into
mainfrom
dh/recheck-sdpa-inplace
Oct 28, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/recheck-sdpa-inplace

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

#15445 works around the torch 2.6 - 2.13 cuDNN SDPA backward plan cache. Under torch.compile, its scaled_dot_product_attention wrapper passes every qualifying SDPA output through _GradInOutputLayout, whose forward returned out.view_as(out). PyTorch forbids in-place ops on a view returned by a custom autograd Function ("Output 0 of ... is a view and is being modified inplace ... You can fix this by cloning the output of the custom Function"). So compiled code that modifies an SDPA output in place, for example out = F.scaled_dot_product_attention(q, k, v); out.mul_(2), now fails at trace time under fullgraph=True and False, with the aot_eager and inductor backends alike. The same code compiles and trains without the wrapper.

The eager path is not affected: it uses a gradient hook, not the Function. On torch 2.14 and later nothing is installed.

Fix

The Function's forward returns out.clone(). Only the compiled path uses this Function, and Inductor's post-grad remove_noop_ops drops a same-layout aten.clone (true_noop). The generated forward and backward code is the same as before: 5 kernels and 29 buffer allocations with and without the change (CPU Inductor, torch 2.13, counted with torch._inductor.utils.run_and_get_code). The backward that gives the gradient the output's layout is unchanged.

Tests

tests/test_cudnn_sdpa_backward_grad_layout.py:

test main this branch
TestCompiledPathFunction::test_compiled_inplace_consumer[aot_eager] (fullgraph, in-place mul_, loss and grads vs no wrapper) FAIL (view modified inplace) PASS
TestCompiledPathFunction::test_compiled_inplace_consumer[inductor] FAIL (same) PASS
TestGradInOutputLayout::test_forward_is_identity (now also checks the output is not a view and takes an in-place op) FAIL PASS

The whole file passes on torch 2.6, 2.10, 2.13 and 2.14 on CPU (27 passed, 1 skipped GPU test).

Not covered

  • No GPU run. The cuDNN numerics test (test_cudnn_sdpa_backward_is_right_across_grad_layouts_after_import) needs CUDA with torch 2.13. The change does not touch the backward or the eager hook path.
  • No stock transformers or Unsloth model was found that modifies an SDPA output in place, so this affects user code and custom modeling code only.

…in place

Under torch.compile the wrapper hands every SDPA output to _GradInOutputLayout,
whose forward returned out.view_as(out). A custom Function view rejects in-place
ops, so compiled code doing out.mul_(...) on an SDPA output raised. Return a
clone instead; Inductor removes it as a no-op (same kernels and allocations).
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T08:40:59.588492Z 70660a1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 70660a16b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

[skip ci]
@danielhanchen
danielhanchen merged commit 0512f4e into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the dh/recheck-sdpa-inplace branch October 28, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant