Skip to content

[w2] spec: reduced draft LM head over a trimmed vocabulary (--spec-draft-vocab, port of upstream 31031) - #165

Merged
rafasmour merged 1 commit into
masterfrom
opt/w2-spec-trimmed-draft-vocab
Dec 4, 2026
Merged

rafasmour merged 1 commit into
masterfrom
opt/w2-spec-trimmed-draft-vocab

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What

Opt-in reduced LM head for the draft context: --spec-draft-vocab FILE (env LLAMA_ARG_SPEC_DRAFT_VOCAB, one id per line or JSON array). Default off, no behaviour change without the flag.

At draft-context creation the listed rows (plus all EOG ids) of the draft's LM head are byte-copied into a new tensor of the same quant type. In build_lora_mm the draft multiplies by that copy and scatters the result into a full-vocabulary -inf logit tensor (ggml_fill + ggml_set_rows). Because the output stays full-vocab, no id map is needed in common/speculative.cpp (greedy, top-k and p-min selection and backend sampling see the usual shape). This deviates from the plan's "return trimmed logits plus id map" on purpose: it is what upstream does and it cannot get an id-mapping path wrong. The target verifies every token, so output is lossless; unlisted ids are practically never drafted.

This is a port of ggml-org/llama.cpp PR 31031 (read-only fetch; nothing was opened or commented upstream; it applied cleanly on master) plus:

  • heads with more rows than the vocabulary (the padded target head shared by DFlash) are accepted
  • test-llama-archs --draft-vocab: listed logits equal the full head, unlisted are -inf, for llama/qwen35/qwen35moe/qwen4exp (CPU)
  • tools/rdna3-w2/spec-trimmed-draft-vocab/mkvocab.py (python only + CPU llama-tokenize): 16k/32k/64k lists, tokenizer fingerprints
  • docs notes

Coverage (all go through build_lora_mm)

  • qwen35 / qwen35moe MTP: nextn.shared_head_head or model.output
  • qwen4exp graph_mtp: model.output (shared with the target)
  • separate Qwen3.8 MTP drafter (mtp-Qwen3.8-27B-Q4_0.gguf, own Q3_K output.weight)
  • DFlash/DSpark (dflash.cpp build_logits): own head, or the target head via ctx_other. LFM3 DSpark has its own 32000-row head with d2t and ignores the list.

Tokenizer check (token-list hash via mkvocab.py info, vocab sizes): Qwen3.8, Qwen3.8 MTP/DFlash2 and Qwen4 all share the same 248320-token list, so one qwen-*.txt serves them all. Gemma-4-31B, Nemotron-Bolt-E4B and its DFlash drafter share one 262144-token list (gemma-*.txt). MiniCPM5 (130560) and LFM3 (128000) have their own.

VRAM cost

The copy lives in the buffer type of the head. Per-head copy at 16k/32k/64k tokens:

  • Qwen4 target head Q6_K 417 MB -> 28 / 55 / 110 MB (matters for n-cpu-moe: preset f16-mtp has n-cpu-moe 10)
  • Qwen3.8 MTP drafter head Q3_K 546 MB -> 36 / 72 / 144 MB
  • Nemotron-Bolt tied head Q5_K 461 MB -> 31 / 61 / 123 MB
    The target keeps its full head. --fit does not account for the copy.

Expected gain (estimates, not measured)

Qwen4 MTP n-max 2: about -1 ms per spec cycle, +3-5% if acceptance holds; Qwen3.8 MTP +5-6%; Nemotron DFlash +3-4%. Ship only if acceptance loss costs less than the head time saved.

Env switch

--spec-draft-vocab FILE (opt-in CLI flag, env LLAMA_ARG_SPEC_DRAFT_VOCAB; default off). Presets not edited; the preset key is the user's decision.

Files

include/llama.h, src/llama-context.{h,cpp}, src/llama-graph.{h,cpp}, common/{common.h,arg.cpp,speculative.cpp}, tests/{test-draft-vocab.cpp,test-llama-archs.cpp,CMakeLists.txt}, scripts/build-draft-vocab.py (upstream), tools/rdna3-w2/spec-trimmed-draft-vocab/mkvocab.py, docs.

Risks

  • Acceptance drops for tokens outside the list (non-English, code symbols). Lists built locally from English docs/code (held-out coverage 98.3/99.5/99.9% at 16k/32k/64k for Qwen); real chat traffic may differ.
  • Extra VRAM for the copy of shared heads (above).
  • Softcap drafts: unlisted logits sit at the softcap floor, not -inf.
  • Conflicts with dflash-launch-cuts in common/speculative.cpp (merge after it; my hunks are only the loader and 4 lines in common_speculative_init_result).
  • Found while preparing: origin/master cannot load the Qwen4 GGUF (unknown pre-tokenizer type: 'qwen4'); upstream commit 74e7f44 ("model: qwen4", also qwen4exp 40-layer type and dense-FFN layers) is not in the fork. Not touched here; needs a sync before tonight's Qwen4 runs.

Checks done (CPU only, no GPU)

  • CPU build of llama-server, llama-speculative-simple, llama-tokenize, test-draft-vocab, test-llama-archs: ok, no new warnings
  • test-draft-vocab: OK
  • test-llama-archs --draft-vocab -a '^(llama|qwen35|qwen35moe|qwen4exp)$': 5/5 OK (worst listed-logit diff 8e-8)
  • test-llama-archs -a '^(qwen35|qwen4exp)$' -b CPU: pass
  • No CUDA code changed, so no HIP compile. Checked that the CUDA backend supports FILL and SET_ROWS (F32, I32 ids) and that no K/V-store fusion can match the new SET_ROWS.
  • mkvocab.py produced qwen/gemma/minicpm 16k/32k/64k lists in build-bins/w2/spec-trimmed-draft-vocab/.

GPU validation tonight (NOT YET RUN)

Preamble (P):

R=/home/tom/docker/projects/llama.cpp-RDNA3; K=$R/build-bins/kern; TB=$R/build-bins/tools/bench; cd $K
B0=$R/build-bins/w2/base-build/bin; BA=$R/build-bins/w2/all-build/bin

Qwen4 MTP:

./gpublock.sh python3 -I $TB/specbench.py q4-mtp-full $BA --n 256 --modes greedy,preset -- $(python3 -I $TB/preset2args.py qwen-4-35b-a3b-128k-f16-mtp)
./gpublock.sh python3 -I $TB/specbench.py q4-mtp-v32k $BA --n 256 --modes greedy,preset -- $(python3 -I $TB/preset2args.py qwen-4-35b-a3b-128k-f16-mtp) --spec-draft-vocab $R/build-bins/w2/spec-trimmed-draft-vocab/qwen-32k.txt

Repeat with qwen-16k.txt and qwen-64k.txt. Qwen3.8 MTP: the same pair with preset qwen-3.8-27b-mtp. Nemotron DFlash: the same pair with preset nemotron-3.5-bolt-e4b-dflash and gemma-32k.txt (tokenizer hash matches).
Pass: greedy hashes identical to the full-head run; compare "mean len" and t/s; keep only configurations that gain. Look for vocab_subset_init: N of M ids ... LM head(s) in the log (a warning means the list was ignored).

Hard rules followed: no GPU, no production, nothing touched in ggml-org/llama.cpp.

Assisted-by: Claude Sonnet 5.5

…R 31031), opt-in

--spec-draft-vocab FILE (env LLAMA_ARG_SPEC_DRAFT_VOCAB, default off) makes the
draft context multiply by a byte copy of the listed rows of its LM head (same quant
type) and scatter the result into -inf logits, so no id map is needed downstream
(greedy, top-k and p-min selection are unchanged). The target verifies every token,
so output is unchanged; unlisted ids are practically never drafted.

Covers every head that goes through build_lora_mm(): model.output, MTP
nextn.shared_head_head (qwen35, qwen35moe, qwen4exp MTP) and the target head shared
with DFlash/DSpark drafters via ctx_other.

Cherry-picked from ggml-org/llama.cpp PR 31031 (6 commits squashed), plus:
- accept heads with more rows than the vocabulary (padded shared head of DFlash)
- test-llama-archs --draft-vocab: listed logits equal the full head, others -inf,
  for llama, qwen35, qwen35moe, qwen4exp (CPU)
- tools/rdna3-w2/spec-trimmed-draft-vocab/mkvocab.py: 16k/32k/64k lists (python only,
  CPU llama-tokenize), tokenizer fingerprint of GGUF files
- docs notes (VRAM of the copy, tokenizer sharing)

Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit 356e964 into master Dec 4, 2026
10 of 39 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation.

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.

1 participant