Inference: Decoding Strategies

By Ludovico Bessi, Senior ML Engineer at Google · Updated

Breaking the one-token-per-forward-pass limit: speculative, staged, lookahead and non-autoregressive decoding.

The Problem

Autoregressive decoding generates one token per forward pass of the model, and each pass is memory-bound: it reads every weight to do very little math. At small batch sizes the GPU's compute sits mostly idle.

Here is the opportunity: a forward pass over several tokens costs almost the same time as a pass over one, because the cost is reading the weights, not computing with them. If we could get several useful tokens out of one pass of the big model, we would decode several times faster.

All the techniques in this chapter exploit that idea. They differ in where the candidate tokens come from and whether output quality is exactly preserved.

Speculative Decoding

Speculative decoding (Leviathan et al., 2023; Chen et al., 2023) uses a small, fast draft model to guess the next few tokens, then has the large target model check all the guesses in a single forward pass.

The algorithm

  1. The draft model generates k tokens autoregressively (cheap, because it is small).
  2. The target model runs one forward pass over the prompt plus those k tokens, producing its own probability distribution at each of the k + 1 positions.
  3. Draft tokens are checked left to right. Each is accepted with probability min(1, p_target / p_draft).
  4. At the first rejection, a replacement token is sampled from the adjusted distribution max(0, p_target − p_draft) (normalized), and the rest are discarded. If all k are accepted, one extra token is sampled from the target's last distribution.

Each target pass therefore yields between 1 and k + 1 tokens.

Why quality is unchanged

The acceptance rule is a form of rejection sampling. The tokens produced follow exactly the target model's output distribution, for any sampling temperature. With greedy decoding, the output is identical to running the target model alone. Speculative decoding is a pure speed optimization.

What determines the speedup

  • Acceptance rate: how often the draft agrees with the target. Code, structured output and predictable text accept more; creative writing accepts less.
  • Draft cost: the draft must be much faster than the target — typically 10–50× smaller, from the same model family so tokenizers match.
  • Number of speculated tokens k: more tokens mean more upside if accepted but more wasted work if rejected.

Typical speedups are 2–3× at small batch sizes. At large batch sizes the target model is already closer to compute-bound, so the gains shrink and can disappear.

Variants without a separate draft model

  • Medusa adds several extra decoding heads to the target model, each predicting a token further ahead, and verifies candidate continuations organized as a tree.
  • EAGLE drafts at the feature level, using a lightweight layer that predicts the target model's next hidden states, and achieves high acceptance rates.
  • Self-speculative decoding (for example LayerSkip) uses the target model's own early layers as the draft.

For a production-focused summary, see Speculative Decoding.

Staged Speculative Decoding

Staged speculative decoding (Spector and Ré, 2023) improves on the basic algorithm in two ways, aimed at small-batch, on-device inference:

  1. Tree-structured speculation. Instead of a single chain of k draft tokens, the draft proposes a tree of likely continuations (several candidates at uncertain positions). The target verifies the whole tree in one batched pass with an attention mask that keeps branches separate. More candidates per pass means more expected accepted tokens.
  2. A second stage of speculation. The draft model itself is sped up by speculative decoding with an even smaller drafter (such as an n-gram model). Since the draft model spends most of its time decoding, accelerating it raises overall throughput.

The authors reported a 3.16× reduction in single-batch decoding latency on a 762M-parameter GPT-2-L model, with output quality still exactly preserved.

Lookahead Decoding

Lookahead decoding (Fu et al., 2024) needs no draft model at all. It builds on Jacobi iteration, which treats decoding as solving a system of equations: guess several future tokens at once, run the model on all of them in parallel, update every guess with the model's predictions, and repeat. Guesses that stop changing are correct.

Plain Jacobi decoding rarely converges faster than normal decoding. Lookahead decoding makes it practical by running two branches in every forward pass:

  • A lookahead branch performs Jacobi updates over a window of future positions and collects the n-grams that appear along the way into an n-gram pool.
  • A verification branch takes n-grams from the pool that start with the current last token and checks them, exactly as speculative decoding would.

Accepted n-grams advance generation by several tokens per step. The output matches standard greedy decoding exactly. The authors reported speedups of up to 1.8× on MT-bench and up to 4× on code completion when scaling across multiple GPUs. Gains are highest on repetitive or structured text such as code, and the method trades extra FLOPs per step for fewer steps — which pays off when decode is memory-bound.

Non-Autoregressive Decoding

Non-autoregressive (NAR) models generate many or all output tokens in parallel instead of left to right.

  • Fully parallel NAR (originally for machine translation) predicts the output length, then all tokens at once. It is very fast but suffers from the multimodality problem: when several outputs are plausible, independently predicted tokens can mix them ("thank you thank you").
  • Iterative refinement (for example Mask-Predict) generates all tokens, then repeatedly re-masks and re-predicts the least confident ones for a fixed number of steps.
  • Diffusion language models generalize this: start from fully masked or noisy text and denoise over a number of steps, each step updating many tokens in parallel. They allow trading the number of steps for quality.

NAR methods change the model and the output distribution, so unlike speculative and lookahead decoding they are not drop-in, quality-preserving accelerations. They are an active research area, especially for latency-critical applications.

Choosing a Strategy

Method Extra model? Output identical to target? Best when
Speculative decoding Small draft model Yes (distribution-exact) Small batches, matching draft available
Medusa / EAGLE Extra heads / small layer, trained Yes with exact verification You can fine-tune the serving model
Staged speculative Draft + tiny drafter Yes Single-user, on-device latency
Lookahead decoding None Yes (greedy) No draft available, spare compute
Non-autoregressive Different model No Research, extreme latency needs

At high load, all speculative methods lose ground: the GPU is already busy, so extra verification work competes with other requests. Serving systems increasingly adapt the speculation length to load, speculating aggressively when traffic is low and turning it off at peak. See Batching Techniques.

Key Takeaways

  1. A forward pass over several tokens costs about the same as one, because decode is memory-bound.
  2. Speculative decoding drafts tokens cheaply and verifies them in one target pass, with exactly the target's output distribution.
  3. Staged speculative decoding adds tree-structured drafts and speculates on the draft model itself.
  4. Lookahead decoding uses Jacobi iteration and an n-gram pool to speed up decoding with no draft model.
  5. Non-autoregressive and diffusion models generate in parallel but change the output distribution.
  6. Speculation helps most at low batch sizes; adapt it to load.