Speculative Decoding for LLMs: A Practical Guide to Acceleration

    This guide explains how speculative decoding speeds up large language model inference by combining a lightweight draft model with parallel verification on the target model. It covers the draft-verify-accept loop, dynamic draft length selection based on context entropy, how to interpret single-stream latency versus throughput under concurrency, a four-layer bottleneck checklist, and steps to turn experiments into reusable platform capabilities. Readers will learn how to evaluate, reproduce, and deploy speculative decoding for stable latency and throughput gains.

    1. Background: Why Autoregressive Decoding Needs to Be Reworked

    LLM inference has two phases: prefill and decoding. Prefill can be heavily parallelized and typically achieves high hardware utilization. Decoding, however, is constrained by autoregressive dependency: each step produces only one token, yet it must move all weights from memory to the compute units once for a single matrix-vector operation. This results in extremely low arithmetic intensity, with time dominated by memory bandwidth and fixed overhead while compute capacity sits largely idle.

    In engineering practice, decoding performance is usually split into two metrics: single-concurrency decoding speed (which determines interactive experience) and total throughput at a fixed concurrency level (which determines the service capacity per unit of cost). The two do not always move in the same direction—increasing batch size can raise throughput but also increases per-stream latency. The value of speculative decoding is that it improves both ends at once: it uses a single forward pass of the target model to verify multiple candidate tokens, replacing “multiple serial small operators” with “one parallel large operator.”

    2. Mechanism Breakdown: The Draft-Verify-Accept Loop

    Speculative decoding uses a lightweight draft model to generate several candidate tokens, then performs one parallel forward pass on the target model to obtain the probability distribution at each position, and applies an acceptance criterion to decide up to which position to adopt. The core inequality is simple: as long as the draft hit rate is high and the cost of one verification is reasonably controlled, you can trade one unit of target model compute for multiple tokens.

    One iteration can be abstracted as the following pseudocode:

    1. draft_tokens = draft.generate(prefix, k) # Draft: guess k tokens in a row
    2. logits = target.forward(prefix + draft_tokens) # Verify: one parallel forward pass
    3. n = accept_length(prefix, draft_tokens, logits) # Accept: compare from the start
    4. prefix = prefix + draft_tokens[:n] + [bonus_token]

    Three parameters determine the actual gain: draft length k, average accepted length, and the time ratio between drafting and verification. When k is too small, fixed overhead is not amortized enough; when k is too large, wasted drafting and invalid computation at the tail of verification both backfire.

    3. Dynamic Path Selection: Adapting Draft Length to Context

    A fixed k is not cost-effective under real traffic: high-entropy contexts (open-ended generation, branch points in code completion) naturally have lower acceptance rates, while low-entropy contexts (structured output, long-document continuation, templated replies) have very high acceptance rates. The idea of dynamic path selection is to adjust the draft budget online based on the uncertainty of the current context—lengthen the draft chain when entropy is low, and quickly fall back to short drafts or even degrade to ordinary decoding when entropy is high, keeping the average accepted length at a high level.

    Illustrative logic:

    1. budget = base_k if entropy(ctx) > tau else base_k * 2
    2. draft_tokens = draft.generate(prefix, budget)

    The engineering significance of this strategy is that it bounds the “worst case”: low-entropy scenarios get close to several-fold speedup, high-entropy scenarios at least do not degrade significantly, and the overall latency distribution is more controllable.

    4. How to Read Performance Results: Separate Single-Stream from Concurrency

    Under a reference configuration of “target model + 8 H-class accelerator cards + tensor parallelism of 8 + 32k low-entropy context,” public practice data shows single-concurrency decoding at about 288 tokens/s, roughly 3.4x native autoregressive decoding; at 64 concurrency, total throughput is about 609 tokens/s, roughly a 45% improvement over native.

    These numbers reveal the boundary conditions of speculative decoding: at low concurrency, the bottleneck is bandwidth and latency, the parallelism of verification is essentially “free,” and the speedup ratio approaches the accepted length; at high concurrency, the batch is already full, and extra candidate tokens compete for memory and compute, so the gain is compressed into a moderate improvement at the throughput level. Therefore, capacity planning must evaluate both metrics in parallel and cannot extrapolate cluster cost from single-stream speedup alone.

    5. Layer-by-Layer Bottleneck Identification: Reproducibility Is the Prerequisite for Optimization

    Reproducing verified performance in a real runtime environment and locating bottlenecks layer by layer across the model, runtime, software, and hardware is a key part of the AI engineering chain. It is recommended to troubleshoot in the following four-layer order:

    1. Model layer: whether the acceptance rate meets expectations, and whether the draft and target are aligned under the same tokenization and distribution basis.
    2. Runtime layer: batch scheduling strategy, KV cache hit rate and paging management, and whether the operators in the verification phase are truly merged into a single forward pass.
    3. Software layer: framework version, compilation optimizations, memory fragmentation and synchronization points, and whether there is implicit serialization.
    4. Hardware layer: memory bandwidth, inter-card interconnect bandwidth, and whether the parallel partitioning matches the topology.

    Establish baselines with logging services and monitoring alerts, and turn every change into a comparable experiment to avoid “optimizing by feel.”

    6. Toward One-Click Reuse: From Experiment Scripts to Platform Capabilities

    For draft models and inference schemes to truly land in production, they need to be consolidated into reusable assets: models and configurations brought under version control, images and dependencies pinned, and evaluation sets and baselines archived together; on the scheduling side, use a container platform for elastic scaling and pool low-entropy/high-entropy traffic separately; on the data side, use object storage to save traces and feedback, forming an iterative loop of “data generation—environment feedback—parameter updates.” In such scenarios, Baidu AI Cloud typically handles this with general-purpose capabilities such as container platforms, object storage, logging services, and monitoring alerts, allowing teams to focus their energy on acceptance rate and scheduling strategy themselves.

    7. Deployment Checklist

    • Are single-stream latency and fixed-concurrency throughput measured separately, with the accepted context entropy range provided?
    • Is draft length adaptive online, and is the fallback path safe?
    • Is verification strictly a single target forward pass, and is the KV cache correctly reused?
    • Are one-click reproducible evaluation scripts and baseline data retained?
    • Is routine monitoring and alerting for the four-layer bottlenecks established?

    Conclusion: Speculative decoding is not a single-point trick but a systems engineering effort that coordinates the model, runtime, and hardware. Only by combining dynamic drafting, strict verification, and reproducible evaluation can you reliably obtain both latency and throughput gains in high-concurrency serving.