Speculative decoding: guessing ahead and checking in one pass
Writing one token at a time is slow. Could a model check several guesses faster than it can make them?
A 70-billion-parameter model is writing an answer, one token per pass. Multi-token prediction let one pass check a guess for free. Why was the check free?
Most of the pass is waiting
To write one token, the model multiplies the token’s vector by every one of its weights. Those weights live in memory, and all 140 GB of them have to travel to the compute units on every pass.
The compute units finish their share almost at once, then wait for the next weights to arrive. Writing is limited by memory, not by the maths.
Check five tokens for the price of one
The weights arrive once per pass, however many tokens they are used on. Five tokens of maths add less than a millisecond.
Checking a guessed token is just predicting at its position and comparing. So if we had guesses for the next few tokens, one pass could check them all. But who makes the guesses?
Let a small model guess
A model a tenth the size reads a tenth of the weights, so it writes far faster, though less reliably. Let it guess a few tokens ahead, then let the big model check them in one pass.
The big model keeps the guesses up to its first disagreement and puts its own token there. Even if every guess is wrong, the pass still writes one token, as before.
How far ahead should it guess?
Longer guesses can give more tokens per check. But each guess depends on all the ones before it being right, and the draft model still has to write every one.
There is a best length, and it grows as the guesses get better. Past it, the draft spends more time writing guesses than they save.
The same answers, only faster
When the model always writes its most likely word, a kept guess is one it would have written anyway. The text comes out identical.
Usually it samples instead, picking words at random in proportion to how likely it finds them. Now the draft’s taste matters. It gives “lawn” 50%, the big model only 20%, so keeping every guess would write “lawn” far too often.
So when the draft liked a word more than the big model did, keep it only part of the time: “lawn” two times in five. When a guess is thrown out, draw a replacement from the words the big model wanted more than the draft offered, here “grass”.
Every word now appears exactly as often as if the big model had written alone.
Each new token still waits for the ones before it. What if a model could write a whole block of text at once? Next, text diffusion.
Sources and assumptions
- Fast Inference from Transformers via Speculative Decoding, Leviathan, Kalman and Matias, 2022: a smaller model’s guesses checked in parallel by the large model, without changing its output distribution; 2–3 times faster on T5-XXL with identical outputs. The formula for tokens per big pass, (1 − ak+1) ÷ (1 − a), is theirs.
- Accelerating Large Language Model Decoding with Speculative Sampling, Chen and colleagues at DeepMind, 2023: the same idea with a modified rejection sampling rule, 2–2.5 times faster on Chinchilla 70B.
- NVIDIA H100: 3.35 TB/s of memory bandwidth for the SXM version, and 1,979 trillion BF16 operations a second with sparsity, so about 990 trillion without.
The memory and five-token figures count only the weights: 70 billion at 2 bytes, read at 3.35 TB/s (about 42 ms), against 2 operations per weight per token at 990 trillion a second (about 0.14 ms per token). They ignore the KV cache and assume perfect efficiency; a model this size is spread over several such GPUs, which changes both times by the same factor. The widget assumes each guess is right independently with the chosen chance, and that a draft pass costs a tenth of a big pass. The guessed words and next-word probabilities in the other figures are illustrative. In the last figure, a guess is kept with chance min(1, p ÷ q), with p from the big model and q from the draft, and replacements are drawn in proportion to max(0, p − q); kept guesses and replacements then add up to p for every word.