LLM Inference Acceleration: How Speculative Decoding Works and How to Apply It

    This article explains why large language model inference is often bottlenecked by memory bandwidth rather than compute, and how speculative decoding turns serial autoregressive generation into a semi-parallel process. It covers the draft-and-verify workflow, the key variables that determine real-world speedups such as acceptance rate and draft length, practical engineering recommendations, and common misconceptions, showing how a 27B-class model can move from roughly 40 tokens/s to nearly 120 tokens/s without sacrificing output quality.

    1. Background: Why LLM Inference Stays Slow

    When developers discuss the generation speed of 27B-class large language models in local or cloud environments, usually measured in tokens/s, they are often puzzled: compute is clearly sufficient, yet output speed remains in the tens of tokens/s. Understanding this requires looking at the nature of autoregressive decoding.

    Autoregressive generation is a strictly serial process. At every step, the token produced in the previous step is appended back to the input sequence, and a full forward pass is run to produce the next token. A single forward pass therefore yields only one useful token, while the rest of the computation is spent “reading context.”

    The key bottleneck is not floating-point compute but memory bandwidth. Every token requires moving all model weights from memory to the compute units once. Take a 27B-parameter model stored in half precision: the weights alone occupy about 54GB, and every step reads all of it. The theoretical ceiling for decoding speed is therefore approximately:

    1. Theoretical ceiling (tokens/s) ≈ Memory bandwidth (GB/s) ÷ Bytes read per step (GB)

    This also explains a counterintuitive observation: in bandwidth-constrained scenarios, scaling a model from 7B to 27B often reduces speed far more than the increase in parameter count would suggest, while adding compute does almost nothing to improve per-token latency for a single request. This is the memory wall showing up directly on the inference side.

    2. Core Idea: Turning Serial Decoding into a Semi-Parallel Process

    The approach described in the original practice is essentially speculative decoding, a general technique in the industry. Its logic is similar to a search engine’s “recall first, then rerank and verify.”

    The flow breaks down into four steps:

    1. Draft: A much smaller model autoregressively generates n candidate tokens in a row. Because the small model has fewer weights and lower bandwidth pressure, this step is cheap.
    2. Verify: The n candidate tokens are appended to the original sequence, and the large model runs a single forward pass. Since the input is a known sequence, this step can be fully parallel, producing probability distributions at n+1 positions in one pass.
    3. Accept or roll back: Positions are compared one by one. If the large model agrees, all n tokens are “guessed correctly” and the round emits n tokens directly. If the large model disagrees at some position, a token is resampled from the large model’s distribution starting at that position, and the remaining draft tokens are discarded.
    4. Loop: Continue drafting from the tail of the new sequence in the next round.

    The source of the speedup is clear: what originally required n high-cost large-model forward passes is compressed into 1 large-model forward pass plus n low-cost small-model forward passes. Since the large-model forward pass is the bandwidth-bound term, total time stays close to unchanged, while the number of emitted tokens can multiply. In practice, raising throughput from 40 tokens/s to nearly 120 tokens/s is a direct demonstration of this mechanism.

    It is worth emphasizing that speculative decoding is lossless acceleration. With correct acceptance sampling and residual correction, the final output distribution is identical to decoding with the large model alone, so generation quality is not sacrificed. This is fundamentally different from lossy techniques such as quantization and pruning.

    Illustrative pseudocode:

    1. # Simplified logic of one speculative decoding round
    2. draft_tokens = draft_model.generate(prompt, n) # Small model drafts n tokens
    3. logits = target_model.forward(prompt + draft_tokens) # Large model verifies in one parallel pass
    4. accepted = 0
    5. for i in range(n):
    6. if accept(logits[i], draft_tokens[i]): # Per-position decision
    7. accepted += 1
    8. else:
    9. break
    10. # Emit n tokens if all accepted; otherwise emit accepted tokens + 1 resampled token from the large model
    11. emit(draft_tokens[:accepted], resample_from(logits[accepted]))

    3. Key Variables That Determine Actual Speed

    The same mechanism can produce several-fold differences in tokens/s after deployment, usually for the following reasons:

    • Acceptance rate: The closer the draft distribution is to the large model’s, the higher the acceptance rate. This is the primary driver of gains and usually the main tuning target.
    • Draft length n: Too small and the benefit is limited; too large and rollback waste increases. There is a clear optimal range that must be measured per task.
    • Choice and alignment of the draft model: The small model should ideally come from the same family and use the same tokenizer as the large model. Otherwise, a vocabulary mismatch breaks the verification logic outright.
    • Batching and concurrency: Single-request latency and overall throughput are two different metrics. Speculative decoding delivers the most benefit at low concurrency; under high concurrency the GPU is already saturated and the gains are diluted.
    • Memory and quantization: Weight quantization reduces the bytes moved per step and thus raises the ceiling, but it is a lossy technique whose quality regression must be evaluated.
    • Sampling parameters: Settings such as temperature and top-p affect how predictable the draft is, which in turn affects the acceptance rate.

    4. Engineering Recommendations

    1. Measure the baseline before optimizing. Fix input and output lengths and sampling parameters, and record time to first token and steady-state tokens/s to avoid being misled by peak numbers.
    2. Choose draft length per scenario. High-determinism tasks such as code completion and structured output typically have higher acceptance rates and can tolerate a larger n; open-ended creative tasks call for a conservative setting.
    3. Put monitoring in place early. Bring generation throughput, acceptance rate, memory usage, and batch size into your logging and alerting systems so you can tell whether the bottleneck lies in bandwidth, scheduling, or draft quality.
    4. Deploy in tiers. Run model services on a container platform with elastic scaling to handle concurrency fluctuations; large weight files can be distributed and version-managed with object storage.
    5. Prioritize lossless methods. In quality-sensitive scenarios, favor lossless approaches such as speculative decoding first, and treat quantization and sparsification as supplementary tools under an explicit quality budget.

    5. Common Misconceptions

    • Misconception 1: More compute means more speed. The decoding stage is bottlenecked by memory bandwidth, so simply adding compute does little for per-token latency.
    • Misconception 2: A bigger draft model is better. Once the draft model grows, its own forward-pass cost rises and eats up the savings from verification.
    • Misconception 3: Acceleration necessarily hurts quality. With a correct implementation, speculative decoding leaves the output distribution unchanged; quality loss usually comes from quantization or sampling configuration, not from the mechanism itself.
    • Misconception 4: tokens/s is the only metric. Actual experience also depends on time to first token, stability on long texts, and queueing time under concurrency.

    6. Summary

    The improvement from 40 tokens/s to nearly 120 tokens/s does not come from more aggressive hardware, but from a structural redesign of the decoding flow: use a low-cost small model to recall candidates, use one parallel forward pass of the large model to confirm them in batch, and turn serial dependencies into a parallel verification process. For teams evaluating 27B-class model deployments, the recommended path is baseline measurement, acceptance-rate tuning, draft-length optimization, and concurrency and memory planning. On cloud platforms such as Baidu AI Cloud, combining general capabilities like container platforms, object storage, logging, and monitoring and alerting can deliver a substantial throughput improvement while keeping generation quality unchanged.