TL;DR
This paper introduces "Squeezed Attention," a novel method to significantly speed up inference for Large Language Models (LLMs) dealing with long contexts. By clustering "fixed" parts of the input context based on semantic similarity and using representative centroids, Squeezed Attention dramatically reduces the amount of computation needed during inference, achieving up to 4.3x speedups with minimal accuracy loss.
What Problem Does This Paper Address?
Modern LLMs are increasingly capable of handling vast amounts of text (long contexts) for tasks like document analysis and code generation. However, this comes at a steep price: inference costs, particularly memory and computation, scale linearly with the input sequence length. This is largely due to the attention mechanism, which requires comparing every "query" token with every "key" token. For applications where a large portion of the input context is static (e.g., system instructions, documentation), this fixed context becomes a major bottleneck. The challenge is to efficiently process this fixed context without sacrificing accuracy, especially when new user inputs arrive rapidly.
Methods
The core of Squeezed Attention lies in an offline preprocessing step and an optimized online inference process.
Offline Preprocessing: 1. Semantic Clustering: The "keys" from the fixed context are clustered using K-means based on their semantic similarity (derived from embeddings). Each cluster is then represented by a single "key centroid." 2. Hierarchical Clustering (Optional): For even greater efficiency, a hierarchical approach can be used, creating coarse-grained "Level 1" centroids from finer-grained "Level 2" centroids.
Online Inference: 1. Centroid Lookup: When a new "query" token arrives from the user input, it's compared against the precomputed key centroids (instead of all individual keys). This quickly identifies which clusters (and thus which keys) are semantically relevant. 2. Sparse Attention: Exact attention computation is then performed only with the keys belonging to the identified relevant clusters. This drastically reduces the number of key-value pairs that need to be accessed and processed. 3. Optimized Kernels: The authors developed custom Triton kernels for efficient centroid comparison and sparse FlashAttention, further accelerating the process.
This approach differs from prior methods by using semantic similarity for clustering (rather than just physical proximity) and by dynamically identifying relevant keys for each generation step, rather than permanently discarding tokens.
Datasets
The paper evaluates Squeezed Attention on several long-context benchmarks:
- LongBench: A comprehensive benchmark for document QA, few-shot learning, and code completion.
- RULER: Another benchmark for evaluating long-context LLMs.
- PreFixQA: A newly introduced benchmark designed specifically for fixed-context QA scenarios. It consists of long documents from arXiv papers and generated question-answer pairs, aiming to better evaluate the handling of multiple queries on a single document.
Key Insights and Ideas
- Semantic Similarity is Key: Clustering keys based on their meaning, not just their position, is crucial for accurately identifying relevant context. Tokens far apart in the text can be semantically similar and important.
- Centroids as Proxies: Using centroids as representatives for clusters allows for a much faster initial filtering of relevant keys, avoiding costly comparisons with every single key.
- Dynamic Relevance: Unlike methods that prune tokens permanently, Squeezed Attention dynamically selects relevant keys at each inference step, allowing it to adapt to changing query needs.
- Hierarchical Efficiency: A multi-level clustering approach can further reduce the computational cost of the centroid lookup, making it logarithmic with respect to the fixed context length.
- Kernel Optimization Matters: Developing specialized GPU kernels for centroid comparison and sparse attention is essential to realize the full speedup potential.
Key Results
- Speedups: Achieved more than 4x speedups during both prefill and generation phases for long-context inference. Specifically, up to 4.3x for prefill and 4.2x for decode.
- KV Budget Reduction: Reduced the KV budget (the amount of KV cache loaded) by 3.1x with no noticeable accuracy loss, and up to 8x reduction with only a 0.5 point accuracy gap on models like LLaMA-2-7B-32K.
- Accuracy Preservation: Maintained accuracy comparable to the full attention baseline, even with aggressive sparsity settings.
- Outperforms Baselines: Outperformed the QUEST baseline, especially at higher sparsity levels, demonstrating the advantage of semantic clustering.
Limitations and Future Work
- Hyperparameters: The sparsity threshold and the number of centroids are hyperparameters that need to be tuned. The optimal configuration can depend on the task and input context.
- Fixed Context Focus: The current approach is optimized for applications with a fixed context. Extending it to fully online scenarios where the entire context is dynamic would require further work, potentially by accelerating the initial offline clustering step.
- No Approximation for Less Important Keys: The method doesn't approximate attention for the less important keys. Future work could explore methods to approximate attention for these keys to further improve efficiency.
- Automated Hyperparameter Tuning: Developing automated methods to configure hyperparameters based on target accuracy and input context is a promising direction.
- Online Clustering: Adapting the offline clustering to an online setting would broaden the applicability of the method.
Summary updated