Sparse Attention at 10 Million Tokens: The Key to a 22x Speedup
As context windows grow into the millions, sparse attention is often assumed to solve the cost problem. At 10 million tokens, however, the bottleneck shifts from computing attention to deciding which tokens to attend to. This article explains why the quadratic cost of token selection persists, how moving selection into a low-dimensional latent space reduces it, and why the resulting approximation still preserves accuracy. It also covers practical guidance on measuring index overhead, tuning block size, combining with caching, and running accuracy regression tests.
1. The Background: Longer Contexts Move the Bottleneck
Over the past two years, long-context capability has shifted from an experimental feature to a default specification. Million-token contexts are becoming a standard configuration for many model services, and architectures that mix sparse attention with linear attention are being adopted by more models. These approaches share a common premise: each query vector does not need to scan the entire token history, and only needs to select a small set of the most relevant tokens for computation.
At the million-token scale, this approach has been repeatedly proven effective. But as the context pushes toward 10 million tokens, an initially negligible overhead starts to spiral out of control. Take a global attention layer as an example: when running prefill at the 10-million-token scale, most of the time is not spent on the attention matrix multiplications, but on the indexing step that “decides which tokens to look at.”
The nature of the problem has therefore changed. Sparse attention has long proven that “looking at only a small portion” is enough to preserve accuracy. What truly blocks ultra-long contexts is that the cost of “finding that small portion” still grows quadratically with context length.
2. A Complexity Perspective: The Quadratic Term Has Not Disappeared, It Has Moved
The computation of dense attention grows quadratically with sequence length. Sparse attention reduces the number of tokens involved in computation to a constant or sublinear level, which appears to solve the problem. However, the selection process itself typically requires scoring or clustering the full history first, and then taking the Top-K. The cost of this step is still quadratic.
A typical flow can be described with pseudocode:
# Traditional sparse attention: selection cost is still quadraticscores = query @ keys.T # O(L^2 * d)idx = topk(scores, k) # O(L^2)out = softmax(query @ keys[idx]) # O(L * k * d)
The conclusion is straightforward: the computation has decreased, but memory access and scoring/sorting have not. On real hardware, the latter is often the main source of wall-clock time.
3. The Core Idea: Move “Selection” into Latent Space
The key to latent-space sparse attention is to stop performing indexing in the original token dimension. Instead, construct a low-dimensional latent representation, perform similarity retrieval and candidate filtering in the latent space, and then map back to the original token set. This brings three benefits:
- The scoring dimension is greatly reduced, and the constant in front of the quadratic term is significantly compressed;
- Candidate filtering can be better combined with tensor parallelism and block-wise prefetching, making memory access more contiguous;
- The index structure is jointly optimized with model training, so retrieval quality does not depend on additional heuristic rules.
In engineering practice, this is usually combined with a block-wise strategy: split the history into fixed-size blocks, score block-level representations in the latent space, select blocks first and then select tokens within a block. This forms a two-level filtering process that brings the complexity close to linear.
4. Results and Accuracy: Why “Approximation” Is Still Usable
At the 10-million-token scale, this class of methods can achieve more than a 20x speedup in the prefill stage of a single attention layer, with even higher speedups on some models, while maintaining accuracy close to the original model on tasks such as long-text retrieval, multi-document question answering, and code base understanding.
The reason accuracy is preserved is that latent-space retrieval retains the main structure of semantic similarity, and attention itself is fairly robust to the absence of a small number of irrelevant tokens. Note that the speedup is strongly correlated with model architecture, the number of attention heads, and block size, and cannot be simply extrapolated to all scenarios.
5. Implementation Recommendations
- Measure the index share first: use logging services or monitoring alerts to confirm whether prefill time is actually spent on the indexing step, and avoid optimizing in the wrong direction.
- Stress-test block sizes: too small a block amplifies scheduling overhead, while too large a block degrades back to approximate dense attention.
- Combine with caching: prefix caching and KV reuse can further amortize long-context costs.
- Accuracy regression is not optional: run A/B comparisons with a long-text task set and watch for fluctuations in recall-type metrics.
- Deployment: elastic scaling on container platforms and sample management in object storage can make stress testing and canary rollouts smoother.
6. Conclusion
The next threshold for ultra-long contexts is not “being able to compute,” but “selecting accurately and quickly.” Moving the indexing problem into a low-dimensional latent space is currently one of the most cost-effective engineering directions. For teams that need to serve 10-million-token scenarios, examining index overhead first often brings order-of-magnitude latency improvements compared with continuing to optimize matrix multiplication.
