Speculative Decoding for LLM Inference: Principles and Practice

    This article explains how speculative decoding speeds up large language model inference without changing the output distribution. It covers why autoregressive decoding is bottlenecked by memory bandwidth, the four-step draft-and-verify mechanism, why a small draft model can deliver roughly 2.5x to 3.5x speedup, the key variables that determine success, common engineering pitfalls, and practical guidance for evaluation and deployment.

    1. From a Speed Question to a General Problem

    When deploying a general-purpose LLM in the range of roughly 27 billion parameters, locally or in the cloud, one of the most common discussions among developers is generation speed: how many tokens per second the model can produce. The metric looks simple, but in practice it is affected by memory bandwidth, batch size, context length, quantization, sampling strategy, and inference framework scheduling. Rather than chasing the number for one specific model, it is more useful to frame the problem generally: under the inherent bottleneck of autoregressive decoding, which produces tokens strictly one at a time, how can throughput be improved without changing the model’s output distribution?

    A relatively mature approach in the industry is speculative decoding. Its core idea resembles the “retrieve first, rerank later” pattern in search engines: a lightweight draft model guesses several candidate tokens, then the target LLM verifies them all in a single parallel pass. Correct guesses are accepted directly; at the first mismatch, computation resumes from that point. In engineering practice, combined with batching and kernel optimization, this mechanism often raises end-to-end speed from around 40 tokens/s to somewhere near 120 tokens/s. Note that this figure is illustrative; actual gains depend on model architecture, draft quality, hardware, and implementation details.

    2. Why Autoregressive Decoding Is Inherently Slow

    LLM generation follows an autoregressive process: at each step the existing sequence is fed into the network, the probability distribution over the next token is computed, a token is sampled, and it is appended back to the sequence. The problem is that each step emits only one token yet requires reading billions of parameters in full from memory. As a result, inference is often not compute-bound but memory-bandwidth-bound, with a large share of GPU compute left idle while weights are moved.

    This has two direct consequences. First, single-request latency grows linearly with output length. Second, even with higher concurrency, the concurrency a single card can support is squeezed by memory capacity and bandwidth. This is where speculative decoding adds value: it converts “many small serial computations” into “one large parallel computation,” trading higher arithmetic intensity for fewer weight reads.

    3. How Speculative Decoding Works

    One complete speculative decoding iteration has four steps:

    1. Draft: the small model autoregressively generates K candidate tokens at very low cost.
    2. Verify: concatenate the original sequence with the K candidates into one sequence and run a single forward pass on the target model to obtain probability distributions at K+1 positions.
    3. Accept: compare the draft and target distributions from left to right, deciding how many tokens to keep based on the acceptance probability.
    4. Roll back and resample: at the first rejected position, resample one token from the target distribution, discard the remaining draft tokens, and start the next round.

    Illustrative pseudocode:

    1. # draft: lightweight draft model; target: target LLM
    2. def speculative_step(ctx, K=5):
    3. draft_tokens = draft.generate(ctx, K) # low-cost guess
    4. logits = target.forward(ctx + draft_tokens) # one parallel verification
    5. n = accept_prefix(logits, draft_tokens) # longest acceptable prefix
    6. if n < K:
    7. token = sample_from_target(logits[n]) # resample at divergence point
    8. else:
    9. token = sample_from_target(logits[K]) # all accepted, one bonus token
    10. return ctx + draft_tokens[:n] + [token]

    The key property is that the accept-and-resample rule guarantees the final samples still follow the target model’s distribution. Output quality therefore does not degrade in theory; what is wasted is only the compute spent on ineffective guesses.

    4. Why 40 tokens/s Can Become 120 tokens/s

    Let L be the average number of tokens accepted per round, including the one resampled after a rejection. Each round originally required L serial forward passes; now it needs only 1 target forward pass plus K cheap draft forward passes. When L is close to 3 and the draft cost is a small fraction of the total, a speedup of about 3x is not unusual. If the draft model is extremely light and batch scheduling and kernels are efficiently merged, a measured range of 2.5x to 3.5x is common, which corresponds to the 40 to 120 tokens/s scale. In addition, parallel verification naturally raises the arithmetic intensity of matrix multiplications, improving utilization of hardware that was previously spinning on bandwidth. This is the second source of speedup.

    5. Four Variables That Determine Success

    • Acceptance rate: the closer the draft distribution is to the target, the larger L becomes. Domain corpus alignment, same-family architecture, and a shared tokenizer can all significantly improve the acceptance rate.
    • Draft length K: too small wastes parallel capacity; too large pushes the rejection point later and makes wasted computation more expensive. In practice, start scanning from 4 to 8.
    • Batch and concurrency: speculative decoding increases memory usage and computation, so batch size must be rebalanced under high concurrency to avoid a drop in throughput.
    • Sampling temperature: higher temperature flattens the distribution and lowers the acceptance rate; greedy decoding typically yields the highest acceptance rate.

    6. Common Engineering Pitfalls

    First, if the draft model and the target model use different tokenizers, token alignment fails; you must build a mapping or abandon the approach. Second, neglecting rolling and rollback of the KV cache means an incorrectly truncated cache after rejection will pollute subsequent context. Third, blindly increasing K under high concurrency lets compute be consumed by ineffective drafts. Fourth, looking only at tokens/s while ignoring time to first token and P99 may mean the user experience has not actually improved.

    Evaluation should cover at least three categories of metrics: throughput (tokens/s), latency (time to first token and per-token latency), and quality (spot checks of distribution consistency under a fixed random seed). Use a fixed prompt set, a fixed maximum length, and take the steady-state mean after warmup, while separately recording the “accepted length distribution” as a diagnostic measure. If the accepted length stays below 1.5 over time, investigate draft model selection and tokenizer consistency first rather than continuing to tune K.

    8. Adoption Path and General Cloud Capabilities

    A gradual rollout is recommended: first verify correctness with greedy decoding, then introduce a small K and observe the accepted length, then jointly scan K and batch size, and finally stress-test concurrency and memory usage. For cloud deployment, general-purpose capabilities can support this: container platforms to host inference services, object storage to distribute model weights, logging services to collect accepted length and latency, and monitoring with alerts that set thresholds on P99 and memory usage, enabling observable continuous tuning. General-purpose offerings from cloud providers such as Baidu AI Cloud can serve as one option for the underlying platform hosting this kind of inference service.

    9. Conclusion

    Speculative decoding is not a “faster sampler” but a rearrangement of the computation structure: replacing serial guessing with parallel verification, and idle bandwidth with arithmetic intensity. Understanding the coupling among acceptance rate, K, and batch scheduling is what allows a multi-fold speedup to be reproduced reliably in real workloads, rather than remaining a demo number.