
25 November 2026 · Inference engine notes · 4/4
Inference Engines 04: Speculative Decoding
Part 4 is speculative decoding. My first reaction when I heard about it: after the small model guesses, doesn't the big model still have to compute everything again to check? How is that any faster? It turns out it really is faster, and the output is exactly what the big model would have written one token at a time. mini-sglang doesn't implement it, so this part walks through the idea.
Why it's faster
- The big model still runs over every guess, but checking 5 guesses takes about as long as generating 1 token
- The reason is that decode is bound by moving weights (part 2 covers this): every forward pass streams the whole set of weights from GPU memory (the warehouse) through on-chip storage (the workbench)
- A 0.6B model on an H100 takes about 0.4 ms to move once; the math for 1 token takes about 1 µs
- While the weights are passing through, multiplying 1 row or 6 rows costs about the same
- Plain decode: one pass over the weights per round, 1 token out; 5 tokens means 5 passes
- Speculative decoding: one pass over the weights, 5 guesses checked, 1 to 6 tokens out
- What it saves is the number of big-model calls; total compute actually goes up a little (rows for wrong guesses are wasted)
Why the guesses can be stacked like a prefill
- Plain decode can't run in parallel: token 2 isn't known until token 1 has been computed
- The small model fills in the next few tokens first, so they are known in advance, like the tokens of a prompt, and the big model can process them all in one round the way it does a prefill
- In that round the big model only checks: all 5 guesses at once, keeping as many as match counting from the first
- One difference from a normal prefill: prefill needs logits only for the last row (mini-sglang's
LMHead.forwardusesget_last_indicesto take each request's last row), while verification needs logits for all k+1 rows
A worked example: The weather today + is so nice let's go
- The text so far is "The weather today", and the small model guesses 5 tokens: is so nice let's go (one word per token, to keep it simple)
- The big model gets 6 rows,
today is so nice let's go: the last confirmed token plus the 5 guesses- "today" goes in because it was generated last round and has no KV yet. mini-sglang's plain decode works the same way: each round feeds in the token produced the round before
- One forward pass: the linear layers multiply all 6 rows together; attention uses a causal mask, so each row sees "The weather" from the KV cache plus the rows before it among the 6 (itself included)
| Row | What this row sees | Big model's next token | Against the guess |
|---|---|---|---|
| 1 (today) | The weather today | is | guessed "is", match |
| 2 (is) | The weather today is | so | guessed "so", match |
| 3 (so) | The weather today is so | warm | guessed "nice", miss |
| 4–6 (nice let's go) | built on the wrong "nice" | thrown away |
- Accept "is" and "so", and put the big model's own "warm" in position 3: this round yields 3 tokens, "is so warm"
- Rows 4–6 are thrown away, and the KV pool slots they wrote to go back to the free list
- In mini-sglang's
cached_len/device_lenterms: the 5 guesses are appended afterdevice_len, and this round computes[cached_len, device_len), 6 tokens; after the check,cached_lenmoves forward only 3 (today is so), and "warm", like any freshly decoded token, gets its KV computed next round
Why the output is exactly the same
- Take row 2: its input is "The weather today is". Without speculative decoding, after the big model writes "is", the next round's input is exactly "The weather today is", since "is" is what it would have written anyway
- Same input, and the causal mask keeps this row from seeing the later guesses, so same output
- As long as every earlier guess was right, row i computes exactly what the big model would have computed in round i; rows after a miss rest on a false premise and are dropped
- Greedy: compare directly; a guess is accepted if it equals the big model's argmax
- Sampling: rejection sampling decides what to keep
- Call the big model's distribution at this position and the small model's ; guess is accepted with probability
- On a rejection, a replacement is sampled from , normalized
- The output at each position then has the same distribution as sampling from directly. Here is the distribution after temperature, top-p and the rest; those rules are in the decoding strategies post
- Per round: at least 1 token (the first guess misses, and the big model's own token at position 1 is used), at most k+1 (all k match, and the last row gives one more for free)
The cost (made-up numbers)
- The big model takes 30 ms a round, the small model 10 ms for 5 guesses; counting 6 verification rows the same as 1, a speculative round takes 40 ms
| Case | Round time | Tokens | Plain decode for the same tokens |
|---|---|---|---|
| All 5 match | 40 ms | 6 | 180 ms |
| 2 match | 40 ms | 3 | 90 ms |
| First guess misses | 40 ms | 1 | 30 ms |
- No matches is the worst case: 40 ms for 1 token, slower than plain decode
- Two conditions for a speed-up:
- The big model is in the "slow to move, fast to compute" regime: decode with a small batch. Once the batch is large and the GPU's compute is saturated, the extra verification rows stop being free, and the gain shrinks or turns negative
- The small model guesses well: set phrases and code boilerplate are easy hits
How it connects to extend
- Verification is one extend of length k+1 on the big model (a prefill with
cached_len > 0) - It's the same mechanism as the second chunk of a chunked prefill in part 2: the KV cache already holds the earlier text, a short run of new tokens goes in, and the pass reads the old KV while writing the new
- mini-sglang doesn't implement speculative decoding. Full SGLang does, with EAGLE-style methods
- EAGLE's draft is a very light head attached to the big model, which guesses from the big model's features just before the LM head