LLM Inference Optimization: Prefill/Decode Bottlenecks and Systematic Solutions

    This article explains why autoregressive LLM inference is divided into prefill and decode stages, why decode is often limited by memory bandwidth rather than compute, and how techniques such as quantization, kernel fusion, KV cache management, batching, speculative decoding, and prefill/decode disaggregation address the bottleneck. It also provides a practical order for diagnosing and optimizing inference performance.

    1. Why Is Inference Slow? Understand the Two Stages First

    Autoregressive LLM inference naturally splits into two engineering stages. Their computational characteristics are very different, so the optimization methods for each are also different.

    Prefill stage: The complete user prompt is fed into the model at once, and the hidden states for all tokens can be computed in parallel. This stage has large matrix dimensions and saturated operators, allowing the GPU Tensor Cores to run efficiently. It is a typical compute-bound task. Prefill time is usually approximately linear with input length, and in long-context scenarios it largely determines time to first token.

    Decode stage: The model generates output one token at a time. For each generated token, attention is performed over all previous tokens. At this point, the amount of computation within each batch step is small, and the bottleneck shifts from compute to memory bandwidth. Parameters and the KV cache must be repeatedly moved from GPU memory to the compute units.

    One often-overlooked fact is that during decode, every generated token requires the entire model’s parameters to be read once. This means that for most chat-style inference workloads, decode accounts for the vast majority of latency, and the reason decode is slow is not that compute is insufficient, but that data movement is insufficient.

    2. Core Bottleneck: Compute and Bandwidth Mismatch

    We can use a set of typical order-of-magnitude estimates to illustrate this counterintuitive conclusion.

    • A mainstream data center GPU has FP16 compute throughput on the order of hundreds of TFLOPS, while memory bandwidth is typically only a few TB/s.
    • A 7B-parameter model has weights of about 14 GB at FP16 precision.
    • Each generated token theoretically requires reading all 14 GB of weights from GPU memory and passing them through the compute units.

    Thus, we can roughly estimate the lower bound for a single decode step:

    1. Lower bound for single-token decode time ≈ 14 GB / 2 TB/s ≈ 7 ms
    2. Corresponding upper bound on generation rate ≈ 1 / 7 ms ≈ 140 token/s

    In other words, even if GPU compute were infinite, the single action of moving weights out of GPU memory would cap generation speed. In real systems, the growing KV cache must also be read, and scheduling overhead must be handled, so actual throughput is often far below this theoretical value.

    From this, we can extract the first principle of inference optimization: reduce the amount of GPU memory data movement required per generated token, improve movement efficiency, or make one data movement serve more computation. Almost all subsequent optimization techniques fall into these three categories.

    3. Systematic Optimization Path

    1. Quantization: Directly Reduce the Data That Must Be Moved

    Compressing weights from FP16 to INT8 or lower precision is one of the most direct optimizations. Weight size is halved or reduced even further, data movement drops proportionally, and communication and memory usage also decrease. Common approaches include weight-only quantization, which keeps activations at higher precision to stabilize accuracy. Quantizing the KV cache can further relieve memory pressure in long-context scenarios. Quantization inevitably carries accuracy risk, so the appropriate bit width and calibration method must be selected based on perplexity evaluation and business acceptance criteria.

    2. Operator Fusion and Kernel Optimization

    In the decode stage, operators are small and launch overhead accounts for a high proportion of time. By fusing multiple small operators into one large kernel, the system reduces intermediate result reads/writes to GPU memory and the number of kernel launches, significantly improving effective bandwidth utilization. Common inference engines generally include optimizations such as fused attention and fused layer normalization, which is also an important consideration when choosing an inference framework.

    3. KV Cache Management: The Key to Memory and Throughput

    The KV cache grows with sequence length and concurrency. Poor management causes two problems: memory fragmentation reduces the number of requests that can be served concurrently, and differences in request lengths lead to wasted compute. Common practices in modern inference systems include:

    • Block-based KV cache management, allocating fixed-size physical blocks similar to operating system paging, greatly reducing fragmentation;
    • Prefix reuse: system prompts or common prefixes shared by multiple requests are computed once and stored once, saving both prefill time and memory;
    • Sliding window or eviction strategies: trimming historical KV as needed for very long contexts.

    4. Batching and Continuous Scheduling

    Decode for a single request is essentially “trading the cost of moving the entire model’s weights for one token,” which is extremely inefficient. Increasing batch size allows one weight movement to serve multiple requests, improving throughput almost linearly. Furthermore, continuous batching allows new requests to join without waiting for the entire batch to finish, avoiding the “short requests waiting behind long ones” problem of traditional static batching. It is the de facto standard for current inference services.

    Note that larger batch size is not always better: as batch size increases, attention places more read pressure on the KV cache, and per-request latency is affected. A trade-off must be made between throughput and latency SLAs.

    5. Speculative Decoding: Using a Small Model to “Guess” for the Large Model

    Speculative decoding uses a low-cost draft model to propose multiple candidate tokens at once, and then the large model verifies them in parallel in a single forward pass. Because verification can be batched, multiple candidate tokens share one weight movement, reducing amortized per-token time by a multiple while preserving the output distribution. It is especially suitable for scenarios where decode accounts for a high proportion of time and the draft model has a high acceptance rate.

    6. Prefill and Decode Disaggregated Deployment

    The two stages have different computational characteristics, and co-locating them causes interference. Splitting prefill nodes and decode nodes into separate deployments, connected through KV cache transfer, allows each to scale independently and use appropriate hardware forms. This improves tail latency in long-input, high-concurrency scenarios.

    4. Engineering Practice Recommendations

    Overall, the recommended order for diagnosis and optimization is:

    1. Measure before optimizing: distinguish time to first token (prefill-dominated) from inter-token latency (decode-dominated), and locate the bottleneck separately.
    2. On the decode side, prioritize quantization, continuous batching, and operator fusion, which provide broadly applicable gains.
    3. For long contexts and shared prefixes across requests, focus on KV cache quantization and prefix reuse.
    4. For high-concurrency service scenarios, evaluate speculative decoding and prefill/decode disaggregated architectures.
    5. Before any optimization goes live, validate it with both accuracy evaluation and end-to-end stress testing to avoid hidden degradation from “trading quality for speed.”

    The essence of LLM inference optimization is finding the balance among compute, bandwidth, and memory. Once you understand the core contradiction that “the GPU spends most of its time waiting for data,” you have grasped the common logic behind all optimization techniques.