Speculative Decoding
Speculative decoding uses a small, fast language model to propose several next tokens, then asks a larger target model to verify those proposals in a single forward pass. Tokens the target accepts can be emitted without making the target model generate each one separately.
The result is lower autoregressive inference latency: the output still follows the larger model’s distribution, but the expensive model does less sequential work.
The problem: one expensive step at a time
A language model generates text autoregressively, meaning each new token depends on the tokens before it. To generate a continuation, a typical loop looks like this:
- Run the target model on the current context.
- Sample or choose one next token.
- Append it to the context.
- Run the target model again.
A token is a unit of text, such as a word fragment, punctuation mark, or space-plus-word fragment. The target model is often large and expensive to run, so this loop makes generation latency proportional to the number of output tokens.
Modern implementations cache intermediate attention data from earlier tokens, using a key-value cache so they do not recompute the entire context each time. That reduces work, but it does not remove the sequential dependency: the model still normally needs one target-model step for each newly generated token.
Speculative decoding attacks that dependency by predicting several likely steps in advance with a cheaper model.
Draft first, verify second
The system has two models:
- The target model is the large model whose behavior and output distribution must be preserved.
- The draft model is a smaller, faster model that guesses what the target might generate next.
Suppose the current context ends with The server returned. The draft model might produce a sequence such as:
an error status code
It generates these tokens one at a time, but because it is small, those several steps are relatively cheap. The target model then receives the original context plus the entire proposed continuation. With a causal attention mask—which prevents a position from seeing future tokens—it can calculate the next-token probability distribution for all proposed positions in one target-model forward pass.
Conceptually, the target checks:
- How likely was
anafterThe server returned? - How likely was
errorafterThe server returned an? - How likely was
statusafterThe server returned an error? - And so on.
The target is not blindly asked whether the whole string is acceptable. It evaluates each proposed token under the context that would exist if the earlier proposals were accepted.
If all four proposals are good enough, the system emits all four and starts the next draft from the resulting context. One expensive target pass has produced four output tokens instead of one.
If a proposal is rejected, later proposals cannot be used: they were drafted assuming the rejected token was present. The system keeps the accepted prefix, obtains a replacement token from the target, and begins another draft from there. A target pass often also provides a distribution for one additional token after the proposed block, allowing generation to continue immediately when every draft token was accepted.
Speculation saves time when the draft model is usually right enough, not when it is merely fast.
Why this can preserve the target model’s distribution
Simply replacing target-model sampling with draft-model sampling would change the result. The draft model has different probabilities, so it would produce different text distributions.
Speculative decoding instead uses the draft model as a proposal mechanism. For a proposed token, let q be the draft model’s probability and p be the target model’s probability for that token under the relevant context. The verifier accepts the proposal with probability:
min(1, p / q)
A random draw decides whether the token is accepted. If the target assigns at least as much probability as the draft did (p >= q), the token is accepted automatically. If the target considers it less likely, it is accepted only sometimes.
When a token is rejected, the system samples a replacement from the target’s leftover probability mass: for each token, it considers the positive difference between the target and draft probabilities, max(p - q, 0), and normalizes those differences into a distribution. This correction compensates for proposals that the draft model overestimated.
That accept-or-correct procedure is designed so that the final token has exactly the same conditional distribution as if the target model had generated it directly. The same argument applies at every position, so the complete generated sequence has the target model’s distribution—not merely a similar-looking distribution.
This distinction matters. A greedy implementation that accepts a draft token only when it matches the target’s highest-probability token can improve speed, but it is not the same guarantee as distribution-preserving speculative sampling. Likewise, using the draft model’s output without verification is ordinary approximate generation, not exact speculative decoding.
Where the speedup comes from
The target model’s computation is highly parallel across the proposed positions. It still respects token order logically, but hardware can process the block together. The draft model supplies several likely future inputs, turning several expensive sequential target steps into one larger target step.
The actual gain depends on several factors:
- Acceptance rate: more accepted draft tokens mean more target work replaced.
- Draft cost: a draft model that is too slow can consume the benefit.
- Draft length: longer blocks offer more potential savings but are more likely to contain a rejection.
- Hardware and batching: the target model must efficiently process the verification block.
- Workload: predictable text often speculates better than code, tool calls, or unusual prompts.
A system may therefore report metrics such as “accepted tokens per draft” or “acceptance rate.” Those numbers explain why the optimization helps—or why it has little effect for a particular request. In some workloads, the draft model’s overhead and rejected proposals make speculative decoding no faster than the ordinary one-token-at-a-time loop.
The central idea is simple: use a cheap model to predict the future, use the expensive model to check several predictions at once, and apply probability correction when a prediction is wrong. Done that way, speculative decoding reduces latency without changing what the target model means by a random generation.