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 |
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! |
DSpark (DeepSpec, 2026) on top of the merged DFlash drafter. It reuses the DFlash encoder/decoder graph, target feature extraction and KV-cache injection, and the verify/accept path unchanged; the draft model is a new "dspark" arch adding a low-rank Markov head (markov_w1/w2) and an optional (unused here) confidence head. No new public APIs. The proposal is the only change: the block is anchor-first (position 0 already predicts the first draft) and the decoder graph applies a semi-autoregressive, previous-token conditioned logit bias in-graph, chained per block position: logits'(i) = logits(i) + markov_w2 . markov_w1[prev(i)] prev(0) = the block's anchor token, prev(i>0) = argmax(logits'(i-1)) vectorized across all blocks in the batch; the anchors are fed through a dedicated graph input (token 0 of every block). Greedy stays lossless (verify unchanged, same as DFlash). - new arch "dspark" (llama_model_dspark : llama_model_dflash, reuses the graph, loads the markov/confidence tensors; shares the target's embed/lm_head). - Qwen3DSparkModel converter. - new spec type "draft-dspark" (common_speculative_impl_draft_dspark : common_speculative_impl_draft_dflash, overrides draft() only: submits whole anchor-first blocks and greedily reads back the biased logits).
- confidence head is loaded but not used yet - confidence-scheduled prefix pruning is not implemented - the in-graph Markov chain is greedy-only - only Qwen3 backbones are supported for now (also noted in docs)
Address review: drop LLM_ARCH_DSPARK and the dspark.block_size / markov_rank GGUF keys. A DSpark draft now converts to a DFlash GGUF; the Markov head tensors are detected by presence (like eagle3 d2t), block_size is read from the existing dflash.block_size key, and the block anchors are taken as a strided view of the decoder's token input instead of a separate graph input.
The DSpark confidence head predicts per-position acceptance of the drafted block. --spec-draft-conf-min truncates the block at the first position below the threshold (default 0 = disabled).
|
Ran an independent validation on 2x RTX Pro 6000 (Blackwell, sm120), CUDA build of this branch (27cc3ba), using the in-tree SPEED-Bench client rather than ad-hoc prompts. Setup: target Decode throughput (speed_bench_compare.py output):
A few observations:
Not exhaustively tested. Single machine, one target model, greedy only. Happy to run other configs if useful. |
|
I went ahead and done work on adding DSpark support for Qwen3.5/3.6. But respectfully I'll not attempt to make a PR since I am not fluent in C++ to write the PR for this work to upstream llama.cpp in my words. I leave that to the rest of the community and the respected maintainers of this project to either take the ideas and/or code and add that support to llama.cpp from my forks/branches. Following are the details:
I understand to some extent to guide my AI agents to do the work properly, but not the fluency expected in the PR in my own words. AI can't do all this on its own, I guided it, to as much extreme extent I wish it to produce measurable quality data and code. Much appreciate for your work and contributions. I made sure to preserve/give credit to all on whom my contribution built on top. I hope this helps the community. 🙏 |
…4-Flash backbone drafter (PR ggml-org#25173 + yaniss dsv4 fixes)
Thank you for your contribution. However, I noticed that the currently popular backbone architectures are Qwen3, Gemma4, and DeepSeekV4. I also see that vLLM supports exactly these architectures, and I intend to follow the same approach, as supporting too many architectures may increase maintenance burden. The final decision should be left to the maintainers. |
|
Waiting for this PR to be merged.😁 |
|
spectypelog.txt |
I found that your command is incorrect. If you want to put the command on a single line, please remove the . Alternatively, you can copy the command from my description and keep the line breaks as they are. |
|
I would have posted results from speed-bench, but since they were going to take 12+ hours, i decided not to |
|
/bot review |
Automated code reviewReview of PR #25173 — adding the I traced the new graph code in Blocking(point 1) Markov head assumes all drafting sequences use an identical block size, and silently corrupts logits when they don't. In Will slow the review(point 2) (point 3) (point 4) Verify the conversion's (point 5) No tests or acceptance/perf numbers. This adds a new speculative draft type, a new conversion path, and new GGUF tensors, with no test, no sample GGUF, and no acceptance-rate / latency numbers. The skill guidance for new drafting features expects at least a converted sample plus measured acceptance on a supported target (e.g. Nits(point 6) (point 7) The no-op returns in (point 8) (point 9) The clamp warning text ( The overall approach — reusing the DFLASH architecture, gating the Markov head on tensor presence, and routing the confidence output through the existing This review was generated automatically by pi coding agent using |
|
@wjinxu can you address the first point in the auto review? (feel free to tell if it's valid or not - other points are optional) |
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