Speculative Sampling: How to Double LLM Inference Speed
This article explains why autoregressive generation makes large language model inference slow—especially the memory-bandwidth bottleneck—and how speculative sampling breaks the serial decoding limit with a "draft-then-verify" approach. It covers the core mechanism, why outputs remain distributionally identical to direct generation, where the speedup comes from, and key engineering optimizations such as draft model alignment, KV cache rollback, dynamic speculation length, tree-based candidates, and operator-level tuning, along with practical deployment advice for teams running mid-sized models on their own infrastructure.
1. Background: Why LLM Inference Is Slow
The autoregressive nature of large language models creates an inherent inference bottleneck: generating each token requires a complete forward pass. For a model with around 27B parameters, a single forward pass involves billions of floating-point operations, yet produces only one token—leaving most of the GPU’s parallel compute capacity idle.
Worse, inference is constrained far more by memory bandwidth than by compute. During generation, batch sizes are typically small, so the time spent loading weights from VRAM to compute units far exceeds the actual computation time. This is the classic memory-bound scenario. The key to faster generation is therefore not simply adding more compute, but extracting more useful output from every weight load.
Community discussions like “how many tokens/s are people actually getting on a ~27B model” stay hot for exactly this reason: inference speed directly determines both user experience and serving cost. Naive inference often yields only tens of tokens per second, while optimized setups reaching 100+ tokens/s represent a qualitative leap for real-time chat, code completion, and similar workloads.
2. Speculative Sampling: Breaking the Serial Bottleneck with “Guess and Verify”
2.1 How It Works
The core idea of speculative sampling fits in one sentence: let a small model quickly guess several tokens, then have the large model verify them all in parallel. The workflow:
- Draft phase: A small draft model autoregressively generates n candidate tokens. Its forward passes are extremely cheap, so these n steps cost little.
- Verification phase: Concatenate the n candidates with the context and run a single forward pass through the target model. Because Transformers compute over the entire input in parallel, this one pass yields the probability distributions at all n+1 positions simultaneously.
- Acceptance check: Compare the draft predictions against the target model’s distributions position by position. Accepted tokens are kept; once a position is rejected, that position and everything after it are discarded, and the target model resamples at the rejection point.
- Free bonus: Even when all n candidates are accepted, the verification forward pass produces the (n+1)-th token for free—an extra step gained at no cost.
2.2 Why the Output Matches Direct Generation
Speculative sampling is not a lossy approximation. With a carefully designed accept-reject sampling scheme that corrects each candidate using both the draft and target distributions, it can be rigorously proven that the final output distribution is identical to direct autoregressive generation by the target model. This is what distinguishes it from earlier “greedy guessing” approaches, and it is the mathematical foundation for its broad adoption in production systems.
2.3 Where the Speedup Comes From
The speedup is the combined effect of acceptance rate and the draft-to-target speed ratio:
- If the draft model’s distribution closely matches the target’s (e.g., a smaller model distilled from the same family or trained on the same data), 3–5 tokens are accepted per round on average.
- The draft model is typically several to tens of times faster than the target.
- Verifying n+1 tokens in one forward pass takes almost the same time as processing a single token—the memory-bound nature of decoding works in our favor here.
Combined, throughput can improve several-fold in ideal conditions. The referenced improvement from roughly 40 tokens/s to nearly 120 tokens/s is a typical real-world result of combining these techniques—close to a 3x speedup.
3. Engineering-Level Optimizations
Speculative sampling is only the framework; production deployment requires several system-level optimizations:
1. Draft model selection and alignment. The draft model must balance “small and fast” against “close to the target distribution.” A common practice is to use a smaller model trained or distilled from the same family as the target to raise the acceptance rate. Every 10% gain in acceptance rate produces a noticeable end-to-end improvement.
2. Efficient KV cache management. When verification rejects tokens, the KV cache must be rolled back precisely to the rejection position to avoid redundant computation and cache corruption. Mature inference frameworks implement fine-grained cache reclamation and tree-structured attention, enabling verification of multiple candidate branches in one pass.
3. Dynamically adjusting speculation length. A fixed n is suboptimal: guess more when the acceptance rate is high, guess less—or fall back to plain generation—when it is low. Adjusting n based on a sliding window of recent acceptance rates lets the system adapt across tasks (code, dialogue, translation).
4. Tree-based candidate expansion. Rather than guessing a single chain, the draft model can generate a tree of candidates. A tree attention mask lets the target model verify multiple branches in a single forward pass, selecting the longest acceptable prefix and further increasing average tokens produced per round.
5. Low-level operator and scheduling optimizations. Combine speculative sampling with general inference optimizations—operator fusion, continuous batching, and paged memory management—to eliminate the overhead introduced by verification and prevent system bottlenecks from eroding the gains.
4. Practical Recommendations
- Measure the baseline first: Benchmark raw inference speed and time-to-first-token under a stable workload before optimizing, so you have a reference point.
- Acceptance rate above all: When choosing a draft model, prioritize distributional alignment with the target model over the draft’s raw speed.
- Mind task characteristics: Pattern-heavy tasks like code generation and structured text achieve high acceptance rates and large gains; highly open-ended creative writing benefits less.
- Integrate incrementally: Compare end-to-end latency with speculative sampling on and off in a test environment first, then roll out gradually, monitoring tail latency caused by rejection rollbacks.
5. Conclusion
By recalling candidates with a small model and verifying them in parallel with the large model, speculative sampling converts memory-bound serial decoding into a high-throughput guess-and-verify loop—without changing the output distribution. It is one of the most cost-effective inference acceleration techniques available today. Combined with cache management, dynamic speculation length, tree-based candidates, and other engineering optimizations, raising naive inference from tens of tokens per second to the 100+ tokens/s range is an entirely achievable goal. For teams self-hosting mid-sized models, this is a technical path well worth studying and putting into practice.
