Speculative Decoding
Using a small draft model to accelerate large model inference: the acceptance-rejection algorithm, theoretical speedup bounds, draft model selection, tree-based speculation, and Medusa multi-head decoding.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Autoregressive Bottleneck
- Speculative Decoding Algorithm
- Acceptance-Rejection Proof
- Expected Speedup Analysis
- Draft Model Selection
- Tree-Based Speculation (SpecInfer)
- Medusa: Multi-Head Decoding
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive the acceptance-rejection scheme for speculative decoding.
- Prove that speculative decoding produces samples from the exact target distribution.
- Compute the expected number of accepted tokens for a given acceptance rate.
- Explain tree-based speculation and its theoretical advantages.
- Describe Medusa's approach to self-speculative decoding.
Notation
- — target (large) model distribution
- — draft (small) model distribution
- — number of speculative tokens (draft length)
- — average acceptance rate:
- — per-token cost of target and draft models
Core Intuition
Autoregressive generation is slow because each token requires a full forward pass through the large model — sequentially, one at a time. But the large model can verify multiple tokens in parallel (same cost as generating one). Speculative decoding exploits this: a cheap draft model guesses tokens, and the large model verifies all in one pass, accepting those that match its distribution.
Speculative Decoding
The Autoregressive Bottleneck
Standard generation: Each step requires loading all model weights from HBM for a single token. For a 70B model in FP16:
- Weights: 140 GB loaded per token.
- At 2 TB/s bandwidth: ~70 ms per token minimum (memory-bound).
- Generating 1000 tokens: ~70 seconds.
Key observation: The large model processes batch-size-1 inputs inefficiently. Whether it processes 1 token or 8 tokens, the weights must be loaded once — so verifying 8 tokens costs roughly the same as generating 1.
Speculative Decoding Algorithm
Input: Target model , draft model , draft length , prefix tokens .
Algorithm:
-
Draft phase: Generate tokens from draft model: .
-
Verify phase: Run target model on all positions simultaneously: Compute for .
-
Accept/Reject: For each position :
- Sample .
- If : accept .
- Else: reject. Sample correction token from adjusted distribution. Stop.
-
Bonus token: If all tokens accepted, sample one additional token from at position .
Acceptance-Rejection Proof
Theorem. The output of speculative decoding is distributed exactly as .
Proof for position : The probability of outputting token is:
Case 1: . Draft proposes with probability , always accepted (ratio ). Rejection correction also samples with probability where .
Total: . Since (the total rejection mass), this equals .
Case 2: . Accepted with probability . Contribution: . Not sampled from correction (numerator is 0). Total: .
Expected Speedup Analysis
Expected accepted tokens per round: If each token has independent acceptance probability :
For : .
Tokens per round (including bonus): .
Speedup over standard decoding:
where cost per round = 1 target forward pass + draft forward passes.
Example: :
- Expected tokens: .
- Cost: .
- Speedup: .
Draft Model Selection
Requirements:
- Fast (much smaller than target): .
- Accurate (high ): distribution close to target.
- Same vocabulary as target.
Options:
- Smaller version of same family: LLaMA-7B drafting for LLaMA-70B (–).
- Quantized target: INT4 version of the same model (–).
- Distilled model: Trained to mimic target distribution.
- n-gram model: For highly predictable tokens (boilerplate, formatting).
Tradeoff: Better draft → higher → more accepted tokens, but also higher .
Tree-Based Speculation (SpecInfer)
Instead of a single draft sequence, generate a tree of possibilities:
Structure: At each position, the draft model generates top- continuations, forming a tree of width and depth .
Verification: The target model verifies all paths simultaneously (batch all leaf-to-root paths).
Advantage: Even if one path is rejected, another branch may be accepted. Higher expected acceptance for the same draft cost.
Expected tokens: Significantly more than linear speculation for the same total draft tokens generated.
Medusa: Multi-Head Decoding
Self-speculative decoding without a separate draft model:
Architecture: Add additional "prediction heads" to the target model. Head predicts the token at position given the hidden state at position :
Training: Only the heads are trained (target model frozen). Each head is a small MLP.
Verification: Generate candidates from all heads, form a tree, verify with one target forward pass.
Advantage: No separate draft model; heads add minimal latency; works with any base model.
Common Pitfalls
Pitfall 1. Using too large a draft model. If , the overhead of running the draft model erodes the speedup. Optimal when draft is 10–50x smaller.
Pitfall 2. Setting too high when is low. For : average accepted = 1 token regardless of . Large wastes draft compute.
Pitfall 3. Forgetting that speculative decoding is lossless. The output distribution is exactly — there's no quality-speed tradeoff (unlike quantization or pruning).
Summary
- Speculative decoding: draft tokens cheaply, verify in one target pass.
- Lossless: outputs exact target distribution (provably).
- Expected speedup: – for well-matched draft models.
- Tree-based speculation increases acceptance probability.
- Medusa: self-speculative with additional prediction heads.
- Optimal depends on : higher acceptance → larger worthwhile.
Exercises
Exercise 1. Compute the expected number of accepted tokens for and .
Exercise 2. Prove that the correction distribution is a valid probability distribution.
Exercise 3. For and a draft model 20x faster than target: find the optimal that maximizes tokens/second.
Exercise 4. Explain why speculative decoding provides more speedup for memory-bound inference than compute-bound inference.
Exercise 5. Design a tree-based speculation strategy with branching factor 3 and depth 4. How many candidate sequences are verified?