Model Tells You Where to Merge: Adaptive KV Cache Merging for LLMs on Long-Context Tasks

Zheng Wang, Boxiao Jin, Zhongzhi Yu, Minjia Zhang · DOI

Introduction

As Large Language Models (LLMs) are increasingly deployed for long-document processing, the memory footprint of the Key-Value (KV) cache has emerged as a critical bottleneck. For instance, a 175B parameter model processing a 4,000-token context requires over 1.2 TB of GPU memory just for the KV cache—far exceeding the capacity of standard hardware. Traditional solutions like quantization and eviction suffer from precision loss or permanent information deletion, often leading to "context damage" where crucial distant information is lost forever.

Comparison of KV cache compression strategies: Quantization, Eviction, and the proposed Merging.

Methodology: KVMerger

To address these limitations, we present KVMerger, a training-free approach that adaptively merges similar token states instead of deleting them. KVMerger operates on two primary observations: token locality (adjacent tokens often share >90% similarity) and model persistence (these patterns are inherent to the model architecture regardless of the dataset).

The KVMerger pipeline consists of two main stages:

  1. Merging Set Identification: Utilizing a variant of Agglomerative Hierarchical Clustering (AHC) to identify groups of consecutive tokens that are highly similar. By restricting merging to adjacent tokens, the local structure of the sequence is preserved.
  2. Gaussian Kernel Weighted Merging: Once a set is identified, the algorithm selects a "pivotal" state (the one with the highest attention score). Instead of a simple average, it uses a Gaussian kernel to weight the surrounding tokens. This ensures that tokens closer to the pivot contribute more to the final merged representation, reducing noise and information distortion.

Key Insights: Why Merging Works

The effectiveness of KVMerger stems from its ability to create a "fuzzy" memory rather than a fragmented one. Unlike eviction, which creates "holes" in the model's memory, merging creates a compressed version of the context that allows the model to retain the gist of "unimportant" sections.

Attention and similarity patterns across different layers and heads, demonstrating token locality.

Mathematical analysis reveals that Rotary Position Embeddings (RoPE) are a significant driver of key state similarity, explaining the observed token locality. Because these similarity patterns are persistent across datasets, a fixed similarity threshold can be applied to a model architecture, making the method highly robust and easy to deploy without per-task recalibration.

Evaluation and Results

KVMerger was evaluated on rigorous long-context benchmarks including LongBench, ZeroScrolls, and the "Needle-in-a-Haystack" test using models like Llama2 and Mistral.

  • Superior Performance: KVMerger consistently outperforms existing methods like H2O (eviction-based) and CaM (value-merging), maintaining near-baseline performance even at 35–50% memory budgets.
  • Retrieval Accuracy: In "Needle-in-a-Haystack" tests, KVMerger demonstrated significantly higher retrieval accuracy across various document depths, proving it is much better at preserving specific information that eviction methods might accidentally discard.
  • GQA Compatibility: The method works effectively with Grouped-Query Attention (GQA), making it suitable for modern, efficient architectures like Mistral.

Conclusion and Future Work

KVMerger represents a significant step forward by shifting the strategy from destructive cache eviction to constructive cache amalgamation. While currently training-free, future work could involve fine-tuning models specifically to handle merged KV states or combining merging with ultra-low-bit quantization to achieve even higher compression ratios for LLM serving.

Summary updated

BibTeX
@misc{wang2024model,
  author = {Zheng Wang and Boxiao Jin and Zhongzhi Yu and Minjia Zhang},
  title = {{Model Tells You Where to Merge: Adaptive KV Cache Merging for LLMs on Long-Context Tasks}},
  year = {2024},
  eprint = {2407.08454},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.08454},
}
Figure