Speculative decoding
Draft, verify, and the expected tokens per step.
Check yourself ↓ three questions and what to remember, at the end
Draft and verify, pass by pass: each verify pass keeps the accepted drafts and one more token, as the serving simulator drew them.
Loading the animation (recorded by the simulator)…
Concept
Decode is memory-bound: a step spends its time reading the weights, and the arithmetic units mostly wait. So a step that checks several positions costs about the same as one that produces a single token. Speculative decoding exploits that.
- A small, fast draft model (or a cheap extra head on the big model) guesses the next tokens, one after another.
- The large target model runs one forward pass over all guesses at once, as if they were a short prompt, which gives its own distribution at every one of those positions.
- A verify rule walks the guesses in order, accepting each with probability , where is the target's probability of the guessed token and the draft's. At the first rejection it draws a replacement from the leftover distribution and stops. If all are accepted, the target's pass also gives one more token for free.
Each target pass therefore yields between 1 and tokens. And the rule is built so that the tokens come out with exactly the target model's distribution. Speculative decoding changes the speed, never the output distribution: the draft only decides how much of the target's work is reused. (Under greedy decoding this reduces to "keep the guesses that match the target's argmax".)
What it costs: the draft's own steps, and some wasted target work on rejected guesses. It pays when the draft agrees with the target often (acceptance high) and is cheap (cost ratio low). It helps most at small batch sizes; at large batches the target step is no longer so memory-bound, and the verified positions compete with other users'.
Variants. Medusa adds several extra decoding heads to the target itself, each predicting a token further ahead, and verifies a tree of candidates in one pass. EAGLE drafts at the level of the target's hidden features rather than tokens, with a light head; EAGLE-2 (Li et al. 2024) grows its draft tree dynamically and EAGLE-3 (Li et al. 2025) drafts from several of the target's layers. Models trained with multi-token prediction heads can draft with those heads (Architectures, chapter 7). All of these avoid running a separate draft model. Current engines also verify a tree of drafts in one pass rather than a single chain, and offer n-gram (prompt-lookup) drafting, which copies continuations from the prompt and costs no model at all (vLLM's speculative decoding docs).
Interactive
Draft γ tokens, verify them in one pass
Top: Leviathan et al.'s closed forms (i.i.d. acceptance α; one draft step costs c of a target step). Bottom: 4,000 rounds of the real accept/reject rule on an 8-token toy vocabulary.
Grey: the target distribution p. Blue: what speculative decoding actually emitted. They match whatever the draft is: only the speed changes.
Maths
Let the target be and the draft . A drafted is accepted with probability , so the per-token acceptance is
On rejection the replacement comes from . The emitted token's distribution is then
exactly the target's (Leviathan et al., §3; Chen et al.). With i.i.d. acceptance and drafts, the expected tokens per target pass are
and if a draft step costs target steps, the expected speed-up is (their Theorem 3.8). For example , gives 3.36 tokens per pass, and with a 2.80× speed-up.
Code
// src/lib/inference/speculative.ts (excerpt)
export function verify(
p: Vector,
q: Vector,
x: number,
u: number,
rng: () => number,
) {
if (u < Math.min(1, p[x]! / q[x]!)) return { accepted: true, token: x };
return { accepted: false, token: sampleFromProbs(residual(p, q), rng) };
}
The tests check the emitted distribution equals to , and run 40,000 Monte Carlo rounds whose acceptance, tokens per pass and token histogram match the formulas.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Commit to a number, then reveal.
Reveal the answer
Answer: 2.8 (counted right within 5%)
2.8 times: the tokens per pass divided by , the cost of four draft steps and one target pass.
Commit to a number, then reveal.
Reveal the answer
Answer: Never: the tokens come out with exactly the target's distribution
Never: accepting each draft with probability and redrawing from the leftover distribution after a rejection reproduces the target's distribution exactly. Only the speed changes.
Pick one, then reveal.