DuoAttention: Efficient Long-Context LLM Inference with Retrieval and Streaming Heads

Guangxuan Xiao, Jiaming Tang, Jingwei Zuo, Junxian Guo, Shang Yang, Haotian Tang, Yao Fu, Song Han

Abstract

Deploying long-context large language models (LLMs) is essential but poses significant computational and memory challenges. Caching all Key and Value (KV) states across all attention heads consumes substantial memory. Existing KV cache pruning methods either damage the long-context capabilities of LLMs or offer only limited efficiency improvements. In this paper, we identify that only a fraction of attention heads, a.k.a, Retrieval Heads, are critical for processing long contexts and require full attention across all tokens. In contrast, all other heads, which primarily focus on recent tokens and attention sinks—referred to as Streaming Heads—do not require full attention. Based on this insight, we introduce DuoAttention, a framework that only applies a full KV cache to retrieval heads while using a light-weight, constant-length KV cache for streaming heads, which reduces both LLM's decoding and pre-filling memory and latency without compromising its long-context abilities. DuoAttention uses a lightweight, optimization-based algorithm with synthetic data to identify retrieval heads accurately. Our method significantly reduces long-context inference memory by up to 2.55x for MHA and 1.67x for GQA models while speeding up decoding by up to 2.18x and 1.50x and accelerating pre-filling by up to 1.73x and 1.63x for MHA and GQA models, respectively, with minimal accuracy loss compared to full attention. Notably, combined with quantization, DuoAttention enables Llama-3-8B decoding with 3.3 million context length on a single A100 GPU.

1. Introduction

As Large Language Models (LLMs) scale toward processing millions of tokens, they encounter a significant bottleneck: the Key-Value (KV) cache. Transformers store KV states for all previous tokens to avoid recomputation, but this cache grows linearly with sequence length. For instance, running Llama-3-8B with a 1-million-token context requires approximately 137 GB of VRAM for the KV cache alone, far exceeding the 80 GB capacity of a top-tier NVIDIA A100 GPU. While existing methods attempt to prune or compress this cache, they often compromise the model's ability to retrieve specific information from the middle of a long text (the "needle in a haystack" problem) or offer only marginal speed improvements.

2. The DuoAttention Framework

DuoAttention addresses this bottleneck by identifying that attention heads are not uniform in their function. The framework categorizes heads into two distinct types:

  1. Retrieval Heads: These are the "heavy lifters" that look back across the entire history to find specific, contextually relevant information. They require a full KV cache to maintain performance.
  2. Streaming Heads: These heads primarily focus on "attention sinks" (the very beginning of the text) and the most recent tokens. They can function effectively with a very small, fixed-size KV cache.
Visualization of Retrieval and Streaming heads and their impact on model accuracy.

By applying a full KV cache only to Retrieval Heads and a lightweight, constant-length cache to Streaming Heads, DuoAttention significantly reduces memory overhead without damaging the model's long-context capabilities. In models like Llama-2-7B, only about 25% of heads are actually Retrieval Heads, meaning 75% of the memory typically spent on the KV cache for other heads is essentially wasted.

3. Identification and System Optimization

To accurately identify Retrieval Heads, DuoAttention uses a lightweight, optimization-based algorithm. Unlike previous methods that rely on noisy attention scores, this method measures actual output deviation. It assigns a trainable "gate" value to each head and optimizes it using synthetic "passkey retrieval" data to minimize the difference between the pruned and full model outputs. This calibration process is efficient, typically requiring only a few hours on a single node.

To maximize speed in practice, the system reorders the model's weights so that Retrieval and Streaming heads are grouped together. This allows for efficient memory slicing during inference, accelerating both the pre-filling and decoding phases.

4. Experimental Results

The researchers evaluated DuoAttention across a variety of benchmarks, including Needle-in-a-Haystack (NIAH), LongBench, and general short-context tasks like MMLU and MBPP.

Efficiency Gains: - Memory Reduction: Up to 2.55x for Multi-Head Attention (MHA) and 1.67x for Grouped-Query Attention (GQA) models. - Latency Speedups: Decoding speed increased by up to 2.18x, while the pre-filling phase saw up to 1.73x acceleration. - The 3.3M Token Milestone: By combining DuoAttention with 4-bit KV cache quantization, the team successfully ran a Llama-3-8B model with a 3.3 million token context on a single 80GB A100 GPU.

Accuracy and Robustness: DuoAttention maintained near-perfect retrieval on NIAH tests at lengths where competing compression methods (like H2O or StreamingLLM) completely failed. Crucially, the pruning of Streaming Heads did not "lobotomize" the model; performance on general intelligence benchmarks remained stable, ensuring the model's utility across diverse tasks.

5. Conclusion

DuoAttention provides a scalable solution for long-context LLM inference by exploiting the functional specialization of attention heads. While it requires a one-time offline calibration for each model architecture, the resulting efficiency enables processing massive contexts on commodity hardware. Future work may explore integrating these attention patterns directly into the training process to develop models that are inherently more efficient.

Summary updated

BibTeX
@inproceedings{xiao2025duoattention,
  author = {Guangxuan Xiao and Jiaming Tang and Jingwei Zuo and Junxian Guo and Shang Yang and Haotian Tang and Yao Fu and Song Han},
  title = {{DuoAttention: Efficient Long-Context LLM Inference with Retrieval and Streaming Heads}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=cFu7ze7xUm},
}
Figure