Latency Optimization › Speculative Decoding

Michael BrenndoerferFebruary 6, 20268 min read
Language AI HandbookAll chapters
Part 65 of 67Production Systems
Chapter 492Latency Optimization › Speculative Decoding
Reading section 5 of 11 · Speculative Decoding · 8 minSection 5 of 11 · 8 min

Book contents

Section 5 of 11 · 8 min read

Latency Optimization › Speculative DecodingOne section · Sections in this chapter5 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The core latency bottleneck during decode is that each token requires a full forward pass through the model. Speculative decoding breaks this constraint by using a small, fast draft model to generate candidate tokens cheaply, then verifying multiple candidates with the large target model in parallel.

The intuition behind speculative decoding is straightforward. Most of the time, the next few tokens in a generation are highly predictable: function words like "the", "is", "of", high-frequency phrases, and continuations that any reasonable language model would select. A small model can predict these tokens quickly. If the large model would have chosen the same tokens, you get those tokens for free. If the small model was wrong, you fall back to the large model's choice and continue. In expectation, you generate multiple tokens per large-model forward pass rather than exactly one.

The Algorithm

Given a target model MqM_q and a draft model MpM_p:

  1. The draft model generates kk candidate tokens x~1,x~2,…,x~k\tilde{x}_1, \tilde{x}_2, \ldots, \tilde{x}_k autoregressively. This is fast because the draft model is much smaller.

  2. The target model performs a single forward pass over the kk draft tokens simultaneously (treating them like a prefill), computing the probability distribution q(⋅∣x<t+i)q(\cdot | x_{<t+i}) for each position.

  3. Each draft token x~i\tilde{x}_i is accepted with probability:

P(accept    x~i)=min⁡ ⁣(1,q(x~i ∣ x<t+i)p(x~i ∣ x<t+i))P(\text{accept} \;\; \tilde{x}_i) = \min\!\left(1, \frac{q(\tilde{x}_i \,|\, x_{<t+i})}{p(\tilde{x}_i \,|\, x_{<t+i})}\right)

where:

  • q(x~i ∣ x<t+i)q(\tilde{x}_i \,|\, x_{<t+i}): probability assigned to the draft token by the target model
  • p(x~i ∣ x<t+i)p(\tilde{x}_i \,|\, x_{<t+i}): probability assigned by the draft model
  • min⁡(1,⋅)\min(1, \cdot): a rejection sampling correction that guarantees the accepted tokens follow the exact target distribution
  1. If a token is rejected, the target model samples a corrected token at that position, and decoding restarts from there. The key guarantee is that the final output distribution is mathematically identical to sampling directly from the target model, token by token.

The acceptance probability formula has a clean probabilistic interpretation. If the target model assigns higher probability to a draft token than the draft model does (q>pq > p), the token is accepted with probability 1: the small model was being conservative about a token the large model is confident in, so there is no downside to accepting. If the target model assigns lower probability (q<pq < p), the token is accepted only with probability q/pq/p: the small model was overconfident, and we accept in proportion to how plausible the target model finds the token. The rejection step, when it occurs, samples from a residual distribution that corrects for the excess probability the draft model placed on incorrect tokens.

The expected number of accepted tokens per target model call, called the acceptance length, determines the speedup. If the acceptance length is kˉ\bar{k}, then speculative decoding reduces the number of target model calls needed for NoutN_{\text{out}} tokens by a factor of kˉ\bar{k}, at the cost of running the draft model for the same tokens. The net speedup depends on the ratio of draft model cost to target model cost and the acceptance length achieved.

Out[9]:
Visualization
Heatmap of speculative decoding speedup for different acceptance rates and draft model cost ratios.
Theoretical speedup of speculative decoding as a function of token acceptance rate and draft-to-target model cost ratio, for speculation depth k=4. Each cell shows how many tokens are effectively generated per unit of target model compute. High acceptance rates combined with a cheap draft model (lower left region) yield the largest speedups. The red dashed contour marks the breakeven line where speculative decoding provides no net improvement; above this line, the overhead of running the draft model exceeds its benefit.

The heatmap reveals the regime where speculative decoding helps most: high acceptance rates (the draft model frequently matches the target) combined with a cheap draft model (low draft-to-target cost ratio). In practice, acceptance rates of 60-80% are typical when the draft model is a smaller member of the same model family, and draft models are often 10-20x cheaper than the target, placing most real systems well into the beneficial zone.

Draft Model Selection

The draft model must be fast (to minimize the cost of generating kk candidates) and accurate (to maximize acceptance rate). The most common approaches are:

  • Smaller model from the same family: A 7B model drafting for a 70B model typically achieves higher acceptance rates than an unrelated model because they were trained on the same data and share similar inductive biases.
  • Distilled draft model: A model trained specifically to mimic the target model's distribution, which can achieve high acceptance rates at very small model sizes.
  • Self-speculative decoding: Drafts are generated using the first few layers of the target model and then verified by the full model. This requires no separate draft model but introduces complexity in the forward pass implementation.
  • Lookahead decoding: Generates draft tokens using n-gram patterns from the current context, requiring no model at all for the draft step. Acceptance rates are lower but draft cost approaches zero.

The choice of draft model strategy depends heavily on the deployment context. If you are already running a smaller model for other purposes, using it as a draft model adds nearly zero marginal cost. If the serving system is memory-constrained (a common situation when running very large models on limited GPU counts), self-speculative decoding avoids the memory overhead of loading a separate draft model while still providing latency improvements. Lookahead decoding is most effective for tasks like code generation where n-gram repetition is high, and least effective for open-ended creative generation where context repetition is rare.

The number of draft tokens kk is also a tunable parameter. More draft tokens mean larger potential speedups when acceptance rates are high, but the benefit saturates because the probability of an unbroken run of kk accepted tokens falls as αk\alpha^k for a geometric acceptance model. A typical value of k=4k = 4 to 88 captures most of the benefit without excessive overhead.

Correctness Guarantee

Speculative decoding is an exact algorithm: the output token distribution is identical to running the target model alone. This follows from the rejection sampling correction. When a draft token is accepted, it is a sample from qq (the target) because the acceptance criterion reweights the proposal pp to match qq. When a draft token is rejected, the target model samples directly from qq. In both cases, the accepted sequence has the same distribution as direct sampling from MqM_q.

This guarantee distinguishes speculative decoding from approximations like quantization or pruning. Speculative decoding never degrades output quality, only output speed. This is a remarkable property: you get a latency reduction for free, with no quality-speed trade-off. The only cost is compute efficiency at the system level: the draft model consumes resources, and rejected draft tokens represent wasted compute. This is why speculative decoding is most attractive when a single request occupies dedicated hardware, and less attractive when the system is already at full capacity.

Speculative Decoding in Practice

Beyond the textbook algorithm, production deployments of speculative decoding require handling several practical concerns. The draft model and target model must use compatible tokenization so that draft tokens correspond to positions in the target model's vocabulary. The draft model's KV cache must be managed separately from the target model's, adding memory overhead. And the serving framework must be able to execute the draft generation and target verification steps efficiently, ideally as consecutive GPU kernel launches without returning to the CPU between steps.

Modern serving frameworks like vLLM provide first-class support for speculative decoding, handling these details transparently. For practitioners, the main decision is selecting the draft model and the speculation depth kk, then measuring whether the acceptance rate on your specific workload is high enough to justify the overhead. If your users are asking questions with predictable, formulaic answers, speculative decoding is likely beneficial. If they are asking for highly creative or unpredictable generations, acceptance rates may be too low for speculative decoding to help.