Skip to content

[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
sgl-project:mainfrom
Heisenberg-Yin:litetop-decode
Open

Heisenberg-Yin wants to merge 3 commits into
sgl-project:mainfrom
Heisenberg-Yin:litetop-decode

Conversation

@Heisenberg-Yin

@Heisenberg-Yin Heisenberg-Yin commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

@DarkSharpness could you take another look? This revision follows sgl-project/DeepGEMM dev after 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 onto main 3671503); 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

Modifications

  • kernels/jit/csrc/litetopk_decode/select.cuh: one selector templated over Bf16Top2048 and Bf16Top512.
    • Rows of up to 16384 scores are selected from registers by one CTA; longer rows are split across CTAs.
    • The scratch returns to zero after every call, so CUDA graphs need no reset kernel. PDL is on.
  • kernels/ops/attention/litetopk_decode.py: configs, storage / plan, device and DeepGEMM API detection, and select(...), which writes physical KV slots (padded with -1).
  • srt/layers/attention/litetopk_decode.py: per-process selector buffers, grown only outside graph capture.
  • Serving is opt-in with SGLANG_OPT_LITETOPK_DECODE=1 (default off):
    • DSA (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.
    • DeepSeek-V4.1 (dsv4/v41_indexer/*, deepseek_v4_backend.py, dsv4/metadata.py): FullTopK and SparseTable decode / verify.
    • Everything else keeps the default top-k.

Accuracy

Speed (kernel level, B200)

What is timed: indexer scores + top-k of one layer per decode / verify step.

GLM-5.2 (DSA), top-2048

Grid: B 1-128 × L 8K-~1M × next_n 1-4, 383 cells.

  • vs [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350: geomean 2.934×. LiteTopK is faster in all 383 cells (minimum 1.191×). Within one rank's KV pool (287 cells) the geomean is 3.048×.
  • Why the gain is this large: [Refactor] [NVIDIA] attention: Migrate DSA/DSV4 indexers to DeepGEMM 0.2.5.post2 #44350 widens the whole BF16 logits buffer to FP32 on every call.
    • Under CUDA-graph decode that buffer is as wide as the page table (1M tokens here), so the copy costs 92-95 µs whatever L is.
    • The speedup therefore falls with L: 5.67× at 8K, 1.29× at ~1M.
    • At B 1, ~1M: 41.6 vs 59.5 µs (1.43×).
  • Selector alone, on identical BF16 logits: 1.623× against topk_transform_paged_v2 on a pre-widened FP32 copy. LiteTopK is faster in 378 of 383 cells.
  • DeepSelect BF16 top-2048, for reference (SGLang does not route DSA to it): LiteTopK is 1.317× faster.
    • It is slower in 44 cells, all at 8K / 16K. The worst is 0.870× (layer 38, B 16, 16K: 24.32 vs 21.22 µs).
    • On layer 38, every score falls in a unit bin, with 76-136-way ties at the k-th value.
L 8K 16K 32K 64K 128K 256K 512K ~1M
vs #44350 5.672× 4.344× 3.549× 2.710× 2.037× 1.626× 1.404× 1.292×
vs DeepSelect 1.010× 1.080× 1.192× 1.295× 1.488× 2.073× 1.868× 1.961×
median µs, #44350 / LiteTopK / DeepSelect 112.3 / 17.7 / 16.9 118.8 / 24.1 / 24.8 125.9 / 29.8 / 34.4 141.3 / 45.3 / 56.2 98.5 / 46.0 / 67.2 55.1 / 33.5 / 76.6 113.8 / 80.1 / 145.6 122.9 / 98.5 / 191.1

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.

tokens per request 1 2 3 4 5 6
vs #44350 2.486× 3.730× 4.702× 5.730× 5.586× 6.069×
vs DeepSelect 1.064× 1.277× 1.432× 1.619× 1.502× 1.556×
vs DeepSelect, within the KV pool 1.069× 1.108× 1.151× 1.202× 1.143× 1.153×

Notes


CI States

Latest PR Test (Base): ❌ Run #39106165330
Latest PR Test (Extra): ❌ Run #39106165283
Latest PR Test (AMD ROCm 10): ❌ Run #39106165266

@github-actions github-actions Bot added documentation Improvements or additions to documentation jit-kernel labels Sep 22, 2026
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 Heisenberg-Yin changed the title [DSA] Add opt-in LiteTopK decode top-k path for SM100 [DSA][DSv4.1] Add opt-in LiteTopK decode top-k for SM100 Oct 9, 2026
@Heisenberg-Yin Heisenberg-Yin changed the title [DSA][DSv4.1] Add opt-in LiteTopK decode top-k for SM100 [Perf] [NVIDIA] attention: Add opt-in LiteTopK decode top-k for DSA and DeepSeek-V4.1 on SM100 Oct 16, 2026
zcnrex and others added 3 commits October 18, 2026 15:01
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]>

This branch has not been deployed

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

Labels

deepseek documentation Improvements or additions to documentation jit-kernel memory-pool

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants