LLM Inference Performance Optimization: From Bottleneck Identification to System Tuning

    This article provides a practical overview of LLM inference performance optimization. It explains why inference bottlenecks differ from training bottlenecks, why the decode phase is often limited by memory bandwidth rather than compute, and how to improve performance by reducing data movement or making data movement more efficient. It covers quantization, pruning, KV cache compression, IO-aware attention, continuous batching, memory management, speculative decoding, parallelism, scheduling, and a measurement-first workflow for choosing and validating optimizations.

    1. Don’t Get Lost in Jargon

    People doing inference optimization can easily become fascinated with terminology. KV cache, attention optimization, speculative decoding—if every term assumes the listener already knows it, the explanation can end up confusing the audience. This article takes a different approach: it uses relatively plain language to re-explain how to make LLM inference faster.

    The intended readers are those who know what a large model is, have a general understanding of the Transformer structure, and have run some inference code, but lack a systematic understanding of inference performance optimization. If you have never touched deep learning, some parts may be challenging. If you are a senior performance optimization engineer, the depth may feel limited. For readers in the middle—who know a little but lack a system—this article aims to lay out the whole map.

    2. First, Identify Where Inference Is Slow

    Before optimizing, you must know what is slow. Otherwise, you are just tuning parameters blindly. A key premise is: the bottleneck of inference is not the same as that of training. Training is mainly pressured by computation: you need many matrix multiplications to compute gradients. Inference is more complex.

    LLM inference is usually split into two phases:

    • Prefill phase: The entire user prompt is fed into the model at once, and hidden states for all input tokens are computed. Since tokens do not affect each other and can be processed in parallel, this phase is typically compute-bound, and the hardware’s compute capability can be fully utilized.
    • Decode phase: Tokens are generated one by one. For each new token, the model must look back at information from all previous tokens to perform attention computation. This phase is constrained by repeatedly moving weights and caches, making it typically memory-bandwidth-bound.

    A counterintuitive fact: for the decode phase of large models, most time is not spent computing; it is spent moving data.

    A Numeric Illustration

    Suppose a mainstream data-center GPU has FP16 compute capability of several hundred TFLOPS per second, which sounds powerful; but its GPU memory bandwidth is often only on the order of several terabytes per second. Compared together, the gap is several orders of magnitude.

    Now consider a relatively small model. A 7B model with 7 billion parameters occupies about 14 GB of GPU memory in FP16 precision. During decode, in theory, generating each new token requires reading these 14 GB of weights from memory and passing them through the compute units. Even under an ideal bandwidth of several TB/s, just reading the weights alone would consume several milliseconds.

    This means: even if compute were unlimited, simply reading parameters from memory would already set a ceiling on how many tokens can be generated per second. In reality, you must also read the KV cache, handle scheduling, and pay framework overhead, so actual speed is only lower.

    The Core Contradiction

    The core contradiction of inference optimization can be summarized in one sentence: most of the time, the GPU is waiting for data, not computing on data. Once you understand this, almost all optimization techniques can be grouped under two main lines:

    1. Reduce the amount of data that needs to be moved.
    2. Improve movement efficiency, or overlap computation with data movement.

    3. Main Line 1: Reduce the Data You Need to Move

    1. Quantization

    The most direct idea is to make each parameter “thinner”. FP16 stores one number with 16 bits. If you switch to 8 bits or an even lower-bit representation, the GPU memory footprint and read volume of weights can decrease proportionally. By sacrificing very little precision, quantization can achieve significant bandwidth savings, making it one of the optimization techniques with the highest return on investment. Common approaches include post-training quantization and quantization-aware training. The key is to choose a quantization granularity and calibration strategy that have little impact on model quality.

    2. Pruning and Sparsification

    Not all weights are equally important during inference. Pruning removes redundant connections or entire structures, making the model smaller and reducing the data that needs to be moved. Sparsification makes many values in matrices zero, so in theory only nonzero parts need to be moved and computed. However, whether the benefits of sparsity can be realized depends heavily on whether the underlying hardware and operators truly support sparse acceleration. Otherwise, it may be “sparse on paper but no faster in practice”.

    3. KV Cache Compression

    As mentioned earlier, the decode phase repeatedly reads the KV cache. As the context becomes longer, this cache takes up more space and becomes an important burden on bandwidth. KV cache compression methods include: keeping only the cache of important tokens, quantizing the cache, or storing historical information in a more compact representation. These methods are especially important in long-context scenarios, because the amount of KV cache reading may already be comparable to the amount of weight reading.

    4. Main Line 2: Move Data More Efficiently and Overlap Computation with Data Movement

    1. IO-Aware Attention Computation

    A standard attention implementation repeatedly writes intermediate results back to GPU memory and then reads them again, causing a lot of avoidable memory reads and writes. The core of IO-aware attention optimization is to do as much computation as possible in on-chip high-speed cache and reduce round trips to GPU memory, thereby reducing bandwidth pressure without significantly increasing computation. This is also a major direction in recent attention operator optimization.

    2. Continuous Batching

    The traditional approach waits for a batch of requests to be assembled and finish at the same time before releasing resources, which can easily leave the GPU idle. Continuous batching allows completed requests to leave and new requests to join dynamically during decoding, keeping the GPU’s “seats” as full as possible. Since the decode phase is inherently bandwidth-limited, merging the computation of multiple requests and moving weights together can significantly improve overall throughput utilization.

    3. Efficient GPU Memory Management

    If the KV cache reserves contiguous GPU memory according to the maximum length, it can produce a lot of fragmentation and waste. Drawing on the paging idea from operating systems, the cache can be split into fixed-size blocks and allocated on demand in a non-contiguous manner. This can greatly reduce GPU memory fragmentation and increase the number of requests that can run concurrently, indirectly improving throughput.

    4. Speculative Decoding

    Since decoding one token at a time is “wasteful”, can we generate several tokens at once and then verify them? Speculative decoding uses a smaller, faster “draft model” to guess several tokens first, then lets the large model verify them in parallel at once. Correct guesses are accepted directly; incorrect ones are rolled back. Because verifying multiple tokens is approximately one forward pass for the large model, this can often deliver real speedup in decode scenarios where compute is abundant but bandwidth is tight.

    5. System-Level View: Parallelism and Scheduling

    Beyond single-GPU optimization, real deployment also requires system-level work:

    • Parallelism strategies: If the model does not fit or cannot compute fast enough, you can introduce tensor parallelism (splitting a single layer’s computation across multiple GPUs), pipeline parallelism (placing different layers on different GPUs), and other schemes to compensate for the limits of a single GPU’s compute and memory through multi-GPU cooperation.
    • Request scheduling: Properly arranging prefill-heavy requests and decode-heavy requests, and avoiding mutual blocking, is key to ensuring time to first token and overall throughput.
    • Operator fusion and graph optimization: Combining multiple small operators, reducing kernel launches, and reducing intermediate data landing are often overlooked engineering methods that can reliably bring benefits.

    6. Practical Advice: Measure First, Optimize Second

    With so many techniques, the worst approach is “use whatever others use”. A pragmatic approach is:

    1. Establish a measurement baseline: Separately measure the time spent in the prefill and decode phases, and determine whether the bottleneck is compute, bandwidth, or scheduling.
    2. Watch the right metrics: Do not look at only a single number. At least pay attention to overall throughput, per-request latency, and time to first token (TTFT). They correspond to different optimization directions.
    3. Prioritize by return on investment: Techniques like quantization, which involve small changes and large benefits, should be done first. Engineering-heavy changes such as parallelism reconstruction and operator rewriting should be reserved for cases where they are truly necessary.
    4. Verify whether the benefits are realized: Optimizations such as sparsity and cache compression must be tested end-to-end. Be wary of “theoretical speedup, but no measured improvement”.

    7. Conclusion

    The essence of LLM inference optimization revolves around the core contradiction that “the GPU is often waiting for data”: either make the data that needs to be moved smaller (quantization, pruning, KV cache compression), or make movement faster and overlap computation with movement (IO-aware attention, continuous batching, speculative decoding, parallelism and scheduling). By connecting these techniques along these two main lines and pairing them with the engineering habit of “measure first, optimize second”, you have a clear map of inference performance optimization, instead of being led around by scattered terms.