Sparse Attention at 10 Million Tokens: Key Practices Behind a 22x Prefill Speedup
As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computation to indexing. This article explains why naive indexing grows quadratically, how Latent Sparse Attention (LSA) moves scoring into a compressed latent space to approach linear complexity, and what the resulting prefill speedups look like. It also covers practical deployment guidance on hybrid layering, compression and caching, parameter tuning, accuracy guardrails, observability, and controlled experiments for validating speedup and recall as sequence length grows.
1. Background: How the Cost Structure Changes as Context Length Jumps
Over the past two years, long context has quickly become a default specification for large model serving. Million-token context has moved from an “optional feature” to a “baseline configuration,” and both research and production practice are now pushing toward tens of millions of tokens. The industry has converged on one idea: instead of having every query scan the entire token history, select a small set of the most relevant segments to participate in the computation. This sparsification approach has been validated repeatedly at the million-token scale, with controllable accuracy loss.
But once length scales further to 10 million tokens, an overhead that used to be unremarkable starts to spiral. Take a certain class of global attention layers as an example: during prefill at the 10-million-token scale, most of the time is not spent on the matrix operations of attention itself, but is consumed by the indexer that “decides which tokens to look at.” In other words, the center of gravity in the cost structure has shifted.
The nature of the problem therefore changes. Sparse attention has already shown that “looking at only a small part” is enough to preserve accuracy; what actually blocks ultra-long context is that “finding that small part” still grows quadratically with context length. The indexing stage has become the new complexity bottleneck, while the computation stage is no longer the main issue.
2. Root Cause: Why Indexing Grows Quadratically
Naive sparse attention can be abstracted into three steps: score, select, and aggregate. Scoring requires computing relevance between each query and all historical tokens, with a scale of number of queries multiplied by history length; during prefill, the number of queries is also of the same order as the history length, so the overall process degrades to O(n²).
# Pseudocode: naive indexing stagefor q in queries: # count ~ nscores = q · K^T # score against n keys, O(n)idx = topk(scores, k) # select k of themout[q] = attn(q, K[idx], V[idx])# Overall complexity O(n^2); indexing and computation are of the same order
In engineering practice, techniques such as chunking, shared prefixes, and local windows are commonly used to mitigate this, but these methods either sacrifice recall or reduce complexity to something like O(n·√n), which is still hard to keep linear at the tens-of-millions scale. The core conflict is that indexing happens in the original token space, where both dimensionality and length inflate at the same time.
3. Core Idea: Move Indexing into the Latent Space
A class of methods known as Latent Sparse Attention (LSA) offers a new solution: instead of performing full scoring in the original token space, first compress the long history into a low-dimensional latent representation, then perform retrieval and localization in the latent space, and finally return to the original space for exact aggregation.
The key insight is that indexing only needs a “good enough candidate set,” not an exact per-token ranking. The information loss caused by compression has limited impact on candidate recall, yet it can reduce indexing complexity from quadratic to nearly linear.
# Pseudocode: latent-space indexingZ = compress(K, m) # compress n keys into m latent slots, m << nfor q in queries:s = q · Z^T # score in latent space, O(m)cand = topk(s, r) # select r latent slotsidx = refine(cand) # refine within the original segments behind the candidatesout[q] = attn(q, K[idx], V[idx])# Indexing complexity ~ O(n + m·n/m) ≈ O(n)
Three points are worth emphasizing: compression is learnable and jointly optimized with the main model; candidate refinement preserves accuracy and avoids “compression equals distortion”; and the number of slots m is the tuning knob that trades off latency and recall.
4. Complexity and Benefit Analysis
Let the history length be n, the number of compression slots be m, and the number of tokens each query ultimately participates in the computation be k. The naive approach has indexing cost O(n²); the latent-space approach has indexing cost of roughly O(n + m·k), which is approximately linear when m is far smaller than n. This means that scaling length by 10x no longer costs 100x in indexing, but close to 10x.
One class of implementations discussed publicly shows that at the 10-million-token scale, prefill for a single global attention layer can achieve more than 20x speedup, with speedup ratios reaching tens of times under some model configurations, while accuracy remains close to the original model across a range of tasks. Note that the specific multiple is strongly correlated with model architecture, layer type, and compression ratio; engineering adoption should be based on measurements against your own baseline.
5. Key Practices for Production
- Hybrid layering: enable latent-space indexing only for global layers, and keep window attention for local layers, balancing cost and accuracy.
- Compression and caching: latent representations can be reused, and combined with prefix caching this significantly reduces repeated prefill cost.
- Parameter tuning: tune m and the candidate count r first, observe the recall and latency curves, and find the inflection point.
- Accuracy guardrails: build regression sets for long-document retrieval, multi-hop question answering, summarization, and similar tasks to prevent “faster but less accurate.”
- Observability: instrument indexing time, candidate hit rate, and compression error separately to make it easier to locate bottlenecks.
In cloud practice, you can combine a container platform to host inference services, object storage to hold long-document corpora, and logging plus monitoring and alerting to track prefill latency and recall metrics, forming a closed loop.
6. Recommended Validation Method
A controlled experiment is recommended: fix the model and data, switch only the indexing strategy, and compare three curves: prefill latency, end-to-end throughput, and task accuracy; scale length gradually from 1M to 10M and observe whether the speedup ratio stays stable. If the speedup ratio widens as length increases, it indicates that complexity has shifted from quadratic to linear and the approach is working.
7. Conclusion
The next threshold for ultra-long context is not “computing fast,” but “finding accurately and cheaply.” Migrating indexing from the original token space to a compressed latent space is currently one of the most cost-effective directions. For developers, grasping the three knobs of compression ratio, candidate size, and hybrid layering makes it possible to obtain both an order-of-magnitude speedup and usable accuracy at the tens-of-millions context scale.
