cuDNN Linear Attention: Optimized Mamba-2, GDN, GDP, KDA, and GDN-2 Kernels for NVIDIA GPUs
Organizations: NVIDIA
Abstract
Linear attention replaces the softmax attention of Transformers with a fixed-size recurrent state, so that the runtime of prefill grows linearly rather than quadratically with the sequence length and the runtime of every decode step is constant in it. Many recent models have adopted a hybrid architecture where some layers swap dense attention for linear attention variants. cuDNN now ships optimized fused kernels for five linear attention variants, Mamba-2, Gated DeltaNet (GDN), Gated DeltaProduct (GDP), Kimi Delta Attention (KDA), and Gated DeltaNet-2 (GDN-2), on Blackwell (SM100, SM103), covering the training forward and backward passes. All of the kernels are open source and ship in cuDNN at https://github.com/NVIDIA/cudnn-frontend. In this paper, we walk through the optimizations that produce speedups over the state-of-the-art baselines. We start the kernel design from a single persistent kernel per pass. Then, in low settings, where a grid of one CTA per (batch, head) pair does not cover the SMs, we use intra-card context parallelism (intra-card CP) or a split of the value dimension (split-), both introduced by prior work in FlashQLA. We also fuse the other components that surround the linear attention operation in the block, the L2 normalization of the queries and keys and the gate and write-strength transforms, in both the forward and the backward pass. On GB200, the fused kernels for the four delta-rule variants run up to faster than the reference Triton implementations in the forward pass and up to in the backward pass, up to faster than FlashQLA, and up to faster than FlashKDA. The Mamba-2 kernels run up to faster than FLA and up to faster than mamba_ssm in the forward pass, and up to and in the backward pass.
Figures & tables
| decay | erase gate | write gate | updates per token | |
|---|---|---|---|---|
| Mamba-2 | scalar | none | scalar ( ) | 1 |
| GDN | scalar | scalar | tied to | 1 |
| GDP | scalar | scalar per update | tied to | |
| KDA | per key channel | scalar | tied to | 1 |
| GDN-2 | per key channel | per key channel | per value channel | 1 |
| MMA | |||
|---|---|---|---|
| MMA | ||||
|---|---|---|---|---|
| unchanged | ||||
| unchanged | ||||
| swap-AB | ||||
| swap-AB | ||||
| swap-AB | ||||
| swap-AB |
| MMA | ||||
|---|---|---|---|---|
| 128 | 64 | 128 | fused pair | |
| 128 | 64 | 128 | swap-AB | |
| 128 | 64 | 128 | swap-AB | |
| 128 | 64 | 64 | swap-AB | |
| 128 | 64 | 64 | swap-AB | |
| 128 | 128 | 64 | swap-AB |
| Warps | Role |
|---|---|
| 0–3 | compute group 0: runs the and epilogues and the blockwise inverse that yields |
| 4–7 | compute group 1: computes , downcasts , runs the epilogue, and rescales |
| 8 | gate warp: loads the gate logits and and runs the prefix scan into , , and |
| 9 | TMA warp: loads the , , and tiles |
| 10 | MMA warp: issues every tcgen05 MMA |
| 11 | epilogue warp: stores and the state checkpoints |
| Warps | Role |
|---|---|
| 0–7 | compute group 0, two ping-pong warpgroups: runs the per-channel gate prefix scan, computes the decay-folded operands , , , and , and runs the epilogue for the first half of the key channels |
| 8–11 | compute group 1: stages and , runs the epilogue for the second half of the key channels, and runs the epilogue |
| 12 | register-MMA warp: computes and the blockwise inverse that yields |
| 13 | MMA warp: issues every tcgen05 MMA |
| 14 | TMA warp: loads the , , , and gate tiles and stages one chunk ahead |
| 15 | epilogue warp: computes on register MMAs and stores and the state checkpoints |
| forward | backward | |
|---|---|---|
| intra-card CP (Section 6.1 ) | intra-card CP | |
| , | split- (Section 6.2 ) | intra-card CP |
| , | split-K (Section 6.3 ) | intra-card CP |
| split-K | split-K | |
| tail (Section 6.4 ) | same |
| MMA | ||||
|---|---|---|---|---|
| shared | ||||
| shared | ||||
| split | ||||
| split | ||||
| split | ||||
| split |
| Ours | ||||||
|---|---|---|---|---|---|---|
| Transform | Pass | FLA | FlashQLA | FlashKDA | GDN, GDP | KDA, GDN-2 |
| L2 normalization | forward | separate | separate | preparation | separate | fused |
| backward | separate | separate | – | fused | fused | |
| write strength | forward | separate | – | preparation | fused | fused |
| backward | separate | – | – | fused | fused | |
| gate | forward | cumsum | – | – | fused | – |
| Library | Version | Commit |
|---|---|---|
| FLA | 0.6.0 | 9f38d249 |
| mamba_ssm | 2.3.2.post1 | e9594ce1 |
| FlashQLA | 0.1.3 | da06429d |
| FlashKDA | master | 7afb9f45 |
| cuDNN Frontend | 1.34.0 | develop |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
| Warps | Role |
|---|---|
| 0–3 | compute group 0: runs the and epilogues and the blockwise inverse that yields , and reduces the terms of and and the state term of |
| 4–7 | compute group 1: rescales , runs the and epilogues, and reduces the terms of and and the terms of |
| 8–11 | compute group 2: rescales the inter-chunk , reduces the terms of , and runs the epilogue |
| 12 | MMA warp: issues every tcgen05 MMA |
| 13 | TMA warp: loads the , , , and tiles and the entering state from the checkpoint series |
| 14 | gate warp: loads the gate logits and and stores and |
| Warps | Role |
|---|---|
| 0–3 | compute group 0: runs the per-channel gate prefix scan and computes the decay-folded operands , , and |
| 4–7 | compute group 1: rescales , stages , , , , and , runs the epilogue (and the epilogue for GDN-2), and reduces and stores for KDA |
| 8–11 | compute group 2: forms the dot products and runs the , , and epilogues (and the per-channel epilogue for GDN-2) with the diagonal, the L2-norm backward, and the reverse cumulative sum |
| 12 | register-MMA warp: computes , the blockwise inverse that yields , , and the term of |
| 13 | MMA warp: issues every tcgen05 MMA |
| 14 | TMA warp: loads the , , , , and gate tiles and the entering state from the checkpoint series |
GPU Kernel Optimization
GPU kernel optimization improves the speed and memory efficiency of computations by tailoring code to GPU hardware and workloads.
See all 245 papersSystolic Linear Attention: Co-Designing Tile-Grouped Gated Delta Attention for TPUs
SLA-R4 (four value heads per key head) runs -- faster than open-source MaxText KDA and -- faster than open-source Tokamax KDA, while using -- less temporary high-bandwidth memory (HBM). Across a -chip TPU v7x pre-training study of sparse Mixture-of-Experts (MoE) models from to nominal parameters, SLA trains -- faster than MaxText KDA (-- faster than Tokamax KDA), matches KDA perplexity on three paired seeds at , and stays within at other scales.