Abstract
Large Language models (LLMs) have become a research hotspot. To accelerate the inference of LLMs, storing computed caches in memory has become the standard technique. However, as the inference length increases, growing KV caches might lead to out-of-memory issues. Many existing methods address this issue through KV cache compression, primarily by preserving key tokens throughout all layers to reduce information loss. Most of them allocate a uniform budget size for each layer to retain. However, we observe that the minimum budget sizes needed to retain essential information vary across layers and models based on the perspectives of attention and hidden state output. Building on this observation, this paper proposes a simple yet effective KV cache compression method that leverages layer uncertainty to allocate budget size for each layer. Experimental results show that the proposed method can reduce memory usage of the KV caches to only ~20% when compared to full KV inference while achieving nearly lossless performance.
Introduction
As Large Language Models (LLMs) are increasingly deployed for long-context tasks exceeding 100K tokens, they encounter a significant memory bottleneck: the Key-Value (KV) Cache. Storing previous token representations in GPU memory is essential to avoid redundant computations, but the memory footprint scales linearly with context length. For instance, a LLaMA-2 7B model requires over 50GB of VRAM for a 100K-token context, surpassing the capacity of many standard GPUs.
Current compression techniques, often referred to as "PartialKV" solutions, attempt to mitigate this by evicting less important tokens. However, a common limitation of these methods is the use of a uniform budget allocation across all transformer layers. This approach fails to account for the varying informational needs of different layers; some layers exhibit concentrated attention and require fewer tokens, while others have diffuse attention patterns and need a larger cache to maintain accuracy.
Methodology: Dynamic Budget Allocation
ZigZagKV addresses the inefficiencies of uniform allocation by introducing a dynamic, uncertainty-aware strategy for KV cache management.
The core of ZigZagKV involves three key steps:
- Measuring Layer Uncertainty: The researchers quantify the "uncertainty" of each layer using metrics like LMBA (Layer Minimum Budget size to maintain Attention score). They observed that the required budget to preserve 90% of attention information fluctuates significantly across layers, often following a "zigzag" pattern rather than a linear or uniform one.
- Dynamic Budget Distribution: Instead of a flat budget, ZigZagKV ensures every layer receives a fixed minimum floor to prevent information collapse. The remaining available budget is then distributed proportionally based on each layer's measured uncertainty. Layers with diffuse attention (high uncertainty) are allocated more cache space.
- Instruction-Centric Token Selection: To fill the allocated budget, ZigZagKV identifies critical tokens by analyzing cumulative attention scores relative to the most recent instruction tokens. This ensures the preserved cache is highly relevant to the current task context.
Experimental Results
The effectiveness of ZigZagKV was evaluated on long-context benchmarks including Needle-in-a-Haystack and LongBench, using LLaMA-3.1-8B-Instruct and Mistral-7B-Instruct-v0.3 models.
Key findings include:
- Memory Efficiency: ZigZagKV maintains nearly lossless performance while utilizing only approximately 20% of the memory required for a full KV cache.
- Superior Retrieval Performance: In the Needle-in-a-Haystack test with a restricted budget of 256 tokens, ZigZagKV achieved 89.33% accuracy, outperforming H2O (75.25%) and SnapKV (86.00%).
- Denoising Effect: On the TriviaQA few-shot task, ZigZagKV with a 128-token budget actually outperformed full KV inference. This suggests that selective compression may act as a "denoiser," removing irrelevant historical information that could otherwise distract the model.
- Low Computational Overhead: The dynamic allocation process introduces negligible latency, making ZigZagKV's inference speed comparable to static methods like PyramidKV.
Conclusion and Future Work
ZigZagKV demonstrates that moving beyond uniform budget allocation is crucial for efficient long-context LLM inference. By adapting to the unique "uncertainty" profile of each layer, it achieves significant memory savings without sacrificing performance.
Future research directions include extending this methodology to encoder-decoder architectures and exploring automated ways to determine the optimal budget floor. Additionally, investigating whether layer uncertainty profiles remain stable across different domains (e.g., legal or medical text) will be vital for broader applicability.
Summary updated