Introduction
Large Language Models (LLMs) have revolutionized natural language processing but face significant deployment challenges due to their immense memory requirements. During inference, LLMs utilize a KV (Key-Value) cache to store previously computed hidden states, preventing redundant calculations. However, as sequence lengths and batch sizes increase, the KV cache size grows linearly, often exceeding the memory capacity of even high-end GPUs. For instance, while a 7B parameter model requires approximately 14GB of memory for weights, its KV cache for long-context prompts can balloon to over 70GB, leading to "Out of Memory" (OOM) errors.
The Memory Bottleneck in LLM Inference
Existing KV cache compression techniques typically focus on pruning the cache after the full context has been processed in the prefill phase. This approach fails to address the "prefill bottleneck": if the initial prompt is sufficiently long, the system may crash before compression can even be applied. Furthermore, many methods apply a uniform compression ratio across all layers, ignoring the varying importance of context at different depths of the transformer architecture.
PyramidInfer: A Layer-wise Compression Strategy
To address these limitations, the authors propose PyramidInfer, a novel compression method that reduces the KV cache footprint during both the prefill and generation phases. PyramidInfer is built upon two key observations:
- Inference Context Redundancy (ICR): A significant portion of historical tokens are redundant for future token generation.
- Recent Attention Consistency (RAC): The model's recent attention weights serve as a reliable indicator of which historical tokens (Pivotal Context, or PvC) will be important for future steps.
As illustrated in the figure above, unlike traditional methods that maintain a uniform "block" of context across all layers, PyramidInfer employs a pyramid structure. It retains a larger context in shallow layers and progressively evicts more KV pairs in deeper layers. This strategy leverages the "Power Law of Redundancy," where deeper layers are found to be more resilient to context pruning.
Key Insights
- Prefill-Phase Compression: By identifying and maintaining only the Pivotal Context (PvC) tokens early in the computation, PyramidInfer prevents memory spikes during the initial processing of long prompts.
- Attention as an Oracle: The discovery of Recent Attention Consistency (RAC) allows the system to use a sliding window of recent tokens to dynamically update the set of pivotal tokens without requiring complex external heuristics.
- Layer-wise Sensitivity: Empirical analysis shows that the influence of historical context follows a power-law distribution. In a LLaMA-13B model, for example, the output remains stable even when 80% of the KV cache is evicted in the final layers.
Experimental Results
PyramidInfer was evaluated across various models (LLaMA 2, Vicuna, CodeLLaMA) and benchmarks (MMLU, GSM8K, HumanEval). The results demonstrate significant improvements in both efficiency and capability.
As shown in the memory analysis, PyramidInfer reduces KV cache memory usage by over 54%. This efficiency allows for a 2.24x increase in throughput for LLaMA 2-13B. Most notably, PyramidInfer enables the inference of a 70B parameter model on a single 80GB A100 GPU—a feat impossible for standard implementations or previous compression methods like H2O, which consistently hit OOM limits. Crucially, these gains are achieved with negligible impact on model accuracy across standard language modeling and reasoning tasks.
Conclusion
PyramidInfer represents a significant step forward in making large-scale LLM inference more accessible and efficient. By recognizing the hierarchical nature of context importance in transformers, it optimizes memory usage where it matters most. While the method introduces a minor computational overhead for sorting attention weights, the resulting throughput gains and memory savings make it a compelling solution for high-throughput, long-context LLM applications.
Summary updated