TL;DR
Decoding a Transformer is slow for a simple reason: producing tokens takes serial calls to a large model, and each call is limited by reading the weights from memory, not by arithmetic. Leviathan, Kalman and Matias noticed that many tokens are easy to guess. A small model drafts several tokens, the large model scores all of them in one parallel pass, and a sampling rule accepts the drafts that are consistent with the large model's distribution. The output has exactly the same distribution as the large model alone. On T5-XXL, the method gave 2 to 3 times lower latency than the standard T5X implementation, with identical outputs.
Where you'll see it: the "assisted generation" and draft-model options in production inference servers, which run a small model next to a large one to speed up chat and translation without changing answers.
The problem
Large autoregressive models such as GPT-3, LaMDA, PaLM and Parti are much more capable than small ones, but a single decode step of a large model is much slower. The steps also run one after another, because each token depends on the one before. The paper's introduction gives the cost directly: decoding tokens takes serial runs of the model.
Earlier speedups fell into two groups. Methods that reduce cost for all inputs, such as distillation, quantisation and architecture changes, usually need retraining and change the outputs. Adaptive computation methods spend less work on easy steps, but they too usually change the architecture or training and do not keep outputs identical. The paper's observation is that inference from large models is often limited by memory bandwidth and communication, not by arithmetic. That leaves spare compute, and spare compute can buy concurrency.
Key idea
The idea borrows from speculative execution in processors. A CPU guesses the outcome of a branch and starts the next instructions before the check finishes. If the guess is right, time is saved. If not, the work is thrown away and the correct path runs.
The paper extends this to stochastic outputs. Here a guess is a token sampled from a cheap model , the approximation model. The large model , the target, scores the guess in parallel with the others. A new rule, speculative sampling, decides which guesses to keep. The rule guarantees that the final tokens are distributed exactly as if had sampled them alone.
Here is the loop in words. The draft model proposes tokens. The target model runs once on the prefix and on each prefix extended by drafts, which gives target distributions at the price of one parallel call. The drafts are checked left to right. The first rejected draft is replaced by a corrected token, and the rest are discarded. If every draft is accepted, the target distribution at the last position supplies one bonus token. Each round therefore produces between 1 and tokens, and never fewer than one, so the target is never called more often than ordinary decoding.
Figure 1 of the paper shows a 38-token sentence generated with only 9 serial runs of the 97-million-parameter target, using a 6-million-parameter draft. In the first line of the figure, the target runs once and 5 tokens come out.
The method
The sampling rule
Let be the target's distribution for the next token given a prefix, and the draft's. To sample from , draw and keep it with probability . If it is rejected, draw again from the adjusted distribution
The paper calls this norm(max(0, p - q)). The proof is short. The accepted probability for a value is . The rejected mass is , where is the overall acceptance probability. Adding the two gives exactly, for any draft distribution at all. The draft affects only the speed, never the output.
The paper's Algorithm 1 generalises this to drafts. It draws from , runs on all prefixes in one batch, draws one uniform random number per draft, and accepts the first drafts with . If , the next token comes from built at position . If , it comes from .
Acceptance rate
The paper names the key quantity , the acceptance probability of one draft given its prefix. It shows that , where is the total-variation distance between the two distributions (the paper's Lemma 3.3 shows ). In words, the more the draft and target distributions overlap, the more drafts survive.
Averaged over prefixes and assuming the values are independent with a common mean , the expected number of tokens from one round is
This is a capped geometric count. Figure 2 of the paper plots it against for several . The formula is the paper's Equation 1.
Walltime
Let be the time for one draft call divided by the time for one target call. The paper assumes the target can run positions in parallel at the cost of one call. A round then costs target units, and the expected speedup over ordinary decoding is
Corollary 3.9 gives a clean condition. If , some yields a gain, and at the gain is at least . The paper reports that the draft cost was always below 0.05 in its experiments, and often close to zero.
Operations and memory
Speedup costs arithmetic. The target runs on positions per round, and rejected positions are wasted. Theorem 3.11 gives the expected increase in total operations. Table 1 shows that with and , speed rises to 2.71 times while operations rise only to 1.11 times, but with the speed reaches 6.86 times and the operations rise to 1.60 times.
Memory traffic can fall. The target's weights and key-value cache are read once per round, not once per token, so the number of such reads shrinks by the same factor as the token count. The paper argues that this is why the method helps when memory bandwidth is the bottleneck.
Choosing gamma
Given and , the best maximises . Because is an integer, it is found by trying values. Figure 3 gives the optimum for several . The paper also estimates that letting vary during decoding could add up to about 60 percent more speedup, which it leaves for future work.
- gamma = 3
- gamma = 7
Results
Walltime on T5-XXL
The target is T5-XXL with 11 billion parameters. The drafts are T5-large (800 million), T5-base (250 million) and T5-small (77 million). Tasks are English to German translation on WMT and news summarisation on CNN/DailyMail. The test used a batch size of 1 on one TPU-v4, with argmax sampling (temperature 0) and standard sampling (temperature 1).
| Task | Draft | Temp | Speed | ||
|---|---|---|---|---|---|
| EnDe | T5-small | 0 | 7 | 0.75 | 3.4X |
| EnDe | T5-base | 0 | 7 | 0.80 | 2.8X |
| EnDe | T5-small | 1 | 7 | 0.62 | 2.6X |
| CNNDM | T5-small | 0 | 5 | 0.65 | 3.1X |
| CNNDM | T5-small | 1 | 5 | 0.53 | 2.3X |
T5-small gave the best balance of cost and acceptance. Larger drafts had higher but cost more, so T5-large was slower overall (1.7X on translation at temperature 0). Argmax sampling gave higher and larger speedups than standard sampling. The paper says the empirical results match the theory, with differences due to implementation details.
Acceptance rates across models
Table 3 covers more pairs. A 6-million-parameter GPT-like draft gives of about 0.88 to 0.89 for a 97-million-parameter GPT-like target on the lm1b benchmark. On LaMDA 137B, drafts of 100 million, 2 billion and 8 billion parameters give between 0.57 and 0.75. Unigram and bigram models, which cost almost nothing, reach only 0.03 to 0.23. Even so, the paper reports that a bigram draft on English to German translation gives a 1.25X speedup with , because is zero.
The paper's broad finding is that drafts two orders of magnitude smaller than the target usually have between 0.5 and 0.9.
Theory against measurement
Table 4 of the appendix compares the predicted speedup from Theorem 3.8 with the measured one. For T5-small on translation at temperature 0, the prediction is 3.2 and the measurement 3.4. For T5-large at temperature 1, the prediction is 2.0 and the measurement 1.4. The paper attributes the gaps to differences in optimisation against the baseline and to the i.i.d. assumption on .
Lenience and beam search
The appendix also covers two extensions. A lenience factor multiplies the draft probability before the comparison. This relaxes the exact guarantee, but it still bounds how much more likely any token can become, at most times the target probability. With on translation, the paper reports rising from 0.62 to 0.84 for T5-small (temperature 1), and the improvement factor rising from 2.5X to 5X as falls from 1 to 0.1. Beam search can also be sped up, with a cost in compute, but the paper leaves its analysis for future work.
Why it mattered
The paper gave a way to speed up existing models without retraining, changing the architecture or changing the output distribution. Those three properties are why the method could be deployed as a drop-in change. The authors also note its limit: it trades extra arithmetic for lower latency, so it helps only when spare compute exists, which is common when memory bandwidth is the bottleneck.
The paper notes that Chen et al. (2023), an independent implementation, reported 2X to 2.5X speedups on Chinchilla 70B. The paper's central claim, that the output distribution is unchanged, is what separates it from the adaptive methods it compares against.
Reading guide
Read the paper in this order, using these prerequisite lessons:
- Section 1, Introduction. Needs Language Model for the idea of decoding one token at a time, and Transformer for the models named. The analogy to speculative execution in processors needs no further prerequisite.
- Section 2.1 and 2.2, Overview and standardised sampling. Needs Softmax Temperature for how argmax, top-k and temperature all become one adjusted distribution. Needs Conditional Probability for the notation .
- Section 2.3 and Algorithm 1. Needs Rejection Sampling for the accept-or-resample idea and Speculative Decoding for the overall loop. Read the proof in Appendix A.1 next to this section.
- Section 3.1 and 3.2, acceptance rate. Needs Expected Value for the average over prefixes, and Kullback–Leibler Divergence or total variation for .
- Section 3.3, walltime. Needs only the algebra of the geometric sum. Work through first, since Corollary 3.9 reduces to it.
- Section 3.6, approximation models. Needs Knowledge Distillation to see why a distilled draft would raise , a point the paper raises as future work.
- Section 4, experiments. Needs Softmax for the argmax and temperature settings.
- Appendix A.4, beam search. Needs Beam Search.
- Appendix A.5, lenience. Needs Importance Sampling only as a comparison: the lenience bound is a loose analogue of a bounded weight.
Check yourself
QuestionIn one round with , the first three drafts are accepted and the fourth is rejected. How many tokens does the round produce, and which model supplies each one?Show answer
Four tokens. The three accepted drafts come from the draft model. The fourth position is rejected, so the replacement token is drawn from the adjusted distribution , built from the target's distribution at that position. The target therefore supplies one token and the draft supplies three. That is one serial target call for four tokens, instead of four calls.
QuestionWith and , compute the expected tokens per round and the expected speedup when the draft costs .Show answer
Expected tokens: . Cost per round: target units. Speedup: . The round produces about 2.95 tokens for about 1.15 target-call units, so each token costs about 0.39 of an ordinary target call.
QuestionWhy does the paper's proof hold for any draft distribution q, even a bad one?Show answer
The accepted mass for each value is , and the rejected mass is the remainder of spread over . The two parts sum to whatever is. A bad draft lowers , so it wastes more target calls, but the output distribution is still exactly . The draft controls speed, and the rule controls correctness.
QuestionOn English to German translation a unigram draft gives of about 0.07 and a bigram draft about 0.19 (temperature 1). Why can a trivial draft still be worth using?Show answer
With near zero, the speedup is with no cost term. At this is only about 1.07 tokens per round, so a unigram draft barely helps by itself. The bigram draft at gives about 1.25 times with , which the paper calls surprisingly high for so simple a model. Trivial drafts help only when is essentially zero and is not tiny.
QuestionWhy does the method leave the output distribution unchanged, but lenience does not?Show answer
Exact speculative sampling accepts with probability and resamples the rest, which makes the output exactly . Lenience multiplies by before the comparison, so accepted tokens follow a different distribution. The paper's guarantee is weaker: no token can become more than times as likely as under . That buys a higher and more speed, at the price of exactness.
Sources
- Leviathan, Kalman, Matias (2022). Fast Inference from Transformers via Speculative Decoding, arXiv:2211.17192.
- Chen et al. (2023). Accelerating Large Language Model Decoding with Speculative Sampling, arXiv:2302.01318.