Is speculative decoding lossless? Why greedy output changes, and a test that tells numerics from bugs
In exact arithmetic, yes: with the standard acceptance rule, speculative decoding returns the same greedy text and samples from the same distribution as the target model alone. On real engines, three things break that. (1) Numerics: the target checks k drafted tokens in one forward pass over k+1 rows, while ordinary decoding runs one row at a time, and the two do not round identically. Where the top two tokens are nearly tied, the choice flips and greedy output diverges from there on. This is expected and not a bug in the acceptance rule. (2) Bugs in the speculative path: open reports in llama.cpp, vLLM and SGLang (table below) show divergence where the baseline's token was a clear winner, or sampled answers that are measurably skewed. (3) Settings that are lossy by design, such as relaxed acceptance thresholds or synthetic acceptance. specdiff.py sends the same prompts to your endpoint with and without speculation and reports which of the three you have.
Why can greedy output differ with speculative decoding on?
Because the verify pass and plain decoding compute the same logits with different kernels or different launch shapes, and greedy decoding turns one flipped near-tie into a different continuation.
- The acceptance rule itself is exact. At temperature 0 the target accepts a drafted token only if it is the target's own argmax; at temperature > 0 the rejection-sampling rule of Leviathan et al. and Chen et al. keeps the target distribution. Neither depends on how good the draft is: a bad draft costs speed, not quality.
- The arithmetic is not exact. vLLM's documentation says speculative sampling is "theoretically lossless up to the precision limits of hardware numerics" and that vLLM "does not currently guarantee stable token log probabilities" (vLLM docs, latest). vLLM RFC #54506 isolates the mechanism: the verify pass computes rows with M = k+1, decode with M = 1, and split-K GEMMs and M-dependent reductions give different bits for the same row. In the reporters' workload, that alone dropped acceptance from about 100% to 72%.
- Quantized targets make it more visible. In llama.cpp #25618, draft-model speculation (DSpark, MTP) diverged from plain decoding on Q4_K_M targets but matched on a bf16 target (Vulkan).
- Divergence at 100% acceptance is a tell. In llama.cpp #31472, 184 of 184 draft tokens were accepted and the output still differed, so every emitted token came from a multi-token verify batch. Rejections play no part in that.
How do I tell a numerics flip from a real bug?
Look at how far ahead the baseline's token was at the first divergent position. A flip caused by rounding needs a near-tie: the two candidates are within about the size of the numeric noise. A clear winner that gets replaced is a bug in the speculative path, or a lossy setting.
| What you observe | Most likely cause | What to do |
|---|---|---|
Divergence at a position where the baseline's top two tokens are within the noise (specdiff: near-tie) | Verify-pass numerics (M = k+1 vs M = 1) | Accept it, or serve reproducibility-critical traffic without speculation. Test task metrics, not bytes. |
Divergence where the baseline's token was clearly ahead (large-gap) | Bug in the speculative path (state rollback, penalties, prefill path), or a lossy setting | Check the settings table, then the issue table; report with the specdiff JSON. |
| Sampled answers differ beyond chance at temperature > 0 | Lossy setting, or a sampling bug (e.g. padded draft trees, SGLang #43725) | Same as above. Near-ties cannot move a whole distribution. |
| Output differs at the very first token | The prefill path differs between the two servers (vLLM #58973); no draft is involved yet | Compare launch flags other than the speculative ones. |
| Same request gives different output depending on what was served before | State shared across requests, e.g. an n-gram corpus (SGLang #50395) | Restart and send the prompts in a fixed order, or use a per-request drafter. |
| The baseline disagrees with itself | Not speculation at all: batching, prefix cache, other traffic | Fix that first: vLLM batch invariance: what still breaks determinism. |
| No draft token was accepted | The test proves nothing: every token came from ordinary decoding | Use prompts the drafter can predict (repetition, code, copying). |
Is my speculative setting meant to be lossless?
Not always. These options trade the guarantee for speed on purpose. Checked in each project's source on 2026-11-18 (vLLM 92fd8cc, SGLang 21dae9b, llama.cpp 10f173b).
| Engine | Setting | Lossless? | What it does |
|---|---|---|---|
| vLLM | rejection_sample_method="standard" (default) | yes | Probabilistic rejection sampling; the setting the documented guarantee covers. |
| vLLM | rejection_sample_method="fly" | no | "FLy deliberately relaxes verification and is not distribution preserving" (docs). |
| vLLM | rejection_sample_method="synthetic" | no | Accepts drafts "with a decaying probability calibrated to synthetic_acceptance_rate" (config docstring): a benchmarking mode. |
| SGLang | --speculative-accept-threshold-single / -acc below 1.0 | no | Relaxed acceptance for EAGLE-family and DFlash verification. 1.0 is the default; rejection-sampling mode refuses other values. |
| SGLang | repetition_penalty ≠ 1 with EAGLE, EAGLE3, STANDALONE, NEXTN | no | The code applies "a relaxed version of penalties for speculative decoding"; output differs from plain decoding (#49403, open). |
| llama.cpp | --spec-synth-len, --spec-synth-rates | no | "benchmarking only": synthetic acceptance replaces verification entirely (server-context.cpp). Measured below. |
| llama.cpp | --spec-draft-p-min | yes | Stops drafting when the draft's top probability is below P. It changes what gets drafted, not how the target verifies. |
specdiff.py: test your own endpoint
Run the target twice, once without and once with speculation, then point the script at both. It needs nothing beyond Python 3.8. Download specdiff.py.
# vLLM: same model, same flags, only the speculative config differs
vllm serve MODEL --port 8000
vllm serve MODEL --port 8001 --speculative-config '{"method":"ngram","num_speculative_tokens":4,"prompt_lookup_max":4}' \
--per-request-spec-decode-metrics summary
# SGLang
python -m sglang.launch_server --model-path MODEL --port 30000
python -m sglang.launch_server --model-path MODEL --port 30001 --speculative-algorithm NGRAM
# llama.cpp
llama-server -m target.gguf --port 8080
llama-server -m target.gguf --port 8081 -md draft.gguf --spec-type draft-simple
curl -O https://erimaki-kobo.github.io/en/spec-decode-lossless-check/specdiff.py
python specdiff.py --base http://localhost:8000/v1 --spec http://localhost:8001/v1 --json report.json
What it does:
- Greedy test. Eight built-in prompts (or yours with
--prompts file.jsonl) at temperature 0, sent twice to the baseline and once to the speculative endpoint, one request at a time so batching stays out of the picture. For each pair it reports the first divergent token, the baseline's logprob gap between its own token and the one the speculative side chose, and the drafted/accepted counts. - Near-tie threshold from your own data. On the tokens both sides agree on, the largest logprob difference is the numeric noise of your stack. A divergence counts as a near-tie when the baseline's gap is at most max(0.05, 4 × that noise) nats, and as large-gap otherwise, including when the other token is not even in the baseline's top-k. Override with
--tie-threshold. - Sampling test. 200 samples per endpoint (
--samples) at temperature 1 for prompts with short answers of several tokens, then a chi-square test of homogeneity, plus total variation distance as an effect size. The first token comes from prefill on both sides, so the test looks at whole answers, whose later tokens go through verification. Small cells are pooled so the test stays valid. With many distinct answers TV is biased upward even when nothing is wrong; read the p-value. - Did speculation run? Draft and accepted counts come from vLLM's
metrics.speculative_decoding(needs--per-request-spec-decode-metrics), SGLang'ssglext.spec_tokens_details(the script sendsreturn_spec_tokens_details) and llama-server'stimings.draft_n/draft_n_accepted. If nothing was accepted, the verdict is inconclusive. - Bad logprobs are caught. llama.cpp #27972 (open) reports llama-server returning logprob 0.0 with empty
top_logprobsfor every token when speculation is on. If the speculative side does that, the script warns and classifies from the baseline's logprobs only. Our build at10f173breturned real logprobs with speculation on.
| Verdict | Exit | Meaning |
|---|---|---|
NO_DIFFERENCE_FOUND | 0 | Greedy outputs identical, sampled answers consistent, drafts accepted. |
SUSPECTED_BUG | 1 | A large-gap divergence, or a distribution difference at p < 0.001. |
NEAR_TIE_DIVERGENCE | 3 | Every divergence began at a near-tie: numerics. |
INCONCLUSIVE_* | 4 | The baseline is not reproducible on its own, or no draft token was accepted. |
| – | 2 | Could not run (connection, model id). |
Use --completions for models without a chat template, --extra '{"chat_template_kwargs":{"enable_thinking":false}}' to pass engine-specific fields, --skip-sample for the greedy test only. Both endpoints must serve the same weights; the script cannot check that.
What did the test find on a real engine?
On llama.cpp's CPU backend, draft-model speculation matched plain decoding token for token; n-gram speculation on a Q4_0 target flipped one near-tie; the benchmarking-only synthetic mode skewed the sampled distribution by a wide margin.
Setup: llama.cpp built from 10f173b (2026-11-18), CPU, 1 thread, llama-server with -np 1, /v1/completions. The target is a 4-layer Llama-architecture model with random weights (256 hidden, vocabulary 516, made with make_toy_gguf.py), so the text is nonsense, but every token goes through llama.cpp's real decode and verify kernels. Eight prompts × 256 greedy tokens; sampling: 600 samples per endpoint of 3-token answers for two prompts.
| Target | Speculation | Greedy: identical | Drafts accepted | Max |Δlogprob| on matching tokens | Sampling (p-value) | Verdict |
|---|---|---|---|---|---|---|
| Q4_0 | draft-simple, draft = same file, n_max 8 | 8 / 8 | 1656 / 1662 | 1.9e-6 | 0.73, 0.91 | no difference |
| Q4_0 | ngram-mod | 7 / 8 (diverged at token 148, gap 0.018 nats) | 61 / 128 | 0.030 | – | near-tie |
| F32 | ngram-mod | 8 / 8 | 37 / 317 | 2.1e-5 | – | no difference |
| Q4_0 | draft-simple + --spec-synth-len 4 | 8 / 8 | 1382 / 3803 | 1.9e-6 | 1.3e-37, 1.9e-15 | suspected bug (lossy by design) |
- The Q4_0 n-gram divergence reproduced at the same token on a second run, even though the drafted counts changed (156 / 532 accepted): ngram-mod keeps its n-gram state across requests, so what gets drafted depends on what was served before, but the flip itself was deterministic.
- In the synthetic row, greedy output still matched because the draft is the target itself, so every accepted token was the target's own argmax. At temperature 1 the most common answer appeared 122 times in 600 instead of 37. This is the kind of shift the greedy test alone would miss.
- Not measured here: GPU backends (CUDA, Vulkan, ROCm), real model weights, vLLM and SGLang. The issues below report those. The tool's classification and its statistics were also checked against a toy server in six failure modes plus clean runs (mock_server.py, run_mock_tests.sh).
Which engines and methods are known to break greedy equivalence?
As of 2026-11-18, every major engine has at least one open report. "Class" is our reading of the report: numerics means divergence at near-ties from verify-pass arithmetic; bug means output that is wrong beyond rounding; design means a deliberate relaxation.
| Engine | Report | Method / condition | What happens | Class | State |
|---|---|---|---|---|---|
| llama.cpp | #25618 | draft-mtp, draft-dspark; quantized target (Q4_K_M), Vulkan | Greedy text differs from plain decoding; bf16 target matches; n-gram matches on the same Q4 target | numerics (path-dependent quantized compute) | open |
| llama.cpp | #27407 | draft-simple and DFlash2, CUDA (RTX 3090 Ti), IQ2_M target | Deterministic near-tie flips under batched verification ("a bit-exactness concern only") | numerics | closed 2026-10-09 |
| llama.cpp | #31472 | draft-dflash, draft-dspark on qwen35moe (Gated DeltaNet) targets; CPU and SYCL | Differs on 7 of 8 prompts, including with 100% acceptance | numerics or recurrent-state handling (not isolated) | open |
| llama.cpp | #27972 | llama-server, any spec type (reported with draft-mtp, Vulkan) | logprob 0.0 and empty top_logprobs for every token | bug (reporting only; tokens unaffected) | open |
| vLLM | #54506 | Any method; MTP on a hybrid MoE model, TP4 | Verify pass (M = k+1) and decode (M = 1) differ in bits; RFC, no fix proposed | numerics | open |
| vLLM | #62304 | n-gram on Qwen3.5 (Gated DeltaNet), v0.31.0 | Wrong greedy output when drafts are partly accepted; GDN state misses the last accepted token; reproduced with weights insensitive to reduction order | bug | open |
| vLLM | #54928 | DFlash2 on Qwen3.8-27B with thinking, TP4 | Diverges at generated token 30, including with K = 1 and --enforce-eager | not isolated | open |
| vLLM | #58973 | V2 speculative path, DFlash2 or DSpark drafters, Qwen3-4B | The first greedy token differs: target prefill picks a different RMSNorm autotune configuration | numerics (prefill path) | open |
| vLLM | #52071 | Pipeline parallelism with --no-async-scheduling | Wrong output or a CUDA fault; async scheduling avoids it | bug | open |
| SGLang | #45525 | DSPARK on DeepSeek-V4-Flash | Verify and decode give different logits and next tokens for the same prefix | not isolated | open |
| SGLang | #49403 | EAGLE family with repetition_penalty ≠ 1 | Penalty applied to one token per verify step | design | open |
| SGLang | #50395 | NGRAM with --enable-deterministic-inference | Output depends on earlier requests (shared n-gram corpus); EAGLE3 and no-spec are bit-identical | numerics (tree layout) | open |
| SGLang | #43725 | NGRAM at temperature > 0, CUDA | Token id 0 sampled far too often (0.538 instead of 0.0485 in the report) when the corpus has few candidates | bug (distribution) | open |
| SGLang | #39597 | DFLASH with FlashInfer autotune | Greedy output changes across server restarts; --disable-flashinfer-autotune fixes it | numerics | open |
States read on 2026-11-18. A closed issue is not necessarily a fixed one: #27407 was filed as a documentation request with no fix claimed.
FAQ
- Is speculative decoding lossless?
- The algorithm is: with the standard acceptance rule, greedy output and the sampling distribution equal the target's. Implementations are lossless only up to floating-point rounding, which can flip near-tied tokens, and only when the speculative path has no bugs and no relaxed settings are on.
- Why does my output change at temperature 0 when I turn on speculative decoding?
- Most often a near-tie flipped because the verify pass computes several tokens at once with different rounding. Once one token differs, the rest of the greedy continuation differs too. If the baseline's token was clearly ahead at the first difference, suspect a bug instead.
- Does n-gram speculation avoid the problem?
- Not reliably. It adds no draft model, but the target still verifies in batches. In llama.cpp #25618 n-gram matched plain decoding on a Q4 target; in our CPU run an n-gram setup flipped one near-tie on Q4_0. vLLM #62304 and SGLang #50395 report n-gram-specific problems.
- Does a low acceptance rate mean lower quality?
- No. Acceptance affects speed only. Quality changes come from numerics, bugs or lossy settings, none of which show up in the acceptance rate.
- Can I trust logprobs with speculative decoding on?
- Check them. vLLM does not guarantee stable logprobs, and llama.cpp #27972 reports all-zero logprobs from llama-server with speculation. specdiff.py detects the all-zero case.
Limitations
- The near-tie threshold is a heuristic. A bug can produce a small-gap divergence and pass as numerics; repeated near-tie verdicts on many prompts are still worth a look.
- top_logprobs are capped (vLLM's default
--max-logprobsis 20). If the speculative side's token is not in the baseline's list, the gap is a lower bound and the divergence is classed as large-gap. - One request at a time isolates speculation from batching. Production traffic adds batch effects on top; test those separately.
- The measurements on this page are from a CPU build with a random-weight model. GPU numbers and real models are not measured here.
Sources
- Leviathan, Kalman, Matias, Fast Inference from Transformers via Speculative Decoding, 2022.
- Chen et al., Accelerating Large Language Model Decoding with Speculative Sampling, 2023.
- vLLM docs: Speculative Decoding (latest, dated 2026-11-16), Per-Request Acceptance Metrics.
- Source code read on 2026-11-18: vllm/config/speculative.py; sglang …/openai/protocol.py; llama.cpp tools/server/server-context.cpp, common/arg.cpp.
- Issues as linked in the table above.
- Related: vLLM batch invariance: what still breaks determinism; vLLM logprobs vs Hugging Face: what’s normal.