A small model drafts tokens and the big model checks them in one pass, giving 2x to 3x faster output with exactly the same probabilities.
A large language model writes one token (a word or word piece) at a time, and each token needs a full run of the model. For a big model this is slow, and the runs cannot be done in parallel because each token depends on the one before it. This paper shows how to get several tokens from one run of the big model, while the output stays exactly what the big model would have written alone.
Fast Inference from Transformers via Speculative Decoding. Yaniv Leviathan, Matan Kalman, Yossi Matias (Google Research). ICML 2023. arXiv:2211.17192. A team at DeepMind, Chen et al., published the same idea independently: arXiv:2302.01318.
A small, fast draft model guesses the next few tokens, and the big target model checks all of the guesses in a single run, keeping the ones that pass a fair test.
Many tokens are easy, so a small model often guesses them right. The test is built so that the kept tokens follow exactly the same probabilities as the big model alone. Nothing is retrained and the big model is not changed.
Writing a token means reading all the model's weights out of memory. The paper notes that this is often limited by memory speed, not by arithmetic, so the chip has spare calculating power. Checking five positions in one run costs about the same time as one position. The paper uses this spare power.
Two words used below. A distribution is a list of probabilities for every possible next token. Autoregressive means each token is chosen using all the tokens before it.
The draft model writes γ guessed tokens one by one (γ is a small number such as 4)
So a round always gives at least one token and at most .
Call the target's probability for a token and the draft's . (Chen et al. write it the other way round, with for the target. The rule is the same.) The draft proposes a token drawn from . Then:
In one line, accept with probability .
If it is rejected, draw a replacement from the leftover distribution:
This is where the target likes a token more than the draft does, scaled so the probabilities add to 1.
Toy example (our numbers, not from the paper). Three possible tokens A, B, C.
| Token | Target | Draft | Accept chance | Chance draft proposes it and it is accepted: |
|---|---|---|---|---|
| A | 0.6 | 0.3 | 1 | 0.3 |
| B | 0.3 | 0.5 | 0.6 | 0.3 |
| C | 0.1 | 0.2 | 0.5 | 0.1 |
The chance that a proposal is accepted is . So a proposal is rejected with probability . Leftover distribution: is for A and for B and C. After scaling, A has probability 1. So after a rejection the token is always A.
Final probability of each token:
That is exactly . The draft model's quirks are removed by the leftover step. This is why the method does not change the output distribution.
The acceptance rate is the average chance that a draft token is accepted. In the toy above, . The paper also shows , where measures how far apart the two distributions are, so a closer draft gives a higher .
If each guess is accepted with the same chance , the expected number of tokens per round is (paper, Equation 1):
Check with , : this is tokens per big-model run.
Let be the cost of one draft run divided by the cost of one target run. One round costs target-runs. The paper's Theorem 3.8 gives the expected speed-up in time:
Toy example, , , : tokens per round . Cost . Speed-up .
Guessing further is not always better. The last guess only counts if all earlier guesses passed, but its cost is paid every round. With the same and , gives about , the same as .
Leviathan et al. tested T5-XXL (11 billion parameters) on a TPU-v4 with batch size 1, against the standard T5X code. The draft was a smaller T5 model. They report 2x to 3x faster with identical outputs.
| Task | Draft | Temp | Speed-up | ||
|---|---|---|---|---|---|
| English to German | T5-small | 0 | 7 | 0.75 | 3.4x |
| English to German | T5-small | 1 | 7 | 0.62 | 2.6x |
| English to German | T5-large | 0 | 7 | 0.82 | 1.7x |
| Summarising (CNN/DM) | T5-small | 0 | 5 | 0.65 | 3.1x |
| Summarising (CNN/DM) | T5-small | 1 | 5 | 0.53 | 2.3x |
Temperature 0 means always pick the most likely token. Temperature 1 means sample normally. The smallest draft was the fastest overall because its low cost outweighed its lower .
Chen et al. tested Chinchilla (70 billion parameters) with a 4 billion parameter draft and guesses.
| Benchmark | Sampling | Time per token, normal | Time per token, speculative | Speed-up |
|---|---|---|---|---|
| XSum | Nucleus | 14.1 ms | 7.52 ms | 1.92x |
| XSum | Greedy | 14.1 ms | 7.00 ms | 2.01x |
| HumanEval (code) | Nucleus | 14.1 ms | 5.73 ms | 2.46x |
Scores stayed the same, for example XSum ROUGE-2 of 0.157 and 0.156 with greedy sampling. These numbers show real speed-ups on two model families. They do not mean every task gets the same speed-up. The code task did best, which the authors link to code having many repeated pieces.