KV Cache Compression in LLM Inference: A Technical Deep Dive
This article explains the main approaches to compressing the KV cache in large language model inference, including quantization, token eviction and sparsification, attention head sharing, and low-rank decomposition. It shows how KV cache memory scales with context length and concurrency, and outlines a layered design for balancing memory use, latency, and output quality in production long-context services.
Introduction: Why the KV Cache Becomes a Bottleneck
In autoregressive decoding, a large language model must compute attention over all previous tokens each time it generates a new token. To avoid repeating this work, the industry commonly uses the KV cache. It stores the Key and Value tensors of past tokens, so each decoding step performs only incremental computation. This reduces decoding complexity from O(n²) to O(n), which makes it the foundation for usable long-text inference.
The cost is equally clear. KV cache memory grows linearly with sequence length. Consider a typical model with dozens of layers and multi-head attention. When it processes a context on the order of 100,000 tokens, the KV cache often exceeds the size of the model weights themselves, reaching tens of gigabytes.
In multi-request serving, the KV cache can become the main factor limiting throughput. When the cache fills GPU memory, the batch size cannot grow, and expensive compute sits idle.
For this reason, KV cache compression is not a nice-to-have optimization. It determines whether long-context inference can be deployed commercially. This article reviews the main compression approaches from the perspective of close reading of technical reports.
1. Quantifying KV Cache Memory Usage
Before covering compression techniques, establish an accurate memory estimate. Total KV cache memory can be expressed as:
KV cache memory = 2 × layers × sequence length × head dimension × KV heads × bytes per value × batch size
The factor of 2 accounts for both the Key cache and the Value cache. Assuming FP16 precision (2 bytes per value), a model with 32 layers, 8 KV heads, and a head dimension of 128 needs the following for a single 100,000-token sequence:
2 × 32 × 100000 × 128 × 8 × 2 ≈ 13.1 GB
This figure covers a single request. At concurrency levels of dozens of requests, memory demand easily exceeds the capacity of one GPU.
The formula also points to four ways to compress the cache:
- Reduce bytes per value (quantization).
- Reduce sequence length (sparsification and eviction).
- Reduce the number of KV heads (sharing).
- Reduce the head dimension (low-rank decomposition).
The following sections cover each approach.
2. Quantization: The Most Direct Way to Reduce Bytes
2.1 Basic Principle
Quantization converts KV tensors stored in FP16 or BF16 into INT8, INT4, or even lower bit widths. INT8 halves the memory footprint, and INT4 reduces it to one quarter. The main difficulty is that Key and Value tensors contain outliers. Uniform quantization without special handling introduces significant error and degrades the accuracy of attention distributions.
2.2 Common Optimization Strategies
Widely accepted practices include the following:
- Per-channel quantization: Outliers in Key tensors tend to concentrate in specific channels. Computing scaling factors per channel can substantially reduce error. Value tensors are better suited to per-token quantization.
- Residual retention: Keep recent tokens in high precision, such as FP16, and apply low-bit quantization only to older history. Recent tokens contribute most of the attention for the current generation step, so they are more sensitive to precision loss.
- Group quantization: Divide channels into groups, each with its own scale and zero-point. This balances compression ratio and accuracy.
2.3 Engineering Considerations
Quantization saves bandwidth as well as memory. The decoding stage is memory-bandwidth bound, so reading smaller KV tensors directly speeds up decoding. However, this requires fused dequantization kernels. Otherwise, dequantization overhead can cancel the gains.
When deploying inference services on container platforms, monitor output quality before and after quantization. Set up alerts so that accuracy degradation is caught early.
3. Sparsification and Token Eviction: Reducing the Sequence Dimension
3.1 Core Idea
Many studies show that attention is highly sparse. When generating the current token, only a small number of historical tokens contribute most of the attention weight. The KV cache entries for unimportant historical tokens can therefore be evicted or reduced to summaries. This brings the memory cost along the sequence dimension from O(n) to roughly constant.
3.2 Typical Strategies
- Attention-score-based eviction: Track the cumulative attention weight each historical token receives. Keep the top-k tokens with the highest weights and discard the rest.
- Sliding window with attention sinks: Keep tokens in the most recent window, and permanently keep a few tokens at the start of the sequence. In practice, these positions consistently receive high attention and are called attention sinks. All middle tokens are evicted.
- Retrieval-based dynamic loading: Instead of discarding the cache, swap it out to host memory or even object storage. At each step, retrieve the most relevant KV blocks based on the query vector and load them back into GPU memory. This approach causes the smallest accuracy loss but has the highest system complexity.
3.3 Risks and Trade-offs
The main risk of sparsification is permanent loss of information. An evicted token may become important at a later generation step. For example, in long-document question answering, a passage containing the answer may receive little attention early on.
Production systems often use tiered strategies. Short sequences are not compressed. Medium sequences receive conservative eviction. Only very long sequences trigger aggressive policies. Compression decisions are recorded in a logging service so that quality issues can be traced back.
4. Head Sharing: Source-Level Compression in the Architecture
4.1 From MHA to MQA and GQA
In multi-head attention (MHA), each head maintains its own KV cache. Multi-query attention (MQA) lets all heads share a single set of KV. Grouped-query attention (GQA) takes a middle path, letting groups of heads share KV.
These designs set the number of KV heads during training. They can reduce KV cache memory by roughly an order of magnitude with almost no accuracy loss. This is why recently trained models commonly adopt GQA.
4.2 Retrofitting Existing MHA Models
For models already trained with MHA, fine-tuning can merge multiple KV heads into a shared representation. This achieves architectural compression. The approach offers the largest gains but requires additional training cost, so it suits model providers who can do it before release.
5. Low-Rank Decomposition: Compressing the Feature Dimension
Keys and Values are obtained from the input through projection matrices. If these matrices have a low-rank structure, only a lower-dimensional compressed representation, called a latent vector, needs to be cached. The full tensors are reconstructed when needed. This reduces the cache dimension from “number of KV heads × head dimension” to a smaller joint dimension.
A representative design is Multi-head Latent Attention (MLA). All heads share one low-rank latent cache. Combined with a matrix absorption technique, it reduces the KV cache to a small fraction of its original size without adding computation. It also maintains expressiveness comparable to MHA.
The low-rank approach achieves a high compression ratio, and it is orthogonal to quantization. A low-rank cache can be quantized further, and combining the two yields a strong overall compression rate.
6. Combined Strategies and Practical Recommendations
Production systems rarely rely on a single technique. The following layered combination is recommended:
- Architecture layer: Prefer models that use GQA or low-rank attention to control cache size at the source.
- Storage layer: Apply group quantization to the KV cache. Keep recent tokens in high precision and use INT4 for distant tokens.
- Scheduling layer: Use paged KV cache memory management to e
