Efficient Long-Context LLM Inference via KV Cache Clustering

Jie Hu, Shengnan Wang, Yutong He, Ping Gong, Jiawei Yi, Juncheng Zhang, Youhui Bai, Renhai Chen, Gong Zhang, Cheng Li, Kun Yuan · OpenReview

TL;DR

This paper introduces CentroidKV, a novel framework for efficiently compressing the Key-Value (KV) cache in Large Language Models (LLMs) during long-context inference. By clustering similar KV states and merging them into centroids, CentroidKV significantly reduces memory usage (up to 75%) and speeds up inference (up to 1.92x) without sacrificing model performance, addressing a major bottleneck in deploying LLMs for complex, long-context tasks.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly being used for tasks requiring understanding and generating text over very long sequences (e.g., summarizing entire books, engaging in extended dialogues). A critical component for this capability is the Key-Value (KV) cache, which stores intermediate computations to avoid redundant calculations during inference. However, the KV cache's memory footprint scales linearly with the context length, becoming a substantial bottleneck for deploying LLMs with extended context windows. Existing methods to manage this KV cache either discard potentially important information (leading to performance degradation) or offer only marginal efficiency gains due to high computational overhead. This paper tackles the challenge of reducing KV cache size and improving inference efficiency in a way that preserves crucial information and minimizes computational cost.

Methods

CentroidKV introduces a novel approach called Chunked Soft Matching for online KV cache clustering. The core idea is to leverage the observation that key states within an LLM's KV cache tend to be highly similar along the sequence dimension.

Here's a breakdown of the method:

  1. Chunking: The input sequence is divided into smaller chunks. This helps to manage the computational complexity of similarity calculations.
  2. Chunked Soft Matching: Within each chunk, an "alternating partition strategy" is employed. This strategy divides the tokens in a chunk into two sets (A and B) in an alternating fashion. The paper theoretically proves the optimality of this partitioning for identifying similar token pairs.
  3. Clustering and Merging: Highly similar token pairs identified across chunks are grouped into clusters. The KV cache states within each cluster are then merged into a single "centroid" representation. This effectively compresses the KV cache by replacing multiple similar states with one representative state.
  4. Dynamic Compression: The clustering process is applied dynamically during inference. If the KV cache size exceeds a predefined budget, CentroidKV is invoked to compress it.

The paper also provides a theoretical analysis of the computational complexity and the optimality of the intra-chunk partitioning strategy.

Datasets

The authors evaluated CentroidKV on two widely recognized long-context benchmarks:

  • RULER: This benchmark consists of 13 long-sequence tasks designed to assess LLMs' long-context understanding capabilities.
  • LongBench: This benchmark includes tasks covering various application scenarios, such as single-document QA, multi-document QA, summarization, few-shot learning, synthetic tasks, and code completion.

The models used for evaluation were Llama-3.1-8B-Instruct and Mistral-7B-Instruct-v0.2.

Key Insights and Ideas

  • Key States are Highly Similar: The fundamental observation is that key states in LLMs exhibit high similarity along the sequence dimension, especially within local regions. This is a crucial insight that justifies clustering.
  • Chunking for Scalability: Dividing the long sequence into smaller chunks is essential for making the similarity computation and clustering process computationally feasible for long contexts.
  • Alternating Partitioning is Optimal: The paper theoretically proves that an alternating partitioning strategy within chunks is optimal for identifying similar token pairs, which is key to effective clustering.
  • Soft Matching for Robustness: The "soft matching" aspect likely refers to how similarity scores are used to form clusters, allowing for more flexible and robust grouping compared to hard clustering methods.
  • Centroids Preserve Information: By merging similar KV states into centroids, the method aims to retain the essential information while discarding redundancy, thus preserving model performance.

Key Results

CentroidKV demonstrates significant improvements in both efficiency and performance:

  • Memory Reduction: Achieves up to 75% reduction in KV cache memory usage.
  • Inference Speedup: Accelerates the decoding stage of inference by up to 1.92×.
  • Serving Throughput: Increases serving throughput by up to 4× when integrated with the vLLM serving framework.
  • Performance Preservation: Maintains comparable model performance across various benchmarks and models, even under aggressive compression.
  • Efficiency Profile: CentroidKV shows a favorable efficiency profile, with moderate pre-filling overhead but substantial decoding speedups and memory savings that grow with context length.

Limitations and Future Work

The authors acknowledge a few limitations and suggest future research directions:

  • GPU-Centric Compression: CentroidKV currently performs KV cache compression on the GPU. Future work could explore performing clustering on the CPU and transferring centroids to the GPU to potentially further optimize memory usage.
  • Memory Offloading: The current work does not investigate memory offloading strategies, which could be another avenue for managing large KV caches.
  • Manual Compression Schedule: CentroidKV uses a manually specified compression-ratio schedule. An adaptive strategy that dynamically adjusts compression based on real-time needs could be more effective.
  • Fixed Compression Step: The current approach compresses the cache by at most half at each clustering step. Future work could explore more flexible compression ratios.
  • UUID Token Handling: The method struggles with tokens that lack meaningful semantic structure (e.g., UUIDs) because it relies on cosine similarity. This limitation is shared by other content-aware methods.

Summary updated

BibTeX
@misc{hu2025efficient,
  author = {Jie Hu and Shengnan Wang and Yutong He and Ping Gong and Jiawei Yi and Juncheng Zhang and Youhui Bai and Renhai Chen and Gong Zhang and Cheng Li and Kun Yuan},
  title = {{Efficient Long-Context LLM Inference via KV Cache Clustering}},
  year = {2025},
  eprint = {2506.11418},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.11418},
}
Figure