LLM Inference Performance Optimization: A Complete Guide to Bottlenecks and Techniques

    This article breaks down why large language model inference is slow and how to optimize it. It explains the two distinct phases of inference—prefill and decode—and shows that most time is spent moving data rather than computing, making memory bandwidth the primary bottleneck. It then covers two optimization directions: reducing data movement through quantization, KV cache compression, and pruning/sparsification; and improving transfer efficiency and overlap through operator fusion, parallelism strategies, continuous batching, and speculative decoding. The article closes with practical guidance for a measure–locate–verify optimization workflow.

    1. Locate the Bottleneck First: Why Inference Is Slow

    Before discussing any optimization technique, you need to answer one question: where does the time actually go? Training is mainly constrained by compute, since backpropagation requires large amounts of matrix multiplication. Inference has a completely different pressure profile, split into two phases with very different characteristics.

    Prefill phase: The entire input prompt is fed into the model at once, and the hidden states of all input tokens are computed in parallel. Because there are no dependencies between tokens, the matrices are large and computationally dense, so the bottleneck leans toward compute.

    Decode phase: Tokens are generated autoregressively, one at a time. Each new token must attend to all historical tokens and re-read all model parameters. The matrix involved in each step is “tall and thin” (batch × 1), with very low arithmetic intensity, so the bottleneck falls entirely on memory bandwidth.

    An often underestimated fact is that most of the time in LLM inference is not spent computing, but moving data. Take a generation of data center accelerators as an example: FP16 compute can reach the hundreds of TFLOPS range, while memory bandwidth is only on the order of a few TB per second. A 7B parameter model occupies roughly 14GB of memory in FP16, and generating each token theoretically requires reading all 14GB in full. At an estimated 2TB/s bandwidth, reading the parameters alone takes about 7ms, which corresponds to a single-stream upper limit of roughly 140 tokens per second—impossible to exceed even with unlimited compute. Add KV cache reads, kernel launch, and scheduling overhead, and measured throughput is usually lower.

    This leads to the core tension in inference optimization: accelerators spend most of their time waiting for data, not processing it. All subsequent techniques fall into two categories: reducing the amount of data that needs to be moved, and improving transfer efficiency while overlapping computation with data movement.

    2. Reduce Data Movement

    2.1 Quantization

    Lowering weights and activations from FP16 to INT8, INT4, or even lower bit widths directly compresses the read volume proportionally. In practice, three points matter: first, use per-channel or group-wise quantization to control precision loss; second, activations contain outliers, so mixed precision or smoothing is needed; third, dequantization itself has overhead, so quantized compute should be fused with memory access as much as possible to avoid “saving bandwidth but losing compute.”

    2.2 KV Cache Compression

    Autoregressive generation requires caching historical keys/values, and their size grows linearly with sequence length—in long-context scenarios it can even exceed the weights themselves. Common approaches include keeping the cache for only some attention heads, evicting historical entries by importance, and using low-rank or quantized representations of the cache. Compressing the KV cache reduces both memory footprint and per-step read volume, making it one of the most effective techniques for long-text scenarios.

    2.3 Pruning and Sparsification

    Structured pruning removes redundant channels or attention heads, reducing model size and memory access together. Sparse attention reduces the range of history that must participate in computation at each step. Both require fine-tuning or distillation to compensate for accuracy, avoiding “trading accuracy for speed.”

    3. Improve Transfer Efficiency and Overlap

    3.1 Operator Fusion

    Merging adjacent small operators into a single kernel reduces the number of times intermediate tensors are written back to memory. A typical example is combining the scaling, masking, and softmax in attention into one implementation, which significantly lowers memory access and launch overhead.

    3.2 Parallelism Strategies

    Tensor parallelism splits the parameters of a single layer across multiple devices, reducing per-device read volume but introducing communication. Pipeline parallelism splits by layer to improve device utilization. Data parallelism increases overall throughput. The choice depends on how well single-device bandwidth, interconnect bandwidth, and model size match; excessive communication will eat into the gains.

    3.3 Continuous Batching and Scheduling

    Traditional static batching requires all requests to advance in lockstep, so short requests must wait for long ones. Continuous batching dynamically swaps requests in and out at the token level, keeping high-arithmetic-intensity batches as full as possible and spreading bandwidth across more requests. Combined with paged cache management, it reduces memory fragmentation and increases the concurrency the system can hold.

    3.4 Speculative Decoding

    A lightweight draft model generates several candidate tokens in a row, and the main model verifies them all in parallel in one pass. Because verification is parallel, the accepted portion effectively trades one memory access for multiple tokens, breaking through the single-stream bandwidth limit.

    4. Engineering Best Practices

    Optimization should follow a measure–locate–verify loop: first clarify whether the metric is time to first token, single-stream speed, or overall throughput; then use profiling to determine whether the constraint is bandwidth, communication, or scheduling; finally, change one variable at a time and run an accuracy regression. Cloud practice typically also uses container platforms for elastic scaling and monitoring/alerting services to observe latency distributions, turning optimization results into capacity planning.