Skip to content

[Spec] Support quantized target lm_head in the DFlash2 selector - #35496

Merged
kpham-sgl merged 1 commit into
sgl-project:mainfrom
Jiminator:feat/dflash2-quantized-lm-head
Aug 20, 2026
Merged

kpham-sgl merged 1 commit into
sgl-project:mainfrom
Jiminator:feat/dflash2-quantized-lm-head

Conversation

@Jiminator

@Jiminator Jiminator commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

DFlash2's selector matmuls draft hiddens against the target lm_head.weight directly, so quantized targets (ModelOpt NVFP4, compressed-tensors FP8) fail with RuntimeError: DFlash2 selector requires a dense FP16/BF16/FP32 target lm_head — on 32GB GPUs those checkpoints are the only way to serve Qwen3.8-27B. #34859 fixed the same problem for DSpark via quant_method.apply; this does the same for the DFlash2 selector.

Overlaps #35462 (@ryou315, credited as co-author): this PR additionally keeps the quantized logits contiguous by masking the padded tail instead of cropping — a padded local vocab crop otherwise fails flashinfer's radix top-k CHECK_INPUT at TP=1 and can hang TP>1, since only the rank holding the tail errors while its peers enter the all-gather.

Follow-ups split out of this PR so each stays reviewable on its own: #35580 (degenerate-config guard), #35581 (optional cleanup).

Modifications

  • models/dflash.py: _project_candidate_logits projects draft hiddens through the target head, restricted to the org vocab. When should_apply_lm_head_quant_method admits the head it runs quant_method.apply over the padded local vocab and masks the padded tail to -inf (a packed weight cannot be row-sliced like the dense path; masking keeps the logits contiguous with no copy, and does nothing when the vocab is unpadded). Both TP branches call it; their control flow is unchanged.
  • speculative/dflash_worker_v2.py _maybe_build_draft_sampler: the selector branch accepts a dense head or one the quant gate admits, so quantized and dense heads take the same graph-folded _SelectorDraftSampler path (the captured kernel is the one the target's own logits path already runs under CUDA graphs). The dense-only screen still guards the non-selector static-matmul sampler.

Accuracy Tests

RTX 5090, target RadixArk/Qwen3.8-27B-NVFP4 (NVFP4-packed head), draft incoai/Qwen3.8-27B-DFlash2, greedy: both arms are byte-deterministic across fresh server boots (8/8 prompts each); spec vs target-only outputs fork only at near-tie tokens and 22/28 completed chains reach the same final answer — a stable numerics offset, not corruption. sgl-eval run gsm8k 200q: 95.5% with spec vs 98.0% target-only.

test_dflash_logits.py: 7/7 pass. The quant-path test asserts apply() runs on the packed weight, dominant padded garbage never reaches the candidates, and the logits handed to top-k are contiguous with sorted/deterministic set — the fake quant method returns a deliberately strided view and the top-k stub mirrors flashinfer's call contract, so an unmaterialized output fails the way the real kernel would. The TP test pins the per-shard org-vocab restriction, the global id offset, and the fp32 cast before the all-gather. A worker test pins the pre-capture screen: a gate-passing packed head is admitted to the folded selector sampler and attached before construction, an unsupported one stays eager. All mutation-verified: dropping the mask, the .contiguous(), the top-k flags, the id offset, the fp32 cast, or the worker admission each turns a test red. The dense-rejection test is kept for the negative branch.

Speed Tests and Profiling

No effect on dense-head targets (same matmul, same screen outcome). Quantized targets go from refusing to serve to serving. Same cell (RTX 5090, fp8 KV, concurrency 1):

  • sglang.benchmark.serving random 8192/1024: TTFT 746.9 ms, median TPOT 7.10 ms, 129.5 output tok/s, accept 3.01 (DSPARK on the same cell: 7.73 ms, 116.5 tok/s, accept 2.59).
  • GSM8K 200q: 186.5 output tok/s vs 66.6 target-only, accept length 4.85.
  • Folded vs eager selector sampler isolated on 1024/1024: 5.58 vs 5.61 ms median TPOT with bit-identical accept lengths — the worker change is path consistency, and the two samplers demonstrably compute the same candidates.

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 #32313633532
Latest PR Test (Extra): ❌ Run #32313633314

@Jiminator
Jiminator force-pushed the feat/dflash2-quantized-lm-head branch 4 times, most recently from 000d975 to 6f06c58 Compare August 19, 2026 11:14
@Jiminator
Jiminator force-pushed the feat/dflash2-quantized-lm-head branch from 6f06c58 to 28198c8 Compare August 19, 2026 11:19
@kpham-sgl kpham-sgl self-assigned this Aug 19, 2026
@Jiminator
Jiminator force-pushed the feat/dflash2-quantized-lm-head branch from 28198c8 to c55f68f Compare August 19, 2026 22:32
@Jiminator Jiminator changed the title [Spec] Support quantized target lm_head in the DFlash2 selector (graph-folded) [Spec] Support quantized target lm_head in the DFlash2 selector Aug 19, 2026
@Jiminator
Jiminator force-pushed the feat/dflash2-quantized-lm-head branch from c55f68f to 4df0df1 Compare August 19, 2026 22:59
DFlash2's candidate selector borrows the target lm_head and matmuls
draft hiddens against .weight directly, so a quantized target (ModelOpt
NVFP4 packed, compressed-tensors FP8 per-channel) fails the dense-head
screen and DFLASH refuses to serve such checkpoints.

Route an applicable quantized head through quant_method.apply — the
same gate (should_apply_lm_head_quant_method) and kernel the target's
own logits path uses. A packed weight cannot be row-sliced to the org
vocab like the dense path, so the padded tail is masked to -inf out of
the top-k instead: a crop view would either break the contiguity
flashinfer's radix top-k CHECK_INPUTs or copy the whole valid vocab to
restore it. Both TP paths project through one helper; their control
flow is unchanged.

The worker's pre-capture screen admits a gate-passing quantized head
into the graph-folded selector sampler rather than leaving it on the
eager per-round fallback, so quantized and dense heads take the same
code path; the kernel it captures is the one the target's own logits
path already runs under CUDA graphs. The dense-only screen still guards
the non-selector static-matmul sampler. Folded and eager measure equal
on the same cell (accept lengths bit-identical), so this is path
consistency, not the speedup itself.

Co-authored-by: LING ZHI <[email protected]>
@Jiminator
Jiminator force-pushed the feat/dflash2-quantized-lm-head branch from 4df0df1 to 1bb0253 Compare August 19, 2026 23:31
@kpham-sgl

Copy link
Copy Markdown
Collaborator

/rerun-test test_dflash_logits.py test_dflash.py test_basic_sanity_dflash.py test_gemma4_dflash_31b_extra.py

@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test_dflash_logits.py test_dflash.py test_basic_sanity_dflash.py test_gemma4_dflash_31b_extra.py:

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/spec/test_dflash_logits.py

🚀 1-gpu-5090 (2 tests): ✅ View workflow run

cd test/ && python3 registered/spec/dflash/test_dflash.py
cd test/ && python3 registered/core/test_basic_sanity_dflash.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/spec/test_gemma4_dflash_31b_extra.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator

/rerun-test test_dflash_overlap_hostsync.py

@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test_dflash_overlap_hostsync.py:

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/spec/test_dflash_overlap_hostsync.py

@kpham-sgl
kpham-sgl merged commit 1cf2b8c into sgl-project:main Aug 20, 2026
105 of 121 checks passed
Ligator added a commit to WaxacaBytes/spark-ai-hub that referenced this pull request Aug 26, 2026
SGLang published RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead on 2026-08-24: the
same NVFP4 body with the output projection left dense. Their finding is that
FP8 KV cache plus a 4-bit lm_head is where NVFP4 loses the most accuracy,
since every logit is computed from 4-bit weights on top of an already-lossy
KV read, and a dense head recovers it.

Swapped in as the NVFP4 DFlash2 build rather than added alongside. The new
recipe is otherwise identical to the one it replaces -- same engine build,
same drafter, same flags, same mem-fraction -- so the only variable is the
checkpoint. It is also DFlash2's native target shape, since the quantized-head
path only landed in sgl-project/sglang#35496.

Measured back-to-back on one box with tools/benchmark_editing_column.py:

                     writing   editing   weights
  BF16 head            27.8      60.3    23.8 GB
  packed head          30.3      68.3    21.9 GB

So the accuracy option costs 8-12% of throughput and 1.9 GB, which is about
what bandwidth predicts for a head that grows from ~0.5 GB to ~1.9 GB and is
read on every decode step. Recorded in the recipe description so the tradeoff
is visible before install. Re-measuring the outgoing recipe put it at 30.3 vs
its published 30.0, so the gap is real rather than drift.

Also add bf16head to models.js BUILD_TOKENS. The model-grouping key strips
trailing build tokens off the slug, and an unknown token halts the strip, so
the recipe grouped under qwen38-27b-nvfp4-bf16head and sat outside the
Qwen3.8-27B table as a lone row. Per-tensor precision describes how a
checkpoint was built, not which model it is, so it belongs with the quant and
drafter tokens already there.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E3KJUKJpW1zjUSzKK7A5R2
chinazhangchao pushed a commit to chinazhangchao/sglang-windows that referenced this pull request Sep 15, 2026
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