DiffKV: Differentiated Memory Management for Large Language Models with Parallel KV Compaction

Yanqi Zhang, Yuwei Hu, Runyuan Zhao, John C. S. Lui, Haibo Chen

TL;DR

Large Language Models (LLMs) are memory-hungry, with the KV cache being a major bottleneck. DiffKV tackles this by intelligently compressing the KV cache, differentiating between keys and values, token importance, and attention heads. This leads to significant memory savings and throughput improvements without sacrificing accuracy.

What Problem Does This Paper Address?

LLMs, despite their impressive capabilities, are incredibly demanding on computational resources, particularly memory. The Key-Value (KV) cache, which stores intermediate computations for attention mechanisms, is a primary culprit, often consuming over 90% of total memory. As LLMs are used for longer sequences and more complex reasoning tasks (like chain-of-thought), this memory bottleneck severely limits serving efficiency, increasing latency and reducing the number of concurrent requests. Existing compression techniques like quantization and pruning often apply a uniform approach, overlooking the nuanced importance of different parts of the KV cache.

Methods

DiffKV introduces a novel framework for KV cache compression that leverages three key differentiations:

  1. Differentiated Keys and Values: DiffKV assigns higher precision to key vectors than to value vectors. This is based on the observation that keys play a more critical role in attention score computation than values, which are primarily used for aggregation.
  2. Token Importance Differentiation: Tokens are not treated equally. DiffKV uses attention scores to determine token importance, applying higher precision to critical tokens, lower precision to less critical ones, and pruning the least significant.
  3. Per-Head Dynamic Sparsity: The importance of tokens varies not only across requests but also across different attention heads. DiffKV dynamically identifies critical tokens on a per-head, per-request basis, allowing for adaptive memory allocation.

To manage the resulting irregular memory usage patterns, DiffKV employs an on-GPU memory manager that uses parallel KV compaction. This technique efficiently packs fragmented memory into contiguous regions, optimizing allocation and recycling directly on the GPU. It relies on three core GPU-resident data structures:

  • Unified Pages: Fixed-size pages that can store tokens at specific precisions.
  • Circular Free Page List: A centralized list managing free and used pages for efficient allocation and recycling.
  • Bidirectional Page Table: A consolidated table minimizing metadata overhead for tracking different precision levels.

Datasets

The paper evaluates DiffKV on various benchmarks and models, including:

  • Models: Llama3-8B, Llama3-70B, Qwen2.5-7B, Qwen2.5-32B, QwQ-32B, R1-Distill-Qwen-14B, R1-Distill-Llama-8B.
  • Benchmarks: GSM8K, MATH, HumanEval+, MBPP+, MMLU, MMLU-Pro, LongBench, AIME24, GPQA.
  • Calibration Dataset: MATH (training split) was used for parameter tuning.

Key Insights and Ideas

  • Keys are More Important Than Values: The paper provides strong evidence that key vectors have a more significant impact on attention computation than value vectors, justifying differentiated precision.
  • Token Importance Varies Greatly: Attention scores reveal a highly non-uniform distribution of token importance, making uniform compression strategies suboptimal.
  • Sparsity is Dynamic and Per-Head: Attention patterns are not static; they vary significantly across attention heads and individual requests, necessitating dynamic memory management.
  • On-GPU Memory Management is Crucial: The irregular memory demands of differentiated compression require an efficient, parallelized memory management system on the GPU to avoid negating compression benefits with overhead.

Key Results

  • Compression Ratio: DiffKV compresses the KV cache by 2.7x to 5.7x.
  • Accuracy: Achieves near-lossless accuracy compared to FP16 baselines, with an average degradation of only 0.3%.
  • Throughput Improvement: Enhances throughput by 1.9x to 5.4x.
  • Memory Management Efficiency: DiffKV's on-GPU parallel KV compaction reduces memory management latency by up to three orders of magnitude compared to CPU-based approaches.
  • Dynamic Sparsity Effectiveness: Per-head dynamic sparsity outperforms static sparsity, maintaining full accuracy with significantly higher pruning ratios.
  • Thinking Models: DiffKV is among the first to demonstrate effective KV cache compression on "thinking models" requiring complex chain-of-thought reasoning, achieving FP16-comparable generation quality.

Limitations and Future Work

  • Parameter Calibration: While DiffKV's parameters were calibrated on the MATH dataset, the paper notes that the profiled values cover a limited portion of the parameter space.
  • Future Work: The authors suggest exploring more flexible compression policies, such as tuning thresholds for each head individually, which could potentially further optimize the balance between model accuracy and memory efficiency. They also mention that future models with higher queries-per-KV ratios might benefit from additional high-precision levels.

Summary updated

BibTeX
@inproceedings{zhang2025diffkv,
  author = {Yanqi Zhang and Yuwei Hu and Runyuan Zhao and John C. S. Lui and Haibo Chen},
  title = {{DiffKV: Differentiated Memory Management for Large Language Models with Parallel KV Compaction}},
  booktitle = {SOSP},
  year = {2025},
  doi = {10.1145/3731569.3764810},
}
Figure