Repository navigation
[Perf] [NVIDIA] speculative: Move the host work between Qwen3.5 MTP decode graphs into the graphs (1/2) - #45474
Merged
Merged
Conversation
YAMY1234
force-pushed
the
nvidia/qwen35-mega-perf
branch
from
October 21, 2026 05:09
9efe81e to
36d1aba
Compare
4 of 5 tasks
YAMY1234
force-pushed
the
nvidia/qwen35-mega-perf
branch
from
October 21, 2026 13:14
36d1aba to
0b6a068
Compare
…E verify, behind an opt-in switch With SGLANG_ENABLE_EAGLE_POST_VERIFY_CUDA_GRAPH, which is off by default, SGLang captures the EAGLE verify's post-section in its own CUDA graphs: sampling and acceptance, the commit outputs and, on a side stream, the mamba state commit. With the switch off nothing changes. - Per key (batch size, sampling mode, forced acceptance, tracking, the verify logits buffer), two slots of staged inputs and outputs alternate, so one round's outputs stay valid while the host consumes them. Batches a graph cannot reproduce run SGLang's eager path: grammar, penalties, seeds, min-p, watermarks, and an eager verify. A DeepSeek-V4 KV pool, whose rejected c128 draft states the eager verify clears, always runs the eager path. - The switch is ignored, with a warning, without CUDA, for a tree draft (topk above 1) and under DP attention: each key is captured at its first rounds, which attention-DP ranks do not reach together, and a capture may end in a collective over the TP group. - The side-stream commit is joined once the draft extend is enqueued, in the EAGLE and the frozen-KV MTP decode paths. - The forced acceptance length (SGLANG_SIMULATE_ACC_LEN) is one stream: the worker seeds a generator from the server seed, and the graphs and the eager verify both draw from it, once per round. Every rank therefore forces the same lengths, whatever else uses its global RNG, and rounds that mix graph and eager paths force the lengths an all-eager run forces. In the graphs the length reaches the device through a pinned ring, and eagle_sample(simulated_acc_len=) applies it with generate_simulated_accept_index_device, which writes what the host-length version writes. With the graphs off the eager verify draws from the global CPU RNG as before. - The switch is an environment variable, like the other experimental speculative and CUDA-graph toggles in environ.py. - CPU tests: the switch turns the graphs on for every verify of a setup they can run and nowhere else, only batches a graph can reproduce take them, a KV pool with c128 draft states never reaches them, and the forced lengths are one rank-identical stream with the graphs and unchanged without them.
…bitwise up to bs 64 A GPU test runs SGLang's post-verify graphs at batch sizes 1, 3, 16 and 64, greedy and with forced acceptance (real draft tokens). Every round must equal SGLang's eager sampling on the same inputs bitwise: the eager warm-ups, the capture of each slot, and the replays of both alternating slots. A round's outputs must also stay unchanged through the next round, since the host may still be reading them. Every third round runs eagerly, as when its verify ran without a graph, and draws from the worker's forced-length generator, so the mixed run must force the lengths of an all-eager reference; this also checks the device-length injection against eagle_sample's host-length draw. It joins the CPU cases of the same file, which is registered for both suites.
…aptured prefill buckets The FLA chunk and chunk_o kernels take optional caller-built chunk tables, and the Triton GDN extend passes them through; without them the eager path is unchanged. gdn_prefill_graph.py holds a captured bucket's static tables, refilled before each replay by one H2D copy, and gdn_prefill_track.py takes the prefix-cache snapshots of conv windows and SSM states inside the graph, converting a BF16 chunk state before the store. Nothing calls them yet.
… graphs, behind an opt-in switch With SGLANG_ENABLE_GDN_PREFILL_GRAPH_EXTEND, which is off by default, GDNAttnBackend implements the linear-backend prefill-graph protocol, as KDA does. Breakable prefill CUDA graphs then capture each GDN layer's extend instead of breaking at it: the extend launches the eager extend's Triton kernels on the bucket's static tables, which are refilled before every replay. With the switch off nothing changes. - The hybrid backend asks supports_prefill_graph_extend() once, at construction, as for KDA. The switch is read there, and the support check runs only when it is on, since that check reads model_config. - Only the target's extend is captured. A draft model's prefill graphs keep their eager path, so an MTP draft, which has no GDN layer, does not refill the tables on every replay. - A setup the graph-safe chain does not support keeps breaking at every layer: a kernel other than Triton, multi-item scoring, a mamba chunk size other than the delta rule's, non-contiguous state pools, or captured buckets that change chunk_fwd_o's block size. - The switch is an environment variable next to KDA's SGLANG_DISABLE_KDA_PREFILL_GRAPH_EXTEND. It is separate from the post-verify graphs' switch, since the captured extend does not need speculative decoding. - A GPU test captures one bucket and replays batches of several shapes. It compares the output, the conv and SSM pools and the track slots with the eager forward_extend, bit for bit, for FP32 and BF16 SSM states. A CPU test pins the switch, the support check and the target-only scope.
…ocks and moves aligned fields in 16-byte units With SGLANG_Q35MF_BK_VEC_COPY (off by default), small_field_copy and SmallFieldCopyPlan launch small_field_copy_vec_kernel: each field owns a block range sized to it, a contiguous field becomes one row, and a raw field whose rows, strides and base addresses are 16-byte aligned moves in uint4 units. Same bytes and the same refusals as the per-element launch, which stays the default. It speeds up the small_field_copy user, the post-verify input load (SGLANG_Q35MF_MKH_N3C, a later commit). This commit also adds small_field_copy itself (kernels/ops/memory/small_field_copy, a JIT kernel copying up to 16 small fields, registered for CUDA only). Tests: test_small_copy.py (GPU) checks small_field_copy and SmallFieldCopyPlan against the source bytes (width casts, rows, refusals, pinned host sources, graph replay), and that the vectorized launch writes the default launch's bytes (misaligned, strided and width-cast fields, row padding untouched), its refusals, and plan graph replay.
…dant launches
Behind two switches that are off by default; with both off the same GPU ops run
in the same order as on main.
- SGLANG_Q35MF_MKH_N3B, two halves:
- prepare_mamba_track_for_verify keeps the last verify's mamba track
indices (VerifyTrackIndices) and reuses them while the request slots and
the ping-pong mapping are the same tensors at the same versions, no track
position moved and the kept indices are unwritten; a reused round skips
two gathers, a pinned host tensor and its host-to-device copy. The lazy
track plan always rebuilds. Read once at import, so every caller of the
helper takes it.
- DecodeCudaGraphRunner: a pre-planned verify replay skips copying
input_ids and positions again when the full load_batch copied the same
two tensors, at the same versions, on the same stream (a plan-stream load
is never trusted).
- SGLANG_Q35MF_MKH_N3C: SGLang's post-verify graphs (which need
SGLANG_ENABLE_EAGLE_POST_VERIFY_CUDA_GRAPH) stage each round's inputs and
the forced acceptance length with one small_field_copy launch that reads
the length from its pinned host ring slot; a refused layout falls back to
the per-dtype copies.
_restage_is_redundant returns before building a signature when nothing was
staged, so with N3B off the replay adds only a flag check and that call. The
replay reads the staged inputs only with N3B on, and both attributes have
class-level defaults for runners built without __init__.
Tests: test_mamba_prefill_track_metadata.py (reuse equals a fresh build after
every kind of change), test_decode_cuda_graph_runner.py (the replay skips
only an unchanged same-stream load), test_eagle_post_verify_graphs.py (CPU:
the one-launch load and its fallback stage the same values; GPU: forced
acceptance with the one-launch load matches eager bitwise).
… and compact top-k verify sampling Behind two switches that are off by default; with both off the code paths are unchanged. - SGLANG_Q35MF_EDGE_CHAIN_TREE: TRT-LLM MHA runs target verify with its decode kernel, causal within each request's rows, and reads no tree mask. A topk-1 target then declares an unread mask in the compact QLEN_ONLY layout through the existing verify_mask contract (as trtllm_mla does), so the EAGLE draft builds the chain with build_chain_tree, which also carries the verify's out_cache_loc, instead of a mask over each request's whole context. A tree, a draft runner, or the switch off declare no mask. - SGLANG_Q35MF_EDGE_SPARSE_VERIFY_SAMPLING: when every request of a verify samples with a finite top-k within the compact branch's limit (128), EAGLEWorkerV2.verify passes that support as uno_target_max_top_k, and eagle_sample's existing compact top-k branch samples on it, in SGLang's post-verify graphs (the support enters their key) and in the eager verify alike; compact_verify_max_top_k in eagle_utils.py reads the switch. With forced acceptance the bonus stays the target argmax (eagle_sample(sparse_bonus_argmax=)). The sampled tokens are not bitwise equal to the dense branch's, so this switch is checked with sampled GSM8K and acceptance-length runs rather than bitwise. Tests: test_verify_mask.py (which backends declare the unread mask); test_eagle_post_verify_graphs.py (which batches take the compact branch, what the worker hands run_eagle_verify with and without the switch, and that compact and dense rounds get different graphs). The fake runner in test_trtllm_mha_graph_metadata.py gains is_draft_worker, which the backend now reads at construction.
… TP group once With SGLANG_Q35MF_EDGE_PACKED_VERIFY_BCAST (off by default), eagle_sample allocates predict, accept_index and num_correct_drafts as views of one int32 buffer (_verify_outputs), and a sampled result, on the compact top-k branch and on the dense branch, goes to the TP group in one broadcast instead of three. The values are unchanged; the HIP greedy branch keeps its three broadcasts. Read once at import, so every caller of eagle_sample takes it, graphed or eager. With the switch off the three tensors are allocated and broadcast as on main. Tests: test_eagle_post_verify_graphs.py checks that the packed tensors are views of one buffer, in order, holding the values the unpacked ones start from.
… memory, with a host-leader exchange across hosts The scheduler runs two tiny CPU collectives on every pass: the request broadcast size and the HiCache ready counts. Behind three switches that are off by default: - SGLANG_Q35MF_EDGE_SHM_SYNC: both go through a shared-memory table when the group is on one host (shm_tag_exchange.ShmTagExchange). - SGLANG_Q35MF_EDGE_SHM_SYNC_MULTIHOST: the HiCache ready counts of a group spanning hosts go through each host's table plus one leader all_gather (HierTagExchange); the request broadcast stays on gloo. - SGLANG_Q35MF_EDGE_MULTIHOST_TCP: two-host groups swap the leaders' rows over one TCP connection. A table row holds 8 values; a HiCache row wider than that (storage on, one more count per extra host pool) keeps the gloo reduce. Off, get_shm_exchange returns None and both syncs run main's gloo path. Each scheduler pass then adds at most one environment lookup per sync, plus _shm_count_exchange's group-size queries (local, not collectives). The switches are read with os.environ in shm_tag_exchange.py, not through environ.py. Tests: test_shm_tag_exchange.py (CPU, two gloo ranks on one host). HiCache rows of 8, 9 and 10 values (6 counts, one more per extra host pool, plus the folded digest) reduce to the minimum over ranks of each count, and only the 8-value row goes through shared memory. The exchange was also checked at TP8 across two hosts, where the outputs matched the gloo path's bit for bit.
The section comment of the SGLANG_Q35MF_* runtime switches used internal labels. It now names SGLang branch nvidia/qwen35-agentx-gb300, where the same switch names are read, all off by default. Comment only.
YAMY1234
force-pushed
the
nvidia/qwen35-mega-perf
branch
from
October 21, 2026 18:46
0b6a068 to
2a57a75
Compare
YAMY1234
marked this pull request as ready for review
October 21, 2026 22:42
YAMY1234
requested review from
Fridge003,
Qiaolin-Yu,
Ying1123,
hnyls2002,
ispobock,
kpham-sgl,
merrymercy and
pyc96
as code owners
October 21, 2026 22:42
Collaborator
Author
|
@Oasis-Git Po-Han's comments are addressed and the conflicts are resolved; could you take a look when you have a moment? Thanks! |
Collaborator
Author
Collaborator
|
CI Pass. Wait for #45266 |
YAMY1234
requested review from
ByronHsu,
Duyi-Wang,
ShangmingCai and
sogalin
as code owners
October 27, 2026 20:50
5 tasks done
nvrohanv
added a commit
to nvrohanv/sglang
that referenced
this pull request
Oct 29, 2026
Conflicts, each resolved to keep both sides: - arg_groups/field_order.py: main's new fields, then enable_speculative_step_cuda_graph last. - entrypoints/http_server.py: the Rust-server line about the step CUDA graph warm-up, then main's rust_server_sidecar block. - speculative/spec_utils.py: main's KV-cache imports and this branch's child_graph_capture import. - speculative/eagle_utils.py: sgl-project#45474 added the same packed verify broadcast behind SGLANG_Q35MF_EDGE_PACKED_VERIFY_BCAST (_verify_outputs). eagle_sample keeps this branch's _alloc_verify_outputs and _broadcast_verify_outputs, and _alloc_verify_outputs now allocates through _verify_outputs: packed when either variable is set, with the same layout.
nvrohanv
added a commit
to nvrohanv/sglang
that referenced
this pull request
Oct 29, 2026
- sgl-project#46719 records an eager_on_graph break at capture without running it, so a break returns None (sgl-project#46792 renamed capture_stub to on_capture). The step graph's grammar mask break fills the runner-owned mask and returns None, and the child-graph test's break writes into a buffer the test owns. - sgl-project#45474 draws the simulated acceptance length from the worker's generator (generate_simulated_accept_index(generator=)). The step graph's override takes the generator and keeps it with the draw arguments, so a replay draws from it as the eager step does.
3 of 5 tasks
3 of 5 tasks
This was referenced Nov 5, 2026
Draft
2 of 4 tasks
edwingao28
added a commit
to edwingao28/sglang-omni
that referenced
this pull request
Nov 12, 2026
sglang main captures the GDN extend in its own prefill graph (sgl-project/sglang#45474). Drama 3 enables that one at the next sglang pin bump.
edwingao28
added a commit
to edwingao28/sglang-omni
that referenced
this pull request
Nov 12, 2026
Without it requests/s at concurrency 16 fall 6% on H100. Remove it once the sglang pin includes sgl-project/sglang#45474.
4 of 5 tasks
2 of 5 tasks
PoTaTo-Mika
pushed a commit
to sgl-project/sglang-omni
that referenced
this pull request
Nov 15, 2026
sglang main captures the GDN extend in its own prefill graph (sgl-project/sglang#45474). Drama 3 enables that one at the next sglang pin bump.
PoTaTo-Mika
pushed a commit
to sgl-project/sglang-omni
that referenced
this pull request
Nov 15, 2026
Without it requests/s at concurrency 16 fall 6% on H100. Remove it once the sglang pin includes sgl-project/sglang#45474.
PoTaTo-Mika
pushed a commit
to sgl-project/sglang-omni
that referenced
this pull request
Nov 15, 2026
…ine kernels (#3648) * perf: gather and publish Drama 3 decode state inside the decode graph * perf: serve tuned H100 fused-MoE configs for Drama 3 decode and prefill * perf: capture the Drama 3 GDN prefill extend in the breakable graph * perf: stage Drama 3 prefill inputs without host syncs * perf: run the Drama 3 samplers with 16 warps * perf: fuse the Drama 3 tower for decode passes up to 32 rows * test: give the first-frame setup's hybrid backend double its linear backend * test: cover bfloat16 SSM states in the Drama 3 GDN prefill graph kernel test * refactor: trim Drama 3 engine docstrings and spell out names * test: spell out names in Drama 3 engine tests * refactor: reuse the Drama 3 frame width when sizing the tower prefill * refactor: drop unused Drama 3 decode state re-exports * fix: name the state pool in the Drama 3 missing-buffers error * docs: keep the Drama 3 MoE tuning note beside its configs * refactor: name the Drama 3 state pool state_pool on the model too * docs: say the Drama 3 MoE lookup also serves the down projection * docs: drop two Drama 3 comments the code already shows * test: drop the Drama 3 decode gather case the state pool walk covers * test: check the Drama 3 MoE lookup at a count whose default the tuned tile replaces * test: compare the Drama 3 GDN prefill graph only with the eager-prefill engine * chore: drop the Drama 3 GDN prefill graph that sglang main now ships sglang main captures the GDN extend in its own prefill graph (sgl-project/sglang#45474). Drama 3 enables that one at the next sglang pin bump. * feat: restore the Drama 3 GDN prefill graph as a backport of sglang's Without it requests/s at concurrency 16 fall 6% on H100. Remove it once the sglang pin includes sgl-project/sglang#45474. * chore: sort the Drama 3 head sampling imports * fix: refuse Drama 3 GDN prefill graphs on buckets that resize chunk_fwd_o blocks
3 of 5 tasks
4 tasks done
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.
Motivation
Qwen3.5-397B-A17B (NVFP4) low-latency decode on GB300, TP8, MTP (top-k 1, 7 draft tokens). With the NVIDIA megakernel plugin, the GPU part of a decode round gets short enough that SGLang's host work becomes the bottleneck: input staging between graphs, the verify's eager sampling and commits, and the scheduler's per-pass TP syncs. This PR moves that work into CUDA graphs, into fewer launches, or off the critical path. It is the Qwen3.5 counterpart of #45450.
This PR contains no megakernel code. It is independent of the wiring PR, #45266: both are based on main, and either can merge first. The plugin is sgl-project/sgl-nvidia-mega#20.
Every new code path is behind an environment switch that defaults to off. With all switches off, the same GPU operations run in the same order as on main; the accuracy tests below check this. Note that main's Qwen3.5 AgentX recipes (
docs/src/snippets/agentx/Qwen/qwen3.5.jsx) already set six of these switches to1.Modifications
One commit per change. The TPS/User figures are what removing that change alone would cost on the full stack (this PR + #45266 + plugin, TP8, one request, 128K) ¹.
SGLANG_ENABLE_EAGLE_POST_VERIFY_CUDA_GRAPH, ≈ 78 TPS/User): the EAGLE verify's sampling, acceptance and commit outputs replay as CUDA graphs, and the mamba state commit runs on a side stream. Batches a graph cannot reproduce (grammar, penalties, per-request seeds, ...) keep the eager path.SGLANG_ENABLE_GDN_PREFILL_GRAPH_EXTEND): prefill no longer leaves the graph at every GDN layer. It does not affect decode; TTFT drops from 82.9 to 40.6 ms for 256-token prompts at TP4.small_field_copy(SGLANG_Q35MF_BK_VEC_COPY, < 1 TPS/User): a JIT kernel that copies up to 16 small fields in one launch, with a 16-byte vectorized path. Item 6 uses it.SGLANG_Q35MF_MKH_N3B,_N3C, ≈ 19 TPS/User):SGLANG_Q35MF_EDGE_CHAIN_TREE,_SPARSE_VERIFY_SAMPLING, ≈ 70 TPS/User):SGLANG_Q35MF_EDGE_PACKED_VERIFY_BCAST, ≈ 3 TPS/User): a sampled verify result reaches the TP group in one broadcast instead of three.SGLANG_Q35MF_EDGE_SHM_SYNC,_SHM_SYNC_MULTIHOST,_MULTIHOST_TCP, ≈ 121 TPS/User): the request broadcast and the HiCache ready counts go through a shared-memory table instead of gloo. Across hosts, the counts use one exchange between the host leaders.environ.pysection comment names SGLang branchnvidia/qwen35-agentx-gb300, which reads the same switch names.¹ Measured on an earlier head of the full stack (1,267 TPS/User at P50). Items 1, 7 and 9 are derived from host-time and Kineto measurements rather than separate runs, so the rows do not add up. Four host-side items of earlier heads were dropped: together they gave ~1.3% for 1,204 production lines.
Accuracy Tests
GB300, real acceptance, GSM8K on a fixed 250-question subset.
On CPU, the changed test files and the speculative and megakernel unit tests fail only where main fails on the same machine.
Speed Tests and Profiling
GB300, TP8 across two 4-GPU trays. Static decode of one request at 128K context with forced acceptance 4.80 (
SGLANG_SIMULATE_ACC_LEN=4.80), about 1,780 rounds per server start. TPS/User = 4.80 tokens / round time. Rows 1–4 are the mean of two pairs of trays run in opposite orders. The plugin was measured at3509187d74, an earlier revision of sgl-project/sgl-nvidia-mega#20; the PR head differs only by an opt-in for more than one request, off by default.² On a third pair of trays, row 4 reaches 101.0% of the reference at P50 at 128K, and 100.8% / 100.6% and 101.9% / 101.6% (P50 / P90) at 8K and 32K.
With the plugin fixed (rows 3 and 4), this PR adds +68.7% at P50 and +74.9% at P90.
These results were measured on main
28448a86d4. The branch has since been rebased onto mainb57b5e2286, where two commits differ only by an import path that #45112 moved; the GPU checks are being repeated on the new base. Without the plugin it adds less, because main's longer GPU step hides much of the host work it removes.On InferenceX AgentX (TP8, concurrency 11, 3,600 s, one request per server at a time), the full stack scores X 1234.57 against the development build's 1219.51 on the same trays. TPS/User is 1314.35 / 1236.99 against 1302.52 / 1230.18 (P50 / P90).
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ⏳ Run #40581662695
Latest PR Test (Extra): ❌ Run #40581662779
Latest PR Test (AMD ROCm 10): ⏳ Run #40581662701