Skip to content

[GDN/KDA] Fuse SM100 CuteDSL prefill state I/O into the chunk h kernel - #30169

Merged
BBuf merged 1 commit into
sgl-project:mainfrom
yuan-luo:fuse_cutedsl_prefill_state_io
Jul 16, 2026
Merged

BBuf merged 1 commit into
sgl-project:mainfrom
yuan-luo:fuse_cutedsl_prefill_state_io

Conversation

@yuan-luo

@yuan-luo yuan-luo commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The SM100 CuteDSL chunk prefill wrappers (GDN gdn_cutedsl.py and KDA kda_cutedsl.py) did eager pool state I/O around the kernels on every layer call: ssm_states[indices].contiguous() to gather initial states in, and index_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 h0 load, in-place indexed ht store, 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_h takes a state_indices [N] int32 tensor and resolves each sequence's state row as state_slot = state_indices[seq_id] (same runtime-tensor-indexing pattern as the existing cu_seqlens[seq_id]); the H0 TMA load and the final-state HT TMA store address tmaH0/tmaHT[state_slot, head_id].
  • h0/ht may therefore be the full state pool ([num_slots, HV, V, K]) instead of a per-sequence gather; the launch grid's sequence count moves from h0.shape[0] to cu_seqlens.shape[0] - 1.
  • The compile-time fake tensors give h0/ht their own symbolic dim (pool size) independent of the sequence count; dtype handling is unchanged (h_dtype from h0.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):

  • New optional h0_indices / initial_state_indices argument. 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 internal arange preserves the old return-a-fresh-ht contract bit-for-bit, so existing callers and tests are unchanged.

Wrappers (kda_cutedsl.py, gdn_cutedsl.py extend):

  • Pass the pool + int32 slots instead of gathering; drop the gather, the .contiguous(), and the index_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+s is the standalone gather+scatter cost (the theoretical ceiling of the saving):

N len KDA old→new ms KDA saved GDN old→new ms GDN saved g+s ms
8 1024 1.090 → 1.065 2.3% 0.241 → 0.218 9.8% 0.019
16 512 1.109 → 1.071 3.4% 0.259 → 0.224 13.5% 0.029
64 128 1.215 → 1.099 9.5% 0.364 → 0.249 31.5% 0.105
128 64 1.353 → 1.134 16.2% 0.510 → 0.292 42.7% 0.215

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

  • New test_{kda,gdn}_chunk_cutedsl_pool_mode_matches_dense (bf16 + fp32 pool dtypes): pool mode must reproduce the dense path bit-for-bit — identical o, 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.
  • The existing correctness tests (vs the token-by-token recurrent reference) now exercise the new kernel signature through the dense-mode path unchanged, covering the baseline math.
  • Both test files are CI-registered on the 4-gpu-b200 runner (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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@yuan-luo yuan-luo added blackwell SM100/SM120 run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) linear-attention labels Jul 5, 2026
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.
@yuan-luo
yuan-luo force-pushed the fuse_cutedsl_prefill_state_io branch from 7ff87cb to 054efb0 Compare July 15, 2026 03:46

@kaixih kaixih left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@BBuf
BBuf merged commit a798a2a into sgl-project:main Jul 16, 2026
269 of 302 checks passed
@yuan-luo yuan-luo added the release-highlight Candidate PR for release note highlight label Jul 16, 2026
@yuan-luo
yuan-luo deleted the fuse_cutedsl_prefill_state_io branch July 16, 2026 09:28
@nvpohanh

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120 linear-attention release-highlight Candidate PR for release note highlight run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants