
Speculative Decoding: 2x Throughput for Free?
An examination of speculative decoding techniques that offer up to 2x throughput improvements for large language models by leveraging draft models and acceptance rates.
Large language model (LLM) inference is typically bound by memory bandwidth, not compute. In standard autoregressive decoding, the model generates one token at a time. For each token, the entire context window must be processed to compute the next probability distribution. This serialization means that doubling the sequence length does not double the latency, but it does linearly increase the time between tokens (TTFT and Inter-Token Latency). Speculative decoding (SD) attempts to break this serialization bottleneck by allowing the system to predict multiple tokens ahead, verify them in a single batched pass, and discard invalid ones.
The Bottleneck: Serialized Autoregression
Consider a standard LLM inference loop. The input is a sequence of tokens . To generate , the model computes:
This computation is performed sequentially. Even if the GPU is idle after processing , it cannot start computing until is fully generated. This creates a "latency wall" where the throughput (tokens per second) is capped by the time required to process one token through the full transformer layers.
The Speculative Hypothesis
Speculative decoding introduces a second, smaller model—the draft model—to hypothesize the next tokens. The draft model is typically a smaller version of the target LLM (e.g., a 7B parameter model drafting for a 70B parameter model). Because the draft model is smaller, it generates tokens significantly faster than the target model.
The core insight is that the output distribution of a small model often closely approximates that of a large model. This approximation holds because smaller models trained on the same data share similar linguistic patterns and structural priors, even if they lack the capacity for rare or complex reasoning. If the draft model can accurately predict the next few tokens, the large model does not need to compute them from scratch. Instead, it only needs to verify them against its more precise distribution.
The Mechanism: Draft and Verify
The speculative decoding process operates in a loop with two distinct phases:
1. Drafting Phase
The draft model generates tokens autoregressively. Let the draft model be and the target model be . Given the current context , generates a sequence of draft tokens:
where is the -th draft token. This phase is still sequential but much faster because has fewer parameters and less compute per layer.
2. Verification Phase
The target model takes the original context concatenated with the draft sequence as input. Crucially, performs a single forward pass on the combined sequence . While this processes all draft tokens simultaneously in terms of kernel execution, it is not "parallel" in the sense of independent computation. The causal attention mask ensures that the prediction for each token depends strictly on the previous tokens . This is a batched computation that amortizes the cost of the target model's heavy layers across multiple tokens.
For each draft token , computes its own probability distribution . The verification uses a rejection sampling criterion to decide whether to accept or reject each draft token.
Acceptance Criteria and Rejection Sampling
The acceptance criterion, first formalized by Leviathan et al. in their 2023 paper "Fast Inference from Transformers via Speculative Decoding," ensures that the final output distribution remains identical to what would have been produced by sampling from alone. This is achieved through a Metropolis-Hastings correction mechanism. The formula is a simplified representation of this correction, which adjusts for discrepancies between the draft and target distributions to maintain statistical correctness.
For each draft token , let be the probability assigned by the target model, and be the probability assigned by the draft model. The token is accepted with probability:
If the token is accepted, it is added to the output sequence. If rejected, the target model samples a new token from its own distribution, and the speculative chain breaks. Any subsequent draft tokens are discarded.
Example Scenario
Imagine a draft model predicts the next token is "cat" with probability 0.8. The target model assigns "cat" a probability of 0.9. The acceptance probability is . The token "cat" is accepted.
Now, imagine the draft model predicts "dog" with probability 0.5, but the target model assigns "dog" only 0.3. The acceptance probability is . The system rolls a random number; if it is less than 0.6, "dog" is accepted. If not, "dog" is rejected, and the target model samples a replacement token (say, "bird"). The speculative sequence terminates at "bird".
Throughput Analysis: Why 2x?
The theoretical speedup of speculative decoding depends on the acceptance rate () and the number of draft tokens ().
Let be the time to generate one draft token with the small model, and be the time to generate one token with the large model. In standard decoding, generating tokens takes .
In speculative decoding with draft tokens:
- Drafting takes .
- Verification takes (batched forward pass).
- If all tokens are accepted, the total time is .
The speedup is:
If (the draft model is much faster) and (high acceptance rate), the speedup approaches . In practice, is not zero, and acceptance rates drop as the draft deviates from the target. However, even with moderate acceptance rates, using or draft tokens can yield 1.5x to 2x throughput improvements.
Tradeoffs and Limitations
Speculative decoding is not free. It introduces several overheads that must be balanced against the throughput gains:
- Compute Cost of Verification: The primary computational cost is the target model's forward pass on the extended sequence. While this is amortized over tokens, it is still the dominant expense. If the draft model is not significantly faster, the drafting phase becomes a bottleneck rather than a help.
- Memory Management Overhead: Managing the KV cache for the draft tokens during verification increases memory usage. This is distinct from the computational cost; it involves allocating memory for the draft tokens' keys and values and potentially causing cache misses if the sequence length grows too large or if the memory footprint exceeds the GPU's capacity.
- Compute Imbalance: If the draft model architecture is similar to the target (e.g., same attention heads but fewer layers), the drafting phase may not be fast enough to offset the verification cost. The ideal draft model is one that can generate tokens in a fraction of the time of the target model.
- Variability: The variable number of accepted tokens leads to non-deterministic latency. While average throughput improves, tail latency may worsen if many drafts are rejected, causing pipeline stalls.
Conclusion
Speculative decoding achieves throughput improvements by shifting the computational load from the expensive target model to a cheaper draft model, while leveraging a single batched forward pass to amortize the cost of the target model's verification. The 2x speedup is not guaranteed; it is a function of the draft model's accuracy (acceptance rate) and the relative speed difference between draft and target models. For ML engineers, optimizing the draft model selection and tuning the number of draft tokens are critical levers for maximizing inference efficiency.
Common Pitfalls
- Ignoring Memory Limits: Selecting a draft model that is too large or setting too high can exhaust GPU memory, leading to OOM errors or swapping, which destroys performance.
- Mismatched Architectures: Using a draft model with a fundamentally different architecture (e.g., RNN vs. Transformer) can lead to poor acceptance rates because the latent spaces do not align well.
- Overlooking Tail Latency: Optimizing only for average throughput can hide spikes in latency caused by frequent rejections, which is detrimental for interactive applications requiring consistent response times.
Practical Takeaways
- Draft Model Selection: Prioritize draft models that are significantly smaller and faster than the target, even if their raw accuracy is slightly lower, as long as the acceptance rate remains high.
- Dynamic Tuning: Consider implementing dynamic draft lengths that adjust based on the current acceptance rate, increasing when the draft is confident and decreasing it when confidence drops.
- Benchmarking: Always measure both average throughput and tail latency (p99) in your specific hardware environment, as theoretical speedups rarely match real-world performance due to system overheads.
FAQ
Q: Does speculative decoding affect the quality of the generated text? A: No. The acceptance criteria ensure that the final output distribution is mathematically identical to sampling directly from the target model. The quality remains unchanged.
Q: Can I use any small model as a draft? A: Ideally, the draft model should be trained on the same data as the target model. Models fine-tuned or distilled from the target usually yield higher acceptance rates.
Q: Is speculative decoding suitable for very short sequences? A: It is most effective for longer sequences where the overhead of the draft model is amortized over many tokens. For very short sequences, the overhead may outweigh the benefits.
Related posts
SGLang's RadixAttention, Explained
An examination of SGLang's RadixAttention mechanism, focusing on prefix caching, radix trees, and throughput optimization for ML infrastructure.
Medusa, EAGLE, and Lookahead Decoding Compared
A technical comparison of Medusa, EAGLE, and Lookahead Decoding methods for accelerating large language model inference.
Multi-Head, Grouped-Query, and Multi-Head Latent Attention
An examination of Multi-Head Attention, Grouped-Query Attention, and Multi-Head Latent Attention variants, focusing on KV cache size optimization and inference speed.