TL;DR
A large language model writes text one token at a time, and each token costs a full pass through the model. Speculative decoding runs a small, cheap model first to guess the next few tokens, then asks the large model to check all of those guesses in one parallel pass. Guesses that the large model would have produced are kept, and the first wrong guess is replaced by a corrected token. A rejection rule makes sure the final text has exactly the distribution the large model would have produced on its own, so the speed comes with no change in quality.
Where you'll see it: in serving large language models with lower latency, in the "assisted generation" option of open-source inference libraries, and in the Speculative Decoding paper that gave the method its name.
Intuition
A fast intern and a slow expert
Picture an expert who must approve every sentence an intern drafts. Reading a whole draft and checking it takes about as long as checking one word, because the expert can scan the page at once. So the intern writes five words quickly, and the expert approves the words up to the first mistake, fixes that one, and the intern continues from there. When the intern is usually right, the expert approves several words per look instead of one.
That is the whole idea. The intern is the small draft model . The expert is the large target model . The expert's check is the parallel pass.
Why it exists
Generation from a Transformer is slow for a structural reason. To produce token number , the model needs token , so decoding tokens takes serial runs of the big model. Running the big model is usually limited by memory traffic, not arithmetic: the weights and the cached keys and values have to be read from memory on every step, and the processor sits partly idle while it waits. A single pass that checks several positions costs about the same memory traffic as one pass that produces one token, so the idle capacity can be used.
Most of the simple fixes change the model. Distillation, quantization and early exit all alter the outputs or require retraining. Speculative decoding is different. It keeps the big model and its output distribution fixed, and only changes the order in which work is done.
The math
Setting
Let be the next-token distribution of the target model for a prefix , and let be the same for the draft model. Both are distributions over the vocabulary, after any sampling adjustments such as temperature or top- truncation. Write and when the prefix is clear.
The goal is to produce a token whose distribution is exactly , while calling the draft model as often as needed and the target model as rarely as possible.
The acceptance rule
Draw a candidate . Draw . If , accept . Otherwise reject and draw a replacement from the residual distribution
The rule is rejection sampling in disguise. The draft is the proposal, and the ratio plays the role of the acceptance test. The difference from plain rejection sampling is that a rejected candidate is not thrown away entirely: it is replaced by a token drawn from the part of that the draft under-covered. That replacement is what makes the scheme exact.
Why the output has distribution p
Let . This is the probability that a single candidate is accepted. The residual normaliser is , because
Now compute the probability that the output equals a given token . There are two ways to get it. The candidate is and it is accepted, or the candidate is rejected and the replacement is :
The first term is . The second is . Their sum is , which is the target distribution. The draft model's distribution never appears in the final answer, which is why any draft model is allowed.
Expected tokens per target call
Let the draft propose tokens. The target model runs once on all of them in parallel, and the output is between 1 and tokens: the accepted draft prefix plus one token from the target. If the per-token acceptance probabilities are treated as independent with a common value , the number of tokens from one round is a capped geometric variable, and its expectation is
Let be the cost of one draft call divided by the cost of one target call. Each round costs target-call units. The expected speedup over ordinary decoding is therefore
The paper shows that a speedup above 1 is guaranteed for some whenever . The best is found by trying integers, since the formula is cheap to evaluate.
Worked examples
Vocabulary . Target . Draft .
- Acceptance probability: .
- Residual numerator: . Its sum is , as required.
- Residual distribution: . Any rejection is replaced by token .
- Accept probability for each candidate: gives . gives . gives .
- Output probabilities. For : the draft proposes it with probability and it is always accepted, plus the replacement mass from rejections. Total . For : . For : . These match exactly.
- Interpretation: the draft over-proposes , so it is sometimes rejected. The lost probability goes to , which the draft under-proposes. The sampled text is indistinguishable from the target's.
Take , , and a draft that costs of a target call.
- Expected tokens per round: .
- Cost per round in target units: .
- Speedup: .
- Check against ordinary decoding, which produces one token per target call, so its speedup is 1 by definition. The speculative version produces 3.36 tokens for 1.2 target calls, so it is about 2.8 times faster.
- Interpretation: four drafted tokens per round buy about 2.8 times the throughput at this acceptance rate. Raising helps more than raising once is around 4 or 5, because the term flattens out.
The paper reports measured speedups for T5-XXL (11 billion parameters) with a T5-small draft (77 million parameters) on English-to-German translation at argmax sampling, with and . Its measured speedup is 3.4.
- With the formula gives .
- The draft is not free. The paper uses a cost ratio of about for T5-small (a draft roughly 140 times smaller in parameters, 77 million against 11 billion). The formula then gives .
- The measured 3.4 is close to this prediction. The paper attributes the small gap to implementation details of the real system.
- The paper's Table 1 also tabulates the formula at . With and its entry is , which matches .
- Interpretation: the formula is a good predictor of measured speedup, and the cost term is what separates the ideal number from a real one.
flowchart LR
A[Prefix] --> B[Draft model proposes gamma tokens]
B --> C[Target model scores all positions in one parallel pass]
C --> D{Accept token i?}
D -- yes --> E[Keep token i, check next]
D -- no --> F[Sample replacement from residual, stop]
E --> G{All gamma accepted?}
G -- yes --> H[Sample one bonus token from target]
G -- no --> F
- gamma = 2
- gamma = 4
- gamma = 8
Tradeoffs and failure modes
Speculative decoding is exact, so the only question is speed. The speedup depends on three things: how often the draft agrees with the target, how cheap the draft is, and whether the hardware has spare capacity to run the parallel checks. Those conditions hold most easily for long, predictable outputs run on one request at a time. They fail under heavy batching, where the target is already compute-bound and the extra checks cost real time.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Large batch, target already compute-bound | Parallel checks add work with no spare capacity; speedup disappears | Plain decoding, or speculation turned down with a smaller |
| Draft disagrees with target often (low ) | Most drafted tokens are wasted; can be slower than baseline | A better draft, or fewer drafted tokens |
| Draft is nearly as costly as target ( large) | The draft's own time eats the gain | A smaller draft model, or a parameter-free draft |
| Output is repetitive or copies the prompt (summarisation, code edits) | Little problem; this is where the method shines | Copy-from-context drafts, which cost almost nothing |
| You need sampling with a very low temperature | Agreement is high, so the method works well | Speculation as is |
Two practical traps. First, the draft and target must share a tokenizer, or the two distributions are not over the same tokens and the rule has no meaning. Second, the guarantee is about distributions, not about a fixed random seed: with the same seed, the output may differ from plain decoding even though both are valid samples, because the random numbers are consumed in a different order. Users who need bit-identical text must check that the implementation does this.
Speculative decoding also does not help with time to first token, because the prompt must be processed before any drafting can start.
Alternatives include multi-token prediction heads trained into the target, and methods that verify a tree of candidates rather than a single chain.
History
The name comes from speculative execution in processors, where a CPU starts work on a branch before knowing it is needed, and discards it if the guess was wrong. Computer architecture texts describe the technique in the 1980s and 1990s, and the idea became standard in branch prediction.
Leviathan, Kalman and Matias, working at Google, posted the method in late 2022 under the title "Fast Inference from Transformers via Speculative Decoding". They showed that the speculative sampling rule keeps the target's distribution exact, and reported a 2X to 3X latency improvement for T5-XXL on a TPU-v4 at batch size 1, with no change to the model or its outputs. The paper was later published at a major machine learning conference in 2023.
Around the same time, a team at DeepMind described a closely related scheme for the Chinchilla model family. Speculative decoding caught on because it asks for nothing new from the model owner: no retraining, no architecture change, and no change to outputs. Inference libraries added it as an option soon after, often under the name "assisted generation".
Check yourself
QuestionRecall: why is the residual distribution p' built from max(0, p - q) and not from p alone?Show answer
If were just , the output would sometimes be drawn from the target's mass at tokens the draft already covered, double-counting them. Taking keeps only the mass the accepted draft did not supply. Together with the accepted mass , it adds up to exactly , so the overall output distribution is correct.
QuestionComputation: with and and cost , what is the expected number of tokens per target call? Compare with plain decoding.Show answer
Expected tokens per round is . With the cost per round is one target call, so the speedup is also . Plain decoding produces exactly one token per target call, so this is a times improvement.
QuestionWhy: the target model is run once on all drafted positions. Why does this not change the output distribution, even though the draft's guesses are used?Show answer
Each drafted position is checked against the target's own distribution at that exact prefix. The acceptance test and the replacement rule were designed so that the probability of every output token equals the target's probability, regardless of which draft proposed it. The draft only changes which tokens get tried first, and therefore how many are accepted per round.
QuestionApplied: a summarisation service runs a copy-from-context draft that costs almost nothing, with measured acceptance around 0.6 and gamma of 5. Is it worth enabling for a batch job that already saturates the GPU?Show answer
At batch size 1 the formula gives , a clear gain. But the batch job saturates the GPU, so the parallel checks compete with other requests for compute, and the gain shrinks or disappears. The right move is to measure throughput at the real batch size, because the formula assumes spare compute.