Key takeaways
- Decoding is memory-bound, so verifying several tokens at once takes about as long as generating one. Speculative decoding exploits exactly this: prefill is much faster than generating token by token.
- A small draft model q guesses k tokens ahead, and the big target model p scores all of them in a single forward pass.
- The rule: accept each draft with probability min(1,q(x)p(x)); if it's rejected, redraw from max(0,p−q), normalized; if everything passes, you get one extra token for free.
- The two paths add up to exactly p(x), so the output distribution doesn't change at all: speculative decoding is lossless.
- The higher the acceptance rate α=∑xmin(p,q), the more tokens per round and the bigger the speedup, but the drafts shouldn't be too long.
Only one word at a time?
When you chat with a large language model, the answer pops out one word at a time, and for every single word, the model runs all the way through itself. Even to produce a single comma, a 7-billion-parameter model puts all 7 billion parameters to work.
Could we get it to produce several words at once, with a result that's statistically identical to going one word at a time, and zero loss in quality? It sounds like a free lunch, but this one really exists. It's called speculative decoding: guess first, check later. This episode answers three questions: why it's faster, how it decides to accept or reject a guess, and, best of all, why the output distribution doesn't change one bit.
Why decoding is slow: moving, not math
Every time the model generates a token, where does the time go?
- Moving: the parameters live in GPU memory. At 7 billion parameters and two bytes each, that's about 14 GB. The parts that do the math aren't in that memory, so for every token, all 14 GB have to be hauled over to the compute cores. A typical data-center GPU moves about 2 TB per second, so one read-through takes about 7 milliseconds.
- Math: back in episode 10 we saw that each token costs about twice the parameter count in operations, here 14 billion. This GPU does about 300 trillion operations per second, so the math takes roughly 0.05 milliseconds, well over a hundred times faster than the moving.
So while a token is being generated, the compute cores spend most of their time waiting for data. That's what memory-bound means: speed is limited by memory bandwidth.
That has an interesting consequence. Feed in 5 tokens at once and the parameters still make the trip just once; the math goes up five times, but that's still only 0.23 milliseconds. The total time barely changes: handling 5 tokens takes about as long as handling 1. Plot time against tokens per pass and the line starts out flat; only at around 150 tokens does the math become the bottleneck again. That's also why, last episode, prefill could chew through a whole prompt in one go, with each token coming out so cheap.
But decoding can't enjoy this, because you can't know the second token until the first has been generated, and the third has to wait for the second. So what if we could guess the next few tokens ahead of time? If the guesses are right, the big model can check them all at once, just like prefill.
The small model drafts, the big model checks
That's the idea behind speculative decoding:
- Take a much smaller draft model and let it race ahead and guess the next few tokens, say 4. It still generates them one at a time, but it might be dozens of times smaller than the big model, so those 4 steps barely take any time.
- Glue the context and the 4 drafts together and hand them, all at once, to the big model, the target model.
- Thanks to the causal mask, the big model's output at each position is exactly its prediction for the next word at that point. One forward pass gives 5 such distributions (one per draft, plus one at the last position).
Start with the simplest case, where both models just pick their most likely word (greedy). The context is "Today I'm so", and the small model guesses "tired , need coffee". Compare them one by one:
- "tired": the big model would pick it too. Pass.
- ",": also passes.
- "need": the big model's top choice is "I". They disagree, so stop right here and replace it with the big model's "I".
- "coffee": it was guessed as a follow-up to "need", and that premise no longer holds, so it's thrown away, along with the big model's outputs at those positions.
In this round, the big model ran only once but locked in three tokens: the two that passed, plus the one it swapped in.
Another round. Now the context is "Today I'm so tired, I". The small model guesses "just want to sleep", and the big model agrees with every word. And don't forget: the output at the last position hands us one extra distribution, so we take one more word from it, a period. This round yields 5 tokens.
In the worst case, the very first draft is rejected. We still get the big model's replacement word, so we always move forward by at least one token; all we've wasted is a little of the small model's time. Put differently: because scoring the drafts gives us the next-token logits "for free", every round always gets one token straight from the big model, either a replacement or a bonus.
Intuition: It's like a seasoned editor working with an intern. The intern dashes off a draft, and the editor reads it all in one go: "These first few lines are fine; from here on, I'll take over." The more the intern gets right, the faster the whole thing goes.
With sampling: how likely to accept?
In practice, models usually don't just pick the most likely word; they sample at random according to the probabilities (episode 11). So what does "agreeing with the big model" even mean now?
Zoom in on the first position. The context is "Today I'm so", and we consider just five candidates. Call the small model's distribution q and the big model's p:
| Word |
q |
p |
| tired |
0.4 |
0.3 |
| happy |
0.1 |
0.25 |
| busy |
0.15 |
0.2 |
| bored |
0.3 |
0.15 |
| hungry |
0.05 |
0.1 |
They don't quite agree. Our goal is clear: the word we output must follow the big model's distribution p exactly, as if the small model had never been involved. If we accepted whatever the small model drew, the output would follow q instead, which is wrong. So sometimes we have to say no. With what probability?
The acceptance rule: when the small model draws a word x, accept it with probability
P(accept x)=min(1, q(x)p(x))
If the ratio is above one (the big model likes the word more than the small one does), always accept.
- "happy", "busy", "hungry": ratio above one, so they're accepted right away whenever they're drawn.
- "tired": 0.3/0.4=0.75, a three-in-four chance of being accepted.
- "bored": 0.15/0.3=0.5, a fifty-fifty chance.
How do you "accept with probability 0.75"? Roll a random number between 0 and 1: below 0.75, accept; otherwise, reject.
After a rejection we can't leave a hole, so we draw a new word from a corrected distribution. It only looks at where p sticks out above q: p−q, with negatives set to zero:
max(0, p(x)−q(x))
Here only "happy", "busy" and "hungry" have anything left over: 0.15, 0.05 and 0.05. Normalize them (divide by their total, 0.25) to get 0.6, 0.2 and 0.2, and after a rejection, redraw in exactly those proportions:
∑x′max(0, p(x′)−q(x′))max(0, p(x)−q(x))
Why the output follows p exactly: the example
The most important question of the episode: under this rule, is the probability of outputting a word x really p(x)?
There are only two ways to output x:
- Path 1: the small model happens to draw x, and it's accepted.
- Path 2: the small model draws some word that gets rejected, and the redraw lands on x.
Path 1 is "draw x" times "accept it": q⋅min(1,p/q). If p≥q, the product is q; if p<q, it's q⋅qp=p. Together: whichever of p and q is smaller. On the chart, that's exactly where the bars overlap: tired 0.3, happy 0.1, busy 0.15, bored 0.15, hungry 0.05, adding up to 0.75. So a draft has a 75% chance of being accepted outright.
The part where q pokes out above p is the chance of rejection: "tired" by 0.1 and "bored" by 0.15, 0.25 in total. Meanwhile the gaps still left in p (on "happy", "busy" and "hungry") also add up to exactly 0.25. That's no coincidence. q adds up to one, so does p, and they share the same overlap; take the overlap away, and what's left of q must equal what's left of p.
Path 2: the total chance of rejection is 0.25, and after a rejection we redraw with weights 0.6, 0.2, 0.2, so that 0.25 splits into 0.15, 0.05 and 0.05. The chance of getting "happy" through path 2 is 0.25×0.6=0.15, which fills its gap exactly; "busy" and "hungry" get 0.05 each, which fills theirs too.
| Word |
Path 1 |
Path 2 |
Total |
| tired |
0.3 |
0 |
0.3 |
| happy |
0.1 |
0.15 |
0.25 |
| busy |
0.15 |
0.05 |
0.2 |
| bored |
0.15 |
0 |
0.15 |
| hungry |
0.05 |
0.05 |
0.1 |
Add the two paths, and every entry is filled perfectly, nothing missing and nothing extra. It's exactly the big model's distribution p.
It works for any p and q
Now the same reasoning as a general formula, where the first term is path 1 and the second is path 2:
P(emit x)=P(draft x)⋅P(accept x)+P(rejected)⋅P(resample x)
Path 1 (x accepted from the draft):
q(x)⋅min(1,q(x)p(x))=min(q(x),p(x))
Path 2 (x chosen after a rejection). First, the total chance of rejection: add up, over every word, the chance it's drawn and then rejected. Drawing and rejecting x′ has probability 0 if p(x′)>q(x′) (it's always accepted), and otherwise
q(x′)⋅(1−q(x′)p(x′))=q(x′)−p(x′)
So
P(rejection)=x′∑max(0, q(x′)−p(x′))=1−x′∑min(p(x′),q(x′))=x′∑max(0, p(x′)−q(x′))
The last step holds because p and q each sum to 1 and share the overlap, so the total amount by which q exceeds p equals the total amount by which p exceeds q. And that's exactly the denominator of the redraw distribution, so the two cancel:
P(rejection)⋅∑x′max(0, p(x′)−q(x′))max(0, p(x)−q(x))=max(0, p(x)−q(x))
Add the two paths, one case at a time:
min(q(x),p(x))+max(0, p(x)−q(x))={q+(p−q)=p,p+0=p,p≥qp<q
So no matter what p and q look like, the probability of outputting x is exactly p(x). Speculative decoding is lossless: it changes the speed, never the distribution.
Watch out: The proof never uses how accurate the small model is. Even a terrible draft model gives correct results; it just gets rejected more often, so things slow down.
Running it 10,000 times
Still not convinced? Check it with code. All the logic for one position fits in six lines: draw a draft from q; roll a random number and accept if it's below p/q (the random number is always below 1, so a ratio above 1 always accepts, which gives exactly min(1,p/q)); otherwise, redraw from the leftover part, normalized.
x = sample(q) # small model drafts
if random() < p[x] / q[x]: # accept w.p. min(1, p/q)
out = x
else: # rejected: redraw
r = np.maximum(p - q, 0)
out = sample(r / r.sum())
Run it ten thousand times with the tired / happy / busy / bored / hungry numbers and count how often each word comes out: the frequencies line up almost perfectly with p, 0.3, 0.25, 0.2, 0.15 and 0.1. For comparison, if we never rejected anything and kept whatever the small model drew, we'd get q instead, and "tired" would come out a third more often than it should (0.4 instead of 0.3).
One full round
Extending the one-position rule to k drafts, one round of speculative decoding goes like this:
- Draft: the small model drafts k tokens one after another and notes down its distribution q at each step.
- One forward pass: the big model runs once over context plus drafts and gets the distribution p at k+1 positions. Now every draft's acceptance probability can be worked out.
- Check left to right: say the first draft has acceptance probability 0.75 and its random number is 0.42, so it passes; the second is 0.8 against 0.37, so it passes too; the third is 0.5, but its random number is 0.71, so it's rejected.
- On a rejection, wrap up: redraw a word from the leftover distribution at that position (say "I"), and throw away every draft after it ("coffee"). The KV cache the big model already computed for the discarded positions has to be rolled back too. The round is over.
- If everything passes: draw one more bonus token from the distribution at position k+1.
Why does the proof for one position carry over to every position? Because whether a word was accepted or redrawn, its distribution is exactly the same as if the big model had sampled it itself. So the context the next position sees is distributed exactly as if the big model had written it alone. Pass the baton along, one position at a time, and the whole output follows the big model's distribution.
How much faster?
The key is how likely each draft is to be accepted. In the proof, the total acceptance probability was exactly the overlap. We call it the acceptance rate and write it α:
α=x∑min(p(x),q(x))
The more alike the two models, the more the bars overlap and the closer α gets to 1. In our example, α=0.75.
To keep the math easy, assume every position is accepted with probability α, independently of the others. That's a simplification; in reality every position is different. How many tokens does one round produce?
- At least 1: a replacement word if something is rejected, a bonus word if everything passes.
- The first draft is accepted with probability α, earning 1 more; the first two with probability α2, 1 more again; and so on, up to αk.
Adding these up gives the expected tokens per round, a geometric series:
E[tokens per round]=1+α+α2+⋯+αk=1−α1−αk+1
With α=0.75 and k=4:
1+0.75+0.56+0.42+0.32≈3.05
Each run of the big model yields just over three tokens on average instead of one. Of course, the small model takes time too. If one small-model step costs c times a big-model step, a round costs 1+k⋅c. With c=0.05, that's 1.2:
speedup=1+4×0.053.05=1.23.05≈2.5×
Plot speedup against draft length k for a few acceptance rates (small-model cost 0.05), and each curve rises and then falls. The higher the acceptance rate, the bigger the speedup and the larger the best k. But k can't grow forever: the further ahead you guess, the more likely a rejection partway through, and every draft after it was guessed for nothing.
- At an acceptance rate of 0.6, things top out around k=4, at a bit under two times faster.
- At 0.75, six or seven drafts is best.
- At 0.9, you can guess a dozen or so in one go, for nearly five times the speed.
Watch out: Papers report real-world speedups of about two to three times, and the precondition is that the big model is memory-bound. If the batch is large and the GPU's compute is already stretched, the extra verified tokens are no longer free, and the speedup shrinks.
Also, the drafts don't have to come from a separate small model. They can come from repeated chunks of the prompt, or from a few extra prediction heads bolted onto the big model. As long as they're checked with the same rule, the distribution stays the same.
Recap
- Decoding is memory-bound: moving parameters is far slower than the math, so verifying a few tokens costs about the same as generating one.
- A small model drafts k tokens and the big model scores them all in one forward pass. Stop at the first rejected draft and discard the rest (rolling back their KV cache).
- Accept with probability min(1,p/q); on rejection, redraw from max(0,p−q), normalized; if all pass, take a bonus token. Every round moves forward at least one token.
- Path 1 contributes min(p,q), path 2 contributes max(0,p−q), and together they make exactly p: the output distribution is unchanged, so speculative decoding is lossless.
- The higher the acceptance rate α=∑min(p,q), the more tokens per round, 1−α1−αk+1 on average, and the bigger the speedup. But don't make the drafts too long.
One idea kept coming up: moving parameters is far slower than doing the math. Next time we go inside the GPU to see what really happens between its memory and its compute cores, and how FlashAttention uses that to make attention fast.