Skip to content

[Fix] speculative: FR-Spec (--speculative-token-map) with a quantized (FP8) target lm_head - #44277

Open
AppNetDeveloper wants to merge 1 commit into
sgl-project:mainfrom
AppNetDeveloper:frspec-quantized-lm-head
Open

AppNetDeveloper wants to merge 1 commit into
sgl-project:mainfrom
AppNetDeveloper:frspec-quantized-lm-head

Conversation

@AppNetDeveloper

@AppNetDeveloper AppNetDeveloper commented Oct 14, 2026 •

Copy link
Copy Markdown

Motivation

--speculative-token-map (FR-Spec) fails at startup when the target lm_head is quantized. With a compressed-tensors W8A8 FP8 (per-channel) lm_head, SGLang keeps the weight transposed ([K, V]), so head.data = head.data[self.hot_token_id] in EagleDraftWorker.init_lm_head slices the K axis instead of the vocab: on Qwen3.8-27B (vocab 248,320, hidden 5,120) a 40,960-id map asks for a [40960, 248320] tensor and OOMs (9.47 GiB). The per-row weight_scale would also stay full size.

Modifications

  • spec_utils.prune_quantized_lm_head(lm_head, hot_token_id): for a quantized lm_head, returns a shallow copy whose weight / weight_scale / weight_scale_inv / bias keep only the hot vocab rows, in the original dtype and layout (handles [V, ...] and the transposed [K, V] view). Per-tensor scales are shared. Returns None for unquantized heads, so the existing path is unchanged. Raises a clear error for layouts it cannot slice per row (block-wise scales, sharded/packed vocab) or for ids outside the vocab, instead of producing garbage. The target lm_head is never modified.
  • EagleDraftWorker.init_lm_head: when the pruned head is available, hand it to the draft via set_lm_head_from_target (with a clear error if the draft model can't take a target head).
  • test/registered/unit/speculative/test_frspec_quantized_lm_head.py: CPU tests (shape/stride/dtype, logits equal to the hot rows of the full head, target untouched, unquantized → old path, block scales and out-of-range ids refused).

Accuracy Tests

Real Qwen3.8-27B FP8 lm_head (248,320 × 5,120, ThinkingCap-Qwen3.8-27B-NVFP4A4-AWQ, compressed-tensors): pruned logits == the corresponding rows of the full head (max |diff| = 0, top-1/top-4 identical) at M = 1/4/8/16. End to end with MTP (EAGLE 3/1/4) and a 40,000-id map: reasoning 48/48 (3 runs × 16, same as baseline), tool calls 24/24, needle 4/4; accept length ~3.0 vs ~3.1.

Speed Tests and Profiling

RTX PRO 6000 (SM120), SGLang 0.5.21 (same code as this branch for these two files): draft lm_head 0.97–1.17 ms → 0.17–0.20 ms per call (3 calls per step). Single stream: +3.6 % at 45k context, +11.5 % at 141k; 4 concurrent: +8 %. Combined with other SM120 kernel changes (published separately, not part of this PR: https://github.com/AppNetDeveloper/sglang-sm120-fast-decode) it reached +22–38 % tokens/s on production traffic.

Checklist

  • Format your code according to the pre-commit guide (black/isort on the changed code; unrelated lines untouched).
  • Add unit tests (CPU, base-a-test-cpu).
  • Update documentation (not needed: no new flag; existing --speculative-token-map now works with quantized heads).
  • Provide accuracy and speed benchmark results (above).
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ❌ Run #43592472292
Latest PR Test (Extra): ❌ Run #43592471960
Latest PR Test (AMD ROCm 10): ❌ Run #43592472193

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

…lm_head

With an FP8 (compressed-tensors W8A8, per-channel) target lm_head, SGLang keeps the
weight transposed ([K, V]), so `head.data[hot_token_id]` sliced K instead of the vocab
(OOM at startup: 9.47 GiB for 40,960 ids on Qwen3.8-27B) and left the per-row scale
full size. Add `prune_quantized_lm_head`, which builds a pruned copy of the target head
slicing weight and per-row scale along the vocab axis (same dtype/layout), returns None
for unquantized heads (existing path unchanged) and refuses layouts it cannot slice per
row (block scales, sharded vocab) with a clear error. CPU unit tests included.
@AppNetDeveloper

Copy link
Copy Markdown
Author

Rebased onto current main (b77d7b8573, 2026-11-14). The change is the same as before and applied without conflicts: init_lm_head on main still does head.data = head.data[self.hot_token_id], so the bug is still there.

What it fixes. --speculative-token-map (FR-Spec) crashes at startup when the target lm_head is quantized. A compressed-tensors W8A8 FP8 (per-channel) lm_head is stored transposed ([K, V]), so head.data[hot_token_id] indexes the hidden axis instead of the vocab. On Qwen3.8-27B (vocab 248,320, hidden 5,120) with a 40,960-id map, it tries to allocate a [40960, 248320] tensor and fails with an OOM (9.47 GiB). The per-row scale would also stay at full size.

What changes. prune_quantized_lm_head() builds a pruned shallow copy of the target head. It slices the weight and the per-row scale along the vocab axis and keeps the original dtype and layout. The draft receives it through set_lm_head_from_target.

  • Unquantized heads return None and keep the existing path.
  • Layouts it cannot slice per row (block scales, packed or vocab-sharded heads, ids outside the vocab) raise a clear error instead of producing wrong logits.
  • The target head is never modified.

This does not overlap with #42424, #43249 and #43347, which handle a vocab-sharded head at TP>1. Here a quantized head whose weight holds only part of the vocab is refused with an explicit error (checked on CPU with a half-vocab shard). Whichever lands first, the other needs only a small rebase of init_lm_head.

How it was tested

  • CPU unit test: test/registered/unit/speculative/test_frspec_quantized_lm_head.py (base-a-test-cpu) passes on the rebased branch, 6/6, together with the rest of test/registered/unit/speculative/. pre-commit passes on the changed files.

  • GPU kernel check: real FP8 lm_head of the checkpoint, through apply_fp8_linear, in a CUDA graph. RTX PRO 6000 Blackwell (SM120), SGLang 0.5.21, 2026-10-14.

    • The pruned logits equal the hot rows of the full head (max |diff| = 0, top-1 and top-4 identical) at M = 1, 4, 8 and 16.
    • Each draft lm_head call drops from 0.95–1.17 ms to 0.17–0.20 ms. The draft calls it once per draft step.
  • End-to-end A/B: same GPU and day, same flags. The only difference is --speculative-token-map (45,319-id map). EAGLE/MTP with topk 1, 3 steps, 4 draft tokens; 400 output tokens per request.

    context, concurrency without map (tok/s per request) with map
    45k, 1 98.5 102.0 (+3.6 %)
    45k, 4 45.9 49.7 (+8.3 %)
    27k, 8 38.7 38.9 (≈0)
    141k, 1 93.9 104.7 (+11.5 %)
    mean accept length 3.11 3.04

    Quality with the map on our internal set, same run: reasoning 16/16, tool calls 24/24, needle 4/4.

  • Production use: the same patch has been running in production since 2026-10-14 on 0.5.21, 0.5.22 and 0.5.23, now with 5 draft steps and a 40,000-id map.

  • On a recent main build (2026-11-14): the FR-Spec part of this change ran on GPU on the nightly wheel 0.5.24.dev20261114+g56fa7ca6e7, same RTX PRO 6000 Blackwell and checkpoint, with a 40,000-id map and 5 draft steps. The server started and logged FR-Spec: draft lm_head pruned to 40000 rows (quantized, weight (5120, 40000) torch.float8_e4m3fn). Our internal set passed (reasoning 16/16, tool calls 24/24, needle 4/4) with a mean accept length of 3.68. Two caveats: that build also carried other local patches, so we report no tok/s for this change alone; and the code that ran was spec_utils.py exactly as in this branch plus the eagle_worker_v2.py hook without the hasattr(..., "set_lm_head_from_target") guard, which is the only thing this branch adds on top (a 5-line explicit error; not exercised in that run).

Why it fits 1.0 (#50045). It fixes a startup crash in an existing speculative-decoding flag. Where it cannot handle a layout, it raises an explicit error instead of producing wrong logits. Nothing changes for unquantized heads. That matches the tracker's principles for core features until the branch cut: bug fixes, tests, and explicit errors for unsupported configurations.

CI. The red checks on the previous head were all pr-gate, because the PR has no run-ci label. Lint and the title check passed. The new test runs on CPU only, so /run-cpu-ci would cover it if a maintainer can trigger it. A review from the speculative-decoding owners would be much appreciated.

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

quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant