Fast Inference from Transformers via Speculative Decoding

Yaniv Leviathan, Matan Kalman, Yossi Matias

TL;DR

Speculative decoding is an algorithm that accelerates Large Language Model (LLM) inference by 2x–3x without altering the final output. It leverages a small, fast "draft" model to predict several tokens ahead, which are then verified by the large "target" model in a single parallel step.

The Bottleneck in Autoregressive Decoding

The standard method for generating text from a Transformer is autoregressive decoding. Because each token depends on the preceding ones, generating 100 tokens requires running the model 100 consecutive times. For massive models like GPT-4 or PaLM, this serial process is incredibly slow.

Interestingly, the primary bottleneck is rarely raw compute (FLOPs); it is memory bandwidth. Moving the massive weights of a 100B+ parameter model from memory to the processor takes significantly longer than the actual mathematical operations. Consequently, during inference, the GPU or TPU often sits idle waiting for data. Speculative decoding capitalizes on this "wasted" compute power to evaluate multiple tokens simultaneously.

The Speculative Decoding Method

The authors introduce a framework relying on two distinct models:

  1. The Target Model ($M_p$): The large, slow model intended for the final output (e.g., T5-XXL).
  2. The Approximation Model ($M_q$): A much smaller, faster "draft" model (e.g., T5-small).

The Algorithm

Instead of the large model generating one token at a time, speculative decoding follows a draft-and-verify loop:

  • Drafting: The small model $M_q$ quickly predicts a sequence of $\gamma$ tokens (the "speculative prefix").
  • Verification: The large model $M_p$ runs a single forward pass on this entire prefix. Because Transformers can process sequences in parallel, it calculates the probability of every drafted token simultaneously.
  • Speculative Sampling: A novel sampling method determines how many of the small model's guesses to accept. If a guess is rejected, the large model provides a corrected token, and the process restarts from that point.
A trace diagram showing how speculative decoding uses multiple small model calls to look ahead before a single large model verification.

Crucially, the authors prove that their Speculative Sampling mathematics ensure the final output follows the exact same probability distribution as the large model alone. The acceleration is mathematically "lossless."

Key Insights

  • Language is often predictable: Many segments of a sentence are highly predictable (e.g., finishing the word "Transformer" after "Transf..."). A tiny model can predict these just as accurately as a massive one, allowing speculative decoding to capture these "easy wins."
  • Compute is "free" when memory is the bottleneck: Since the time cost of loading the large model's weights into memory is already paid, those weights might as well be used to verify multiple tokens rather than just generating one.
  • The Acceptance Rate ($\alpha$): The overall speedup depends heavily on $\alpha$—the probability that the large model agrees with the small model's draft. Higher agreement directly translates to more tokens generated per iteration.

Empirical Results

The researchers evaluated the method across diverse tasks, including English-to-German translation (WMT EnDe), news summarization (CNN/DailyMail), and unconditional language modeling. They demonstrated significant "out-of-the-box" speedups using standard hardware (TPU-v4) and existing model checkpoints.

  • T5-XXL (11B parameters): Achieved a 2.3x to 3.4x speedup when using T5-small (77M parameters) as the drafter.
  • LaMDA (137B parameters): Observed high acceptance rates ($\alpha$ up to 0.75), suggesting massive potential for wall-time improvements in production dialog systems.
  • Zero Quality Loss: Unlike distillation or quantization, the outputs remain bit-for-bit identical to the original model's standard sampling.
Empirical results for T5-XXL showing speedups across different tasks and sampling temperatures.

Limitations and Future Directions

  • Compute Availability: This method relies on having "spare" compute capacity. If a system is already 100% compute-bound (e.g., running very large batch sizes), the overhead of the small model and parallel verification might degrade performance.
  • Tuning the Guess Length ($\gamma$): Choosing the optimal number of tokens to draft is a balancing act. Guessing too many risks wasted effort on incorrect branches, while guessing too few leaves potential speedups on the table.
  • Future Work: The authors suggest exploring hierarchical speculative decoding (cascading drafts from tiny to medium to giant models) and extending the approach to other autoregressive modalities, such as image or audio generation.

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