Repository navigation
[Perf] dsa: Add an opt-in FlashInfer Prims-TS sparse MLA backend - #48602
Conversation
… port) Copy the backend-generic hunks of the FlashInfer Prims-TS sparse MLA DSA backend from the Kimi-K3.1 branch (sgl-project#48414) byte for byte. The Kimi-K3.1-only and sharded-DCP hunks are not taken. This commit does not build against main yet; the next commits adapt it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eturn Main has no sharded index-K DCP, so its DSA DCP combine always merges base-2 LSE partials, while Prims-TS returns a natural-log LSE. _validate_prims_ts_dcp drops shards_index_k and rejects any --dcp-size > 1, and _forward_prims_ts drops partial / clamp_lengths: it always clamps the lengths to the top-k width and returns the output alone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…P rejection A single-rank check of the exact (24 rows) and packed (1025 rows) plans against an FP32 reference, and a CPU check that the DSA DCP combine merges base 2, so prims_ts is rejected under DCP. Registered on B200, since the kernel needs SM100/SM103; the GPU cases skip until FlashInfer ships Prims-TS D576 native-FP8 sparse MLA. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Cherry-pick the SGLang part of the Kimi-K3.1 branch's SM107 change: - prims_ts.py: admit compute capability (10, 7) next to (10, 0) and (10, 3); SM107 needs flashinfer-ai/flashinfer#8186. When FlashInfer's probe refuses the device, its reason is now part of the startup error (on SM107 with a CuTe DSL older than 4.8 it names the CUTE_DSL_ARCH=sm_100f fallback). - Update the --dsa-*-backend help text, the comment that points at the check, and the test's skip message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@nvpohanh i think this can be reviewed now |
Main's _forward_trtllm now plans rows with _RowPlan; _forward_prims_ts takes row_plan and restores the output with _restore_live_rows in place of the removed DP-padding helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…imi-K3.1 Main now has Kimi-K3.1's sharded index-K DCP (whose combine merges non-trtllm DSA partials base e) and its opt-in trtllm DSA backend, so take the sgl-project#48414 hunks this port had left for them, copied from the branch: - _validate_prims_ts_dcp takes shards_index_k again and rejects prims_ts only under replicated index-K DCP (base-2 combine). - _forward_prims_ts takes partial / clamp_lengths again: under DCP it returns the natural-log LSE with the output, and a sharded-DCP decode partial keeps the owned-row lengths. - Kimi-K3.1 admits prims_ts next to flashmla_kv and trtllm, with its layout error and PD warning. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r DCP mode Take the branch's tests now that main has sharded index-K DCP: per-rank Prims-TS partials over the owned top-k rows merge to the single-rank result (exact and packed plans, a rank owning none of a row's keys gives zero output and -inf LSE), and the combine merges prims_ts base e under sharded index-K while replicated index-K rejects it. FlashInfer 0.7.4 ships the kernel, so a planning failure on SM100/SM103/SM107 now fails instead of skipping. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
nvpohanh
left a comment
There was a problem hiding this comment.
[by Claude Code] This review is done by agents. Please feel free to disagree if they do not make sense.
The raw FP8 MLA store casts K/V unscaled, and the trtllm route applies k_scale to the logits only, while Prims-TS would also scale the output by it. Fail loudly on k_scale != 1 instead of diverging from trtllm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…wn plan Co-Authored-By: Claude Opus 5.5 <[email protected]>
…overlap check Unwrap TboAttnBackend and check isinstance(DeepseekSparseAttnBackend) instead of a defensive getattr of dsa_prefill_impl. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
/tag-and-rerun-ci |
|
[by Claude Code] I've started babysitting the NVIDIA CI of this PR. I'll check it every 30 minutes until all NV pipelines pass. If a relevant job fails, I may rerun it, merge the latest Anyone can stop me at any time: just post a comment asking me to stop (for example, |
|
/rerun-failed-ci |
|
/rerun-failed-ci |
# Conflicts: # python/sglang/srt/arg_groups/overrides.py # python/sglang/srt/layers/attention/dsa/prims_ts.py # python/sglang/srt/layers/attention/dsa_backend.py # python/sglang/srt/layers/cp/interleave.py
[by Claude Code]
Original authors: @nvpohanh
Port of #48414
This port takes the Kimi-K3.1 branch's opt-in DSA backend
--dsa-prefill-backend prims_ts/--dsa-decode-backend prims_ts. It runs FlashInfer's Prims-TS sparse MLA (BatchSparseMLADecodePagedTSWrapper: D576, native E4M3 KV, E4M3 query, FP8 MMA, natural-log LSE) instead of trtllm-gen, on the existingtrtllmDSA route. The route stays the same end to end (raw FP8 MLA cache, q quantize/concat, KV store, top-k transforms, row plan, DCP page-table localization); only the attention kernel call changes. It is faster than trtllm-gen and FlashMLA at decode, and its cache needs fewer bytes per token than FlashMLA's (see Speed). The defaults do not change.It needs FlashInfer 0.7.4 (#49771), which ships Prims-TS D576 native-FP8 sparse MLA: flashinfer-ai/flashinfer#7410 and flashinfer-ai/flashinfer#7984, plus flashinfer-ai/flashinfer#8186 for SM107.
It takes all of the branch's
prims_tscode that main now has a base for. That includes Kimi-K3.1, under its sharded index-K DCP, now that main has that DCP and Kimi-K3.1's opt-intrtllmDSA backend. It also includes SM107 (Rubin) support.What changed against the raw port:
Main's row plan.
_forward_prims_tsrestores rows with_restore_live_rows(row_plan), as main's_forward_trtllmdoes, as the branch now does too.Tests. The branch's
prims_tstests lived in its sharded-DCP test file, which main does not have. Here they are a new file,test_dsa_prims_ts.py, with the same cases:layers/dcp/layout.py, where main keepsis_mla_dcp_lse_base_on_e.The file is registered on B200 rather than
1-gpu-large(H100), since the kernel needs SM100/SM103/SM107. With FlashInfer 0.7.4 pinned, a planning failure on those GPUs now fails the test instead of skipping it.Docs. The two DSA backend tables list
prims_ts.KV scale.
_forward_prims_tsrejects a checkpoint KV scale (k_scale != 1). The raw FP8 store writes K/V unscaled, andtrtllmappliesk_scaleto the logits only, so the branch's output scaling byk_scalewould makeprims_tsdiverge fromtrtllm. Kimi-K3.1 has no KV scale.Overlap check.
DSAInterleaveCPIndexerOverlap._can_overlapunwrapsTboAttnBackendand narrows the backend withisinstance(DeepseekSparseAttnBackend)instead of agetattrofdsa_prefill_impl.The SM107 change. Its FlashInfer overlay in the branch's container image is not taken: main installs FlashInfer 0.7.4 as a package.
Left for the port of the branch's fused FP8 q builder (
_trtllm_q_fp8): its no-head-padding mode forprims_ts(pad_heads=not use_prims_ts). Without the builder,prims_tsreads q from main's FP8 quantize path, which already keeps the layer's head count; main pads heads only right before trtllm-gen.Left for the port of the branch's alt-stream decode absorb: its raw-FP8 branch in
forward_mla.py, which skips the adjacent bf16 q buffer.Modifications
layers/attention/dsa/prims_ts.py(new):PrimsTSSparseMLAholds the planned launches. Plans are exact per row count up to 1024 rows and use power-of-two packed capacities above that.run(validate=False).check_prims_ts_sparse_mla_supportis the startup check (D576 MLA, SM100/SM103/SM107, and an installed FlashInfer that can plan the kernel). If FlashInfer refuses, its reason is part of the error.dsa_backend.py:prims_tsjoins_DSA_IMPL_T.uses_raw_fp8_dsa_kv_layout(backend)(trtllmorprims_ts) replaces the== "trtllm"layout checks.fp8_e4m3KV cache, a GPU or FlashInfer that cannot run the kernel, and replicated index-K DCP (_validate_prims_ts_dcp). That DCP combine merges base-2 LSE partials, while Prims-TS returns a natural-log LSE. Under Kimi-K3.1's sharded index-K the combine merges non-trtllm partials base e, soprims_tsis accepted there._forward_trtllm(use_prims_ts=...)hands off to_forward_prims_tsonce the q, KV store, row plan and page table are ready. Prims-TS runs any head count, so q is not padded._forward_prims_tsreturns the partial and its LSE for the merge. A sharded-DCP decode partial keeps the owned-row lengths from the localized page table._forward_prims_tsrejectsk_scale != 1(see above).arg_groups/fields/exec_.py: addsprims_tsas a choice for both DSA backend flags.arg_groups/overrides.py:prims_tsneeds CUDA and--kv-cache-dtype fp8_e4m3.prims_tsnext toflashmla_kvandtrtllm. It warns that the numerics differ from the referencefp8_ds_mla, and that PD peers must both use a raw-FP8-KV backend.mem_cache/kv_cache_configurator.py,forward_mha.py,forward_mla.py(_fuse_rope_for_trtllm_mla),layers/cp/interleave.py: treatprims_tsliketrtllmthroughuses_raw_fp8_dsa_kv_layout. The interleave overlap check narrows the backend by type.An unset phase gets a layout-compatible backend:
trtllm, which uses the same 576 B/token layout.In PD disaggregation, both servers need a raw-FP8-KV backend (
trtllmorprims_ts).Tests
At this head, on CPU only (no SM100 GPU in this environment):
test_dsa_prims_ts.py: 2 passed (the LSE base under sharded and replicated index-K), 2 skipped (GPU, "needs SM100/SM103/SM107").test_dsa_tilelang_fp8_validation.py,test_dsa_dcp_backend_defaults.py,test_pool_configurator.py,test_dsa_backend_dp_padding.py,test_model_overrides.py,test_rocm_mla_absorb_fusion_gate.py.test_page_interleave_shard.pyalso passes, apart from its Rust-core variants, which need a Cargo build this environment lacks.BatchSparseMLADecodePagedTSWrapper.plan/runandget_prims_ts_sparse_mla_decode_workspace_sizetake the argumentsprims_ts.pypasses (checked against thev0.7.4tag).On the Kimi-K3.1 branch (#48414), on GB300, with Prims-TS from FlashInfer main copied onto 0.7.2:
784bfaf5(perf(prims-ts): run sparse MLA on SM107 and trim the NVFP4 KV converter flashinfer-ai/flashinfer#8186):check_prims_ts_sparse_mla_supportpassed for 12, 24, 48 and 96 heads;This port has not been run on a GPU on main, and nothing has run on SM107 hardware: Rubin support goes only as far as flashinfer-ai/flashinfer#8186 and its tests. No model other than Kimi-K3.1 has been served with
prims_ts. That includes DeepSeek-V3.2 and GLM-5, which this port admits without DCP. The GPU cases oftest_dsa_prims_ts.pyrun in this PR's B200 CI.Accuracy
All measured on the Kimi-K3.1 branch (#48414):
Speed
Kernel time per DSA layer (us), measured on the Kimi-K3.1 branch: GB300, 96 heads, about 256 keys per row (one DCP8 rank's share of 2048 top-k), 6 query rows per request, cold L2, CUDA graph.
flashmla_kv)prims_ts)Not yet covered:
--mem-fraction-statichas sized the KV pool.FLASHINFER_WORKSPACE_BASEat a persistent path to keep the cache.🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ⏳ Run #43157175119
Latest PR Test (Extra): ❌ Run #43157174952
Latest PR Test (AMD ROCm 10): ⏳ Run #43157175002
Tip
Base CI only covers the core cases, and its workload may not exercise your change. Use
/rerun-testto run the tests your change touches, or copy this prompt for an agent: