Search

Search papers and lessons

Speculative Decoding

Level 4 · 12 min read

Speculative decoding lets a small, fast model draft several tokens that a large model checks in one parallel pass, and a rejection rule keeps the output distribution exactly the same as the large model alone.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Losses & regularization
  • Efficiency
  • Neural network basics
  • Sign in to see your progress

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 MqM_q. The expert is the large target model MpM_p. 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 tt, the model needs token t−1t-1, so decoding KK tokens takes KK 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 p(xt∣x<t)p(x_t \mid x_{\lt t}) be the next-token distribution of the target model for a prefix x<tx_{\lt t}, and let q(xt∣x<t)q(x_t \mid x_{\lt t}) be the same for the draft model. Both are distributions over the vocabulary, after any sampling adjustments such as temperature or top-kk truncation. Write p(x)p(x) and q(x)q(x) when the prefix is clear.

The goal is to produce a token xx whose distribution is exactly pp, while calling the draft model as often as needed and the target model as rarely as possible.

The acceptance rule

Definition: Speculative sampling step

Draw a candidate x∼qx \sim q. Draw r∼Uniform(0,1)r \sim \mathrm{Uniform}(0,1). If r≤p(x)/q(x)r \le p(x)/q(x), accept xx. Otherwise reject xx and draw a replacement from the residual distribution

p′(x)=max⁡(0,  p(x)−q(x))∑ymax⁡(0,  p(y)−q(y)).p'(x) = \frac{\max\bigl(0,\; p(x) - q(x)\bigr)}{\sum_y \max\bigl(0,\; p(y) - q(y)\bigr)}.

The rule is rejection sampling in disguise. The draft is the proposal, and the ratio p(x)/q(x)p(x)/q(x) 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 pp that the draft under-covered. That replacement is what makes the scheme exact.

Why the output has distribution p

Let β=∑xmin⁡(p(x),q(x))\beta = \sum_x \min(p(x), q(x)). This is the probability that a single candidate is accepted. The residual normaliser is 1−β1-\beta, because

∑xmax⁡(0, p(x)−q(x))=∑x(p(x)−min⁡(p(x),q(x)))=1−β.\sum_x \max\bigl(0,\, p(x)-q(x)\bigr) = \sum_x \bigl(p(x) - \min(p(x), q(x))\bigr) = 1 - \beta.

Now compute the probability that the output equals a given token xx. There are two ways to get it. The candidate is xx and it is accepted, or the candidate is rejected and the replacement is xx:

Pr⁡(output=x)=q(x)min⁡ ⁣(1,p(x)q(x))+(1−β)⋅max⁡(0, p(x)−q(x))1−β.\Pr(\text{output} = x) = q(x)\min\!\Bigl(1, \frac{p(x)}{q(x)}\Bigr) + (1-\beta)\cdot\frac{\max(0,\, p(x)-q(x))}{1-\beta}.

The first term is min⁡(p(x),q(x))\min(p(x), q(x)). The second is max⁡(0,p(x)−q(x))\max(0, p(x)-q(x)). Their sum is p(x)p(x), 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 γ\gamma tokens. The target model runs once on all of them in parallel, and the output is between 1 and γ+1\gamma+1 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 α\alpha, the number of tokens from one round is a capped geometric variable, and its expectation is

E[tokens per round]=1−αγ+11−α.\mathbb{E}[\text{tokens per round}] = \frac{1-\alpha^{\gamma+1}}{1-\alpha}.

Let cc be the cost of one draft call divided by the cost of one target call. Each round costs γc+1\gamma c + 1 target-call units. The expected speedup over ordinary decoding is therefore

S=1−αγ+1(1−α)(γc+1).S = \frac{1-\alpha^{\gamma+1}}{(1-\alpha)(\gamma c + 1)}.

The paper shows that a speedup above 1 is guaranteed for some γ\gamma whenever α>c\alpha > c. The best γ\gamma is found by trying integers, since the formula is cheap to evaluate.

Worked examples

Example
A three-token vocabulary, by hand

Vocabulary {a,b,c}\{a, b, c\}. Target p=(0.5, 0.3, 0.2)p = (0.5,\ 0.3,\ 0.2). Draft q=(0.2, 0.5, 0.3)q = (0.2,\ 0.5,\ 0.3).

  1. Acceptance probability: β=min⁡(0.5,0.2)+min⁡(0.3,0.5)+min⁡(0.2,0.3)=0.2+0.3+0.2=0.7\beta = \min(0.5, 0.2) + \min(0.3, 0.5) + \min(0.2, 0.3) = 0.2 + 0.3 + 0.2 = 0.7.
  2. Residual numerator: max⁡(0,p−q)=(0.3, 0, 0)\max(0, p-q) = (0.3,\ 0,\ 0). Its sum is 0.3=1−β0.3 = 1 - \beta, as required.
  3. Residual distribution: p′=(1,0,0)p' = (1, 0, 0). Any rejection is replaced by token aa.
  4. Accept probability for each candidate: aa gives min⁡(1,0.5/0.2)=1\min(1, 0.5/0.2) = 1. bb gives min⁡(1,0.3/0.5)=0.6\min(1, 0.3/0.5) = 0.6. cc gives min⁡(1,0.2/0.3)≈0.667\min(1, 0.2/0.3) \approx 0.667.
  5. Output probabilities. For aa: the draft proposes it with probability 0.20.2 and it is always accepted, plus the replacement mass 0.30.3 from rejections. Total 0.50.5. For bb: 0.5×0.6=0.30.5 \times 0.6 = 0.3. For cc: 0.3×0.667=0.20.3 \times 0.667 = 0.2. These match p=(0.5,0.3,0.2)p = (0.5, 0.3, 0.2) exactly.
  6. Interpretation: the draft over-proposes bb, so it is sometimes rejected. The lost probability goes to aa, which the draft under-proposes. The sampled text is indistinguishable from the target's.
Example
Expected speedup from the formula

Take α=0.8\alpha = 0.8, γ=4\gamma = 4, and a draft that costs c=0.05c = 0.05 of a target call.

  1. Expected tokens per round: 1−0.851−0.8=1−0.327680.2=3.36\dfrac{1 - 0.8^5}{1 - 0.8} = \dfrac{1 - 0.32768}{0.2} = 3.36.
  2. Cost per round in target units: γc+1=4(0.05)+1=1.2\gamma c + 1 = 4(0.05) + 1 = 1.2.
  3. Speedup: S=3.361.2=2.80S = \dfrac{3.36}{1.2} = 2.80.
  4. 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.
  5. Interpretation: four drafted tokens per round buy about 2.8 times the throughput at this acceptance rate. Raising α\alpha helps more than raising γ\gamma once γ\gamma is around 4 or 5, because the αγ+1\alpha^{\gamma+1} term flattens out.
Example
Checking the formula against the paper

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 γ=7\gamma = 7 and α=0.75\alpha = 0.75. Its measured speedup is 3.4.

  1. With c=0c = 0 the formula gives 1−0.7580.25=1−0.10010.25=3.60\dfrac{1 - 0.75^8}{0.25} = \dfrac{1 - 0.1001}{0.25} = 3.60.
  2. The draft is not free. The paper uses a cost ratio of about c=0.015c = 0.015 for T5-small (a draft roughly 140 times smaller in parameters, 77 million against 11 billion). The formula then gives 3.60/(7⋅0.015+1)=3.60/1.105≈3.263.60 / (7 \cdot 0.015 + 1) = 3.60 / 1.105 \approx 3.26.
  3. The measured 3.4 is close to this prediction. The paper attributes the small gap to implementation details of the real system.
  4. The paper's Table 1 also tabulates the formula at c=0c = 0. With α=0.8\alpha = 0.8 and γ=5\gamma = 5 its entry is 3.69×3.69\times, which matches (1−0.86)/0.2=3.69(1 - 0.8^6)/0.2 = 3.69.
  5. Interpretation: the formula is a good predictor of measured speedup, and the cost term cc 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
00.20.40.60.812345alpha (acceptance rate)expected tokens per round
  • gamma = 2
  • gamma = 4
  • gamma = 8
Tokens per target call rise with the acceptance rate alpha, with diminishing returns in gamma. The marked point is alpha 0.8 with gamma 4.

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.

SituationWhat goes wrongBetter choice
Large batch, target already compute-boundParallel checks add work with no spare capacity; speedup disappearsPlain decoding, or speculation turned down with a smaller γ\gamma
Draft disagrees with target often (low α\alpha)Most drafted tokens are wasted; can be slower than baselineA better draft, or fewer drafted tokens
Draft is nearly as costly as target (cc large)The draft's own time eats the gainA 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 shinesCopy-from-context drafts, which cost almost nothing
You need sampling with a very low temperatureAgreement is high, so the method works wellSpeculation 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 p′p' were just pp, the output would sometimes be drawn from the target's mass at tokens the draft already covered, double-counting them. Taking max⁡(0,p−q)\max(0, p-q) keeps only the mass the accepted draft did not supply. Together with the accepted mass min⁡(p,q)\min(p,q), it adds up to exactly pp, so the overall output distribution is correct.

QuestionComputation: with α=0.9\alpha = 0.9 and γ=2\gamma = 2 and cost c=0c = 0, what is the expected number of tokens per target call? Compare with plain decoding.Show answer

Expected tokens per round is 1−0.931−0.9=1−0.7290.1=2.71\dfrac{1 - 0.9^3}{1 - 0.9} = \dfrac{1 - 0.729}{0.1} = 2.71. With c=0c = 0 the cost per round is one target call, so the speedup is also 2.712.71. Plain decoding produces exactly one token per target call, so this is a 2.712.71 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 1−0.660.4=1−0.04670.4≈2.38\dfrac{1 - 0.6^6}{0.4} = \dfrac{1 - 0.0467}{0.4} \approx 2.38, 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.

Sources