Repository navigation
[Perf] [NVIDIA] attention: Add opt-in LiteTopK decode top-k for DSA and DeepSeek-V4.1 on SM100 - #40764
Open
Heisenberg-Yin wants to merge 3 commits into
Open
[Perf] [NVIDIA] attention: Add opt-in LiteTopK decode top-k for DSA and DeepSeek-V4.1 on SM100#40764Heisenberg-Yin wants to merge 3 commits into
Heisenberg-Yin wants to merge 3 commits into
Conversation
Heisenberg-Yin
requested review from
BBuf,
DarkSharpness,
HaiShaw,
HydraQYH,
celve and
yuan-luo
as code owners
September 22, 2026 16:34
Heisenberg-Yin
added a commit
to Heisenberg-Yin/sglang
that referenced
this pull request
Oct 3, 2026
…layers on SM100 Add the exact BF16 LiteTopK path and an opt-in DeepSeek-V4.1 serving hook. Default dispatch is unchanged. Depends on sgl-project#40764 for the experimental LiteTopK package and its FP32 module. - kernels/experimental/litetopk_decode/bf16.py + bf16_exact/: Bf16Dsv41DecodeStorage / Bf16Dsv41DecodePlan pair the sgl-gemm producer (fp4_paged_mqa_logits_bf16 with an exact per-score histogram, get_paged_mqa_logits_bf16_metadata) with one selector launch. Output follows topk_transform_paged_v2 with page 128: unordered int32 slots padded with -1, ties to the lower physical slot. - SGLANG_OPT_DSV41_LITETOPK_DECODE (off by default) routes FullTopKIndexer.topk_decode and the top-512 of SparseTableBackend.publish_decode through the BF16 plan (dsv4/v41_indexer/litetopk.py). DeepseekV4AttnBackend rebuilds the paired schedule once per decode / target-verify forward from live lengths and request IDs, so CUDA graph replay stays correct. An unsupported GPU or DeepGEMM keeps the default top-k. - SGLANG_DEBUG_DSV41_LITETOPK_CHECK[_FILE] also runs the default top-k and reports rows whose slot sets differ. - amax8_varlen accepts BF16 scores and still emits FP32 block keys. - Tests: test_litetopk_decode_dsv41_bf16.py (SGLANG_TEST_DSV41_BF16_EXACT=1) and test_dsv41_litetopk_decode.py. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Heisenberg-Yin
added a commit
to Heisenberg-Yin/sglang
that referenced
this pull request
Oct 3, 2026
Add the experimental kernels' .clang-format to bf16_exact/, and state in its README that the BF16 path builds on sgl-project#40764's design but does not depend on its standalone FP32 module. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Heisenberg-Yin
force-pushed
the
litetop-decode
branch
from
October 9, 2026 09:26
7e252c5 to
ef7e6fb
Compare
Heisenberg-Yin
requested review from
1am9trash,
Fridge003,
Qiaolin-Yu,
YAMY1234,
hebiao064,
hubertlu-tw,
ispobock,
kkHuang-amd,
merrymercy and
rainj-me
as code owners
October 9, 2026 09:26
Heisenberg-Yin
force-pushed
the
litetop-decode
branch
from
October 9, 2026 11:17
ef7e6fb to
abd4c4a
Compare
Heisenberg-Yin
force-pushed
the
litetop-decode
branch
from
October 16, 2026 13:20
abd4c4a to
107a3c5
Compare
DeepGEMM's upstream sync (sgl-project/DeepGEMM#103) removes fp8_mqa_logits and fp8_paged_mqa_logits and changes fp8_fp4_*mqa_logits: no clean_logits or logits_dtype, compressed ragged logits, and on SM100 MXFP8/MXFP4 Q/K (int32 packed UE8M0 scales), BF16 weights, BF16 logits, a varlen `indices` per paged query row and a new paged schedule format. SM90 keeps E4M3 + FP32 scales; SM120 has no DeepGEMM MQA kernels any more. - deep_gemm_mqa_logits.py wraps the new API in the old call shapes (FP32 logits, 256-column row stride): unit Q scales when the Q scale is folded into the weights, BF16 weights, K scales repacked to UE8M0, a Triton kernel that widens compressed rows back to full width for the DSA top-k, and indices / varlen-flattened metadata on SM100. - Index-K caches on SM100 round each token's scale to a power of two and, when DeepGEMM reads the paged cache, store it as packed UE8M0 (lossless; FP8 bytes unchanged). Writers: kpool Triton kernels, DSA fused_store_index_k_cache / fused_k_indexer_norm_rope_store / act_quant fallback, DSV4 fused_norm_rope_indexer. Other paged backends keep FP32. - kpool and DSA ragged paths read compressed logits; DSA paged verify uses one row per token on SM100. - DSV4 C4 builds DeepGEMM metadata on SM100; V4.1 runs DeepGEMM index logits only on SM100 (SM120 takes the Triton indexer), and the FP4 C4 indexer requires SM100 or gfx95. - Logits row-chunk budgets count the BF16 buffer on SM100. - Tests: kpool DeepGEMM logits, fused-store UE8M0 packing, DSA paged DeepGEMM on SM100; updated mocks and direct calls for the new API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
On SM100 DeepGEMM's MQA logits take BF16 weights and return BF16, so two tests that assumed FP32 failed on a B300 without a kernel fault: - test_deepgemm_paged_mqa_logits: elementwise atol=2e-2/rtol=8e-3 rejects logits near zero, where positive and negative head terms cancel. Across configs calc_diff is ~1e-5 and the worst error is <=1% of the sum of |terms| (~2.5 BF16 ulps). On SM100 it now checks calc_diff < 3e-5, upstream DeepGEMM's own SM100 bound; SM90 keeps the elementwise check. - test_dsv41_prefill_sparse_indexer: the dense reference scores are now BF16, whose step at |logit| ~100 is 0.5-1, so block and position scores tie exactly and torch vs kernel tie-breaking pick different members (88/256 rows at 256x40000, every swap an exact tie). Block and own-top-k selections are compared as score multisets, and picks tied with the selection floor count toward the overlap. Both pass on B300 (SM103) with a sgl-deep-gemm wheel built from sgl-project/DeepGEMM#103: 192/192 and 6/6 (+6 subtests). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd DeepSeek-V4.1 on SM100 One exact, histogram-driven decode top-k selector for the DSA indexer (top-2048, 64-slot pages) and the DeepSeek-V4.1 ratio-1/2 index layers (top-512, 128-slot pages), both reading the BF16 logits of DeepGEMM's SM100 paged MQA logits kernel. The kernel counts each row's scores into 1024 coarse bins while it writes them (histogram=); the selector derives the crossing bin from the histogram, selects everything above it and ranks the bin exactly (ties to the lower physical slot), falling back to an exact whole-row select when the histogram disagrees with the scores or the crossing bin overflows the candidate buffer. Rows of up to 16384 scores are selected by one CTA from registers. Scratch returns to rest after every call, so CUDA graph replays need no reset. Grid sizes follow the GPU's SM count, which the storage passes to every call; the row thresholds between launch shapes were measured on B200 (148 SMs). - kernels/jit/csrc/litetopk_decode/select.cuh: the selector, templated over the two shipped configurations (Bf16Top2048, Bf16Top512). - kernels/ops/attention/litetopk_decode.py: configs, storage / plan, and runtime detection of the device and of DeepGEMM's BF16 paged MQA logits API with a histogram (declared Python parameters: indices and histogram, no logits_dtype). - srt/layers/attention/litetopk_decode.py: per-process selector buffers, plans per row count, CUDA-graph-safe growth. - DSA, on the MXFP8 (UE8M0) index-K cache: Indexer._get_topk_paged passes the plan's histogram through deep_gemm_mqa_logits.fp8_paged_mqa_logits, which then returns the BF16 logits without widening them, and replaces the sgl-kernel fused paged top-k transform for decode and verify; when LiteTopK serves verify, verify tokens get their causal lengths in the producer schedule. The torch paged MQA logits fallback and the flashinfer / torch top-k backends keep the default top-k. - DeepSeek-V4.1: FullTopKIndexer.topk_decode and SparseTableBackend.publish_decode count the histogram with fp8_fp4_paged_mqa_logits and select in place of the default top-k (topk_transform_paged_v2 on the widened FP32 logits). Decode reuses the forward's DeepGEMM schedule; verify schedules the tokens of a request as one request (one KV pass), once per forward. Off by default (SGLANG_OPT_LITETOPK_DECODE); unsupported devices, DeepGEMM builds, modes and shapes keep the default top-k. Builds on sgl-project#44350 (the fp8_fp4 MQA logits API); the histogram producer comes from sgl-project/DeepGEMM#94. Also covers DeepSeek-V4.1, replacing sgl-project#42351. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Heisenberg-Yin
force-pushed
the
litetop-decode
branch
from
October 18, 2026 07:01
107a3c5 to
01e2939
Compare
Heisenberg-Yin
requested review from
Ying1123,
alphabetc1,
hanming-lu,
hnyls2002,
huangtingwei9988,
hzh0425,
xiezhq-hermann and
yizhang2077
as code owners
October 18, 2026 07:01
This was referenced Nov 5, 2026
This branch has not been deployed
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.
@DarkSharpness could you take another look? This revision follows sgl-project/DeepGEMM
devafter deepseek-ai/DeepGEMM#462, where SM100 paged MQA logits are BF16-only. It is stacked on #44350 (zcnrex), which moves the DSA / DSV4 indexers onto that API. Until #44350 lands, this branch also carries #44350's two commits (rebased ontomain3671503); this PR is the last commit.Motivation
DSA models (GLM-5 family, DeepSeek-V3.2) and DeepSeek-V4.1's ratio-1/2 index layers score every KV position of each decode / verify token with DeepGEMM's paged MQA kernel, then pick the top-k (2048 for DSA, 512 for V4.1) in a separate kernel. With #44350, the kernel writes BF16 logits and SGLang widens them to FP32 for
topk_transform_paged_v2. topk v2's first pass over the scores only builds a coarse histogram.With sgl-project/DeepGEMM#94, the scoring kernel counts that histogram while it writes the scores. LiteTopK reads the histogram, finds the bin holding the k-th score, takes everything above that bin, and ranks the bin exactly, all in one pass over the BF16 scores. Nothing is widened.
The selection is exact, with ties going to the lower physical slot. If the histogram disagrees with the scores, or the crossing bin is too large, LiteTopK falls back to an exact radix select.
Changes since the last revision
Fp32Top2048is removed. The newBf16Top2048(64-slot pages) serves DSA.Bf16Top512(V4.1) is unchanged, and its 4 kernels have the same SASS as before.DSA_MAX_NEXT_Nis removed).dev'sfp8_fp4_paged_mqa_logits(..., indices=, histogram=).indicesandhistogram, and must not declarelogits_dtype. Builds without DeepGEMM#94 (including the pinned 0.2.2) and the previous how to use the finetuned mistral model for inference with sglang #94 revision are refused with one warning..clone()), and the class default ofpaged_mqa_causal_ctx_lens.Modifications
kernels/jit/csrc/litetopk_decode/select.cuh: one selector templated overBf16Top2048andBf16Top512.kernels/ops/attention/litetopk_decode.py: configs, storage / plan, device and DeepGEMM API detection, andselect(...), which writes physical KV slots (padded with-1).srt/layers/attention/litetopk_decode.py: per-process selector buffers, grown only outside graph capture.SGLANG_OPT_LITETOPK_DECODE=1(default off):dsa/dsa_indexer.py,dsa_backend.py,deep_gemm_mqa_logits.py): decode and target verify, for 32 index heads of dim 128 and top-2048. Verify tokens get causal lengths.dsv4/v41_indexer/*,deepseek_v4_backend.py,dsv4/metadata.py): FullTopK and SparseTable decode / verify.Accuracy
main/ [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350: 201 passed ([Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350 alone: 187).main/ [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350 GPU tests (11 files), flag off: the same results on [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350's head and on this branch. Flag on:test_dsa_metadata_replay.py,test_dsa_metadata.py,test_dsa_kpool_metadata_fusion.pyandtest_dsa_indexer.pypass.topk_v2's by value on 1,008 / 1,008 rows.topk_transform_paged_v2on the widened logits returned picks below the true 2,048th score on 92 of 992 decode rows (long rows, its cluster path).Speed (kernel level, B200)
What is timed: indexer scores + top-k of one layer per decode / verify step.
dev's producer →to_fp32_logits→topk_transform_paged_v2. Speedup = baseline / LiteTopK.GLM-5.2 (DSA), top-2048
Grid: B 1-128 × L 8K-~1M ×
next_n1-4, 383 cells.topk_transform_paged_v2on a pre-widened FP32 copy. LiteTopK is faster in 378 of 383 cells.DeepSeek-V4.1, top-512
Grid: 1-6 tokens per request × B {1, 16, 64} × L {4K, 16K, 64K, 256K, ~1M} = 90 core cells, plus 52 extra cells (DSpark, high batch, DeepSelect tier boundaries). Every cell is averaged over two runs.
main's opt-in route; on [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350 it is unreachable, because [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350 widens.Notes
CI States
Latest PR Test (Base): ❌ Run #39106165330
Latest PR Test (Extra): ❌ Run #39106165283
Latest PR Test (AMD ROCm 10): ❌ Run #39106165266