Speculative Decoding for LLMs: A Practical Guide to Faster Inference

    This guide explains how speculative decoding accelerates large language model inference and how to deploy it in high-concurrency production environments. It covers the core mechanism, semi-autoregressive generation, confidence-based verification scheduling, draft source selection, the relationship between acceptance rate and speedup, capacity testing methods, and common pitfalls, helping teams improve per-user generation speed without sacrificing overall throughput or output quality.

    1. The Problem: Inference Efficiency Bottlenecks Under High Concurrency

    The cost structure of large model online serving is fundamentally different from that of traditional web services. In a traditional service, the compute cost of a single request is roughly constant. In autoregressive decoding, however, every step requires a full forward pass, so generating N tokens means N sequential forward passes. This leads to two direct consequences. First, the generation speed a single user sees after the first token is limited by memory bandwidth rather than compute, leaving a large portion of the GPU’s compute units idle. Second, concurrent requests degrade into memory-bound tasks during the decoding phase, so the throughput gains from batching diminish quickly.

    Engineering teams are often squeezed by two metrics at the same time: the service side cares about total throughput per unit cost (tokens/s/GPU), while the user side cares about the generation speed of a single request (tokens/s/request). The two trade off against each other on the same hardware, and simply scaling out or switching to cards with more memory only stacks cost linearly. What is needed is an algorithm-level solution that raises the single-user experience speed without sacrificing overall throughput, and this is exactly where speculative decoding techniques provide value.

    2. How the Core Mechanism Works

    2.1 The Basic Paradigm of Speculative Decoding

    Speculative decoding rewrites “one token at a time” into “guess several at once, then verify once.” The process has three steps. In the draft stage, a lightweight module generates K candidate tokens in sequence. In the verification stage, the target large model performs one parallel forward pass over these K+1 positions to obtain the true distribution at each position. In the acceptance stage, rules based on distribution consistency decide the length of the prefix to keep, and any rejected position is immediately replaced with the correct sampling result from the target model. Because verification is a single parallel forward pass, its latency is close to that of a single decoding step, yet it may produce multiple tokens, effectively amortizing the memory access cost per token.

    The key is mathematical equivalence: as long as the acceptance and rollback rules are designed properly, the final output distribution is exactly identical to token-by-token sampling from the target model, and the speedup introduces no quality loss. This is also the first premise that must be stated when evaluating such solutions.

    2.2 Semi-Autoregressive Generation

    Pure autoregression is strictly sequential, while pure parallel generation collapses in quality. Semi-autoregressive generation is a compromise: it splits the output sequence into blocks of length G, predicting within a block in parallel and across blocks autoregressively. It naturally fits speculative decoding—the draft stage spreads candidates out in parallel within a block, and the verification stage compares the whole block at once. The block length G is the core hyperparameter: if G is too small, it degrades to token-by-token generation; if G is too large, the conditional dependencies within the block are weakened and the acceptance rate drops. It needs to be tuned by sweeping over the specific workload.

    2.3 Confidence-Based Verification Scheduling

    The naive approach of a fixed K is very inefficient under fluctuating load: when concurrency is low, compute is abundant, so K can be enlarged to trade for speed; when concurrency is high, compute is tight, and an overly large K squeezes the batching space and instead lowers total throughput. The idea behind confidence-based verification scheduling is to let the verification budget change dynamically with real-time state—based on the confidence distribution of draft tokens, the current batch size, queue wait time, and remaining GPU memory, it decides online how many tokens to guess and how wide to verify in the current round. In engineering practice, this usually takes the form of a constrained scheduler whose objective is to maximize single-user speed under the hard constraint that “total throughput does not fall below the baseline.”

    Illustrative logic (pseudocode, expressing scheduling intent only):

    1. def schedule(current_batch, gpu_mem_free, draft_conf):
    2. # Hard constraint: preserve total throughput; soft goal: raise single-request speed
    3. k = K_MAX
    4. if current_batch > BATCH_HIGH_WATERMARK:
    5. k = min(k, K_LOW)
    6. if gpu_mem_free < MEM_SAFE_MARGIN:
    7. k = min(k, K_LOW)
    8. # Low-confidence drafts are not worth guessing much
    9. k = min(k, count_tokens_above(draft_conf, TAU))
    10. return max(k, K_MIN)

    3. Key Points for Engineering Implementation

    3.1 Choosing a Draft Source

    The three mainstream approaches each have trade-offs. An independent small-parameter draft model is simple to implement but adds memory and operational costs. A lightweight head distilled from the target model usually achieves a higher acceptance rate and a more compact deployment. A draft-free self-speculation approach saves the extra weights but limits candidate quality. The choice should be based on the acceptance rate measured by replaying real online traffic, not on offline leaderboards.

    3.2 The Relationship Between Acceptance Rate and Speedup

    Let the average accepted length be L, then the ideal speedup is approximately L, while in practice it is discounted by verification overhead, scheduling jitter, and padding waste from uneven lengths within a batch. The improvement directions, ranked by cost-effectiveness, are: first, make the draft distribution close to the target distribution (distillation, temperature alignment); second, adapt K to request difficulty, guessing more for simple requests and less for complex ones; third, pool long and short requests separately to reduce in-batch waste.

    3.3 Capacity and Load Testing Methods

    A two-dimensional “throughput-speed” observation must be established, rather than looking at a single average. It is recommended to fix total throughput for an iso-throughput comparison and record the P50/P90 of single-user generation speed; at the same time, monitor acceptance rate, average verification width, draft proportion, and batch size distribution. Load testing should cover three concurrency levels—low peak, average peak, and burst—to verify that the scheduler does not break through the throughput floor at boundary watermarks.

    3.4 Working with General Cloud Capabilities

    Implementation usually does not involve special proprietary components, and a complete pipeline can be assembled with general categories: a container platform hosts the inference service and elastic scaling, object storage holds the model and draft weights, a message queue handles request peak shaving and priority-based routing, and logging services plus monitoring alerts provide dashboards visualizing acceptance rate, latency, and memory watermarks. In these scenarios, Baidu AI Cloud provides exactly these general infrastructure categories, making it easier for teams to decouple algorithm optimization from resource scheduling.

    4. Common Pitfalls

    The first is extrapolating the acceptance rate from a small offline sample directly to production, ignoring traffic distribution drift. The second is tuning only K without pooling requests, causing throughput to collapse under high concurrency. The third is neglecting numerical consistency verification and lacking regression tests for output distribution alignment. The fourth is treating the speedup ratio as the acceptance metric, when the real acceptance standard should be the improvement in user-side latency under an iso-throughput constraint.

    5. Conclusion

    The gains from speculative decoding come not from any single technique, but from the synergy of three things: the semi-autoregressive structure, confidence-based scheduling, and capacity-oriented load testing. If teams proceed along the pipeline described above, they can achieve a considerable improvement in user-side speed within the existing hardware budget while holding the two bottom lines of overall throughput and output quality.