Skip to content

[CUDA] faster window attention - #4927

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
pierreablin:cuda-sdpa-window
Nov 22, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
pierreablin:cuda-sdpa-window

Conversation

@pierreablin

Copy link
Copy Markdown
Contributor

This allows for faster and more memory efficient window attention in cuda, forward and backward.

Window attention now goes through the CUDA kernels instead of defaulting back to unfused fallback which uses full attention.

headline numbers

Results table (claude produced it)

Measured on one h100, bf16, 16 Q heads, 8 KV heads, dim 128; causal and window attention

Time in ms, peak in GB

L window main fwd patched fwd main fwd+bwd patched fwd+bwd main peak patched peak
4096 none 0.24 0.25 0.78 0.78 0.17 0.17
4096 512 2.41 0.14 5.45 0.43 1.9 0.17
4096 1024 2.46 0.17 5.54 0.51 1.9 0.17
8192 none 0.56 0.57 2.04 2.05 0.34 0.34
8192 512 8.63 0.18 19.91 0.64 7.3 0.34
8192 1024 8.47 0.24 20.27 0.82 7.3 0.34
8192 4096 8.62 0.46 19.67 1.66 7.3 0.34
16384 none 1.81 1.82 6.92 6.93 0.67 0.67
16384 512 33.61 0.27 80.95 1.07 28.5 0.67
16384 1024 33.60 0.37 81.07 1.41 28.5 0.67
16384 4096 33.54 0.91 81.09 3.41 28.5 0.67
32768 none 7.12 7.25 26.19 26.15 1.35 1.35
32768 512 133.49 0.43 out of memory 1.99 (fwd 41.3) 1.35
32768 1024 133.56 0.65 out of memory 2.69 (fwd 41.3) 1.35
32768 4096 133.55 1.85 out of memory 7.08 (fwd 41.3) 1.35
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: I used claude to write the code and benchmark. I double checked everything.

@pierreablin

Copy link
Copy Markdown
Contributor Author

BTW here is the same table with B200

L window main fwd patched fwd main fwd+bwd patched fwd+bwd main peak GB patched peak GB
4096 none 0.114 0.115 0.342 0.343 0.17 0.17
4096 512 1.74 0.097 3.48 0.258 1.9 0.17
8192 none 0.251 0.250 0.872 0.873 0.34 0.34
8192 512 5.89 0.110 13.08 0.37 7.3 0.34
8192 4096 5.78 0.24 13.09 0.99 7.3 0.34
16384 none 0.825 0.802 3.04 3.03 0.67 0.67
16384 1024 18.65 0.19 46.55 0.83 28.5 0.67
32768 none 3.14 3.21 12.13 12.18 1.35 1.35
32768 512 71.45 0.31 182.39 1.16 112.3 1.35
32768 4096 71.24 0.93 182.47 4.11 112.3 1.35

@zcbenz
zcbenz marked this pull request as ready for review November 22, 2026 08:11

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@zcbenz
zcbenz merged commit 58d91ee into ml-explore:main Nov 22, 2026
47 of 48 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
osaurus-ai/mlx takes only the part of this commit that is independent of the window size: the
VJP fallback (gradients of the mask or the sinks, and the fallback's primals) and the gradient
test, without windows. The window size needs ml-explore#4734, which osaurus-ai/mlx does not take.

Ported: Apple's commit adds `|| other_grads` to an `if (outputs.size() != 2)` that came
upstream with ml-explore/mlx PR 4565, which the fork does not have. So this commit also adds
that check: without the logsumexp output, the VJP takes the fallback. The fused Metal backward
needs that output.

(cherry picked from commit 58d91ee)
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.

2 participants