Squeezed Attention: Accelerating Long Context Length LLM Inference

Coleman Hooper, Sehoon Kim, Hiva Mohammadzadeh, Monishwaran Maheswaran, June Paik, Michael W. Mahoney, Kurt Keutzer, Amir Gholami · DOI

Abstract

Squeezed Attention is a novel optimization technique designed to accelerate Large Language Model (LLM) inference for long context lengths. By clustering fixed-context keys offline based on semantic similarity and dynamically retrieving only the most relevant clusters during online inference, it achieves up to an 8x reduction in KV cache memory requirements and over 4x wall-clock speedups in both prefill and generation phases, with negligible loss in accuracy.

Introduction

As LLMs are increasingly deployed for complex tasks like multi-document analysis and long-form code generation, the demand for long input contexts has grown exponentially. However, long contexts necessitate a massive "KV cache"—the stored keys and values of previous tokens—which scales linearly with sequence length. This creates two primary bottlenecks: memory capacity, where the cache can exceed GPU limits, and memory bandwidth, where loading the entire cache for every generated token significantly slows down inference.

Squeezed Attention addresses these challenges by observing that in many real-world applications, such as RAG-based chatbots or document analysis tools, a large portion of the prompt (the "fixed context") remains static across multiple queries. By performing offline optimizations on this fixed context, the system can drastically reduce the online computational burden.

Methodology: Semantic Clustering and Hierarchical Retrieval

The core of Squeezed Attention is a shift from exhaustive search over the KV cache to a sparse, cluster-based retrieval system.

Offline Clustering

Before processing user queries, the algorithm performs K-means clustering on the keys of the fixed context. Unlike previous methods that group tokens by physical proximity in the text, Squeezed Attention groups keys based on their semantic similarity (embedding distance). This ensures that relevant information, even if scattered across a document, is captured within the same cluster.

Offline Hierarchical Clustering based on Semantic Similarity

Online Query-Aware Retrieval

During inference, the model compares the query token to cluster centroids using a Softmax-based importance metric. This metric predicts which clusters are likely to yield high attention scores. Only the keys and values from these "important" clusters are loaded into memory for exact attention computation.

Online Hierarchical Retrieval and Sparse Attention Computation

Hierarchical Scaling

To support contexts reaching millions of tokens, the authors introduce a hierarchical lookup. By clustering centroids into "coarse" and "fine" levels, the search complexity is reduced from linear to logarithmic relative to the context length. This allows the model to scale context size without the retrieval process itself becoming a new bottleneck.

System Optimization

The implementation utilizes custom Triton kernels for centroid comparison and a specialized FlashAttention variant. These optimizations allow the GPU to efficiently handle the sparse and imbalanced workloads generated by the retrieval process across different attention heads.

Evaluation and Results

The authors evaluated Squeezed Attention using benchmarks such as LongBench, RULER, and a new benchmark, PreFixQA, which consists of 1,127 QA pairs derived from 2024 arXiv papers to ensure no data leakage from the model's training set.

Key Performance Metrics

  • Memory Efficiency: On LongBench, the method achieved a 3.1x reduction in KV cache budget with zero accuracy loss. Even at an aggressive 8x reduction, the LLaMA-2-7B-32K model showed only a 0.5-point drop in accuracy.
  • Inference Speed: Using an NVIDIA H100 GPU, the custom kernels delivered over 4x speedups in both prefill and decode phases for a 512K context length.
  • Global Thresholding: By applying a single global Softmax-normalized threshold across all layers and heads, the model dynamically allocates more "budget" to heads with broad attention distributions and less to those with sparse "needle-in-a-haystack" patterns.

Conclusion and Future Work

Squeezed Attention demonstrates that semantic-based sparse retrieval can effectively scale LLM context lengths to millions of tokens without linear growth in latency. While currently optimized for fixed-context scenarios, future work may extend these techniques to fully dynamic contexts or incorporate low-rank approximations for "unimportant" clusters to further refine accuracy at extreme sparsity levels.

Summary updated

BibTeX
@inproceedings{hooper2025squeezed,
  author = {Coleman Hooper and Sehoon Kim and Hiva Mohammadzadeh and Monishwaran Maheswaran and June Paik and Michael W. Mahoney and Kurt Keutzer and Amir Gholami},
  title = {{Squeezed Attention: Accelerating Long Context Length LLM Inference}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure