Inference Engineering

Lesson 11 of 14

Speculative decoding

When one person is chatting, a GPU spends nearly every step reading the model out of memory, so checking several words costs about the same as writing one. Speculative decoding lets a small fast model guess ahead and the big model check all its guesses in one step, for several words per step with every word drawn from the same odds as before.

A small model guesses, a big one checks

One shot, three stages, with real numbers for Llama 3.1 70B and Llama 3.1 8B in 8 bits on one NVIDIA H200, one user, slowed down so passes of a few thousandths of a second can be seen. Each tower has one plate per layer, sized to the model's weight bytes. The sentence is a toy: the 70B always picks its single most likely word, so a guess is kept when it matches that word, and the final full stop counts as a token. It plays by itself; press a stage to replay it, or drag to look around.

01 · Why it can work

Checking costs the same as writing

A model writes its reply one token at a time (a token is a piece of a word, about three-quarters of one on average). Each token takes one pass through the model, and every pass reads all the model’s weights from GPU memory: lesson 1’s belt carrying up the whole rulebook. For one user, that reading is nearly all the time, so batching other users cannot speed up your demo.

But a pass can carry several tokens, as it does when the model reads your prompt. The weights still come up once, so checking five tokens takes about as long as writing one.

One pass, more tokens: the time hardly moves

  • reading the weights
  • arithmetic
  • the same tokens, one pass each

Llama 3.1 70B in 8 bits on one H200, one user, 1,024 tokens of conversation so far. Times are roofline floors.

02 · Guess, then check

The odds of every word stay the same

Parrot’s 8B has no smaller sibling with its vocabulary, so take the papers’ pair: Llama 3.1 70B, with Llama 3.1 8B as the draft model. In 8 bits they need 81.7 GB of weights, so they share one H200, the H100’s 141 GB sibling.

The draft guesses 4 tokens; the 70B checks them in one pass. It keeps guesses up to the first it disagrees with, writes its own token there and drops the rest. Most chatbots pick words at random by their odds; then a guess is kept outright if the 70B likes it at least as much as the draft did, and only sometimes if not. Either way every word comes out with the odds the 70B alone would give it. A poor draft can slow things down; it cannot change what is written.

Whatever the draft, the same odds come out

Plays by itself; press a button or pick a draft to take over.

✓ kept, ✗ rejected: a guess is kept when the uniform draw u is below its keep probability min(1, p/q). After a rejection the round ends with a token resampled from the residual; if every guess is kept, a bonus token comes from p.

target pdraft qproduced

A toy with six words, where every position has the same odds. Outlined bars: the big model's odds. Short marks: the draft's. Green: what actually came out.

03 · How much faster

Good guesses pay, bad ones cost

If each guess has an 8-in-10 chance of being kept (the acceptance rate) and the draft guesses 4 at a time, each 70B pass yields 3.36 tokens on average. A guess costs about 12% of a 70B pass, because the 8B reads that much less memory. One user’s reply streams 2.30× as fast: the demo’s “twice as fast”.

With poor guesses the draft’s time is wasted. At 3 in 10 with 4 guesses it is 0.98×, slower than the 70B alone; fewer guesses then do better.

Speedup against the number of guesses

Plays by itself; move a slider to take over.

speeduptokens per big-model pass

The defaults are α = 0.8 and c = 0.12, the 8B drafting for the 70B. Below the dashed line, speculation is slower than no speculation.

04 · The catch

A busy server has nothing to spare

The trick spends arithmetic the GPU had free while it waited on memory. A busy server’s passes are already full of other users’ tokens. Past about 206 tokens per pass, checking guesses makes each pass longer, rejected guesses waste real work, and the draft’s weights and notes take memory more users could have had.

So speculation suits light traffic, when one person’s speed matters. As the server fills, guess fewer tokens or turn it off.

More users per pass, less to gain

Users in one pass (the batch)

  • expected speedup
  • would not fit in memory

The same pair at 8-in-10 acceptance and 4 guesses, each user 1,024 tokens in. Usable memory is the H200's 141 GB minus the 10% safety margin.

05 · Playground

Tune it yourself

Set the acceptance rate and the number of guesses. Find where more guesses stop paying, and where the draft makes every token slower.

Time per token, with and without the draft

Llama 3.1 70B checking Llama 3.1 8B's guesses, 8-bit weights, one H200, one user.

  • the 70B's share of each token
  • the 8B's share of each token

06 · Glossary

The vocabulary, in one place

Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
Pass
One trip through every layer of a model. Each pass reads all its weights from GPU memory: 14.8 ms for the 70B here.
Weights
The model's learned numbers, its rulebook from lesson 1. Llama 3.1 70B in 8 bits has 72.7 GB of them.
Draft model
A much smaller model with the same vocabulary that guesses the next few tokens. Here Llama 3.1 8B, 9.08 GB.
Big model
The model whose reply you actually want; it checks the guesses. Papers call it the target model.
Guesses per round
How many tokens the draft guesses before each check, γ (gamma). 4 here.
Acceptance rate
The chance that the big model keeps a guess, α (alpha). It depends on the two models and on the text.
Cost of a guess
One step of the draft as a share of one big-model pass, c. About 0.12 here, because the 8B reads far fewer bytes.
Speedup
How many times faster one user gets tokens than with the big model alone. Below 1, the draft makes it slower.
Batch
The users served together in one pass. Lesson 6 explains how a server forms it.

You can now

  • Explain why checking several guessed tokens in one pass of a memory-bound model costs about the same as producing one, and why the output keeps the same odds.
  • Compute the expected tokens per big-model pass and the wall-clock speedup from the per-guess acceptance chance, the number of guesses and the draft's cost ratio, and tell that chance apart from the share of drafted guesses kept.
  • Decide when to turn speculative decoding on, and how many tokens to draft, as the batch size grows and verification stops being free.

Try it in the Serving Lab: Make one user’s tokens come twice as fast.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.