BaKlaVa - Budgeted Allocation of KV cache for Long-context Inference

Ahmed Burak Gulhan, Krishna Teja Chitty-Venkata, Murali Emani, Mahmut T. Kandemir, Venkatram VishwanatharXiv 2025 · DOI

Introduction

As Large Language Models (LLMs) are increasingly deployed for long-context tasks—such as multi-document summarization, codebase analysis, and legal document review—the memory requirements for inference have become a critical bottleneck. LLMs utilize a Key-Value (KV) cache to store intermediate computations, which prevents redundant processing but grows linearly with context length. For massive contexts, the KV cache can easily exceed the available GPU memory.

Existing memory management strategies typically employ uniform compression or eviction policies, treating all attention heads within a model as equally important. BaKlaVa challenges this "one-size-fits-all" approach. By identifying that specific attention heads are more critical to model performance than others, BaKlaVa introduces a budgeted, heterogeneous allocation strategy that achieves up to 70% compression with minimal impact on accuracy.

The BaKlaVa Methodology

BaKlaVa moves away from uniform memory reduction by implementing a four-stage pipeline designed to identify and protect "workhorse" attention heads while aggressively compressing "lazy" ones.

  1. One-Time Profiling: The model is initially run on a small set of representative sample prompts. During this pass, the system records the internal activations of every attention head.
  2. Importance Heuristic: The researchers utilize cosine similarity between the input and output of each attention head as a proxy for importance. The intuition is straightforward: if a head's output is nearly identical to its input (high cosine similarity), it is performing minimal transformation on the data and is likely less critical. Conversely, heads that significantly alter the data (low similarity) are deemed essential.
  3. Budgeted Reallocation: Using these importance scores, memory is heterogeneously distributed. Critical heads are granted larger KV cache budgets, while less important heads are compressed.
  4. Efficient Parameter Search: To determine the optimal allocation for a specific memory target, the authors use perplexity as a fast evaluation metric. This allows the system to find the best balance in roughly 10–20 minutes on standard hardware, avoiding the need for expensive full-benchmark evaluations.

Key Insights: Head Consistency and Model Specificity

A foundational discovery of the BaKlaVa research is the temporal and task-based consistency of attention head importance. As illustrated in the similarity maps below, the "importance" of a head remains remarkably stable across different token positions and even across disparate tasks (e.g., summarization vs. coding).

Attention head similarity maps for Llama-3-8B and Qwen2.5-7B across XSum and LCC tasks. The vertical stripes indicate that head importance is consistent across token positions, justifying a one-time profiling approach.

Furthermore, the research highlights that importance maps are model-specific. A strategy optimized for Llama-3-8B will not perform well on Qwen2.5-7B, emphasizing the necessity of BaKlaVa's automated profiling step for each unique architecture.

Experimental Evaluation

The authors evaluated BaKlaVa using LongBench, a benchmark specifically designed for long-context understanding. The evaluation covered 14 English tasks (including multi-document QA and summarization) and 2 coding tasks (lcc and repobench-p), with context lengths ranging from 5,000 to over 32,000 tokens. The models tested included LLaMA-3-8B, Qwen2.5-7B, and Mistral-7B.

Key Results

  • High Compression Efficiency: BaKlaVa successfully maintained near-baseline performance at a 70% compression ratio.
  • Robustness at the Limit: In scenarios with extreme memory constraints, BaKlaVa outperformed uniform methods like StreamingLLM by up to an order of magnitude in accuracy.
  • Practical Deployment: Because profiling is a one-time cost and the method is complementary to existing optimizations like FlashAttention and vLLM, BaKlaVa is highly suitable for production environments.

Conclusion and Future Work

BaKlaVa demonstrates that heterogeneous KV cache allocation is a powerful tool for scaling LLM inference. By recognizing the varying utility of attention heads, the method provides a path toward processing massive contexts on consumer-grade hardware. Future research directions include automating the threshold selection process, exploring dynamic allocation that adjusts during inference, and refining the importance heuristic to include magnitude-based metrics.

Summary updated

BibTeX
@misc{gulhan2025baklava,
  author = {Ahmed Burak Gulhan and Krishna Teja Chitty-Venkata and Murali Emani and Mahmut T. Kandemir and Venkatram Vishwanath},
  title = {{BaKlaVa - Budgeted Allocation of KV cache for Long-context Inference}},
  year = {2025},
  eprint = {2502.13176},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.13176},
}
Figure