PyramidInfer: Pyramid KV Cache Compression for High-throughput LLM Inference

Dongjie Yang, Xiaodong Han, Yan Gao, Yao Hu, Shilin Zhang, Hai Zhao

Abstract

Large Language Models (LLMs) have shown remarkable comprehension abilities but face challenges in GPU memory usage during inference, hindering their scalability for real-time applications like chatbots. To accelerate inference, we store computed keys and values (KV cache) in the GPU memory. Existing methods study the KV cache compression to reduce memory by pruning the pre-computed KV cache. However, they neglect the inter-layer dependency between layers and huge memory consumption in pre-computation. To explore these deficiencies, we find that the number of crucial keys and values that influence future generations decreases layer by layer and we can extract them by the consistency in attention weights. Based on the findings, we propose PyramidInfer, a method that compresses the KV cache by layer-wise retaining crucial context. PyramidInfer saves significant memory by computing fewer keys and values without sacrificing performance. Experimental results show PyramidInfer improves 2.2x throughput compared to Accelerate with over 54% GPU memory reduction in KV cache.

The Memory Bottleneck in LLM Inference

Large Language Models (LLMs) are notoriously memory-hungry. While model weights are static, the KV (Key-Value) cache—which stores previously computed attention data to speed up generation—grows linearly with sequence length and batch size. For a 7B parameter model, the weights might take up 14GB, but the KV cache for long sequences can balloon to over 70GB, quickly leading to "Out of Memory" (OOM) errors on standard hardware.

GPU memory consumption for various LLM sizes. PyramidInfer significantly reduces the KV cache footprint, preventing Out-of-Memory (OOM) errors on a single 80GB GPU for larger models.

Existing compression methods typically only start working after the initial "prefill" phase (where the prompt is processed). This means they cannot prevent OOMs during the prompt processing itself. Furthermore, most methods treat every layer of the transformer the same way, ignoring the fact that different layers contribute differently to the model's understanding of context.

Methods: The Pyramid Strategy

PyramidInfer employs a layer-wise strategy to compress the KV cache during both the prefill and generation phases. Its design is based on two core observations:

  1. Inference Context Redundancy (ICR): During training, every token must predict the next one. During inference, only the last token needs to predict the next. This creates massive redundancy in the stored KV pairs of previous tokens.
  2. Recent Attention Consistency (RAC): A small window of recent tokens tends to attend to the same set of "crucial" historical tokens, termed the Pivotal Context (PvC).

Instead of keeping a fixed number of KV pairs across all layers, PyramidInfer keeps more context in the shallow layers and progressively fewer in the deeper layers. This creates a "pyramid" structure. The selection of which KV pairs to keep is determined by an ensemble of attention weights from recent tokens, acting as an oracle to predict which context will be needed for future generation.

Comparison of KV cache compression strategies. PyramidInfer (c) employs a layer-wise pruning strategy, retaining more context in shallow layers and less in deeper layers, unlike uniform methods (a, b).

Key Insights and Ideas

  • Power Law Redundancy: Redundancy is not uniform; it obeys a power law. As you go deeper into the model, the number of keys and values actually necessary for accurate prediction drops sharply. Shallow layers require the "big picture," while deeper layers focus on a tiny subset of pivotal information.
  • Prefill-Aware Compression: By compressing during the prefill phase, PyramidInfer allows models to handle much longer prompts or larger batches than standard methods, which often crash before they finish processing the prompt.
  • Consistency as a Filter: Using the attention patterns of the most recent tokens to filter history is highly effective. The authors demonstrated an ~86% overlap in what recent tokens find important, allowing them to safely discard the "non-pivotal" 14% without hurting performance.

Experimental Results

The researchers evaluated PyramidInfer across a diverse suite of benchmarks, including Wikitext-v2, MMLU, BBH, GSM8K, and HumanEval. Key findings include:

  • Throughput Gains: On an NVIDIA A100 (80GB), PyramidInfer achieved 2.24x higher throughput than the standard "Accelerate" baseline for LLaMA 2-13B.
  • Memory Efficiency: It reduced KV cache memory usage by over 54%.
  • Scaling Up: PyramidInfer enabled a LLaMA 2-70B model to run on a single A100 GPU during the prefill phase, whereas standard methods (Accelerate, DeepSpeed, H2O) all hit OOM errors.
  • Performance Retention: Despite aggressive pruning, the model maintained near-baseline accuracy on complex tasks, significantly outperforming local sliding window strategies.

Limitations and Future Work

While effective, selecting the Pivotal Context requires sorting attention weights, which can introduce computational overhead at very small batch sizes. The method is most beneficial when the system is memory-bound rather than compute-bound. Future work may explore combining PyramidInfer with KV cache quantization (e.g., 4-bit or 8-bit) to further enhance efficiency.

Summary updated

BibTeX
@inproceedings{yang2024pyramidinfer,
  author = {Dongjie Yang and Xiaodong Han and Yan Gao and Yao Hu and Shilin Zhang and Hai Zhao},
  title = {{PyramidInfer: Pyramid KV Cache Compression for High-throughput LLM Inference}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.195},
}
Figure