Abstract
With the development of large language models (LLMs), efficient inference through Key-Value (KV) cache compression has attracted considerable attention, especially for long-context generation. To compress the KV cache, recent methods identify critical KV tokens through static modeling of attention scores. However, these methods often struggle to accurately determine critical tokens as they neglect the temporal patterns in attention scores, resulting in a noticeable degradation in LLM performance. To address this challenge, we propose AttentionPredictor, which is the first learning-based method to directly predict attention patterns for KV cache compression and critical token identification. Specifically, AttentionPredictor learns a lightweight, unified convolution model to dynamically capture spatiotemporal patterns and predict the next-token attention scores. An appealing feature of AttentionPredictor is that it accurately predicts the attention score and shares the unified prediction model, which consumes negligible memory, among all transformer layers. Moreover, we propose a cross-token critical cache prefetching framework that hides the token estimation time overhead to accelerate the decoding stage. By retaining most of the attention information, AttentionPredictor achieves 13$\times$ KV cache compression and 5.6$\times$ speedup in a cache offloading scenario with comparable LLM performance, significantly outperforming the state-of-the-arts.
The KV Cache Bottleneck in Long-Context LLMs
Large Language Models (LLMs) are increasingly deployed for long-context tasks such as multi-turn conversations and complex document reasoning. However, as context length grows, the Key-Value (KV) cache—which stores past token representations to avoid redundant computation—becomes a massive memory bottleneck. For instance, a 7B parameter model with a 128K context window requires approximately 72 GB for the KV cache, far exceeding the 14 GB needed for the model weights themselves.
Current solutions typically "evict" (delete) less important tokens using static heuristics or simple retrieval. However, these methods often ignore the temporal dynamics of attention. A token that appears unimportant at one step may become critical in the next. Neglecting these shifts leads to "performance cliffs," where model accuracy drops sharply under high compression.
AttentionPredictor: Capturing Spatiotemporal Dynamics
The authors introduce AttentionPredictor, the first learning-based method to treat KV cache management as a 2D time-series prediction problem. Unlike previous methods that rely on static scores or indirect embeddings, AttentionPredictor models the attention score distribution directly.
Key Methodological Innovations:
- Spatiotemporal Prediction: Instead of a single snapshot, the model analyzes a history of block-wise compressed attention scores. It employs a lightweight CNN (two 2D convolution layers and one 1D convolution layer) to capture patterns across both the sequence dimension (space) and decoding steps (time).
- Unified Architecture: A single, tiny model (only 21 KB) is shared across all transformer layers and heads. This is roughly one-millionth the size of a LLaMA-3.1-8B model, making its memory footprint negligible.
- Cross-Token Prefetching: To eliminate prediction latency, the framework operates asynchronously. While the LLM processes the current token, AttentionPredictor identifies and fetches critical tokens for the next step from CPU to GPU.
- Distribution Error Calibration: To prevent drift over long sequences, the system periodically performs a full dense attention pass to reset the history with ground-truth data.
Predictable Attention Patterns
The research identifies three fundamental temporal patterns in attention: Re-access (repeatedly attending to the same tokens), Sequential (moving progressively along the sentence), and Seasonal (periodic recurrence). These patterns are theoretically grounded in the high self-similarity of query vectors and the mathematical properties of Rotary Positional Embeddings (RoPE). By modeling these directly, AttentionPredictor achieves higher accuracy than indirect methods like SeerAttention while being significantly smaller.
Performance Benchmarks
The model was evaluated on extensive long-context and reasoning benchmarks, including LongBench, InfiniteBench, RULER, and AIME.
- Massive Compression & Speed: AttentionPredictor achieved 13× KV cache compression with performance comparable to full-cache models. In CPU-to-GPU offloading scenarios, it delivered a 5.6× speedup over FlashAttention-2.
- Retrieval Accuracy: It maintained 100% accuracy on the "Needle In A Haystack" test at a 64K context with a 1/64 compression ratio.
- Reasoning Resilience: On the AIME math benchmark, AttentionPredictor retained 76.7% accuracy under tight cache budgets, whereas retrieval-based methods like Quest dropped to 43.3%.
Conclusion
AttentionPredictor demonstrates that attention patterns in LLMs are highly predictable through spatiotemporal modeling. By leveraging a tiny, unified CNN and an asynchronous prefetching framework, it effectively resolves the KV cache memory bottleneck without sacrificing reasoning capabilities. Future work may involve extending this approach to diverse architectures like Mixture-of-Experts (MoE) and automating hyperparameter tuning for production environments.
The code is available at https://github.com/MIRALab-USTC/LLM-AttentionPredictor.
Summary updated