Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A fast guesser and a careful checker
A big model writes one word at a time, and each word takes about as long whether it checks one guess or five. So we let a small, quick model guess the next few words, and the big model checks them all at once. Correct guesses are kept for free; at the first wrong guess, the big model writes its own word. The clever part is a rule that makes the final text exactly as if the big model had written it alone. The demo shows how many words get accepted per check.
Speculative decoding cuts response latency 2–3× with identical outputs, valuable where interactive speed matters, such as an operator assistant, without changing the validated model.
- 1. The draft model is…
- 2. Speculative sampling changes the output distribution?
- 3. The target checks the drafted tokens…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Algorithm: draft γ tokens from q; one target pass gives p at every drafted position; accept token x with probability min(1, p(x)/q(x)); at the first rejection sample from norm(max(0, p − q)) and stop; if all are accepted, sample one bonus token from p.
- Exactness: P(output x) = min(p, q)(x) + (1 − Σ min(p, q))·norm(max(0, p − q))(x) = p(x).
- Acceptance rate: α = Σₓ min(p(x), q(x)) = 1 − TV(p, q): the draft must agree with the target in distribution.
- Expected tokens per target pass: (1 − α^(γ+1))/(1 − α) for i.i.d. acceptance; speedup ≈ that divided by (1 + γc), with c the draft/target cost ratio.
- Variants: greedy verification (exact-match prefix); prompt-lookup (n-gram copying from the context); Medusa (extra decoding heads); EAGLE (feature-level draft head); self-speculation (draft with skipped layers).
- Where it helps: latency at small batch sizes; at large batch sizes the GPU is already compute-bound and gains shrink.
- 1. Acceptance probability of a drafted token:
- 2. Expected acceptance α equals…
- 3. Speculative decoding helps most at…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Speculative decoding in practice
Exercise 1.
- Compute the acceptance probability of a drafted token.
- Compute the residual distribution after a rejection.
- Compute expected tokens per target pass.
Practice.
- Hugging Face
generate(..., assistant_model=draft)does assisted generation;prompt_lookup_num_tokensenables n-gram drafting. - vLLM and TensorRT-LLM support draft models, n-gram, Medusa and EAGLE speculators.
- The draft must share the target’s tokenizer (or use universal assisted decoding); a 10–20× smaller model of the same family is typical.
- Measure acceptance on your traffic and tune the number of speculative tokens (often 3–5).
from transformers import AutoModelForCausalLM
target = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
draft = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
out = target.generate(**inputs, assistant_model=draft, do_sample=True, max_new_tokens=256)
# vLLM: LLM(model=..., speculative_config=dict(model="Qwen/Qwen2.5-0.5B-Instruct", num_speculative_tokens=4))- 1. Hugging Face argument for a draft model:
- 2. Draft and target must usually share…
- 3. Typical number of speculative tokens:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Exactness proof. For one position, the output is x either because the draft proposed x and it was accepted, with probability q(x)·min(1, p(x)/q(x)) = min(p(x), q(x)), or because a rejection occurred (probability 1 − Σ min(p, q) = Σ max(0, p − q)) and x was drawn from the residual, with probability max(0, p(x) − q(x))/Σ max(0, p − q). The sum is min(p, q)(x) + max(0, p − q)(x) = p(x). Later positions are conditioned on the accepted prefix, so the whole sequence follows the target.
Speedup. A target pass over γ + 1 positions costs about the same as a pass over one in the memory-bound regime. With i.i.d. acceptance α, the number of tokens per pass is a truncated geometric with mean (1 − α^(γ+1))/(1 − α). Each round also costs γ draft passes, so the speedup is (1 − α^(γ+1))/((1 − α)(1 + γc)). There is an optimal γ: larger α and smaller c favour longer drafts.
Draft quality. Acceptance depends on the total variation between draft and target, so a draft that captures the target’s context dependence (here, a small trigram) is worth more than a cheaper but cruder one.
- 1. A rejected position samples from…
- 2. Tokens per pass follow a…
- 3. The optimal γ grows with…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How do draft quality and draft length affect speculative decoding, and is the output really unchanged?
History.
- M. Stern et al., “Blockwise parallel decoding” (2018).
- Y. Leviathan, M. Kalman and Y. Matias, “Fast inference from transformers via speculative decoding” (ICML 2023); C. Chen et al., “Accelerating LLM decoding with speculative sampling” (2023).
- T. Cai et al., “Medusa” (2024); Y. Li et al., “EAGLE” (2024).
Exercise 2.
- Target: a Kneser–Ney trigram model trained on 30 000 tokens of the F1-2 source.
- Drafts: a bigram model on the same data, a unigram model, and a trigram trained on only 2000 tokens.
- Runs: 3000 tokens per setting.
- Exactness check: token frequencies of 20 000 speculative tokens against 20 000 plain target samples, compared with two independent plain runs.
| draft | γ = 1 | γ = 2 | γ = 4 | γ = 8 |
|---|---|---|---|---|
| tokens per target pass, small trigram | 1.70 | 2.17 | 2.86 | 3.29 |
| tokens per target pass, bigram | 1.63 | 2.04 | 2.44 | 2.65 |
| tokens per target pass, unigram | 1.35 | 1.51 | 1.61 | 1.55 |
| speedup at c = 0.1, small trigram | 1.54 | 1.81 | 2.04 | 1.83 |
| speedup at c = 0.1, bigram | 1.49 | 1.70 | 1.74 | 1.47 |
| speedup at c = 0.1, unigram | 1.23 | 1.26 | 1.15 | 0.86 |
- Exact: total variation between speculative and plain target token frequencies was 0.018, the same as between two plain target runs (0.017): pure sampling noise.
- Draft quality matters most: the trigram draft trained on 15× less data beat the bigram trained on all of it, because it captures the same two-token context as the target.
- There is an optimal γ: with drafts costing 10% of the target, γ = 4 was best for the good drafts; for the unigram draft, γ = 8 made decoding slower than without speculation.
- Seeds 1 and 2: same ordering (small trigram 2.82 and 2.85 tokens per pass at γ = 4; TV 0.027 vs 0.022 and 0.022 vs 0.020).
Speculative decoding in AI, robotics and aerospace
- Production LLM serving: draft models and EAGLE-style heads are standard in vLLM, TensorRT-LLM and provider APIs for lower latency.
- Code assistants: prompt-lookup drafting works well when edits copy large parts of the input.
- Robotics: speculative ideas apply to autoregressive action models where low latency matters.
References
- Y. Leviathan, M. Kalman and Y. Matias (2023). “Fast inference from transformers via speculative decoding.” ICML.
- C. Chen et al. (2023). “Accelerating large language model decoding with speculative sampling.” arXiv:2302.01318.
- T. Cai et al. (2024). “Medusa: simple LLM inference acceleration framework with multiple decoding heads.” ICML.
- Y. Li, F. Wei, C. Zhang and H. Zhang (2024). “EAGLE: speculative sampling requires rethinking feature uncertainty.” ICML.