Sparse Attention at 10 Million Tokens: The Indexing Breakthrough Behind a 22x Speedup
As context windows scale from millions to tens of millions of tokens, sparse attention shifts the bottleneck from attention computation to candidate selection. This article breaks down the cost structure of long-context attention, introduces latent-space sparse attention as a three-stage funnel that reduces indexing from quadratic to sub-quadratic complexity, and explains why the same method delivers different speedups across models. It also covers practical rollout advice and evaluation methods for engineering teams.
Introduction: The Long-Context Race Reaches 10 Million Tokens
Over the past two years, long context has quickly become a standard feature in large language model serving. Million-token context is moving from an experimental capability to a default specification, and some newly released models now mix sparse attention with linear attention at the architecture level to extend the sequence length they can handle under limited compute. The industry has reached a basic consensus: for very long sequences, letting each query stop scanning the entire history and instead focus on a few of the most relevant segments usually strikes a good balance between accuracy and efficiency.
But as context grows from millions to tens of millions of tokens, an originally unremarkable cost begins to dominate total latency. Take a global attention layer as an example: at the 10-million-token scale, the prefill phase spends most of its time not on the attention computation itself, but on the indexing step that “decides which tokens to look at.” This means the optimization focus has shifted. Sparse attention has proven that “looking at only a small part” is enough, but the cost of “finding that small part” can still grow quadratically with context length.
This article addresses that contradiction. It abstracts a general technique known as latent-space sparse attention, analyzes how it reduces the indexing stage from quadratic to sub-quadratic complexity, and outlines the key engineering considerations for putting it into practice.
1. Cost Structure Review: Why Attention Spirals Out of Control at 10 Million Tokens
The cost structure of standard full attention is clear: every query computes relevance against all previous keys. With a context length of T, the prefill phase costs roughly O(T²), and the decode phase costs roughly O(T) per generated token. As context grows from one million to ten million tokens, prefill attention computation grows by about 100x in theory.
Sparse attention splits the work into two steps:
- A lightweight indexer selects the k most relevant keys for each query.
- The actual attention is computed only over those k keys.
The second step is compressed to O(Tk), but if the first step scores every key individually, it is still O(T²). This is the “hidden bill”: attention computation gets cheaper, but the cost of filtering candidates does not drop accordingly.
Common mitigations in the industry include lowering indexing precision, compressing the index dimension, and aggregating multiple keys into a block before scoring. These methods can significantly cut constant factors, but they struggle to change the quadratic relationship between the indexing stage and context length. When T reaches the 10-million scale, the quadratic term still overwhelms all constant-factor optimizations.
A validation reported in the paper’s appendix also supports the sparse route itself: at a scale of roughly a hundred thousand tokens, letting each query attend only to the few keys that are truly most relevant to it (about 0.8% of all keys) still preserves roughly 98% of full-attention accuracy on long-context benchmarks. The problem is therefore precisely located in the indexing step, not in sparsification itself.
2. Latent-Space Sparse Attention: Moving the “Search” into a Low-Dimensional Space
The core idea of latent-space sparse attention is this: instead of doing full scoring in the original token space, first map keys and queries into a low-dimensional latent space, complete candidate selection there, and then return to the original space for exact attention.
It can be understood as a three-stage funnel:
- Stage one, compressed representation: compress the keys of a long sequence into a shorter sequence of latent representations, reducing the length from T to T/m.
- Stage two, latent-space retrieval: run similarity search over the compressed sequence to locate a small number of candidate blocks for each query.
- Stage three, intra-block exact computation: run attention only over the original keys that correspond to the candidate blocks.
Because the scale of the first two stages is compressed, the complexity of the indexing stage drops from O(T²) to sub-quadratic, while the third stage remains O(Tk). The overall bill shifts from “dominated by the quadratic term” to “dominated by the linear term,” which is exactly where the order-of-magnitude speedup at the 10-million-token scale comes from.
Illustrative pseudocode:
# z: latent compressed representation, length T/m# q: current querycand_blocks = latent_index.topk(q @ z.T, k_blocks) # sub-quadratickeys = gather(K, cand_blocks) # fetch candidate blocks onlyout = attention(q, keys) # O(Tk)
It is worth emphasizing that latent-space compression is not lossless. The compression granularity m, the latent dimension, and the number of candidate blocks together determine the trade-off between accuracy and speed, and they usually need to be calibrated against the task distribution.
3. Why the Same Method Delivers Both 22x and 72x Speedups
Differences in speedup across models come mainly from the original design of the indexer. If the original model uses a per-key scoring indexer, its indexing overhead accounts for a very high share, and replacing it with latent-space retrieval naturally yields a larger gain. If the original model already uses block sparsity or a low-dimensional index, the gain is relatively modest. This explains why the same method produces differences of more than twentyfold and more than seventyfold on different models: the denominator of the speedup ratio is the indexing time that was originally wasted, not the attention compute.
Three engineering points also deserve attention:
- Prefill and decode have different bottlenecks. Prefill is most affected by the quadratic term, while decode is mainly affected by memory bandwidth and cache hit rate. The latent representation can stay resident in cache, which brings an additional benefit.
- Candidate block granularity affects recall. Blocks that are too large raise the exact-computation cost, while blocks that are too small hurt recall, so granularity needs to be tuned per layer.
- Mixing with linear attention and local window attention can further reduce the cost of long-range dependencies, but it adds implementation complexity.
4. Rollout Advice and Evaluation Methods
For teams that want to validate this class of approach in their own systems, the recommended order is:
- Quantify the indexing share first: break down prefill latency at the target context length and confirm whether indexing has become the dominant term.
- Establish an accuracy baseline: evaluate on both long-context benchmarks and real business samples, rather than looking only at synthetic tasks.
- Replace incrementally: enable latent-space indexing on a subset of global attention layers first, and observe changes in accuracy and throughput.
- Plan capacity: combine the container platform with monitoring and alerting to watch memory usage, cache hit rate, and tail latency before deciding whether to enable it everywhere.
Conclusion
The real enemy of 10-million-token context is not attention computation, but the quadratic growth of candidate selection. Through the three-stage structure of “compressed representation + low-dimensional retrieval + intra-block exact computation,” latent-space sparse attention pushes indexing cost down to sub-quadratic and brings ultra-long-context inference back into a linear-dominated regime. For engineering teams, this is both an architecture decision and a capacity-and-cost calculation: understand the bill structure first, then decide the optimization order, and only then can you capture real cost-efficiency in the long-context race.
