Fast Inference from Transformers via Speculative Decoding

Yaniv Leviathan, Matan Kalman, Yossi Matias

TL;DR

Speculative decoding is a clever algorithm that accelerates Large Language Model (LLM) inference by 2X–3X without changing the output distribution or requiring model retraining. It works by using a small "draft" model to guess multiple future tokens, which the large "target" model then verifies in a single parallel pass.

What Problem Does This Paper Address?

Large autoregressive models, like T5-XXL or LaMDA, are notoriously slow during inference. Because they are autoregressive, they generate text one token at a time: to produce $K$ tokens, you must run the model $K$ times in sequence.

The authors identify two key opportunities for optimization:

  1. The "Easy" Token Observation: Not every word in a sentence requires the full reasoning power of an 11-billion-parameter model. Simple connectors or common phrases can often be predicted by much smaller, faster models.
  2. The Memory Bandwidth Bottleneck: LLM inference is frequently bottlenecked by memory bandwidth (loading weights) rather than arithmetic compute. This means there is often "spare" compute capacity available on modern accelerators (like TPUs or GPUs) that goes unused during standard serial decoding.

Methods

The core of the paper is Speculative Decoding, which consists of two main components:

  1. Speculative Execution: A small approximation model ($M_q$) generates a sequence of $\gamma$ candidate tokens (guesses). The large target model ($M_p$) then processes all these guesses in parallel in a single step.
  2. Speculative Sampling: This is the "secret sauce" that ensures the final output is mathematically identical to what the large model would have produced alone. If the large model agrees with a guess, it’s accepted. If it disagrees, the algorithm rejects the incorrect guess, samples a "correction" token from a modified distribution, and discards the subsequent guesses.

Crucially, even in the worst-case scenario where every guess is wrong, the target model still produces one valid token per run, meaning the method is never slower than standard serial decoding (ignoring the negligible cost of the small model).

Figure 5. A simplified trace diagram for a full encoder-decoder Transformer stack. The top row shows speculative decoding with γ = 7 so each of the calls to Mp (the purple blocks) is preceded by 7 calls to Mq (the blue blocks). The yellow block on the left is the call to the encoder for Mp and the orange block is the call to the encoder for Mq. Likewise the middle row shows speculative decoding with γ = 3, and the bottom row shows standard decoding.

Datasets

The authors evaluated their method across several tasks and model families:

  • English to German Translation: Using T5-XXL (11B) on the WMT EnDe dataset.
  • Summarization: Using T5-XXL on the CNN/Daily Mail (CNNDM) dataset.
  • Unconditional Language Generation: Using a 97M parameter GPT-like model trained on the One Billion Word Benchmark (lm1b).
  • Dialog: Using the LaMDA (137B) model.

Key Insights and Ideas

  • The Power of Parallelism: By turning serial decoding into a "guess-and-verify" parallel task, the authors exploit the unused compute cycles of memory-bound hardware.
  • Guaranteed Fidelity: Unlike other acceleration methods like distillation or quantization, speculative decoding does not change the model's outputs. It is a "lossless" optimization.
  • Draft Model Versatility: The "draft" model doesn't even have to be a neural network. The authors showed that even a simple n-gram model could provide a 1.25X speedup because language is often predictable enough for basic statistics to get some tokens right.
  • Optimal Guessing ($\gamma$): There is a mathematical "sweet spot" for how many tokens to guess. If you guess too few, you leave speed on the table; if you guess too many, you waste compute on guesses that are likely to be rejected.
Figure 2. The expected number of tokens generated by Algorithm 1 as a function of α for various values of γ.

Key Results

The empirical gains are significant and "out-of-the-box," meaning they require no fine-tuning of the target model:

  • T5-XXL Speedups: On a TPU-v4, the authors achieved a 3.4X speedup for translation and a 3.1X speedup for summarization (at temperature = 0) using T5-small as the draft model.
  • LaMDA Scaling: For the 137B parameter LaMDA model, draft models as small as 100M parameters were able to correctly guess tokens with high frequency ($\alpha \approx 0.57$), leading to substantial latency reductions.
  • Efficiency: Even when accounting for the extra arithmetic operations, the reduction in memory accesses (reading the large model's weights fewer times) leads to a net win in wall-clock time.
Table 2. Empirical results for speeding up inference from a T5- XXL 11B model.

Limitations and Future Work

  • Compute Availability: This method is only effective when the system is memory-bandwidth bound. In a compute-bound scenario (e.g., very large batch sizes), the overhead of running extra parallel operations might actually slow things down.
  • Draft Model Selection: The speedup is highly sensitive to the quality of the draft model. Finding the right balance between a draft model's speed ($c$) and its accuracy ($\alpha$) is critical.
  • Future Directions: The authors suggest exploring hierarchical speculative decoding (where the draft model is itself accelerated by an even smaller model) and adapting the method for beam search, which is more complex than the standard sampling discussed here.

Summary updated

BibTeX
@inproceedings{leviathan2023fast,
  author = {Yaniv Leviathan and Matan Kalman and Yossi Matias},
  title = {{Fast Inference from Transformers via Speculative Decoding}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=C9NEblP8vS},
}
Figure