Speculative Decoding: Fast LLM Inference Without Quality Loss › Worked Example: Step-by-Step Round
Language AI HandbookAll chapters Speculative Decoding: Fast LLM Inference Without Quality Loss › Worked Example: Step-by-Step RoundOne section · Sections in this chapter7 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let us trace through a complete speculative decoding round numerically to make the abstract procedure concrete. We will use a vocabulary of 5 tokens for simplicity and demonstrate both the acceptance and rejection cases.
Setup. Our vocabulary has tokens with IDs . We generate draft tokens. The target model's probability distribution over the next token given the current context is:
The draft model's distribution is:
Step 1: Draft generation. The draft model samples from and produces the sequence with the probabilities it assigned at each step: , (given A was sampled), (given A, B were sampled). For simplicity we treat each step as independent draws.
Step 2: Parallel verification. The target model processes in one forward pass. At each position it computes:
- Position of A:
- Position of B:
- Position of C:
Step 3: Accept/reject for token A. We compute . We accept token A with certainty because the target model assigned it higher probability than the draft model. The key insight is that when the target agrees or prefers the draft's choice, acceptance is guaranteed.
Step 4: Accept/reject for token B. We compute . We draw a uniform random number; suppose it comes up 0.72, which is less than 0.833, so we accept token B. Both A and B are now accepted.
Step 5: Accept/reject for token C. We compute . We accept token C with certainty.
Step 6: Bonus token. All three drafts were accepted. The target model also produced a logit at position after C during the forward pass, so we sample a bonus token directly from that distribution, obtaining token D (say, ). This token costs nothing extra.
Result of the round. We produced 4 tokens from a single target model forward pass (plus the cheap draft passes). Standard generation would have required 4 target model forward passes.
Rejection scenario. Now suppose the draft produced token B at position 1 with , but the target's probability at that position is . Then . If our random draw is 0.65 (above 0.333), we reject B. We compute the correction distribution:
normalized to give . We sample token A with probability 0.67 or token C with probability 0.33. Say we draw A. The round ends with only the accepted tokens from before position 1 plus the correction A. Future drafts will be generated from the corrected context.
This worked example shows both the happy path (high acceptance, multiple tokens per round) and the rejection path (context correction, minimal waste). Even in the rejection case, we still generate one correct token from the target distribution, just as standard generation would have.