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

TL;DR

DuoAttention is a new framework that slashes the memory and latency of long-context LLM inference by identifying that only a small fraction of attention heads (Retrieval Heads) actually need to see the full history. By using a constant-length cache for the remaining "Streaming Heads," the authors achieved up to a 2.55× reduction in memory and enabled a Llama-3-8B model to handle 3.3 million tokens on a single A100 GPU.

What Problem Does This Paper Address?

As Large Language Models (LLMs) move toward processing massive contexts—like entire book series or hour-long videos—they hit a "memory wall." The standard Key-Value (KV) Cache, which stores the history of every token to avoid re-computation, grows linearly with sequence length. For a model like Llama-3-8B, a 1-million-token context requires 137GB of VRAM just for the cache, far exceeding the 80GB capacity of a high-end A100 GPU.

Existing solutions like KV pruning (e.g., H2O or TOVA) often discard information that might be needed later, "breaking" the model's ability to retrieve specific details from the distant past. Furthermore, many of these methods only speed up the decoding (generating text) phase, leaving the pre-filling (initial prompt processing) phase as a massive computational bottleneck.

Methods

The researchers observed a fundamental dichotomy in how attention heads behave:

  1. Retrieval Heads: A small subset of heads that are critical for "needle-in-a-haystack" tasks. They need to attend to specific, relevant tokens anywhere in the past.
  2. Streaming Heads: The majority of heads, which primarily focus on very recent tokens and "attention sinks" (the very first few tokens of a prompt).
Retrieval vs. Streaming Heads and their impact on accuracy.

DuoAttention exploits this by keeping a full KV cache only for Retrieval Heads and a lightweight, constant-length cache for Streaming Heads.

Key Innovations:

  • Optimization-Based Identification: Instead of just looking at attention weights (which can be noisy), the authors use a lightweight optimization process. They assign a trainable "gate" to each head and use a synthetic dataset to see which heads' outputs change significantly when their history is pruned. This allows them to precisely pinpoint the essential Retrieval Heads.
  • Head Reordering: To make deployment efficient, they reorder the model's weights so that Retrieval and Streaming heads are grouped together. This allows for fast, contiguous memory operations during inference.
  • Chunked Pre-filling: DuoAttention is designed to work with chunked pre-filling, reducing the quadratic time complexity of the initial prompt processing to linear time for the streaming heads.

Datasets

  • Synthetic Passkey Dataset: Used for the optimization phase. It involves embedding multiple "passkeys" (random strings) at various depths in a long text and asking the model to recall them.
  • Long-Context Benchmarks: Evaluation was performed on Needle-in-a-Haystack (NIAH), LongBench (comprising 21 different tasks), and RULER.
  • Short-Context Benchmarks: To ensure the pruning didn't hurt general intelligence, they tested on MMLU (knowledge), MBPP (coding), and MT-Bench (chat).

Key Insights and Ideas

  • The 25% Rule: For many models, keeping full history for just 25% of the heads is enough to maintain near-perfect long-context retrieval.
  • Attention Scores are Deceptive: The paper highlights that heads with high attention scores aren't always the most important for the final output. Their optimization-based approach captures the "end-to-end" impact of pruning, which is more robust than simple profiling.
  • Robustness to Query Position: Unlike some recent methods (like SnapKV) that assume the "important" information is always near the end of the prompt, DuoAttention is robust. Because it identifies heads based on their function rather than a specific prompt's patterns, it works even when the query is buried in the middle of the text.

Key Results

  • Memory Efficiency: Reduced KV cache memory by 2.55× for Multi-Head Attention (MHA) models and 1.67× for Grouped-Query Attention (GQA) models.
  • Speedups:
    • Decoding: Up to 2.18× faster.
  • Pre-filling: Up to 1.73× faster.
  • Extreme Scale: By combining DuoAttention with 4-bit KV cache quantization, the team ran a Llama-3-8B model with a 3.3 million token context on a single 80GB A100.
  • Accuracy: On the LongBench suite, DuoAttention consistently outperformed other pruning methods like H2O and StreamingLLM, often matching the performance of the full-cache model.

Limitations and Future Work

  • One-Time Training Cost: Identifying the retrieval heads requires a few hours of optimization on an 8-GPU node. While this is a one-time cost per model architecture, it is an extra step in the deployment pipeline.
  • Hyperparameter Tuning: The "sparsity quantile" (what percentage of heads to prune) is a manual setting. Future work could involve automatically determining the optimal ratio for a specific memory budget.
  • Integration with Other Sparsity: The authors suggest that combining DuoAttention with weight pruning or more advanced quantization could push context limits even further, potentially into the tens of millions of tokens.

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