Repository navigation
[w2] spec: reduced draft LM head over a trimmed vocabulary (--spec-draft-vocab, port of upstream 31031) - #165
Merged
Conversation
…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
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. |
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.
What
Opt-in reduced LM head for the draft context:
--spec-draft-vocab FILE(envLLAMA_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_mmthe draft multiplies by that copy and scatters the result into a full-vocabulary-inflogit tensor (ggml_fill+ggml_set_rows). Because the output stays full-vocab, no id map is needed incommon/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:
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 fingerprintsCoverage (all go through
build_lora_mm)nextn.shared_head_headormodel.outputgraph_mtp:model.output(shared with the target)mtp-Qwen3.8-27B-Q4_0.gguf, own Q3_Koutput.weight)dflash.cppbuild_logits): own head, or the target head viactx_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 oneqwen-*.txtserves 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:
The target keeps its full head.
--fitdoes 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, envLLAMA_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
common/speculative.cpp(merge after it; my hunks are only the loader and 4 lines incommon_speculative_init_result).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)
test-draft-vocab: OKtest-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: passbuild-bins/w2/spec-trimmed-draft-vocab/.GPU validation tonight (NOT YET RUN)
Preamble (P):
Qwen4 MTP:
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 presetnemotron-3.5-bolt-e4b-dflashandgemma-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