Abstract
Recently the generative Large Language Model (LLM) has achieved remarkable success in numerous applications. Notably its inference generates output tokens one-by-one, leading to many redundant computations. The widely-used KV-Cache framework makes a compromise between time and space complexities. However, caching data generates the increasingly growing memory demand, that can quickly exhaust the limited memory capacity of the modern accelerator like GPUs, particularly in long-context inference tasks. Existing studies reduce memory consumption by evicting some of cached data that have less important impact on inference accuracy. But the benefit in practice is far from ideal due to the static cache allocation across different LLM network layers. This paper observes that the layer-specific cached data have very different impacts on accuracy. We quantify this difference, and give experimental and theoretical validation. We accordingly make a formal analysis and shows that customizing the cache size for each layer in a personalized manner can yield a significant memory reduction, while still providing comparable accuracy. We simulate the cache allocation as a combinatorial optimization problem and give a global optimal solution. In particular, we devise a mini- and sampling-based inference over a lightweight variant of the LLM model, so as to quickly capture the difference and then feed it into the personalized algorithms. Extensive experiments on real-world datasets demonstrate that our proposals can reduce KV cache memory consumption by 61.6% on average, improve computational efficiency by 2.1x and then increase the throughput by up to 5.5x.
Introduction
Generative Large Language Models (LLMs) produce text one token at a time. To avoid redundant calculations, models use a KV (Key-Value) Cache to store intermediate mathematical states of previous tokens. While this saves time, it creates a massive memory burden—especially for long-context tasks like summarizing a 100-page document. In these scenarios, the KV cache can grow larger than the model itself, leading to "Out of Memory" (OOM) errors on even high-end GPUs.
Existing solutions attempt to "evict" (delete) less important tokens from the cache. However, most current methods use a static allocation, treating every layer of the neural network the same. The authors of XKV argue that this is inefficient because different layers have vastly different "sensitivities." Some layers can function perfectly with a tiny cache, while others need to retain almost everything to stay accurate.
Layer-Specific Fragility
The "aha moment" of the paper is the discovery that "Importance Retention" (R) varies significantly across the model's hierarchy. Some layers are "sturdy"—allowing for the eviction of up to 90% of their cached tokens with zero impact on the final output—while others are "fragile" and act as bottlenecks for accuracy.
As shown in the figure above, the retention ratio fluctuates significantly across layers. XKV looks at the entire stack of layers as a single system, shifting memory resources from sturdy layers to fragile ones to maximize the global Importance Retention Ratio.
The XKV Framework
The researchers propose a framework built on three core components to dynamically optimize cache allocation:
- Mini-Prefill: Before the actual inference begins, XKV runs a lightweight pass. This pass doesn't store any cache but quickly calculates attention scores to see which tokens are most important for each specific layer.
- Greedy Optimization Algorithm: Using data from the mini-prefill, XKV solves a combinatorial optimization problem. It uses a greedy algorithm to "personalize" the cache size for every single layer, ensuring that memory is spent where it provides the most benefit to accuracy.
- Sampling-based Strategy: To reduce computational overhead, XKV can "sample" a few tasks to create an average allocation profile for specific task categories (e.g., Q&A), which is then applied to future tasks of that type.
Experimental Results
The authors evaluated XKV using LongBench, a comprehensive benchmark for long-context understanding, primarily using the Llama-3.1-8B-Instruct model.
- Memory Efficiency: XKV reduced KV cache memory consumption by 61.6% on average compared to standard full-cache methods.
- Throughput Gains: By shrinking the memory footprint, the system can handle larger batches of requests simultaneously, increasing throughput by up to 5.2x.
- Computational Speed: The reduction in data management improved computational efficiency by 2.1x.
- Superior Accuracy: At extreme compression levels (keeping only 1.2% to 1.6% of the cache), XKV consistently outperformed state-of-the-art competitors like SnapKV and PyramidKV.
Conclusion
XKV demonstrates that personalized, layer-aware KV cache management is essential for efficient long-context LLM inference. By identifying and prioritizing "fragile" layers, XKV achieves high accuracy with a fraction of the memory footprint. Future work will explore the generalization of XKV to other architectures like Mixture-of-Experts (MoE) and its synergy with KV cache quantization techniques.
Summary updated