Speculative decoding
Let a cheap draft model propose several tokens and have the big model verify them in one pass. Explain the accept/reject rule that keeps the output distribution exactly the big model's.
Builds on: Prefill vs decode: compute- vs memory-bound
What the animation says
Why guess ahead?
When a big model writes, every new token means streaming all its weights in from memory. The maths per token is tiny by comparison: about two operations per parameter. The chip mostly waits on memory. So scoring five tokens in one pass reads the weights once, just like scoring one. The catch: each token needs the one before it. Generation is a chain. Suppose five guessed tokens were already lying around. Checking all five in one pass costs how much, compared to one?
Draft guesses, big model checks
About the same. So who makes the guesses? A draft model: same tokenizer, maybe fifty times smaller. It writes four tokens, one at a time. That's cheap. Then the big model runs one pass on the last token plus all four drafts, giving its own prediction at five positions. Left to right, it keeps or rejects each draft. At the first rejection, it puts in a corrected token and throws away the rest. Now suppose all four drafts survive. How many tokens does this pass commit?
The keep-or-reject rule
Five, thanks to a free bonus token. Now the keep-or-reject rule at one position, with three tokens: A, B, C. The big model gives A point six, B point three, C point one. The draft swaps A and B. The draft proposes B. Keep it with chance big over draft: point three over point six, one half. Proposed A or C? Always kept: the ratio is at least one. If B is rejected, the replacement comes only from where the big model wants more than the draft. Tap it.
Why the output is exact
Only A has a gap: point six minus point three. So a rejected B always becomes A. Now total it up. A: proposed directly, point three, always kept. Plus B proposed, point six, rejected half the time, turning into A: another point three. That makes point six. B: point six times one half, point three. C: point one. Exactly the big model's numbers, though every guess came from the draft. Here the draft is kept seventy percent of the time. Now swap in a worse draft. What changes?
Run it, and remember
Only speed. Here's the rule in code. The draft proposes; this line keeps it with chance big over draft. Otherwise, these two lines take the gap, clip it at zero, normalise, and sample the fix. Run it a hundred thousand times: point six, point three, point one. The big model's distribution. With greedy decoding it's simpler: keep a draft only if it matches the big model's top choice. At acceptance point eight with four drafts, a pass yields about three point three tokens, if acceptances are independent.
Draft guesses, big model checks
The accept/reject rule
Three tokens, checked by hand
Simulate one verify step
import numpy as np
rng = np.random.default_rng(0)
p = np.array([0.6, 0.3, 0.1]) # big model
q = np.array([0.3, 0.6, 0.1]) # draft model
def step():
x = rng.choice(3, p=q) # draft proposes
if rng.random() < min(1, p[x] / q[x]):
return x # accept
res = np.maximum(p - q, 0)
return rng.choice(3, p=res / res.sum()) # corrected token
out = [step() for _ in range(100_000)]
print(np.bincount(out) / len(out)) # ~ [0.6 0.3 0.1]
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses