Repository navigation
cuDNN SDPA grad layout fix: let compiled code modify the SDPA output in place - #15721
Merged
Merged
Conversation
…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).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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]
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.
Problem
#15445 works around the torch 2.6 - 2.13 cuDNN SDPA backward plan cache. Under
torch.compile, itsscaled_dot_product_attentionwrapper passes every qualifying SDPA output through_GradInOutputLayout, whose forward returnedout.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 exampleout = F.scaled_dot_product_attention(q, k, v); out.mul_(2), now fails at trace time underfullgraph=TrueandFalse, with theaot_eagerandinductorbackends 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-gradremove_noop_opsdrops a same-layoutaten.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 withtorch._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:TestCompiledPathFunction::test_compiled_inplace_consumer[aot_eager](fullgraph, in-placemul_, loss and grads vs no wrapper)TestCompiledPathFunction::test_compiled_inplace_consumer[inductor]TestGradInOutputLayout::test_forward_is_identity(now also checks the output is not a view and takes an in-place op)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
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.