Skip to content

[Feat] [GB300] deepseek_v41: NVIDIA megakernel plugin for DeepSeek-V4.1 decode on GB300 - #1

Merged
Oasis-Git merged 2 commits into
sgl-project:mainfrom
YAMY1234:deepseek-v41-gb300
Oct 19, 2026
Merged

Oasis-Git merged 2 commits into
sgl-project:mainfrom
YAMY1234:deepseek-v41-gb300

Conversation

@YAMY1234

@YAMY1234 YAMY1234 commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

At 1 to 8 concurrent requests, a DeepSeek-V4.1-Flash DSpark decode step on GB300 is bound by small GEMMs, per-layer kernel boundaries, the two TP all-reduces per layer and the host work between kernels, not by HBM bandwidth.

This PR adds the NVIDIA megakernel for that step as an SGLang plugin (package sgl_nvidia_mega).

  • It registers a spec with SGLang's sglang.srt.megakernels ([Feat] [NVIDIA] megakernels: Add --enable-nvidia-megakernel and a layer for NVIDIA megakernel plugins sglang#44945, which this PR needs), and --enable-nvidia-megakernel turns it on.
  • SGLang then replaces the target model's forward for the covered DSpark verify steps, and runs the DSpark draft step through its draft-sampler entry. Both run over the weights and caches SGLang loaded.
  • Everything the plugin needs from SGLang's runtime state (the attention metadata of the running forward, the KV pool, the Engram tables) reaches it as data through #44945's one binding file.
  • SGLang's DSpark worker is unchanged; it calls nothing in this plugin.
pip install -e .   # with an SGLang that has sgl-project/sglang#44945
SGLANG_SPEC_TP_SYNC=rng,-dspark-accept-sample \
SGLANG_ENABLE_DSV41_ENGRAM_HOST_TABLE=1 SGLANG_DSV41_ENGRAM_HOST_TABLE_LAYOUT=per_rank \
sglang serve --model-path deepseek-ai/DeepSeek-V4.1-Flash --tp 4 --kv-cache-dtype fp8_e4m3 \
  --dsv4-attn-backend flashmla --speculative-algorithm DSPARK --speculative-dspark-block-size 5 \
  --enable-nvidia-megakernel
# every rank, at startup, one line per spec (target and DSpark draft):
# NVIDIA megakernel: active (DeepSeek-V4.1: slots [...]; ...)
# while serving, from the kernels' own dispatch, once per width:
# NVIDIA megakernel: hop A attention post serves 12 rows
# NVIDIA megakernel: fast gate top-k serves 24 rows
  • SGLANG_SPEC_TP_SYNC=rng,-dspark-accept-sample: the spec-decode rank sync every measurement ran with. It removes a sync in SGLang's stock accept path. The target spec requires it, so a launch without it stops with SGLANG_SPEC_TP_SYNC 'all' is not 'rng,-dspark-accept-sample'.
  • SGLANG_ENABLE_DSV41_ENGRAM_HOST_TABLE=1, SGLANG_DSV41_ENGRAM_HOST_TABLE_LAYOUT=per_rank: SGLang's per-rank Engram host table, which the Engram prefetch reads. The target spec requires both; a launch without them stops, naming each.
  • NUMA: SGLang's generic process membind (SGLANG_NUMA_BIND_V2) only; no per-table Engram placement.

Measured with exactly this launch (GB300, TP4, static ISL 8K / OSL 1K, order-balanced on one node):

  • 1,038 tok/s/user at batch 1;
  • decode step p50 3.386 / 4.155 / 5.168 / 7.197 ms at batch 1 / 2 / 4 / 8;
  • GSM8K 197 / 200; long-context sets 96 / 96 and 32 / 32 (accept length 4.22 / 4.27).

Details below.

Modifications

How SGLang runs it

  1. Registration (sgl_nvidia_mega/plugin.py, every SGLang process). SGLang's plugin loader calls register(), which registers two specs with sglang.srt.megakernels: the target (DeepseekV4ForCausalLM) and the DSpark draft (DeepseekV4ForCausalLMDSpark). The spec modules are imported only on a server with the flag.
  2. Argument resolution (launcher, before the weights load).
    • SGLang checks each spec's requirements on the resolved settings, together with its own feature probes.
    • The target spec (deepseek_v41/spec.py) states, as data: SM103, TP 4, 152 SMs, bf16, FP8 KV cache, DSpark, --dsv4-attn-backend flashmla, the measured launch's environment (the TP sync setting and the per-rank Engram host table), no MoE all-to-all, serial weight loading, FULL decode CUDA graphs (required), and the model's HF config.
    • An unsupported setup stops the launch, listing every reason.
  3. Build (each rank, after the model is loaded, before the KV cache is sized).
    • SGLang calls the spec's build with a BuildContext, whose binding is the model's MegakernelBinding.
    • The build allocates the buffers and the Lamport mailboxes, and compiles the kernels and their tactics for every covered width.
    • The Engram prefetch is compared bit for bit with SGLang's wkv(embed(ids)) at every width it serves; on a mismatch it stays off on every rank, with the reason logged.
  4. Dispatch. SGLang replaces forward on that model instance only.
    • plan covers a DSpark verify step whose rows are a built width, while SGLang captures the decode graphs. It decides from host state alone, so every rank decides the same, and a captured CUDA graph replays the decision made at capture.
    • A covered step receives the running forward's ForwardView as Step.view. Every other step, including any step SGLang runs outside a graph, runs the model's own forward.
    • The DSpark draft spec offers SGLang's draft-sampler entry; its step reads its own runner's view through binding.forward_view().
  5. SGLang state, as data (sglang/srt/megakernels/binding.py, #44945):
    • ForwardView: the attention backend's forward metadata, the KV pool (KvPoolView, under the pool's own method names), and the backend's attention, KV-write, SWA-location and compressor / indexer calls.
    • MegakernelBinding: the Engram history and the per-layer host tables.
    • test/unit/test_contract.py pins SGLang's TOLERATED_CALLABLES, TOLERATED_WRITES and TOLERATED_READS lists, so anything new fails a CPU test.
  6. Engagement comes from the dispatch path, not from the request:
    • every rank logs one NVIDIA megakernel: active (...) line per spec with the slots built and their widths;
    • hop A attention post serves N rows and fast gate top-k serves N rows come from the kernels' own dispatch, once per width.

The plugin reads SGLang's modules and ops by name. test/unit/test_contract.py pins every name it reads, on CPU, so an SGLang rename fails a CPU test that names the file to repair, not a server at startup.

Test only: sgl_nvidia_mega/plugin.py registers ONE AROUND hook, runtime/parity.py:around_attention_forward on DeepseekV4AttnBackend.forward, and only with SGLANG_NVIDIA_MEGAKERNEL_PARITY_SORT_TOPK=1. It sorts the sparse-attention indices in both forwards for the parity compare. It is never registered in a normal launch.

The covered step

The executor (deepseek_v41/runtime/) runs the target step from one package per part of the layer (Engram, boundary, attention, MoE, logits). Each part runs over SGLang's loaded weights and caches. Each has a stock implementation that reproduces SGLang's step from SGLang's own ops. The slot plan (plan.py) swaps in this package's kernels where they pay off.

The default build runs these 36 slots:

Group Slots What runs
Target step (20) hop_a, hop_a_batch2, hop_b, hop_b_start_trigger, early_trigger, two_shot wo_b on FlashInfer's low-latency MXFP8 GEMM, storing each row into a Lamport mailbox on every rank; a Lamport consumer reduces and runs the mHC post, combine and RMSNorm. Hop A is the attention post: 1-8 rows, and 9-16 rows (batch 2) on the fused wo_a's row-tile scales. Hop B is the MoE post, with its start and early triggers. The two-shot all-reduce + mHC covers the rows past the routed pair (49-384).
moe, gate, gate_wide the routed experts on a route kernel and a pair of block-scaled tcgen05 GEMMs (1-48 rows); the router top-k, bit for bit SGLang's fused gate, without a fill kernel, at every routed width (1-48 rows)
attn, attn_qkv_a, attn_qkv_a_bf16, index_q_mxfp8_in, kv_before_wait, hc_prenorm the attention chain: wqkv_a on the low-latency GEMM (MXFP8 and BF16 inputs, 1-8 rows); the indexer wq_b on the chain's q_lora quantization; swap-AB attention that gathers the KV cache before its PDL wait, where every KV write is already ordered before it; the mHC prenorm on DeepGEMM
embed, engram, aux_mean_stats, sched_ahead, req_pos_once the replicated embedding; the Engram lookup + WKV on a prefetch stream at batch 1 (rank-local work only; both all-reduces stay on the main stream); the aux mean on the mHC stats stream; FlashMLA schedules and request positions computed once per step
DSpark draft step (16) draft, draft_attn, draft_moe, draft_gate, draft_tail, draft_post, draft_seam, draft_qkv_a, draft_qfront, draft_wo_a, draft_swapab, draft_prenorm, draft_qkv_a_ll, draft_wo_b_ll, markov, draft_lm_head the draft layers on the routed pair, fast gate, ported attention stages, prenorm and post seams; the Markov proposal in one kernel; the draft LM head on the low-M kernel

The sorted list of these 36 names has sha256 prefix 22c638c8af6e (the slot-set identity the GPU runs were checked against).

Outside the default list, opt-in: draft_embed.

The kernels stay separate launches chained with programmatic dependent launch (PDL). On GB300, a kernel boundary with PDL costs less than an exchange between all blocks of one persistent grid.

Not in this PR:

  • the decode-step pieces that needed hooks inside SGLang's DSpark worker: the verify window in one launch, the verify ids written by the draft graph, the fused verify epilogue with the deferred draft-KV commit and the Engram history commit, and the target's low-M verify LM head;
  • the lean indexer query, which needs SGLang's indexer to accept a prepared query.

SGLANG_NVIDIA_MEGAKERNEL_SLOTS overrides the default list for tests and bisecting (empty: every step function's stock code). An unknown slot, or a requested slot that builds at no width, stops the server. For example, dropping hop_a_batch2 restores hop A at 1-8 rows with the 8-row mailbox; dropping gate_wide restores the fast gate's earlier widths.

Build and failure behavior

  • Nothing is compiled inside a step: every kernel compiles at build. A build failure on any rank is gathered and raised on every rank.
  • Weights are read in place.
  • The setup checks read SGLang's resolved settings, not the raw server arguments.
  • The plugin keeps no process-global state; each spec's build holds its own binding.
  • The stock steps are adapted from SGLang's own model code (Apache-2.0); each adapted file names its origin, and NOTICE lists SGLang.
  • Every wait on another GPU (the Lamport consumers) has a deadline: half of --watchdog-timeout, at most 60 s. A rank that misses it faults, so no output of the failed step is used.
  • No collective runs on a side stream: side streams do rank-local work only, and every all-reduce runs on the main stream in graph order on every rank.

Accuracy Tests

GB300, TP4, EP 1, DSpark block 5, FP8 KV cache, the launch above (the default 36 slots), SGLang #44945 with the megakernel binding. Measured on this PR's plugin code (identical to the measured tree).

Check Result
GSM8K-200, real DSpark acceptance, c = 8 / c = 64 197 / 200, 197 / 200
Needle (5 passes) 128 / 135 (26, 24, 25, 27, 26 of 27)
Long-context sets (16K and 24-32K prompts, c = 1) 96 / 96 and 32 / 32; accept length 4.22 and 4.27
Dispatch: hop A / fast gate lines at their widths on 4 / 4 ranks hop A at 6 and 12 rows and the fast gate at 24 rows on 4 / 4 ranks; no DSpark decode hooks installed (0 / 4)
Engram per-table huge-page coverage, per rank and layer 100 % on all 4 ranks x layers 1 and 14
Server errors over the whole run 0
  • The Engram table numbers were measured on nodes gated for free huge pages, with the page cache evicted and the huge pages warmed before each launch. On fragmented nodes the per_rank table can get fewer huge pages without a NUMA placement fix in SGLang.
  • Per slot: the Engram prefetch is compared bit for bit with SGLang at build. The other slots are covered by the GPU kernel tests against SGLang's own ops (bit for bit where the slot claims it, otherwise within the declared tolerances in spec.NUMERICS) and by the serving accuracy runs.
  • Kernel tests (test/kernels, GB300): the routed pair against FlashInfer's routed MoE (1-48 rows, eager and CUDA graph); the top-k against SGLang's fused gate (bitwise, including ties, NaN logits, padded rows and vision-token routing); the publishing wo_b (including the 9-16-row tiles), the Lamport consumers and the wait deadline; the Markov proposal and the low-M LM heads. On the same plugin code before the test files were consolidated, on four GB300s: 568 passed, 0 failed, plus 40 / 40 cases of the poisoned-memory wq_b test. A re-run on this head's consolidated test files is in progress. The multi-GPU tests (hop B, the two-shot all-reduce, wo_b publish) run through their own 4-GPU entry points. 67 are skipped: 64 fused wo_a widths above the 32 rows SGLang serves, 2 that need the model checkpoint, and 1 benchmark.
  • CPU tests (test/unit, against #44945 with the binding): 238 passed, 0 skipped, including test_deepseek_v41_binding_reach, which drives the real build path and checks every package receives the binding.
    • They include the SGLang contract with the pinned binding lists, the slot plan, and the refusal of a launch without the required environment, each variable named.
    • Every Python file of the package imports (51 files, __init__.py included).
    • pre-commit run --all-files passes on a fresh copy.

Speed Tests and Profiling

GB300, TP4, DeepSeek-V4.1-Flash, DSpark, static ISL 8K / OSL 1K, the launch above. One node, fresh launches in the order pre-binding → this PR → this PR → pre-binding, two rounds each. Every number is the mean of the four runs' decode-step p50 (range in brackets); each rank is bound to its GPU's NUMA node.

Requests Decode step p50, pre-binding build (ms) Decode step p50, this PR (ms) Change
1 3.275 [3.257-3.306] 3.386 [3.335-3.447] slower: +110 us (+3.4 %), in both orders (+128 / +93 us); no run overlaps
2 4.156 [4.144-4.178] 4.155 [4.109-4.196] unchanged within noise (-1 us)
4 5.137 [5.093-5.186] 5.168 [5.110-5.218] unchanged within noise (+31 us)
8 7.075 [7.033-7.159] 7.197 [7.154-7.219] slower: +122 us (+1.7 %), in both orders (+82 / +162 us); the ranges overlap by 5 us
  • The pre-binding build is the measured build before this design: SGLang 944fe61fc6c + this plugin at 141fb3e, 41 slots set by env, with the decode-step hooks inside SGLang's DSpark worker and SGLang's per-table Engram NUMA placement. Same node, same session. The SGLang side of this PR has no per-table NUMA placement, so the change column includes it.
  • Per-rank Engram huge-page coverage in these runs: 100 % on every rank and Engram layer (4 ranks x layers 1 and 14) in all four runs, both builds, on a node gated for free huge pages with the page cache evicted before each launch.
  • Batches 2 and 4 are unchanged within the round-to-round noise. Batch 1 is slower: every run of this PR is slower than every run of the pre-binding build. Batch 8 is slower in both orders, but its ranges overlap by 5 us, so its size is less certain. A closely related build (the same plugin defaults plus the lean indexer query and the per-table NUMA placement) was also slower at batch 8 against the same pre-binding build, in a second job on another node (+226 / +174 us by order).
  • Where this PR is slower, the likely cause is what this design leaves in SGLang: the verify tail with the deferred draft-KV commit and the verify ids written by the draft graph (both need hooks in SGLang's DSpark worker), the draft runner's metadata graph, and the lean indexer query. Hop A at 9-16 rows and the fast gate at every routed width offset them at batch 2-4. This run does not separate these pieces, nor the per-table NUMA placement the pre-binding build also had.
  • The table compares two configurations; it is not a re-measurement of the same code. This PR has no decode-step hooks and no lean indexer query; from batch 2 on, it adds hop A at 9-16 rows and the fast gate at every routed width.

Batch-1 tok/s/user: 1,038 for this PR vs 1,067 for the pre-binding build (-2.7 %).

Reproduce: benchmark/README.md.

@YAMY1234 YAMY1234 changed the title [Feat] [GB300] deepseek_v41: Monokernel plugin for DeepSeek-V4.1 decode on GB300 [Feat] [GB300] deepseek_v41: Megakernel plugin for DeepSeek-V4.1 decode on GB300 Oct 17, 2026
YAMY1234 added a commit to YAMY1234/sgl-nvidia-mega-fork that referenced this pull request Oct 18, 2026
…e MoE step

The MoE package builds PR sgl-project#1's routed-expert GEMM pair for every MoE layer
(slot moe) and its fast top-k (slot gate), reading SGLang's loaded MXFP4
experts in place; every rank agrees or none is built. At widths whose plan
routes, the step runs the dual-stream order with the pair: the fast gate takes
the top-k on SGLang's router logits, the pair replaces the routed call and
returns the deferred-finalize output, and the gate, the routed-input quant
stream and the shared expert on the alternate stream stay as SGLang issues
them.
YAMY1234 added a commit to YAMY1234/sgl-nvidia-mega-fork that referenced this pull request Oct 18, 2026
YAMY1234 added a commit to YAMY1234/sgl-nvidia-mega-fork that referenced this pull request Oct 18, 2026
…blished wo_b

boundary/hop_a.py owns PR sgl-project#1's hop-A unit for the executor: the Lamport
mailbox, the publishing wo_b GEMM precompiled for each width up to 8 rows,
and the consumer. The attention step publishes through
Boundaries.publish_wo_b; the attention post then collects a published partial
with the triplet SGLang's post computes and the FFN norm, in place of SGLang's
fused collective. Off unless the hop_a slot is on; any other partial takes
SGLang's post. The boundary module is renamed boundaries.py.
@YAMY1234 YAMY1234 changed the title [Feat] [GB300] deepseek_v41: Megakernel plugin for DeepSeek-V4.1 decode on GB300 [Feat] [GB300] deepseek_v41: NVIDIA megakernel plugin for DeepSeek-V4.1 decode on GB300 Oct 18, 2026
@Oasis-Git Oasis-Git mentioned this pull request Oct 19, 2026
…red steps

The covered step is validated only under FULL decode CUDA graphs (one graphs-off run
read needle 15/27, under investigation). The spec now accepts only FULL decode CUDA
graphs and refuses a launch without them at argument resolution, with the reason.
The target and draft plan a covered step only while SGLang captures the decode graphs
(the warm-up runs included), so an eager step at a covered width runs SGLang's
forward. The stock-parity and hop-fault serving tests step eagerly by design; their
test-only switches still allow it.
@YAMY1234
YAMY1234 marked this pull request as ready for review October 19, 2026 05:43
@YAMY1234
YAMY1234 requested a review from Oasis-Git as a code owner October 19, 2026 05:43
@Oasis-Git
Oasis-Git merged commit d47deb3 into sgl-project:main Oct 19, 2026
3 checks passed
@YAMY1234 YAMY1234 mentioned this pull request Oct 19, 2026
3 of 5 tasks
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.

2 participants