RetrievalAttention: Accelerating Long-Context LLM Inference via Vector Retrieval

Di Liu, Meng Chen, Baotong Lu, Huiqiang Jiang, Zhenhua Han, Qianxi Zhang, Qi Chen, Chengruidong Zhang, Bailu Ding, Kai Zhang, Fan Yang, Yuqing Yang, Lili Qiu

TL;DR

RetrievalAttention is a training-free inference method that enables 8B-parameter Large Language Models (LLMs) to handle 128K-token contexts on a single consumer GPU (like an RTX 4090). By offloading the massive Key-Value (KV) cache to CPU memory and using a novel "attention-aware" vector index to retrieve only the most critical tokens, it achieves near-full accuracy while processing only 1–3% of the context data.

The KV Cache Bottleneck

As LLMs scale to handle "long contexts" (millions of tokens), they hit a massive hardware wall: the KV cache bottleneck.

Every token in a prompt generates a Key and a Value vector that must be stored to generate future tokens. For a 1-million-token prompt on a Llama-3-8B model, this cache consumes a staggering 125GB of memory—far exceeding the 24GB or 80GB found on high-end GPUs.

Existing solutions usually involve:

  1. Pruning/Compression: Deleting "unimportant" tokens, which often tanks accuracy because "importance" changes dynamically as the model generates text.
  2. CPU Offloading: Moving the cache to system RAM, which is plentiful but slow. Fetching the entire cache over the PCIe bus for every single token creates a massive latency bottleneck.

The authors identify a specific gap: while attention is mathematically "sparse" (only a few tokens really matter at any given time), we lack an efficient way to find those tokens without scanning the entire context.

Introducing RetrievalAttention

RetrievalAttention treats attention as a vector retrieval problem, utilizing three core mechanisms to bypass traditional bottlenecks.

1. Hybrid Storage and Co-execution

The system splits the KV cache into two parts:

  • Static/Predictable Tokens: A small set of "sink" tokens (the start of the prompt) and the most recent "sliding window" tokens are kept on the GPU.
  • The Bulk Context: The rest of the context is offloaded to CPU memory.

2. Attention-Aware Vector Indexing

This is the paper's "secret sauce." Standard Approximate Nearest Neighbor Search (ANNS) algorithms—the kind used in vector databases—assume that queries and keys come from the same distribution. However, in LLMs, query and key vectors are projected using different weights, creating an Out-of-Distribution (OOD) problem.

Distribution gap between Query and Key vectors

RetrievalAttention uses a specialized index that:

  • Uses actual query vectors during the offline indexing phase to "bridge" the gap to the key vectors.
  • Projects these connections into a graph structure that allows the CPU to find the most relevant keys in sub-linear time.

3. Parallel Processing

During decoding, the GPU computes attention on the static tokens while the CPU simultaneously searches its index for the most relevant historical tokens. The results are then merged using a re-scaling formula to ensure the final output is mathematically consistent.

Key Insights and Discoveries

  • The OOD Discovery: The authors quantify the "distribution gap," showing that query vectors are often much farther from key vectors than keys are from each other. This explains why "off-the-shelf" vector search fails for LLMs.
  • Dynamic Sparsity: They demonstrate that "critical" tokens are not fixed. In a 128K context, the top-100 tokens change significantly across decoding steps, with only ~30% overlap. This proves why static pruning fails and dynamic retrieval is necessary.
Attention sparsity and dynamic token importance
  • Efficiency via Projection: By using their attention-aware index, they only need to scan 1–3% of the total key vectors to achieve 95% recall of the most important tokens.

Performance and Results

The authors evaluated RetrievalAttention across rigorous long-context benchmarks, including RULER, $\infty$-Bench, and Needle-in-a-Haystack, using models like Llama-3-8B and Yi-9B.

  • Hardware Accessibility: RetrievalAttention allows an 8B model to run a 128K context on a single NVIDIA RTX 4090 (24GB).
  • Speedup: On an RTX 4090, it achieves a decoding speed of 0.107 seconds per token. This is a 7.93x speedup over exact KNN retrieval and 2.8x faster than traditional ANNS methods.
  • Accuracy: It maintains nearly the same accuracy as "full attention" (which uses the entire cache). For example, on Llama-3-8B (RULER), it achieved 81.7% accuracy compared to the full-attention baseline of 83.9%.
  • Scalability: The method also works for 70B models, significantly outperforming other sparse attention methods like Quest in both speed and accuracy.

Limitations and Future Work

While highly effective, RetrievalAttention is currently optimized for "fixed" prefixes (like a large document you ask multiple questions about). It requires an offline index-building step, which takes time (e.g., ~788 seconds for 100K tokens on Llama-3-8B). Furthermore, the paper focuses on the decoding phase; the initial prefill phase still requires loading the cache to the GPU once. Future work will likely explore advanced acceleration techniques to reduce index-building costs for on-the-fly context growth.

Summary updated

BibTeX
@inproceedings{liu2025retrievalattention,
  author = {Di Liu and Meng Chen and Baotong Lu and Huiqiang Jiang and Zhenhua Han and Qianxi Zhang and Qi Chen and Chengruidong Zhang and Bailu Ding and Kai Zhang and Chen Chen and Fan Yang and Yuqing Yang and Lili Qiu},
  title = {{RetrievalAttention: Accelerating Long-Context LLM Inference via Vector Retrieval}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=8z3cOVER4z},
}
Figure