Speculative Decoding is an inference acceleration technique where a small draft model rapidly generates multiple candidate tokens, which a large model verifies in batch — achieving 2-4x speedup for large language models without changing outputs through acceptance/rejection sampling.
Core Algorithm:
- Draft Model Generation: small, fast model (e.g., $1B$ parameters) predicts $\gamma $ tokens ahead ($\gamma =3-5$ typical) in single forward pass — takes 10-20ms on A100
- Batch Verification: large model (e.g., $70B$ Llama) verifies all $\gamma $ candidate tokens simultaneously in one forward pass — computes attention over draft sequence
- Token Acceptance: comparing large model logits $P_{\text{large}}(x_i)$ with draft logits $P_{\text{draft}}(x_i)$, accept token if $P_{\text{large}}(x_i) > P_{\text{draft}}(x_i)$ with probability adjustment — maintains exact output distribution
- Rejection Sampling: if token rejected, resampling from adjusted distribution $P_{\text{new}}(x) = \max(0, P_{\text{large}}(x) - P_{\text{draft}}(x)) / (1 - P_{\text{draft}}(x))$ — preserves correctness
Speedup Mechanism:
- Latency Reduction: expected speedup $\gamma_{\text{accept}} = \Sigma [i=1$ to $\gamma ] P(accept$ all $i$) where $P(accept_i) \approx $ 0.7-0.9 per token — typical speedup 2-3.5x
- Large Model Efficiency: amortizing one large model call across multiple tokens (similar to batch size $\gamma $) — reduces relative overhead of attention computation
- Draft Model Overhead: small model adds 5-10% latency (10-20ms) but saves 50-100ms from large model — net gain 40-90ms per iteration
- Cache Reuse: KV cache from large model verification enables streamlined next iteration — minimal redundant computation
Practical Implementation:
- Model Pairing: Llama $70B$ with Llama $7B$ draft model achieves $3x$ speedup with <0.1% accuracy change — commercial services deploy this pattern
- Medusa Framework: leveraging shared Llama backbone with lightweight head predictors (1.2% parameters) — achieves 2.3x speedup over naive decoding
- HuggingFace Integration: "Assisted Generation" API enabling drop-in replacement with any fine-tuned draft model — compatible with transformers library
- Threshold Tuning: adjusting acceptance threshold to balance speed (higher threshold = lower acceptance rate) — critical for different quality requirements
Advanced Strategies:
- Multi-Draft Ensemble: using 2-3 different draft models and averaging predictions before verification — improves acceptance rate to 0.92-0.95
- Adaptive Gamma: dynamically adjusting lookahead tokens $\gamma $ based on recent acceptance rates (increase if >0.8, decrease if <0.6) — auto-tuning for optimal throughput
- Prefix Sharing: caching draft model outputs for common prefixes in batch inference — 30-40% reduction in draft model compute
- Tree Attention: organizing draft proposals in tree structure enabling parallel verification of competing branches — enables 4-6x speedup with multiple valid continuations
Speculative Decoding is transforming inference economics — enabling production deployment of $70B$ parameter models on limited hardware while maintaining output quality through verification.