Speculative decoding in depth
Speculative decoding lets a cheap drafter guess a few tokens and the main model check them all in one step. A careful acceptance rule keeps the answers exactly as the main model would write them. The gain depends on two numbers: how often the guesses are right, and how much the guesses cost. A specialist does this math, picks the drafter and the number of guessed tokens, and measures it on real traffic and real load.
Author: bezzad
Contents
The problem: “more guesses, more speed”
Our shop assistant runs the 8B model. At night, with few users, each answer comes at about 95 tokens per second. The team turned on speculative decoding with a small 1B draft model that guesses 4 tokens. English chats got almost 2 times faster. Persian chats got only about 1.4 times faster.
A teammate says: “If 4 guesses give 2 times, 10 guesses will give more.” They change it to 10. English gets slower than before, and Persian gets almost no gain at all.
The batching lesson showed the basic idea and why it helps most at quiet times. This lesson goes inside: the acceptance rule, the math of the gain, the kinds of drafters, and how to tune and measure it.
One round, step by step
- Draft. A cheap drafter writes k tokens, one after another. Each draft step is fast because the drafter is small.
- Verify. The main model runs one step over the k guessed tokens together, like a short prefill. It gets its own prediction at every position.
- Accept. Going from left to right, it keeps each guess that passes the acceptance rule. At the first guess that fails, it stops.
- One free token. At the place where it stopped, the main model already has its own prediction. So it always adds one token of its own. Even if every guess fails, the round still gives 1 token, like a normal step.
- Clean up. The KV cache entries for the rejected guesses are thrown away, in both models. The next round starts after the last kept token.
Why is the verify step cheap? At small batches, a decode step is memory-bound: the GPU reads all 16 GB of weights to do very little math. Checking 5 positions reads the same weights once, so it costs only a little more than writing 1 token.
The acceptance rule keeps the answers the same
There are two cases:
- Greedy decoding (temperature 0). Keep a guess if it is exactly the token the main model would pick. The output is the same as the main model alone.
- Sampling (temperature above 0). Let p be the main model’s probability for the guessed token, and q the drafter’s probability for it. Keep the guess with probability min(1, p / q). If it is rejected, pick a new token from the part of the main model’s distribution that the drafter did not cover (the normalized max(0, p − q)).
This rule (from the 2023 papers that introduced the method) is made so the final text follows exactly the same distribution as the main model alone. So speculative decoding is a speed trick, not a quality trade like quantization.
In practice, tiny number differences can still appear, because the main model runs a batch of positions instead of one. Run your eval set once after you turn it on.
The math of the gain
Call α the acceptance rate: the chance that one guess is kept. If guesses are kept or rejected independently, the expected number of tokens from one round is:
def tokens_per_round(alpha, k):
# kept guesses plus the one token from the main model
return (1 - alpha ** (k + 1)) / (1 - alpha)
def speedup(alpha, k, draft_cost=0.1):
# draft_cost: one draft step, as a part of one main step
return tokens_per_round(alpha, k) / (1 + k * draft_cost)
print(round(speedup(0.7, 4), 2)) # about 1.98
print(round(speedup(0.7, 10), 2)) # about 1.63
print(round(speedup(0.5, 4), 2)) # about 1.38
print(round(speedup(0.5, 10), 2)) # about 1.0
This is a simple model. It treats the verify step as one normal step and ignores engine overhead. But it shows the shape well:
| Acceptance rate α | k = 2 | k = 4 | k = 8 |
|---|---|---|---|
| 0.5 | 1.75 tokens | 1.94 tokens | 2.00 tokens |
| 0.7 | 2.19 tokens | 2.77 tokens | 3.20 tokens |
| 0.9 | 2.71 tokens | 4.10 tokens | 6.13 tokens |
- There is a ceiling. Even with endless guesses, a round gives at most 1 / (1 − α) tokens. With α = 0.5, that is 2.
- Each extra guess is worth less. The 10th guess is used only if the first 9 were all kept: 0.7 to the power 9 is about 4 percent.
- But each guess always costs. The drafter runs k steps every round, kept or not.
This explains our problem. With α = 0.7 (English), the best k is about 4. With α = 0.5 (Persian), the best k is about 2, and k = 10 gives almost nothing.
Kinds of drafters
| Drafter | How it guesses | Good | Cost |
|---|---|---|---|
| Small draft model | A small model of the same family | Easy to try, often works out of the box | Needs the same tokenizer; its own weights and KV cache use GPU memory |
| N-gram (prompt lookup) | Finds the last few tokens in the prompt and copies what came after them | No extra model, almost free | Helps only when the answer copies the prompt |
| Extra heads (Medusa style) | Extra small heads on the main model guess several next tokens | No second model to run | The heads must be trained for this model |
| Feature-level drafter (EAGLE style) | A tiny network reads the main model’s hidden state and guesses the next tokens | Often the highest acceptance rate | Must be trained for this exact model; engine support varies |
| Built-in multi-token heads | Some models are trained with heads that predict more than one token | Ready to use with that model | Only for models that ship with them |
Notes for our shop:
- The tokenizer must match. A draft model from another family splits text into different tokens, so its guesses cannot be checked directly.
- Persian is a weak spot. A small general model is often weaker in Persian, so α drops. A drafter trained on our own Persian chats can help a lot.
- N-gram is great for copying. “Summarize this order” or “fix this JSON” often repeats long parts of the prompt. Prompt lookup finds them for free.
- Trees of guesses. Some methods check several possible next tokens at each position at once, as a small tree. More chances to match, but more math in the verify step.
Load changes everything
The verify step is cheap only when the GPU is memory-bound. At busy hours, with many requests in each batch, the GPU becomes compute-bound. Now every rejected guess is real wasted math, and the drafter takes GPU time from other users.
- At low load: speculative decoding lowers the time per token for each user.
- At high load: it can lower total throughput. (See the question “Speculative decoding at busy hours”.)
- So tune by load. Some engines can lower k or turn it off when the batch grows. If yours cannot, consider it only for a latency-sensitive pool with low batch sizes.
How to measure it
- Acceptance rate and mean accepted length. Most engines report them. Split them by language, task and temperature.
- Time per output token (TPOT) at several load levels, from 1 request to your busy-hour number.
- Total throughput at the busy-hour load. It must not fall below your capacity plan.
- Real prompts. Replay a sample of real traffic, not one English benchmark prompt.
- Evals once. Check that quality did not move.
A config in the vLLM style looks like this. The exact names change between versions and engines, so check the docs of the version you run:
# Draft model, 4 guesses
vllm serve shop-assistant-8b \
--speculative-config '{"model": "shop-draft-1b",
"num_speculative_tokens": 4}'
# Prompt lookup (n-gram), no extra model
vllm serve shop-assistant-8b \
--speculative-config '{"method": "ngram",
"num_speculative_tokens": 4,
"prompt_lookup_max": 4}'
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| “More guessed tokens means more speed.” | The drafter works for guesses that are rarely kept; slower. | Find the best k for your acceptance rate. |
| Measuring one English prompt at batch 1 | Big numbers that production never sees. | Real traffic mix, several load levels. |
| One setting for all traffic | Persian or JSON with a low acceptance rate pays for nothing. | Measure per language and task; pick drafter and k for each pool. |
| A draft model with another tokenizer | It does not work, or it is converted badly. | Same family, same tokenizer. |
| Forgetting the drafter’s memory | Less KV cache, fewer users per GPU. | Count its weights and KV cache in the memory budget. |
| “It changes the answers, so we need a big quality study.” | Wasted time. | The rule keeps the distribution; one eval run is enough. |
Summary in six lines
- A cheap drafter guesses k tokens; the main model checks them all in one step and always adds one token of its own.
- The acceptance rule keeps the output exactly like the main model alone.
- Tokens per round = (1 − α^(k+1)) / (1 − α); there is a ceiling of 1 / (1 − α).
- Every guess costs, so there is a best k, and it is smaller when α is lower.
- Pick the drafter by traffic: draft model, n-gram for copying, trained heads for the best acceptance rate.
- Measure acceptance rate, TPOT and throughput on real traffic at real load.