Repository navigation
[GDN/KDA] Fuse SM100 CuteDSL prefill state I/O into the chunk h kernel - #30169
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a "pool mode" for the GDN and KDA Blackwell kernels, fusing state gather and scatter operations directly into the h-kernel's TMA load and store operations using provided state indices. This optimization eliminates intermediate state allocations and explicit out-of-kernel gather/scatter operations. Unit tests have been added to verify that pool mode produces bit-identical results compared to the dense mode. I have no feedback to provide as there are no review comments.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
The CuteDSL (SM100) chunk prefill wrappers did eager pool state I/O around the kernels each layer call: ssm_states[indices].contiguous() gather in, index_copy_ scatter back out -- two extra kernels plus an [N, HV, V, K] intermediate each way. The Triton chunk path already fuses this into the h kernel (indexed h0 load, in-place indexed ht store), and the CuteDSL decode kernel already reads the pool directly; prefill was the remaining gap. Fuse the pool indexing into kernel_h's existing TMA load/store instead: the kernel takes state_indices [N] and addresses tmaH0/tmaHT at state_indices[seq_id], so h0/ht can be the state pool itself and the final state is written back in place at the right slot -- zero extra kernels, zero state intermediates. The grid's sequence count moves from h0.shape[0] to cu_seqlens (h0 may now be the pool). chunk_kda_cutedsl / chunk_gated_delta_rule_cutedsl gain an optional indices argument: pool mode reads and writes pool rows in place; without it, an arange preserves the dense return-fresh-ht contract bit-for-bit (existing tests unchanged). The kda/gdn cutedsl extend wrappers now pass the pool + int32 slots (padding still remapped to the sentinel slot) and drop the gather/scatter.
7ff87cb to
054efb0
Compare
sgl-project#30169) Co-authored-by: luoyuan.luo <[email protected]>
Motivation
The SM100 CuteDSL chunk prefill wrappers (GDN
gdn_cutedsl.pyand KDAkda_cutedsl.py) did eager pool state I/O around the kernels on every layer call:ssm_states[indices].contiguous()to gather initial states in, andindex_copy_to scatter final states back out. That is two extra kernel launches plus an[N, HV, V, K]intermediate allocation each way, per linear-attn layer per prefill step (~1 MB per seq per layer at HV=32, K=V=128, at N=64 that is ~64 MB gathered + 64 MB scattered per layer). This per-call PyTorch overhead is exactly the class that previously dragged the (fast) CuteDSL kernels below Triton before the workspace reuse fix.The rest of the stack already does this right: the Triton chunk path fuses state I/O into its h kernel (indexed
h0load, in-place indexedhtstore,chunk_delta_h.py), and the CuteDSL decode kernel reads the pool directly with per-slot indices. Prefill was the remaining gap.Modifications
Kernel (
kda_blackwell/kernel_h.py,gdn_blackwell/kernel_h.py, identical change):kernel_htakes astate_indices[N]int32 tensor and resolves each sequence's state row asstate_slot = state_indices[seq_id](same runtime-tensor-indexing pattern as the existingcu_seqlens[seq_id]); the H0 TMA load and the final-state HT TMA store addresstmaH0/tmaHT[state_slot, head_id].h0/htmay therefore be the full state pool ([num_slots, HV, V, K]) instead of a per-sequence gather; the launch grid's sequence count moves fromh0.shape[0]tocu_seqlens.shape[0] - 1.h0/httheir own symbolic dim (pool size) independent of the sequence count; dtype handling is unchanged (h_dtypefromh0.dtype, bf16 and fp32 pools both supported, cast fused in the existing store path).Chunk pipeline entry points (
chunk_kda_cutedsl,chunk_gated_delta_rule_cutedsl):h0_indices/initial_state_indicesargument. Pool mode: the h kernel reads and writes the pool rows in place at those slots and the returned final state is the pool tensor itself. Dense mode (indices omitted): an internalarangepreserves the old return-a-fresh-htcontract bit-for-bit, so existing callers and tests are unchanged.Wrappers (
kda_cutedsl.py,gdn_cutedsl.pyextend):.contiguous(), and theindex_copy_scatter. Padding slots (-1) are still remapped to the sentinel (last) pool slot, same as the decode kernels.Net effect per linear-attn layer per prefill call: 2 fewer kernel launches, 2 fewer
[N, HV, V, K]intermediates, and the state round-trip traffic through fresh allocations is gone — the kernel reads/writes the pool rows it always ultimately targeted.Performance (B200)
Wrapper-level bench, old (gather + dense chunk + scatter) vs new (pool-indexed chunk), Hv=32, K=V=128, bf16 pool, fixed 8K-token budget split across N sequences;
g+sis the standalone gather+scatter cost (the theoretical ceiling of the saving):The saved time tracks the standalone gather+scatter cost almost exactly (e.g. 0.219 ms saved vs 0.215 ms g+s at N=128), i.e. the fusion removes the state I/O in full. The relative win grows with concurrency (state I/O is per-sequence, compute is per-token) and is larger for GDN, whose chunk pipeline is ~4.5x faster than KDA's at the same token budget, so the I/O was a bigger fraction.
Validation
test_{kda,gdn}_chunk_cutedsl_pool_mode_matches_dense(bf16 + fp32 pool dtypes): pool mode must reproduce the dense path bit-for-bit — identicalo, identical final-state rows written in place at the indexed slots, and every non-indexed pool row untouched. Same kernels and math, only addressing differs, so the comparison is exact equality, not tolerances.4-gpu-b200runner (base-b-kernel-unit), where the SM100 kernels compile and run.CI States
Latest PR Test (Base): ✅ Run #29387364037
Latest PR Test (Extra): ✅ Run #29487242398