Skip to content

[SM100] Legacy sparse decode: any h_q <= 128 in one launch, token packing, unrolled combine - #31

Merged
Fridge003 merged 1 commit into
sglfrom
kimi-native-heads
Nov 9, 2026
Merged

Fridge003 merged 1 commit into
sglfrom
kimi-native-heads

Conversation

@Fridge003

Copy link
Copy Markdown
Collaborator

Upstreams the FlashMLA change that sgl-project/sglang#48414 (Kimi-K3.1) applies at build time as python/sglang/kernels/aot/patches/flashmla/kimi-native-heads.patch on top of 3620b63, ported to the current sgl layout. Once this is in, SGLang can drop the patch and pin a FlashMLA commit instead.

Draft: compiled and run on no GPU yet. The changed paths are SM100-only, and they need a B200/GB300 run before this can merge (commands below).

Modifications

Everything is on the legacy side (csrc_legacy/, flash_mla_legacy ops). The current V4.1 kernels in csrc/ are untouched.

  • Any h_q in [1, 128] on SM100 sparse decode, in one launch. The head64 kernel uses blockIdx.z to pick a block of 64 heads, and rows past h_q are zero-filled padding that is never stored. This replaces Decode_Sm100_Head64x2_Impl, which made two sequential launches, so Kimi-K3.1's 96 heads (and whatever TP leaves of them) run natively. h_q == 128 with d_qk == 512 still goes to the head128 kernel.
  • One schedule for metadata and launch. sm100_sparse_decode_num_sm_parts (new csrc_legacy/api/sparse_decode_sched.h) is shared by the kernel launch, get_mla_decoding_metadata and a new sparse_decode_num_sm_parts(b, s_q, h_q) op. With several head blocks and few requests, the SMs are divided among the head blocks to avoid a second wave. FLASHMLA_DIVIDE_PARTS=0/1 forces either schedule.
  • Token packing. sparse_decode_fwd gets int heads_per_group=0. When it is > 0 (SM100, V3.2 KV, h_q <= 64, no extra KV), each q token packs up to 5 real tokens and bits 26..30 of an index say which of them attend to it. The default 0 keeps today's behaviour.
  • Unrolled split-KV combine. When requests have many splits (b * s_q * 4 <= num_sm_parts), each thread keeps the loads of 4 splits in flight. FLASHMLA_COMBINE_UNROLL=1/4 forces either. This sits on top of the 192/224/256 split cases restored in Build sm_107a with CUTLASS 4.8.1 and restore the >160-split sparse decode combine #20.
  • Python. flash_mla_with_kvcache(..., heads_per_group=0) (legacy only) and flash_mla.sparse_decode_num_sm_parts.
  • Tests. tests/legacy/test_flash_mla_sparse_decoding.py adds SM100f correctness cases for h_q 8/32/96, over V3.2, V4, and V4 with an extra KV cache.

Porting notes

The 3620b63 patch touched csrc/. #18 moved those files to csrc_legacy/ and replaced python_api.cpp with the stable-ABI op layer, so this is a port, not a cherry-pick:

  • params.h, head64/config.h, head64/kernel.cuh, combine.cu and sparse_decode_sched.h carry exactly the same +/- lines as the SGLang patch. They were checked line by line; only hunk context differs.
  • api/sparse_decode.cpp: the same logic, with STD_TORCH_CHECK and the legacy:: namespace.
  • The python_api.cpp hunks moved to csrc_legacy/sched_meta.cpp (num_sm_parts and the new op), csrc_legacy/ops.cpp (schema) and api/interfaces_legacy.h. sparse_decode_num_sm_parts has no tensor arguments, so it is registered under CompositeExplicitAutograd instead of CUDA.

Test plan (SM100, not run yet)

pip install -v . --no-build-isolation
PYTHONPATH=.:tests python tests/legacy/test_flash_mla_sparse_decoding.py
PYTHONPATH=.:tests python tests/legacy/test_flash_mla_sparse_decoding_v32_bit_equal.py
python -c "import flash_mla; print(flash_mla.sparse_decode_num_sm_parts(4, 1, 96))"

Run the same tests on SM90 to confirm there is no regression; the SM90 paths are unchanged.

🤖 Generated with Claude Code

…king, unrolled combine

Port of the FlashMLA delta that SGLang applies to 3620b63 for Kimi-K3.1
(python/sglang/kernels/aot/patches/flashmla/kimi-native-heads.patch in
sgl-project/sglang#48414) onto csrc_legacy/ and the stable-ABI op layer.

- head64 sparse decode takes any h_q in [1, 128]: blockIdx.z picks a block of
  64 heads and rows past h_q are padding (zero-filled, not stored). It replaces
  the two sequential head64 launches of Decode_Sm100_Head64x2_Impl, so
  Kimi-K3.1's 96 heads (and what TP leaves of them) run natively.
- sm100_sparse_decode_num_sm_parts (api/sparse_decode_sched.h) is shared by the
  kernel launch, get_mla_decoding_metadata and the new
  sparse_decode_num_sm_parts op so the schedule and the launch always agree.
  With several head blocks and few requests the SMs are divided among the head
  blocks; FLASHMLA_DIVIDE_PARTS=0/1 forces either schedule.
- Token packing: sparse_decode_fwd gains heads_per_group (default 0). When > 0
  (SM100, V3.2 KV, h_q <= 64, no extra KV) each q token packs up to 5 real
  tokens and bits 26..30 of an index say which of them attend to it.
- The split-KV combine keeps the loads of 4 splits in flight when requests have
  many splits (b * s_q * 4 <= num_sm_parts); FLASHMLA_COMBINE_UNROLL=1/4 forces
  either.
- Python: flash_mla_with_kvcache(heads_per_group=...) and
  flash_mla.sparse_decode_num_sm_parts; legacy tests cover h_q 8/32/96.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant