Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Generating text

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.

One pass mostly waits for the weights to arrive from memoryMemory holds 140 GB of weights and sends them at 3.35 TB/s to compute units that can do about 990 trillion operations a second. A timeline of one pass shows 42 ms of moving weights and 0.14 ms of maths.Memory3.35 TB/sCompute units990 trillionoperations a secondOne passmoving the weights: 42 msmaths: 0.14 ms
Every weight has to travel from memory to the compute units. Moving them takes 42 ms; the maths on them, 0.14 ms. The compute units sit idle for over 99% of the 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 five tokens in one pass costs about the same as writing oneWrite 1 token: 41.9 ms. Check 5 tokens: 42.5 ms. 5 tokens, 1 per pass: 209.7 ms.Write 1 token41.9 msCheck 5 tokens42.5 ms5 tokens, 1 per pass209.7 msviolet: moving the weights · green: the maths
A pass that checks five tokens takes about as long as one that writes one. Writing the same five tokens one pass at a time takes five times as long.

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.

A draft model guesses, the big model checks in one passAfter "the hare sat", the draft model guesses "on the lawn .". The big model keeps "on" and "the", replaces "lawn" with "grass", and drops the full stop that followed the wrong guess.Draft model4 quick passestheharesatonthelawn.Big modelone passtheharesaton✓the✓grass✗ lawn.+3
The draft model guesses four tokens, one at a time. The big model checks all four in one pass, keeps them up to its first disagreement and writes its own token there: three tokens from one big 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.

How far ahead should the draft guess?

Interactive
Tokens per big pass
Speed-up
Best number of guesses

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”.

The keep rule makes every word appear as often as the big model wantsgrass: the big model wants 60%; keeping every guess gives 30%; with the rule, 30% kept plus 30% from replacements, 60%. lawn: the big model wants 20%; keeping every guess gives 50%; with the rule, 20% kept plus 0% from replacements, 20%. meadow: the big model wants 10%; keeping every guess gives 10%; with the rule, 10% kept plus 0% from replacements, 10%. other: the big model wants 10%; keeping every guess gives 10%; with the rule, 10% kept plus 0% from replacements, 10%.grass60%lawn20%meadow10%other10%keep every guesskeptreplacementsbig model alone
Keeping every guess would write words as often as the draft likes them. With the rule, kept guesses plus replacements land exactly on the big model's own shares.

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.

← Browse all subjectsBack to the beginning ↑