Before you move a control in the demo, say what you expect to change.
Foundation reference
Speculative Decoding: Lossless Multi-Token Generation
Speculative decoding lowers latency without changing the output distribution: Leviathan et al. (2023) report 2–3× faster generation from T5-XXL with the same output distribution.
Study guide
Where to start
Why does accepting a draft token with probability min(1, p/q) and resampling rejections from the normalized max(0, p − q) give samples exactly from the target distribution p?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Speculative decoding lowers latency without changing the output distribution: Leviathan et al. (2023) report 2–3× faster generation from T5-XXL with the same output distribution.
- Chen et al. (2023) report a 2–2.5× decoding speedup for the 70B-parameter Chinchilla model in a distributed setup.
- Tree-based speculation verifies several candidate continuations in one target pass; Sequoia reports up to about 4× faster decoding for Llama 2 7B on an A100 GPU (Chen et al., 2024).
- Per-token latency matters most in interactive use such as chat and code completion.
- The verification pass attends over the cached prefix plus the k draft tokens, so the KV-cache and FlashAttention techniques of Efficient Attention apply to it directly.
Questions to Think About
- How close must the draft model be to the target, and why does agreement on the current context matter more than overall quality?
- Why is checking k draft tokens in one target pass nearly as cheap as generating a single token?
- When does exploring a tree of candidate continuations beat a single draft sequence, given the extra computation it spends?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
An autoregressive language model produces one token per forward pass of the full model, so generation is sequential. Speculative decoding uses a small, fast draft model to propose tokens, then checks all of them with one parallel forward pass of the target model.
The method is exact: the output tokens, including residual replacements, are distributed exactly as if they had been sampled from the target model (Leviathan et al., 2023; Chen et al., 2023).
Acceptance probability for each draft token :
where is the target model distribution and is the draft distribution at position .
Residual sampling at the first rejected position:
Accepting with probability and resampling rejections from this residual gives a token distributed exactly as .
Speedup. If each draft token is accepted with probability (treated as independent), one target pass yields on average
tokens, and the expected wall-time improvement is , where is the cost of one draft step relative to one target step (Leviathan et al., 2023).