Exploiting Sparsity for Long Context Inference: Million Token Contexts on Commodity GPUs

Ryan Synk, Monte Hoover, John Kirchenbauer, Neel Jain, Alex Stein, Manli Shu, Josue Melendez Sanchez, Ramani Duraiswami, Tom Goldstein · DOI

Introduction

As Large Language Model (LLM) context windows expand into the millions of tokens, inference systems face a significant "memory wall." In standard Transformer architectures, every generated token must attend to all previous tokens, whose data is stored in the Key-Value (KV) cache. For a model like Llama-3 8B, a 100,000-token context requires approximately 52GB of VRAM—far exceeding the 16GB–24GB capacity of consumer-grade GPUs. Existing solutions like offloading (moving the cache to CPU) are often bottlenecked by data transfer speeds, while eviction (deleting tokens) risks losing critical information.

Methodology: Top-$k$ Attention and CPU-Side Databases

The proposed approach treats the KV cache as a searchable database rather than a linear list. This system relies on two primary components:

  1. Top-$k$ Attention: Instead of computing attention scores for all $N$ tokens, the model retrieves only the $k$ tokens with the highest potential scores.
  2. CPU-Side Vector Database: The bulk of the KV cache is stored in cheap, plentiful CPU memory. During decoding, the GPU sends a query vector to the CPU, which performs an Approximate Nearest Neighbor (ANN) search to retrieve the top-$k$ keys and values.
Figure 1: Comparison between standard GPU attention and the proposed hybrid GPU-Database architecture.

This hybrid architecture maintains a small "window" of recent tokens on the GPU for local context while querying "distant memory" from the CPU database only when relevant.

Experimental Setup

The method was evaluated using several benchmarks to ensure that sparse attention does not degrade reasoning capabilities:

  • RULER: Tests long-context capabilities up to 128k tokens, including "Needle In A Haystack" (NIAH) tasks.
  • AlpacaEval 2.0: Measures open-ended generation quality.
  • Open LLM Leaderboard: Assesses general reasoning (MMLU, ARC, HellaSwag).
  • Custom 1M Token NIAH: Validates performance at the million-token scale.

Key Insights: The Nature of Attention Sparsity

Analysis reveals that attention is naturally sparse; in a 1,000-token context, models often require only the top 10–20 tokens to maintain performance. Furthermore, attention patterns are layer-dependent: earlier layers exhibit higher entropy (broader attention), while deeper layers are more focused. This allows for layer-wise adaptivity, where smaller $k$ values can be used in later layers to further reduce compute.

Results and Discussion

The system successfully enabled a 1-million token context on a single 16GB VRAM GPU. By attending to less than 2% of input tokens, the models maintained over 95% of their original performance.

Figure 2: Average performance of Llama models as a function of Top-k retrieval.

Notably, the top-$k$ method achieved a 100% success rate on the 1M-token "Needle In A Haystack" task with $k$ as low as 10. Unlike cache eviction methods that might delete the "needle," this search-based approach ensures all tokens remain accessible. Additionally, because ANN search is efficient, the inference cost scales sublinearly with context length.

Limitations and Future Work

Despite these gains, the "prefill" stage (initial document processing) remains an $O(N)$ or $O(N^2)$ bottleneck, currently requiring high-end hardware. Additionally, the overhead of building the CPU-side vector index may outweigh benefits for short contexts. Future optimizations will focus on reducing PCIe transfer latency and accelerating index construction.

Summary updated

BibTeX
@inproceedings{synk2025exploiting,
  author = {Ryan Synk and Monte Hoover and John Kirchenbauer and Neel Jain and Alex Stein and Manli Shu and Josue Melendez Sanchez and Ramani Duraiswami and Tom Goldstein},
  title = {{Exploiting Sparsity for Long Context Inference: Million Token Contexts on Commodity GPUs}},
  booktitle = {VecDB @ ICML},
  year = {2025},
  url = {https://openreview.net/forum?id=1iBrBNngRh},
}
Figure