Paper commentary
Fast Inference from Transformers via Speculative Decoding
- Problem Decoding from large autoregressive Transformers needs one serial run of the target model per token, which makes inference slow.
- Method Speculative decoding drafts several tokens with a cheaper approximation model, checks them in one parallel pass of the target model, and uses speculative sampling to keep the target's output distribution.
- Results Reached 3.4X walltime speedup on English to German translation with T5-XXL as target and T5-small as approximation model at temperature 0 and gamma 7.

Figure 1 . Our technique illustrated in the case of unconditional language modeling. Each line represents one iteration of the algorithm. The green tokens are the suggestions made by the approximation model (here, a GPT-like Transformer decoder with 6M parameters trained on lm1b with 8k tokens) that the target model (here, a GPT-like Transformer decoder with 97M parameters in the same setting) accepted, while the red and blue tokens are the rejected suggestions and their corrections, respectively. For example, in the first line the target model was run only once, and 5 tokens were generated.
Original paper · CC BY 4.0 · Yaniv Leviathan, Matan Kalman, Yossi Matias · Fast Inference from Transformers via Speculative Decoding · Original paper figure · Extracted the figure area from page 2 of the original PDF.Why serial decoding limits large Transformers
Decoding from a large autoregressive Transformer produces one token per pass of the model, and each pass must wait for the token before it. A K-token output therefore costs K serial runs of the target model, and each run is much slower for a large model than for a small one. The authors argue that inference from large models is often limited by memory bandwidth and communication rather than arithmetic, so spare compute is often available during decoding.
Existing speed-up methods for all inputs, such as distillation, sparsification, quantization and architecture changes, apply the same reduction to every step. Adaptive computation methods instead spend less compute on steps that are easier, but they usually change the architecture or the training procedure and do not keep the outputs identical.
This work keeps the model, its training and its outputs fixed and adds concurrency instead.
Speculative execution as the starting point
Speculative execution is a processor technique in which a task runs in parallel before the system has verified that it is needed, so the wait for the check is hidden when the guess is right. Branch prediction is the standard example. Speculative execution works only when there is an efficient way to propose tasks that are likely to be needed.
The authors extend this to the stochastic setting, where a task is needed with some probability. The aim becomes maximizing how often the speculative tasks are accepted, while the final output still follows the distribution of the target model alone. In decoding, the cheaper approximation model supplies the likely tasks and a sampling rule decides which of them to keep.
Drafting, verifying and correcting in one step
Speculative decoding uses two models for the same task: a target model \(M_p\) whose output distribution must be kept, and an approximation model \(M_q\) that is cheaper to run. Each iteration first lets \(M_q\) draft \(\gamma\) tokens, then runs \(M_p\) once over the prefix and every drafted prefix in parallel, and finally decides how many drafts to keep.
Every iteration yields at least one token from the target, so it never needs more serial target runs than ordinary decoding. When the drafts are good, one iteration can yield up to \(\gamma+1\) tokens. Before sampling, every decoding rule is folded into one adjusted distribution. Argmax, top-k, nucleus sampling and temperature all change the probabilities, so the sampling step only has to handle standard sampling from that distribution.
For one token, a draft \(x\) is drawn from \(q\) and kept when \(q(x)\le p(x)\). When \(q(x)>p(x)\), it is rejected with probability \(1-p(x)/q(x)\), and a new token is drawn from the adjusted distribution \[p'(x)=\operatorname{norm}(\max(0,p(x)-q(x)))\] The authors state that tokens sampled this way follow \(p\), and the derivation is given in their appendix.
Algorithm 1 draws \(r_i\) uniformly from (0, 1) and sets the accepted count to \[n=\min(\{i-1\mid 1\le i\le\gamma,\ r_i>p_i(x)/q_i(x)\}\cup\{\gamma\})\] If \(n<\gamma\), the distribution used for the final token is replaced by the normalized \(\max(0,p_{n+1}-q_{n+1})\). The returned sequence is the prefix, the first \(n\) drafts and one token drawn from the target distribution.
Figure 1 traces this procedure on unconditional lm1b generation, with a 6M GPT-like approximation model and a 97M GPT-like target. In that setting, a 38-token sentence needed only 9 serial runs of the 97M target.
Acceptance rate and expected tokens per run
The acceptance rate \(\beta\) for a prefix is the probability that speculative sampling accepts a token drawn from \(q\). Let \(\alpha=E(\beta)\). Assuming the \(\beta\) values are i.i.d., the tokens from one run form a capped geometric variable. Under that assumption the expected number of generated tokens is \[E(\#\text{generated tokens})=\frac{1-\alpha^{\gamma+1}}{1-\alpha}\]
The authors define the divergence \(D_{LK}\) as the sum over \(x\) of the absolute difference between \(p(x)\) and the average \(M(x)\). The average is \(M(x)=(p(x)+q(x))/2\), and the sum taken with \(q\) gives the same value. \[D_{LK}(p,q)=\sum_x |p(x)-M(x)|,\qquad M(x)=\frac{p(x)+q(x)}{2}\] Rewriting the absolute difference gives \[D_{LK}(p,q)=1-\sum_x \min(p(x),q(x))\]
The acceptance probability equals \(1-D_{LK}(p,q)\), which is the overlap \(\sum_x \min(p(x),q(x))\) of the two distributions. Taking the expectation over prefixes gives \(\alpha = 1-E(D_{LK}(p,q)) = E(\min(p,q))\), so \(\alpha\) is an overlap measure between the target and the approximation model.
Figure 2 plots the expected number of generated tokens against \(\alpha\) for several values of \(\gamma\).
From token gain to walltime gain
The cost coefficient \(c\) is the time for one run of \(M_q\) divided by the time for one run of \(M_p\). It depends on hardware and software, and in the authors' experiments, where \(M_q\) was a couple of orders of magnitude smaller, \(c\) stayed below 0.05. The walltime analysis assumes that \(\gamma+1\) concurrent target evaluations take no more walltime than one.
Each iteration costs \(\gamma\) draft runs plus one target run, and it yields the expected number of tokens from the previous section. The expected walltime improvement factor is \[\frac{1-\alpha^{\gamma+1}}{(1-\alpha)(\gamma c+1)}\] This holds for long enough generations, because the target runs at least once per iteration.
If \(\alpha>c\), some \(\gamma\) gives an improvement, and the factor is at least \((1+\alpha)/(1+c)\). The authors get this by evaluating the factor at \(\gamma=1\).
When \(c\) is near zero, the expected improvement is bounded above by \(1/(1-\alpha)\), and it approaches that bound as \(\gamma\) grows. With a bigram approximation model on English to German translation, \(\alpha\) was about 0.2 and the speed improvement was 1.25X at \(\gamma=3\) with \(c=0\).
Extra arithmetic and lower memory traffic
Each iteration runs \(\gamma+1\) target evaluations in parallel, and rejected drafts waste the work done on them. The authors define \(\hat{c}\) as the ratio of arithmetic operations per token of \(M_q\) to those of \(M_p\). The expected factor by which total operations increase is \[\frac{(1-\alpha)(\gamma\hat{c}+\gamma+1)}{1-\alpha^{\gamma+1}}\] so the increase is large when \(\alpha\) is low and small when it is high.
For Transformer decoders, the target's operations, not counting runs of \(M_q\), are bounded from above by one run of the same-size Transformer encoder.
Memory traffic can fall. The target's weights and KV cache are read once per execution of Algorithm 1 instead of once per token, so the memory accesses for them shrink by the same factor as the serial target calls.
Choosing gamma and the approximation model
For given \(c\) and \(\alpha\), the optimal \(\gamma\) maximizes the walltime formula. Because \(\gamma\) is an integer, the authors find it numerically.
With an oracle that picks \(\gamma\) per step, the expected token count would be \(1/(1-\alpha)\) at most. For typical \(c\) and \(\alpha\) with unbounded compute, the improvement could be up to about 60% higher than with a fixed \(\gamma\). That bound assumes the target still verifies the oracle's predictions. The authors leave this direction to future work.
Approximation models in the paper were mostly existing smaller Transformers with the same architecture and standardization as the target, and a model about two orders of magnitude smaller usually performed best, balancing \(\alpha\) and \(c\). Any choice of approximation model keeps the output distribution identical.
Negligible-cost models have \(c\approx 0\). N-gram models are one example, since evaluating them is a table lookup, and the bigram results are reported in the walltime section.
Other options are heuristics that copy tokens from the context, which may suit summarization or chat-like repetition, and non-autoregressive models, which replace the autoregressive loop with a single call.
Walltime on T5-XXL against T5X
Approximation models are T5-large (800M), T5-base (250M) and T5-small (77M), with batch size 1 on a single TPU-v4 for argmax sampling (temp=0) and standard sampling (temp=1). Figure 5 contrasts a trace with gamma 7, a trace with gamma 3 and standard decoding, with the target and approximation calls of each stack shown separately.
On English to German translation, T5-small with gamma 7 reached 3.4X at temp 0 with alpha 0.75, and 2.6X at temp 1 with alpha 0.62. T5-base reached 2.8X at temp 0 and 2.4X at temp 1, and T5-large reached 1.7X and 1.4X.
On CNNDM summarization, T5-small reached 3.1X at temp 0 with gamma 5 and alpha 0.65, and 2.3X at temp 1 with gamma 5 and alpha 0.53. T5-base reached 3.0X and 2.2X, and T5-large reached 2.2X and 1.7X.
Table 2 lists the measured speedup and alpha for each approximation model, task and sampling setting.
Among the tested approximation models, T5-small gave the highest speedup, and alpha rises with approximation size. The authors say the empirical results match the theoretical predictions, with some variance from implementation details.
The overall claim is a 2X-3X speedup against T5X with identical outputs. The T5-large rows fall below 2X, so the range holds for the smaller approximation models in these settings.
Acceptance rates across models and tasks
Table 3 lists alpha for each target, approximation model and sampling setting. Each value is the expectation from Corollary 3.6, computed on 10K tokens generated by the target model.
For the GPT-like 97M target on lm1b, the 6M GPT-like approximation model reached alpha 0.88 at argmax and 0.89 at standard sampling. The unigram model gave 0.03 and the bigram model 0.05 at argmax, and the same values at standard sampling.
For T5-XXL on English to German translation, T5-small reached 0.75 at argmax and 0.62 at standard sampling, T5-base 0.80 and 0.68, and T5-large 0.82 and 0.71. On CNNDM, T5-small reached 0.65 at argmax and 0.53 at standard sampling, T5-base 0.73 and 0.55, and T5-large 0.74 and 0.56.
With LaMDA 137B as target, the 100M, 2B and 8B approximation models reached 0.61, 0.71 and 0.75 at argmax, and 0.57, 0.71 and 0.74 at standard sampling. These are acceptance measurements only, because the authors implemented the method for T5 and the walltime experiments cover T5-XXL. The paper also notes that LaMDA outputs go through a Top 40 filter, which has no effect on argmax.
Comparison with earlier speculative methods
Blockwise Parallel Decoding also decodes several tokens in parallel. It supports only greedy decoding, requires training a custom model, and aims at task quality rather than identical outputs. Shallow Aggressive Decoding copies tokens from the input, so it suits tasks with very similar inputs and outputs, such as grammatical error correction.
It does not cover stochastic sampling.
Wisdom of Committees uses off-the-shelf smaller models, but it is an adaptive computation method with a heuristic stopping rule, so it loses the guarantee of identical outputs. After the initial publication, an independent implementation of speculative decoding reported 2X-2.5X improvements on Chinchilla 70B.
Limitations
Latency falls because concurrency is added, and the total number of arithmetic operations rises. The method is therefore not useful when additional compute is unavailable.
The walltime formula assumes that the acceptance probabilities are i.i.d., and the authors describe this as a simplification. Measured speedups differ from the predictions by some margin.
Speculative decoding was tested only in the text modality. Walltime was measured only for T5-XXL, with batch size 1 on a single TPU-v4.
Most tested approximation models were existing off-the-shelf Transformers, so custom architectures, non-autoregressive models and custom training were not tested. Adaptive gamma, compatibility with beam search and hierarchical use of the approximation model are left for future work.
Paper info
- Authors
- Yaniv Leviathan, Matan Kalman, Yossi Matias
- Venue
- arXiv · Preprint
- First published
- 2022-11-30
- Added
- 2026-10-20
- Updated
- 2023-5-18
- Version
- v2
- arXiv
- 2211.17192
- Category
- cs.LG · cs.CL
- Original paper terms
- CC BY 4.0
- What we show
- Paper info, commentary and permitted figures from the original paper
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Abstract
Inference from large autoregressive models like Transformers is slow - decoding K tokens takes K serial runs of the model. In this work we introduce speculative decoding - an algorithm to sample from autoregressive models faster without any changes to the outputs, by computing several tokens in parallel. At the heart of our approach lie the observations that (1) hard language-modeling tasks often include easier subtasks that can be approximated well by more efficient models, and (2) using speculative execution and a novel sampling method, we can make exact decoding from the large models faster, by running them in parallel on the outputs of the approximation models, potentially generating several tokens concurrently, and without changing the distribution. Our method can accelerate existing off-the-shelf models without retraining or architecture changes. We demonstrate it on T5-XXL and show a 2X-3X acceleration compared to the standard T5X implementation, with identical outputs.
Quote from the paper
Yaniv Leviathan, Matan Kalman, Yossi Matias (2022). Fast Inference from Transformers via Speculative Decoding. arXiv:2211.17192v2. https://arxiv.org/abs/2211.17192v2
BibTeX
@misc{leviathan2022fast,
title = {{Fast Inference from Transformers via Speculative Decoding}},
author = {Yaniv Leviathan and Matan Kalman and Yossi Matias},
year = {2022},
eprint = {2211.17192},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2211.17192v2}
}