Return BSHD-contiguous input grads from XPU efficient attention backward - #5433
Open
orrangetabby17 wants to merge 1 commit into
Open
orrangetabby17 wants to merge 1 commit into
orrangetabby17 wants to merge 1 commit into
Conversation
CuiYifeng
approved these changes
Sep 18, 2026
CuiYifeng
left a comment
Contributor
There was a problem hiding this comment.
Please ensure the readability of PR description.
_scaled_dot_product_efficient_attention_backward has a layout contract that the XPU implementation does not honor. Its meta kernel allocates grad_q/k/v with torch.empty_permuted((B, H, S, D), (0, 2, 1, 3)), i.e. BHSD sizes over BSHD-contiguous memory. The CUDA kernel delivers exactly that; the XPU math-based fallback returns plain contiguous tensors, so compiled code that asserts the meta strides fails. The fix relays out grad_q/k/v to BSHD-contiguous before returning, reusing the same permute/contiguous/permute normalization the XPU forward already applies. grad_bias is left alone: the meta kernel pads only its last dimension to a multiple of 16 and slices back. Test Plan: Built PyTorch and ran the upstream test that motivated this (DISABLED for XPU as pytorch/pytorch#196553): ```bash cd <pytorch_root> pip install -e . -v --no-build-isolation cd test TORCHINDUCTOR_FORCE_DISABLE_CACHES=1 ZE_AFFINITY_MASK=0 \ python -m pytest -v \ inductor/test_cuda_repro.py::CudaReproTests::test_effn_attn_uniform_zero_bias_backward ``` This change was authored with Claude Code. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
CuiYifeng
force-pushed
the
shuxin/fix-ut
branch
from
September 18, 2026 15:03
0b2df81 to
18b512a
Compare
jianyizh
approved these changes
Sep 20, 2026
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.
Motivation (Why)
_scaled_dot_product_efficient_attention_backwardhas a layout contractthat the XPU implementation does not honor. Its meta kernel allocates
grad_q/k/vwithtorch.empty_permuted((B, H, S, D), (0, 2, 1, 3))(
meta__scaled_dot_product_efficient_backwardintorch/_meta_registrations.py),i.e. BHSD sizes over BSHD-contiguous memory — strides
(H*S*D, D, H*D, 1).The XPU backward computes gradients by re-running the math forward under
autograd and returns them untouched, so they come back plain contiguous.
Eager code does not care, but
torch.compileasserts the meta strides, so anycompiled use of the efficient-attention backward fails with e.g.
expected size 4==4, stride 8192==64 at dim=1on[2, 4, 128, 64]grads.This was first hit by
inductor/test_cuda_repro.py::CudaReproTests::test_effn_attn_uniform_zero_bias_backward.Solution (How)
Relay
grad_q/k/vout to BSHD-contiguous before returning them, reusing thesame
permute/contiguous/permutenormalization the XPU forward already appliesto its own output (
Attention.cpp:468-469). This matches what the CUDA kerneldelivers (
attention_backward.cu:1131-1132), so the single shared stridecontract is preserved.
grad_biasis deliberately left alone: the meta kernel pads only its lastdimension up to a multiple of 16 and then slices back, so a bias whose last
dimension is already 16-aligned (e.g. the
[2, 4, 128, 128]in this test)matches without a change. An unaligned bias width is a separate, untested gap.
The alternative — teaching the meta kernel to describe what XPU actually
returns, keyed off device — was rejected: it forks a stride contract CUDA,
ROCm and XPU otherwise share, and bakes a temporary property of the math-based
fallback into a device-agnostic registration.
Test Plan
Built PyTorch at
31527a43with this branch as thethird_party/torch-xpu-opsoverride, then ran the upstream test that motivated this: