Speculative Decoding
AI Engineer path · lesson 105 of 124Let a small draft model guess K tokens ahead and have the real model check them all in one pass. Drive the accept-reject loop, see why the verification is close to free, and reproduce DeepMind's Chinchilla-70B result — half the latency, identical output.
Key insight
The loop yields j+1, not j. Even when the target rejects the very first proposal, it still emits the correct token for that position from the verification pass it already ran — so a completely useless draft model costs you the drafting time and nothing else. That asymmetry is what makes the technique safe to switch on.
Draft and verify
Order one round of speculative decoding.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
In speculative decoding, the draft model's job is to:
The short version
A junior drafting a letter for a senior to sign. The senior reads the whole draft in one pass and signs off everything down to the first thing they disagree with, fixes that line themselves, and hands it back. Reading is fast; writing is slow; and the senior's signature means the final letter is exactly what they would have written.
In more depth
Speculative Decoding
Generating one token requires one full forward pass, and the passes cannot overlap. Speculative decoding breaks the sequential bottleneck without changing the output.
The loop
So the loop yields 1 token if nothing is accepted and K+1 if everything is. Output quality is preserved exactly, because the target signs off on every token.
Three enabling insights
The numbers
DeepMind trained a 4B draft model of the same architecture for Chinchilla-70B (Chen et al., 2023): the draft generates a token eight times faster — 1.8 ms vs 14.1 ms — reducing overall response latency by more than half without compromising quality. Similar for T5-XXL (Leviathan et al., 2022).
Implementable in ~50 lines of PyTorch; already in vLLM, TensorRT-LLM and llama.cpp.
Trade-offs
Four variants
| Variant | Extra cost | Note |
|---|---|---|
| Draft model | A second model | The classic form |
| Inference with reference (Yang et al., 2023) | Nothing | Drafts from the input; 2× speedup where context and output overlap |
| Lookahead / Jacobi (Fu et al., 2024) | No second model | Same decoder; regenerate only failed tokens |
| Medusa (Cai et al., 2024) | Training the heads | k-th head predicts x(t+k+1); tree-based attention verification; NVIDIA claimed up to 1.9× on Llama 3.1 |
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required