Repository navigation
[SM100] Legacy sparse decode: any h_q <= 128 in one launch, token packing, unrolled combine - #31
Merged
Merged
Conversation
…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]>
41 of 42 tasks
Fridge003
marked this pull request as ready for review
November 9, 2026 05:12
This was referenced Nov 12, 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.
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.patchon top of 3620b63, ported to the currentsgllayout. 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_legacyops). The current V4.1 kernels incsrc/are untouched.h_qin [1, 128] on SM100 sparse decode, in one launch. The head64 kernel usesblockIdx.zto pick a block of 64 heads, and rows pasth_qare zero-filled padding that is never stored. This replacesDecode_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 == 128withd_qk == 512still goes to the head128 kernel.sm100_sparse_decode_num_sm_parts(newcsrc_legacy/api/sparse_decode_sched.h) is shared by the kernel launch,get_mla_decoding_metadataand a newsparse_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/1forces either schedule.sparse_decode_fwdgetsint 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.b * s_q * 4 <= num_sm_parts), each thread keeps the loads of 4 splits in flight.FLASHMLA_COMBINE_UNROLL=1/4forces 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.flash_mla_with_kvcache(..., heads_per_group=0)(legacy only) andflash_mla.sparse_decode_num_sm_parts.tests/legacy/test_flash_mla_sparse_decoding.pyadds SM100f correctness cases forh_q8/32/96, over V3.2, V4, and V4 with an extra KV cache.Porting notes
The 3620b63 patch touched
csrc/. #18 moved those files tocsrc_legacy/and replacedpython_api.cppwith the stable-ABI op layer, so this is a port, not a cherry-pick:params.h,head64/config.h,head64/kernel.cuh,combine.cuandsparse_decode_sched.hcarry 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, withSTD_TORCH_CHECKand thelegacy::namespace.python_api.cpphunks moved tocsrc_legacy/sched_meta.cpp(num_sm_parts and the new op),csrc_legacy/ops.cpp(schema) andapi/interfaces_legacy.h.sparse_decode_num_sm_partshas no tensor arguments, so it is registered underCompositeExplicitAutogradinstead ofCUDA.Test plan (SM100, not run yet)
Run the same tests on SM90 to confirm there is no regression; the SM90 paths are unchanged.
🤖 Generated with Claude Code