spec: add DSpark speculative decoding#25173
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
Hi @CISC @ggerganov , this adds DSpark speculative decoding on top of the merged DFlash drafter (#22105). It's a small change — a new dspark draft arch and draft-dspark spec type that reuse DFlash's graph, feature extraction, KV-cache injection and verify path unchanged; the only new logic is the semi-autoregressive Markov head in draft(). Greedy stays lossless. I benchmarked it against the merged DFlash using DeepSeek's released Qwen3 DSpark drafts. On Qwen3-8B at bf16 / Q8_0 / Q4_K_M, DSpark beats DFlash on every domain (e.g. GSM8K bf16 4.06× vs 3.12×; full per-domain tables in the PR description). I believe it's ready for review and I'm happy to walk through any part of it. |
|
Can you run SpeedBench to do the full comparison between DFlash and DSpark with the same |
|
@ruixiang63 I've run the SpeedBench test set as you suggested, and updated the results in the PR description. DSpark does outperform DFlash across the board. |
aae2941 to
d8b38f2
Compare
|
could you give some examples how to use? |
Good point — I've updated the PR description with a more detailed, copy-pasteable end-to-end example (download → convert → build → run → curl). Let me know if anything's unclear. |
|
DSV4 support was merged in #24162, ideally this PR should cover that model as well and try to replicate a similar speedup |
Thanks! DeepSeek hasn't open-sourced the DSpark weights for DeepSeek-V4 though — only the Qwen3 and Gemma4 drafts are released. So this PR covers Qwen3 for now, and I'll add Gemma4 as a small follow-up. |
|
I think they're a part of the spec decoding module https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash-DSpark i.e not distributed separately |
Sorry, and thanks for the heads-up. For this PR I'd like to keep the scope a bit narrower for now - land the Qwen3 DSpark path first and get it solid, then add Gemma4 and DSV4 as follow-ups. Does that sound ok? |
|
Okay, will try to review. From a cursory look it does not look like adding |
I agree that |
|
I folded the DSpark impl into the DFlash one: the duplicated draft() is gone, and a shifted member (derived from the existing spec type, i.e. draft-dspark vs draft-dflash) now selects the block layout — anchor-first blocks read predictions from position 0 and always submit a whole block, in-place blocks keep the old behavior. Happy to adjust if you'd prefer a different approach. I also wired up the confidence head behind a new launch-time flag (--spec-draft-conf-min): it prunes each drafted block at the first position whose predicted acceptance falls below the threshold, default 0 = disabled. This is the static-threshold variant. I'll update the PR description with benchmark numbers shortly. |
ruixiang63
left a comment
There was a problem hiding this comment.
It would be better to touch the current DFlash code as little as possible to avoid any potential breakage, and use is_dspark as the condition where needed.
|
I have rebuilt wjinxu's llama.cpp (dspark-upstream branch) and model loading broke after the latest commits :
|
The latest commits may have made older GGUF files incompatible; could you try reconverting the model and let me know if the issue persists? |
@wjinxu Yes! Thank you! Reconverting the hf draft model file to GGUF fixed the problem! Please let me know if there is anything I can help you with to implement DSpark speculative decoding for the Gemma-4 LLMs. |
I plan to open a draft PR tomorrow with Gemma 4 support for testing. |
ruixiang63
left a comment
There was a problem hiding this comment.
It looks good to me now. I made a few cleanup changes, please test on your side to see if anything breaks. @wjinxu
@ggerganov Could you take another look at the current changes, especially build_dspark_markov_head in dflash.cpp?
I reran the validation for both DSpark and DFlash on the GSM8K dataset, and both produced the expected results. |
|
What kind of actual performance gains will we see from Dspark spec-coding? Obviously we won't be seeing their gains, although i have seen some demos going over the % they showed, but if it's anywhere close to the claim, that would be insane. |
|
Tested llama.cpp PR #25173 on AMD Strix Halo iGPU (Vulkan): Qwen3-8B Q4_K_M target vs DSpark draft (bf16 and Q4_K_M), --spec-draft-n-max 2/3/7, greedy decode, thinking on/off. Script: https://gist.github.com/Ankk98/ee29401e4dcdcae575e649c4a0b75d9e Speed: No config beat vanilla (~45 t/s). Best geomean was ~0.81x (dspark-q4-n2/n3). Even ~93–98% accept only reached ~0.8–0.9x. Larger n-max (7) hurt when accept dropped (~23–45%). Q4 draft helped vs bf16 but still slower overall. Correctness: Repeated greedy mismatches vs vanilla (BUG / TRUNC_BUG), especially open-ended prompts and thinking, probably a bug in llama.cpp master itself. Learnings: Spec decode wins when draft < target. On unified-memory Vulkan that ratio is weak (heavy draft + target share bandwidth), so RTX 4090 CUDA gains do not carry over. Prefer smaller n-max and quantized drafts on iGPU; validate losslessness under greedy before chasing speed. |
@FHRacing
In other words, imho it's impossible to generalize, each individual case will see different performance gains (or no gains at all, or losses). You just have to try it for yourself and see what works for you. |
I also ran tests on CUDA and encountered the same issue, but I didn't see this problem when using BF16 precision. I suspect it might be a precision issue specific to Q4_K_M — you could try testing with BF16 precision as well to confirm.
I also noticed that on Q4_K_M, the speedup isn't very noticeable, because the DSpark draft model isn't actually that small, so the benefit it brings on Q4_K_M is limited. |
|
@wjinxu Did some more investigation and filed a separated issue for divergence bug. This bug threw me offcourse in my attempt to implement dspark speculation as well. I think I am close to finding the root cause. I will create a separate PR for this. |
|
Hi @wjinxu — I've been building on this branch to support the DSpark drafter that ships with a DeepSeek-V4 backbone (drafting for DeepSeek-V4-Flash), and I hit a bug that lives in the shared
What makes it nasty is the failure mode. It does not crash and it does not produce garbage output: block position 0 stays correct, because it is carried by the injected target hidden state and the shared lm_head, so the model keeps generating perfectly reasonable text. The deeper block positions quietly lose their positional signal and start repeating the earlier ones, and acceptance just stalls at ~2 tokens — which reads like "the drafter is mediocre" rather than "the drafter is broken". Selecting the pairing from the loaded backbone takes mean accepted length from 2.0 to 5.2–6.0 (block size 5) on my setup, with the flat per-position acceptance curve the paper describes. Three other things I had to fix in the shared path while getting this working, all of which apply to the Qwen drafter too:
Happy to send these as a small PR against your branch if that is easiest for you, or to fold them into the DeepSeek-V4 PR I'm preparing (which is stacked on this one) and let you cherry-pick — whichever you prefer. Nice work on this, the speculator structure made the backbone swap much easier than I expected. |
|
@YanissAmz that's actually a subtle one and I'm wondering if it doesn't affect the other "derived architectures": EAGLE and DFlash. @ruixiang63 maybe you have an idea? |
@YanissAmz |
This comment was marked as off-topic.
This comment was marked as off-topic.
This is indeed an issue that neither Gemma4 nor Qwen3 has, but perhaps we could unify it to NEOX during the DeepSeekV4 conversion? I noticed that eagle also uses the same approach.
I found that there is cleanup in the server. |
This is expected as draft model is alwasy greedy.
This shouldn't be the case. All past KV caches are either purged or restored.
I agree. This can be handled as the same way as eagle3 does. |
|
Ah, okay, you reorder the tensors to NEOX during the conversion process. |
|
Hi @ggerganov, could you review this PR whenever you get a chance? Thanks in advance! |
This PR adds DSpark speculative decoding, layered on the merged DFlash drafter. DSpark (DeepSeek + PKU, 2026 — "Confidence-Scheduled Speculative Decoding with Semi-Autoregressive Generation", the DeepSpec repo) is DFlash plus a small semi-autoregressive Markov head: where DFlash takes an independent argmax at each block position (every position marginalizes over all possible predecessors, so acceptance decays along the block), DSpark adds a low-rank, previous-token-conditioned logit bias and samples the block left-to-right, so each draft conditions on the one actually sampled before it. This lifts accepted length at near-zero extra draft cost.
DSpark reuses the entire DFlash machinery unchanged — the encoder/decoder graph, target-layer feature extraction (
llama_set_embeddings_layer_inp/_nextn), KV-cache injection, and the verify/accept path. The only additions are:dspark(llama_model_dspark : llama_model_dflash) that reuses the DFlash graph and additionally loads the Markov head (markov_w1,markov_w2) and an optional confidence head; it shares the target's token-embeddings / lm_head (same as DFlash);draft-dspark(common_speculative_impl_draft_dspark : common_speculative_impl_draft_dflash) that reusesprocess()(extraction + injection) and overrides onlydraft(): the block is anchor-first (position 0 already predicts the first draft token) and sampled with the Markov biasbias(prev) = markov_w2 · markov_w1[prev], computed on-device (llama_dspark_markov_bias);Qwen3DSparkModelconverter.Greedy decoding is lossless: the Markov bias only changes which tokens are proposed; every draft is still verified against the target, so the output is identical to non-speculative greedy.
The confidence head is converted/loaded but not used at inference in this PR (phase 1); the draft-quality win from the Markov head is self-contained and is what the numbers below measure.
How to run
Complete example from scratch (Qwen3-8B). Drafts for other sizes are on the same org:
deepseek-ai/dspark_qwen3_{4b,8b,14b}_block7.1. Get the models — target + its DSpark draft:
2. Convert to GGUF — the draft ships no tokenizer and reuses the target's, so pass
--target-model-dir:python convert_hf_to_gguf.py Qwen3-8B --outtype bf16 --outfile Qwen3-8B.gguf python convert_hf_to_gguf.py dspark_qwen3_8b --outtype bf16 \ --target-model-dir Qwen3-8B --outfile Qwen3-8B-DSpark.ggufYou may quantize the target (e.g.
llama-quantize Qwen3-8B.gguf Qwen3-8B-Q4_K_M.gguf Q4_K_M); keep the draft bf16 — it's tiny, and acceptance is unaffected by target quant.3. Build with CUDA:
cmake -B build -DGGML_CUDA=ON && cmake --build build --config Release -j4. Run — the only DSpark-specific flags are
-md <draft>and--spec-type draft-dspark(
--spec-draft-n-max= draft tokens per step; the released checkpoints use block size 7):./build/bin/llama-server -m Qwen3-8B.gguf -md Qwen3-8B-DSpark.gguf \ --spec-type draft-dspark --spec-draft-n-max 7 \ --temp 0 --top-k 1 -np 1 -c 4096 -ngl 99 -fa on --jinja5. Send a request (the server logs
draft acceptance = ...per request):llama-cliworks the same way (-m ... -md ... --spec-type draft-dspark). Note:draft-dsparkneeds the target's hidden states (KV-cache injection), so usellama-server/llama-cli— thespeculative-simpleexample does not drive that path.Performance
SpeedBench (llama.cpp's own
tools/server/bench/speed-bench)Qwen3-8B (bf16), matched
--spec-draft-n-max 7,qualitativesplit (11 categories), greedy. Baseline is the same server with no draft model. DSpark reaches 1.88× overall decode speedup vs baseline (DFlash is 1.55×), and beats the merged DFlash on every one of the 11 categories (overall 1.21×).DSpark vs baseline:
DSpark vs the merged DFlash (same
--spec-draft-n-max 7):Hardware: RTX 4090. Target Qwen/Qwen3-8B (bf16), draft deepseek-ai/dspark_qwen3_8b_block7 (bf16). Greedy (
--temp 0 --top-k 1), no-thinking,--spec-draft-n-max 7. Baseline = same llama-server with no draft model. DFlash is the merged drafter (z-lab/Qwen3-8B-DFlash, b16), run at the same matched draft size for an apples-to-apples comparison. Per-domain aggregate over the listed prompt counts.Losslessness
Greedy decoding is lossless by construction (the draft is verified against the target). Output is coherent and matches non-speculative greedy.
Qwen3-4B, target bf16
DSpark vs baseline (DFlash was not benchmarked at 4B — no nested-schema 4B DFlash draft available):
Qwen3-8B, target bf16
Qwen3-8B, target Q8_0
Qwen3-8B, target Q4_K_M
DSpark beats the merged DFlash on every domain (higher accept rate and higher throughput), for a ~1.16× geomean speedup over DFlash. The gains are largest on reasoning (GSM8K +25pp accept, 1.30× over DFlash) and open chat (MT-Bench, 1.29×); on code (HumanEval) the two are close as both already accept ~80%.
Confidence Evaluation
Qwen3-8B Q4_K_M target, SPEED-Bench qualitative, 132 completed samples, OSL 512.
Percentage changes on the unified-KV rows are relative to the same-concurrency unified-KV
conf_min=0.0baseline.Confidence pruning has no benefit at concurrency 1, begins to help at concurrency 8.The intended operating environment is high-concurrency serving with packed/unified KV batching.
Do not enable confidence pruning with non-unified KV at high concurrency. Ragged verification causing CUDA Graph reuse to collapse.
Future work
Requirements