DynamicKV: Task-Aware Adaptive KV Cache Compression for Long Context LLMs

Xiabin Zhou, Wenbin Wang, Minyan Zeng, Jiaxian Guo, Xuebo Liu, Li Shen, Min Zhang, Liang Ding

Abstract

Efficiently managing the KV cache in Large Language Models (LLMs) is a critical challenge for long-context processing tasks such as retrieval-augmented generation (RAG), long text summarization, and multi-document analysis. Extending the context length substantially increases the KV cache size, leading to excessive memory consumption. Existing KV cache compression methods enforce a fixed pattern, neglecting task-specific characteristics, which hampers the effective retention of essential information while discarding less important tokens. In this paper, we introduce a novel Task-Aware KV cache mechanism that dynamically adjusts the KV cache size across different layers based on the characteristics of the tasks. Our approach builds on the significant observation of distinct activation patterns across layers in various tasks, which highlights the need for adaptive strategies tailored to each task’s unique demands. Based on this insight, we propose DynamicKV, a method that dynamically optimizes token retention by adjusting the number of tokens retained at each layer, adapting to the specific task. DynamicKV establishes global and per-layer maximum KV cache budgets, temporarily retaining the maximum budget for the current layer, and periodically updating the KV cache sizes of all preceding layers during inference. Our method demonstrates exceptional performance on the LongBench dataset, retaining only 1.7% of the KV cache while preserving 90%, 87%, 78%, and 83% of the original accuracy for LlaMA-3-8B-Instruct, Mistral-7B-Instruct-v0.2, Qwen2-7B-Instruct, and InternLM-2.5-7B-Chat-1M, respectively. When the retained KV cache size is increased to 6.9%, the performance becomes nearly indistinguishable from that without any KV cache compression. Notably, even under extreme compression (0.9%), DynamicKV surpasses state-of-the-art (SOTA) methods by 11% in the Needle-in-a-Haystack test using Mistral-7B-Instruct-v0.2.

Introduction

As Large Language Models (LLMs) evolve toward processing "infinite" context windows, they encounter a significant hardware bottleneck: the Key-Value (KV) cache. To avoid redundant computations, LLMs store previous token data in memory, but this cache grows linearly with sequence length. For a model like Llama-2-7B, handling 100,000 tokens requires over 50GB of VRAM, far exceeding the capacity of most consumer-grade GPUs.

Existing solutions attempt to mitigate this by dropping "unimportant" tokens. However, these methods typically follow a fixed, task-agnostic pattern. For example, some strategies always prioritize early layers (a "pyramid" structure), assuming they are universally more important. DynamicKV challenges this assumption by asking: Do all tasks follow the same pattern?

The Limitation of Static Compression

Research into activation patterns reveals that different tasks have distinct "fingerprints." A summarization task might rely heavily on early layers, while a complex coding task might require more memory in the final layers to handle intricate logic. Fixed compression patterns ignore these task-specific needs, leading to significant information loss and degraded performance in long-context scenarios.

Comparison of KV cache allocation strategies (a-d) and their impact on average accuracy.

Methodology: DynamicKV

DynamicKV is a two-step adaptive framework that operates during the "prefill" phase (when the model first processes the input):

  1. Dynamic Budget Allocation: Instead of assigning a uniform number of tokens to every layer, the system calculates an attention score for each layer. It evaluates how much the "instruction tokens" (the query) attend to the "context tokens" (the data).
  2. Progressive Cache Update: The model is partitioned into blocks of layers. As the input is processed, the system periodically re-evaluates retention scores for all preceding layers. If the system determines that a later layer is more critical for a specific prompt, it shifts the memory budget accordingly.

Crucially, this optimization occurs during the prefill phase, leaving the decoding phase untouched. This ensures that DynamicKV remains fully compatible with high-performance inference tools like FlashAttention and vLLM.

Experimental Evaluation and Results

The researchers evaluated DynamicKV across diverse benchmarks, including LongBench (summarization, QA, code completion), Needle-in-a-Haystack (long-context retrieval), and RULER.

Average accuracy vs. KV cache size retention, highlighting DynamicKV's performance at low retention rates.

Key Findings:

  • Extreme Compression: DynamicKV maintains approximately 90% of "Full KV" performance while retaining only 1.7% of the cache. At 6.9% retention, performance is nearly indistinguishable from uncompressed models.
  • Superior Retrieval: In the Needle-in-a-Haystack test under extreme 0.9% compression, DynamicKV outperformed the previous SOTA by 11%.
  • Efficiency Gains: Compared to uncompressed models, DynamicKV delivered a 129% increase in throughput and a 56% reduction in end-to-end latency for 32k context lengths.

Conclusion

DynamicKV demonstrates that task-aware, layer-wise adaptation is essential for efficient long-context processing. By dynamically reallocating memory budgets based on real-time activation patterns, it enables high-performance LLM inference on hardware with limited VRAM without the performance trade-offs inherent in static compression methods.

Summary updated

BibTeX
@inproceedings{zhou2025dynamickv,
  author = {Xiabin Zhou and Wenbin Wang and Minyan Zeng and Jiaxian Guo and Xuebo Liu and Li Shen and Min Zhang and Liang Ding},
  title = {{DynamicKV: Task-Aware Adaptive KV Cache Compression for Long Context LLMs}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
  url = {https://openreview.net/forum?id=jZDKWqZ2Ev},
}
Figure