Skip to content

[Feature] DFLASH EAGLE-style draft-tree verification - #31069

Closed
SubSir wants to merge 13 commits into
sgl-project:mainfrom
SubSir:dflash-tree-verify-upstream
Closed

SubSir wants to merge 13 commits into
sgl-project:mainfrom
SubSir:dflash-tree-verify-upstream

Conversation

@SubSir

@SubSir SubSir commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

[Feature] DFLASH EAGLE-style draft-tree verification

Motivation

Adds EAGLE-style draft tree verification to DFLASH (spec-v2 worker), on top
of the existing chain verify (#16818 / #22077). The draft head proposes a top-k
tree per step; the target verifies it in one pass and accepts the best
root-to-leaf path — ~+22% accept length (budget 32) and higher low-concurrency
throughput. Opt-in via --speculative-dflash-tree-verify (default off = chain,
unchanged).

Modifications

New file

  • kernels/ops/speculative/dflash_tree_expand_topk.py — fused Triton kernel
    running the full per-step expand + top-k recurrence in one launch (power-of-2
    topk in {4, 8, 16}); the per-step loop lives inside the kernel.

Modified files

  • speculative/dflash_worker_v2.py — tree draft-step, tree accept
    (verify_tree_greedy_func greedy / tree_speculative_sampling_target_only
    non-greedy), scattered-accept KV move-to-front. Top-k sampling + tree build are
    folded into the draft decode CUDA graph (mirrors [Spec] Capture DFLASH draft greedy sampling inside the draft decode cuda graph #29395).
  • speculative/dflash_info.py — tree fields + mask/positions/retrieve build in
    prepare_for_verify.
  • speculative/dflash_utils.py — build_tree_verify_tokens (drives the fused
    kernel) + compute_dflash_tree_sampling_accept.
  • kernels/ops/speculative/dflash.py — dflash_tree_accept_compact (tree
    analogue of the chain accept-bonus kernel).
  • model_executor/runner/decode_cuda_graph_runner.py — mask-capable verify CUDA
    graph for tree.
  • arg_groups/speculative_hook.py — validate --speculative-eagle-topk ∈ {4, 8, 16}.
  • server_args.py — the --speculative-dflash-tree-verify flag.
  • test/registered/spec/dflash/test_dflash.py — TestDFlashServerTreeVerify.

Notes

  • Greedy and non-greedy verify (validated: temp 0.7 / top-p 0.9 / top-k 50 →
    1118 tok/s, accept 7.60 at conc=1, Qwen3-8B).
  • CUDA graph for draft (sampling + tree build) and mask-capable verify; overlap
    scheduling supported.
  • --speculative-eagle-topk limited to {4, 8, 16}; tree gains are low-concurrency.

Accuracy Tests

Greedy tree accept is lossless by construction — verify_tree_greedy_func
commits a draft token only when it equals the target argmax.

GSM8K accuracy (chat zero-shot, temp 0, max_tokens 2048), baseline vs chain vs
tree, Qwen3-8B / 1× B200:

conc n baseline chain tree (topk4, b32)
1 128 0.773 0.758 0.711
8 256 0.785 0.742 0.754
64 256 0.766 0.750 0.793

The three track within sampling noise (tree is lowest at conc=1 but highest at
conc=64). TestDFlashServerTreeVerify (max_running_requests=8, batched verify)
gates gsm8k accuracy + accept length in CI — verified passing.

Speed Tests and Profiling

Qwen/Qwen3-8B (+ z-lab/Qwen3-8B-DFlash-b16) and Gemma-4-31B-it (+ z-lab
DFlash draft), 1× B200, triton, overlap. Launch:

SGLANG_ENABLE_OVERLAP_PLAN_STREAM=1 python -m sglang.launch_server \
  --model-path Qwen/Qwen3-8B --trust-remote-code \
  --attention-backend triton --speculative-draft-attention-backend triton \
  --speculative-algorithm DFLASH \
  --speculative-draft-model-path z-lab/Qwen3-8B-DFlash-b16 \
  --speculative-dflash-tree-verify \
  --speculative-eagle-topk 4 \
  --speculative-dflash-block-size 16 \
  --speculative-num-draft-tokens 32 \
  --enable-torch-compile --torch-compile-max-bs 128

Chain baseline = drop --speculative-dflash-tree-verify /
--speculative-num-draft-tokens, set --speculative-eagle-topk 1. torch.compile
is used for Qwen3-8B only — on the 31B model its Triton GEMMs are slower than cuBLAS.

Cross-dataset, conc 1 (tok/s, accept in parens)

enable_thinking=False, 512 samples.

Qwen3-8B:

method budget gsm8k math-500 mt-bench
chain 16 1061 (6.42) 1168 (7.90) 546 (4.04)
ours 32 1228 (7.86) 1265 (9.29) 670 (5.06)
ours 128 1165 (8.72) 1221 (10.19) 671 (5.76)

Gemma-4-31B-it:

method budget gsm8k math-500 mt-bench
chain 16 385 (7.09) 410 (7.75) 182 (3.92)
ours 32 411 (8.13) 441 (8.82) 208 (4.55)
ours 64 409 (8.56) 443 (9.28) 211 (4.85)
ours 128 364 (8.92) 398 (9.66) 192 (5.15)

Budget 32 is the sweet spot: b128 raises accept but its verify cost erodes tok/s
— on the 31B model b128 gsm8k (364) even drops below chain (385).

Concurrency sweep, gsm8k (chain vs tree b32)

Separate config (128 questions, torch.compile on), so conc=1 tok/s differs
slightly from above.

conc chain tok/s (acc) tree tok/s (acc) speedup
1 1022 (6.33) 1199 (7.78) 1.17×
8 6221 (6.47) 5664 (7.89) 0.91×
32 12826 (6.44) 8966 (7.87) 0.70×

Why tree loses at high concurrency

At b16 tree verifies the same 16 tokens/step as chain, isolating tree-build cost:

conc chain tok/s (acc) tree b16 (acc) speedup
1 1037 (6.33) 1139 (7.18) 1.10×
8 6201 (6.47) 6065 (7.24) 0.98×
32 12911 (6.45) 11644 (7.23) 0.90×

Even at equal budget, tree's per-step overhead (tree build, masked attention,
scattered-accept KV move) outgrows the ~12% step savings — hidden at conc=1,
exposed once compute-bound (b32 loses more). Use tree for latency-sensitive /
low-QPS serving, chain for throughput.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #29780528370
Latest PR Test (Extra): ❌ Run #29780527538

SubSir and others added 9 commits July 11, 2026 19:21
Add a single-launch Triton kernel that runs the full per-step
expand + top-k recurrence on the GPU (power-of-2 topk in {4, 8, 16}),
replacing the Python step loop. `build_tree_verify_tokens` in
dflash_utils drives it and emits the EAGLE
(draft_tokens, parent_list, selected_index) contract, so the existing
spec-v2 KV / accept / mask path is reused unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Wire the fused GPU tree builder into DFlashWorkerV2 behind a new
--speculative-dflash-tree-verify flag. When enabled (and
--speculative-eagle-topk > 1), the draft block is expanded into an
EAGLE-style verification tree whose node budget
(--speculative-num-draft-tokens) may exceed the draft block size.

- DFlashVerifyInput carries the tree (parent_list / selected_index) and
  reusable tree-mask / position buffers (EAGLE-style buffer reuse).
- Accepted tree-path KV is moved to the contiguous front of each verify
  block (lossless; mirrors EAGLE v2) and the accept compaction runs in a
  single fused Triton kernel (dflash_tree_accept_compact).
- speculative_hook relaxes the chain-only topk==1 / num_draft_tokens==
  block_size constraints when tree verify is on.

Full-attention targets only; default off = byte-identical chain verify.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…cuda graph

Extend the sgl-project#29395 draft-sampler graph-capture idea to tree verify. Add
_DflashTreeDraftSampler, run as a draft-decode-graph tail hook, which captures
BOTH the per-step top-k over the target LM head AND the fixed-shape draft-tree
pruning (build_tree_verify_tokens: fused expand/select kernel + fast_topk + sort
+ gather) inside the draft cuda graph, writing (draft_tokens, parent_list,
selected_index) to static buffers. On replay the tree draft step reads those
buffers and skips the eager head GEMM, materializing draft_hidden, AND the eager
tree build. Byte-for-byte with the eager path (fused kernel has one fixed
constexpr config, compiled during graph warmup). tp=1 / no-added-vocab only.
Only the verify attention mask build stays eager (its work is seq_len-dependent).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ng scaffolding

Ponytail cleanup, no behavior change on the supported path:
- dflash_tree_expand_topk.py: remove the unused single-step dflash_expand_topk4
  kernel + is_dflash_expand_topk4_available + module topk constants (only the
  fused full-recurrence kernel is used). 310 -> 203 lines.
- build_tree_verify_tokens: drop the eager non-fused fallback + _select_top_k_tokens_no_hidden
  and the dead timing_ctx_factory/timer scaffolding; the fused GPU path is the only
  supported one. Raise a clear error if CUDA/power-of-2-topk preconditions are unmet.
- speculative_hook: validate --speculative-eagle-topk in {4,8,16} at launch for tree verify.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The tree accept was greedy-only (verify_tree_greedy_func + argmax), silently
degrading non-greedy requests. Add compute_dflash_tree_sampling_accept: wraps the
tree-native tree_speculative_sampling_target_only kernel with the pruned tree's
retrieve structures (already built by build_tree_kernel_efficient), returning the
same (predicts, accept_index, accept_token_num) layout so dflash_tree_accept_compact
is unchanged. Tree accept now branches on sampling_info.is_all_greedy, mirroring the
chain path. draft_probs=zeros (DFlash threshold-accept scheme), same as chain.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
TestDFlashServerTreeVerify enables the draft tree on the spec-v2 overlap path
via --speculative-dflash-tree-verify + --speculative-eagle-topk 4 (triton verify
attention). Reuses the chain gsm8k accuracy threshold (tree verify is greedy-
lossless) and raises the accept-length bound.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Tree verify returned speculative_num_draft_tokens=self.block_size (the draft
block size, 16) while next_token_ids = out_tokens.reshape(-1) has a per-request
stride of draft_token_num (the verify budget, 32). batch_result_processor slices
next_token_ids as [i*stride : i*stride + accept_lens[i]], so with the wrong
stride every request after the first (bs > 1) reads another request's slot --
silently committing wrong tokens.

Invisible until now because (a) bs=1 always reads at offset 0 regardless of
stride, and (b) the chain path has block_size == draft_token_num so the two
coincide; only tree verify decouples them. gsm8k accuracy at concurrency 64
collapsed to ~0.05 (vs baseline 0.75); accept_len stayed normal, so throughput
benchmarks never caught it. Pass the verify budget (local block_size, already
= draft_token_num in the tree branch) as the result stride.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Ran TestDFlashServerTreeVerify on the real CI model (Llama-3.1-8B-Instruct +
z-lab UltraChat DFlash head, the sgl-project#22077 setup) — accept length is ~3.37 there
(vs the chain suite's 2.8 bound), not the 3.5 I had guessed from Qwen's ~7.8.
Tree still clears the chain threshold; 3.5 was just too aggressive for this
head. Test now passes (gsm8k accuracy + accept length, batched verify bs>1).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

…upstream

# Conflicts:
#	python/sglang/srt/server_args.py
#	python/sglang/srt/speculative/dflash_info.py
@SubSir
SubSir marked this pull request as ready for review July 13, 2026 21:14
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

SubSir added 2 commits July 20, 2026 14:30
…upstream

# Conflicts:
#	python/sglang/srt/speculative/dflash_worker_v2.py
efschu pushed a commit to efschu/htsglang that referenced this pull request Jul 26, 2026
… group collective

Found by booting it: Nordstern L0 S4 (TP=5 over two hosts, EAGLE3, solo draft
placement, --disable-cuda-graph). Ranks 1-4 reached "Tree cache initialized"
and started their health checks; rank 0 went silent for 8 minutes until the
launcher killed it. py-spy on the solo rank:

    all_gather_object (distributed_c10d.py)
    _harmonize_cuda_graph_plan (model_runner.py:1098)
    init_cuda_graphs (model_runner.py) <- tp_worker <- eagle_worker_v2

Under --speculative-draft-placement solo only the solo rank owns a real draft
runner; the shadow ranks get a stub (install_shadow_draft_runner_surface) and
never call init_cuda_graphs on it. The harmonisation's all_gather_object is
group-wide, so the solo rank entered a collective no other rank would ever
join. This is independent of nnodes -- it is a property of solo placement.

Fix: skip the harmonisation when spec_solo_rank_local_graphs is set. That flag
already declares exactly this ("this runner's graphs are captured rank-locally,
the shadow ranks skip draft capture entirely") and already suppresses the
capture backends' per-warmup TP barrier; the harmonisation is a second group
collective in the same category. It also MUTATES the shared cuda_graph_config,
so letting the draft runner harmonise could disable a phase for the target
runner too.

Also in this commit, from the same window:

* sgl-project#76 GUARD HARDENED (sgl-project#98 sighting). The trigger is no longer "topk > 1" but
  "anything that would activate a tree-masked verify", via the new
  ServerArgs.tree_verify_activation_reason(). Upstream carries three
  unconsolidated PRs (sgl-project#31069/sgl-project#29587/sgl-project#29907) adding
  --speculative-dflash-tree-verify, a SECOND door onto the terrain sgl-project#76 fenced
  off. It does not exist here yet, so it is read with getattr: the guard is
  armed for its arrival instead of waiting to be remembered during a merge.
  The refusal is not theoretical -- see the byte-gate numbers below.
* l0_rank.sh: --max-total-tokens is now optional (MAXTOK=0 leaves the KV pool
  uncapped) because a tok/s number without the context it was reached at is not
  a result (harness rule sgl-project#4).
* l0_launch.sh: forward MAXTOK/MEMFRAC/EXTRA to the second host. They were not
  forwarded, so MAXTOK=0 applied to the main rig only: rank 0 sized its pool to
  133802 tokens while ranks 3/4 silently kept the 2048 default. A capacity
  number measured that way means nothing.

TESTED

CPU: test_uneven_tp_args.py -- 13 passed (TreeSpec*), both directions. The new
flag is rejected under uneven DCP WITH topk=1 (i.e. exactly the case the old
guard would have let through), the healthy path (chain, no tree verify) stays
open with the flag false AND with it absent, the original topk door still
rejects, the message names which flag tripped it, and a single-GPU tree run is
untouched. Full suites: server_args + spec kernel backend -- 271 passed.

GPU, RTX 5090, byte gate step 1 (native sgl_kernel vs Triton spec kernels,
identical inputs, bit comparison of every output tensor of build and verify):
  topk=1 chains  16/16 EXACT   (bs 1-2, steps 2-3, FULL_MASK and QLEN_ONLY)
  topk=2,4 trees 8/32 exact -- tree_mask differs by 6-13 elements, and one
                 case raised a CUDA error inside the Triton kernel
So the group-decision's refusal of topk>1 on the Triton path is now measured,
not precautionary, and this guard's subject matter is the same computation.

GPU, five ranks, both hosts (Llama-3.1-8B fp16, ratio 4,3,3,2,1, ctx 2048,
eager, HTCCL/gloo): S4 (solo) and S3 (split) BOTH BOOT AND SERVE after this
fix. That is the first live proof of the narrowed nnodes gate (f34a343) and
of the capability-keyed spec kernels (4bcb310) -- ranks 3 and 4 have no
sgl_kernel, so the group ran the Triton path end to end.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks @SubSir. Closing this because it has had no updates in 91 days.

Reopen it if the work is still relevant.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant