Sparse Attention at 10M Token Context: The Key to a 22x Speedup
As context windows scale from millions to tens of millions of tokens, the bottleneck in long-context inference shifts from computation to retrieval. This article explains why standard full attention becomes unsustainable at 10M tokens, how sparse attention reduces the main attention cost to O(Tk), and why the indexer itself becomes a hidden O(T²) expense. It then introduces Latent Sparse Attention, which moves relevance scoring into a fixed low-dimensional latent space to make indexing near-linear, alongside pseudocode, engineering considerations, reported speedups, accuracy boundaries, and practical adoption advice for engineering teams.
1. Long Context Shifts the Cost Burden from Compute to Search
Over the past two years, long-context capability has moved quickly from an experimental feature to a default specification. Million-token context windows are now standard in many model services. Push further to 10 million tokens, and the real question is no longer whether a model can read that much text, but whether the cost structure of reading it is reasonable.
The cost of standard full attention is easy to understand. With a context length of T, the compute in the prefill stage is roughly O(T²), and decoding each token requires O(T). When the context grows from 1M to 10M tokens, the compute for the attention portion increases by about 100x. At the million-token scale, this curve can still be handled with raw compute and parallelism. At 10M tokens, it effectively spirals out of control.
Sparse attention offers a division of labor: a lightweight indexer first selects the k most relevant keys for each query, and the actual attention is computed only over those k keys, reducing the main attention cost to O(Tk).
The premise behind this approach has been repeatedly validated: models really do need only a tiny fraction of the keys. With a 128k context, letting each query attend to only its 512 most relevant keys (about 0.8% of all keys) still preserves roughly 98% of full-attention accuracy on long-context retrieval benchmarks. In other words, attending to a small subset is not a compromise; it is sufficient.
2. The Overlooked Hidden Bill: The Indexer Itself
The problem lies with the indexer. To decide which 512 keys are most relevant, the indexer must first score every key, and this step is inherently on the order of O(T²). This creates a paradox: the main attention cost is optimized away, while the remaining indexing step becomes the dominant term.
A quantified example illustrates the issue: in a global attention layer of a model that supports million-token context, roughly 98% of the prefill time for 10M tokens is spent deciding which tokens to attend to, while the actual attention computation accounts for only 2%.
Common industry mitigations all amount to reducing the indexer’s workload: lowering scoring precision, compressing the index dimension, or aggregating multiple keys into a block before scoring (block sparsity). These techniques help, but they only shrink the constant factor. They do not change the fact that indexing complexity grows quadratically with context length. Increase the context tenfold, and the bill still scales with the square.
3. Latent Sparse Attention: Moving the Search into a Low-Dimensional Space
An approach called Latent Sparse Attention reframes the question: why must retrieval happen in the original key space?
Its core idea is to move relevance retrieval entirely into a low-dimensional latent space. A shared projection first compresses historical tokens into low-dimensional latent representations. The query side applies the same projection. Scoring and top-k selection then happen in the latent space, and only the small set of selected tokens is mapped back to the original space to participate in the actual attention. Because scoring and retrieval occur in a fixed-dimension latent space, the complexity of the indexing step drops from quadratic growth with T to approximately linear, taking the overall process from quadratic to sub-quadratic.
The following illustrative pseudocode expresses this structure:
# z: low-dimensional latent representation, fixed dimension d_latent, independent of context lengthz_kv = project_to_latent(kv_hidden) # O(T * d_latent)scores = query_latent @ z_kv.T # near-linear indexing scoresidx = topk(scores, k) # select k relevant positionsattn = softmax(q @ k[idx].T) @ v[idx] # compute only over k keys
Two engineering points complete the design. First, the latent projection needs to be trained jointly with the backbone so that the latent space genuinely carries relevance semantics rather than acting as a simple dimensionality reduction. Second, block-level prefetching and memory-access alignment are needed to prevent the random access of top-k selection from consuming the compute advantage.
4. Results and Boundaries
Public results show that at the 10M token scale, this class of methods speeds up the prefill of a single attention layer by around 22x, reaching on the order of 72x in some model configurations, while staying close to the original model’s accuracy on tasks such as long-context retrieval and multi-hop reasoning.
The boundaries matter. The speedup ratio depends heavily on model architecture, the type of attention layer (global and local layers benefit differently), the value of k, and implementation quality. A latent dimension that is too small loses retrieval accuracy, while one that is too large weakens the speedup. Before deployment, run a joint accuracy-latency evaluation on your own data rather than applying numbers from a paper directly.
5. Practical Advice for Engineering Teams
- Start with cost attribution. Use monitoring and logging services to break down prefill-stage latency, confirm whether the indexing step is truly the dominant term, and then choose an optimization direction.
- Select by layer type. Keep dense computation in local attention layers and introduce latent retrieval in global layers; this usually offers the best benefit-to-risk ratio.
- Guard accuracy. Add long-context retrieval evaluations to the regression pipeline, and require any sparsification change to pass an accuracy threshold.
- Plan capacity and elasticity. Very long context inference is sensitive to GPU memory and bandwidth, so consider pairing elastic scaling on a container platform with externalized session state in object storage.
- Roll out gradually. Use canary releases segmented by context length, covering scenarios such as long-document summarization and code-base understanding first, then expanding access step by step.
The next battle for sparse attention is not to keep proving that attending to less is enough, but to turn “finding the few tokens worth attending to” into an engineering capability that does not inflate quadratically with context length.
