[CUDA] Add decode-optimized LinearAttention (GatedDeltaNet) kernels - #28985
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a decode-optimized CUDA fast path for the LinearAttention contrib op to improve latency when seq_len is small (notably decode), while preserving the existing present_state layout and numerics and leaving the prefill-tuned recurrent kernels as the fallback.
Changes:
- Added a full-warp sum helper (
warp_reduce_sum) for shuffle-based reductions. - Introduced two decode-focused kernels (
LinearAttentionDecodeKernelandLinearAttentionDecodeColKernel) optimized for better GPU occupancy and more coalesced state access. - Updated
LaunchLinearAttentionKerneldispatch to route common decode shapes (seq_len <= 16,d_k ∈ {64,128,256}) to the new kernels and otherwise keep existing behavior.
apsonawane
reviewed
Jun 10, 2026
kunal-vaishnavi
approved these changes
Jun 18, 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.
Description
Adds a decode-optimized CUDA path for the
LinearAttentioncontrib op (thegated-delta / linear-attention recurrence used by hybrid models such as
Qwen3-Next / Qwen3.6). The existing recurrent kernels are tuned for prefill; at
decode (
seq_len == 1) they leave the GPU mostly idle. This PR adds twodecode-specific kernels that saturate the GPU and access the recurrent state in
a coalesced pattern, without changing the op's
present_statelayout ornumerics.
Motivation
LinearAttentionRecurrentKernelFixedShapelaunches one block per(batch, kv_head)and keeps the fulld_k x d_vstate in shared memory acrossthe token loop, with block-wide
__syncthreadsat every step. That designamortizes state I/O during prefill, but at decode it:
kv_num_headsblocks (e.g. 32) — a fraction of the SMs, andso the op is latency-bound. On an H200 profile of Qwen3.6-35B-A3B it was the
single most expensive decode kernel after the dense/MoE GEMMs (~0.69 ms/token).
Key Changes
All in
onnxruntime/contrib_ops/cuda/bert/linear_attention_impl.cu:warp_reduce_sum__shfl_xor_syncfull-warp sum helper.LinearAttentionDecodeKernel<T, DK>(kv_num_heads, batch, ceil(d_v/4)), each warp owns one output column with the state column sharded into registers; reductions via warp shuffles, no shared memory, no block barriers. Handles anyd_v.LinearAttentionDecodeColKernel<T, DK>i, consecutive threads read consecutive addressesi*d_v + col, so state load/store is fully coalesced with no transpose — the row-major[d_k, d_v]present_statelayout is unchanged. Used whend_v % 32 == 0; otherwise falls back to the warp kernel.LaunchLinearAttentionKernelseq_len <= 16andd_k in {64,128,256}to the decode kernels; all other shapes fall through to the existing recurrent kernels, so the prefill path is unchanged.Both kernels cover the full op semantics:
linear/gated/delta/gated_deltaupdate rules, scalar and per-key-dim decay, per-head and scalarbeta, standard GQA and inverse GQA, and
n_k_headsK-sharing.Performance
H200, Qwen3.6-35B-A3B (INT4), single-sequence decode, CUDA graph on. Kernel time
measured with Nsight Systems (steady-state, warmup excluded):
LinearAttentionRecurrentKernelFixedShape(existing)LinearAttentionDecodeKernel(warp-per-column)LinearAttentionDecodeColKernel(column-per-thread)End-to-end decode throughput improved measurably (the kernel is ~half its prior
cost), with no change to prefill.
Testing
All 26
ContribOpLinearAttentionTestparity tests pass (the decode kernels areexercised by the single-token, inverse-GQA, KGQA, and Qwen3.5-like cases):
No public API or
present_statelayout change; the decode path is opt-in byshape and falls back to the existing kernels for unsupported
d_k/d_v.Motivation and Context
Decode-throughput optimization for hybrid linear-attention + MoE models. No
breaking changes; numerics and output layout are preserved.