Quant Memo
Advanced

Speculative Decoding

Speculative decoding speeds up text generation by letting a small, fast model guess several tokens ahead, then having the large model verify all of those guesses in a single parallel pass instead of generating them one at a time.

Prerequisites: The KV Cache and Incremental Decoding, Decoding Strategies and Temperature

Generating text from a large language model is slow for a structural reason unrelated to raw hardware speed: each new token depends on every token before it, so tokens must be produced strictly one at a time — you cannot generate token 51 before knowing token 50. That sequential dependency means large-model generation is memory-bandwidth-bound, not compute-bound: the GPU spends most of its time waiting to move weights through memory for each single-token step, while its compute capacity sits mostly idle, since one token at a time barely uses the parallelism a GPU is built for. Speculative decoding attacks this mismatch directly: use a smaller, cheaper model to guess several tokens ahead quickly, then use the large model's spare compute capacity to check all of those guesses at once, in parallel, in roughly the time it would take to generate just one token normally.

The analogy: a junior associate drafting, a partner reviewing in bulk

Picture a law partner who reviews carefully but is slow to draft from scratch, and a junior associate who drafts quickly but less reliably. Instead of the partner writing every sentence themselves, the associate drafts the next five quickly, and the partner reads all five at once, approving them (reading five at once isn't much slower than reading one) or stopping at the first sentence they'd have written differently, discarding everything after. Whenever the associate's guesses hold up, the partner's expensive time verifies five sentences' worth of work in roughly the time it takes to write one themselves.

The mechanism

A small draft model generates kk candidate tokens autoregressively, one at a time (cheap, since it's small). The large target model then runs a single forward pass over the entire draft sequence at once — verifying kk tokens in parallel, given they're already fixed, is one pass through the network, not kk sequential passes — computing, for each position, the probability it itself would have assigned.

Each draft token is accepted with probability min ⁣(1,ptarget(x)pdraft(x))\min\!\left(1, \dfrac{p_{\text{target}}(x)}{p_{\text{draft}}(x)}\right), and generation stops accepting at the first rejection, resampling that position from a corrected distribution and discarding everything guessed after it.

In words: a draft token is kept if the big model would have been at least as likely to produce it, and even when less confident, it's kept with a probability matched exactly to preserve the target model's true output distribution — designed so the final output is statistically identical to what the large model would have generated alone, just faster.

Worked example 1: an accept-reject pass

Draft model proposes 4 tokens: The, cat, sat, quietly. The target model's single parallel pass returns its own probabilities for each position: it agrees strongly on The (accept) and cat (accept), but assigns sat only a 0.4 acceptance ratio — suppose this draw fails, so sat is rejected. At that point the target model resamples the position from its own corrected distribution, say producing slept, and everything guessed afterward (quietly) is discarded, since it was conditioned on a token that never got accepted. Net result: 2 tokens accepted, 1 generated fresh, in a single verification pass — 3 tokens for roughly the cost of one target-model forward pass, versus 3 sequential passes normally.

Worked example 2: expected speedup from acceptance rate

If the draft model's proposals are accepted with average probability α\alpha per token and the draft proposes k=5k=5 tokens per round, the expected number of tokens accepted per round is 1αk+11α\frac{1-\alpha^{k+1}}{1-\alpha} (a standard geometric-series result). At α=0.7\alpha=0.7, k=5k=5: expected accepted tokens 10.7610.7=10.11760.32.94\approx \frac{1-0.7^6}{1-0.7} = \frac{1-0.1176}{0.3} \approx 2.94 tokens per single target-model verification pass, versus 1 token per pass the normal way — roughly a 2.9× speedup in the number of target-model passes needed, before accounting for the (cheap) draft model's own cost.

Draft model: sequential, cheap, one at a time The cat sat quietly Target model: one parallel pass verifies all four accept accept reject discarded 3 tokens produced (2 accepted + 1 resampled) in one target pass
The draft model guesses several tokens cheaply and sequentially; the target model verifies all of them in a single parallel pass, keeping every token up to the first rejection.

Function explorer
-2260.1
x = 1.00f(x) = 2.718

Expected tokens accepted per round falls off as draft acceptance probability drops — a curve with roughly this shape — which is why speculative decoding pays off most when the small draft model closely mimics the large target model's behaviour.

Speculative decoding produces tokens statistically identical to ordinary autoregressive generation from the large model — the acceptance rule is mathematically constructed to guarantee this — while running the expensive model far less often, because verifying several draft tokens in one parallel pass costs about the same as generating a single token normally.

What this means in practice

Speculative decoding is now standard in production LLM serving stacks, typically paired with Multi-Query and Grouped-Query Attention and FlashAttention and IO-Aware Attention since all three attack different parts of the same generation-latency problem. The draft model is usually a much smaller version of the target model, or a distilled version trained specifically to mimic it — the better the draft matches the target's behaviour, the higher the acceptance rate, and the larger the realised speedup.

The speedup from speculative decoding is highly workload-dependent: on text where the large model's outputs are genuinely hard to predict (creative, high-entropy generation), the draft model's guesses are accepted less often, acceptance probability α\alpha drops, and the expected speedup shrinks toward 1× — there is no free lunch, and speculative decoding never makes output worse, but it can fail to make it meaningfully faster on adversarial or highly unpredictable text.

Practice

  1. With draft acceptance probability α=0.9\alpha=0.9 and k=4k=4 proposed tokens per round, compute the expected number of accepted tokens per target-model pass.
  2. Why must the target model's single parallel verification pass see the entire draft sequence at once, rather than verifying tokens one at a time, for the speedup to materialise?

Related concepts

Practice in interviews

Further reading

  • Leviathan, Kalman & Matias, Fast Inference from Transformers via Speculative Decoding (2023)
  • Chen et al., Accelerating Large Language Model Decoding with Speculative Sampling (2023)
ShareTwitterLinkedIn