Scaling Sparse Attention to 10 Million Tokens: The Indexing Bottleneck and How to Speed It Up
This article explains why sparse attention, once context grows to 10 million tokens, is limited less by the attention computation itself than by the indexing step that selects which tokens to attend to. It describes how selecting tokens in a low-dimensional latent space can cut that cost, along with practical risks and a method for evaluating the approach before deployment. It also summarizes reported prefill speedups and notes that these figures come from specific models and hardware.
1. Long-Context Inference Enters a New Stage
Over the past year, support for million-token context has moved from a few experimental features to the default specification for mainstream services. Models can now read an entire book, a large codebase, or hours of meeting transcripts in one pass, which substantially broadens the range of applications.
At the million-token scale, the common industry approach is to let each query attend to only part of the historical tokens, rather than computing relevance against all of them. The accuracy of these sparse attention methods has been well validated.
When context extends to 10 million tokens, overheads that were once minor begin to dominate total latency. The central question therefore shifts from “Can the model attend to only a subset?” to “How quickly can the model find the subset it should attend to?”
2. Why the Bottleneck Appears in the Indexing Stage
The computational cost of standard attention grows quadratically with sequence length n. Sparse attention limits the number of tokens that actually participate in computation to a fixed budget k, so the cost of the main attention operation becomes close to linear.
However, before selecting these k tokens, the model must first compute a coarse relevance score for every historical position, then pick the highest-scoring positions. This scoring is usually performed by a separate indexer. The indexer’s cost still scales with context length, and it runs once for every query. As a result, the overall cost remains close to O(n²).
In prefill estimates at 10 million tokens, the indexer accounts for most of the latency, while the main attention computation is a small share. This means that optimizing only the core sparse attention computation yields gains that the indexing stage offsets.
3. The Basic Idea of Sparse Selection in Latent Space
A direct approach is to reduce the dimensionality of the scoring itself. Query and key vectors are projected into a low-dimensional latent space, and similarity is computed there. This retains the main information needed to locate key positions while significantly reducing the number of multiply-add operations.
Two conditions are critical for this approach to work:
- The projection must preserve relative ordering. If the ranking of projected scores deviates too much from the original scores, the selected tokens will drift away from the truly relevant positions, and accuracy will drop.
- The projection can be trained jointly with the model. A low-dimensional representation trained specifically to find key tokens is more reliable than a fixed dimensionality-reduction method applied after the fact.
Note that reducing dimensionality only lowers the constant factor. Scoring still grows with context length. Practical systems therefore often add chunked or hierarchical indexing, or reuse existing index results during decoding so that the entire history does not need to be rescanned at every step.
4. Illustrative Implementation
The code below only illustrates the computation flow. It is not a real interface from any framework. The low-dimensional representations of historical keys are computed once when they are written to the cache, then only read, never projected again.
# Illustrative code: shows the computation steps of latent-space sparse selectiondef sparse_attention_step(q, k_cache, v_cache, k_low_cache, proj, budget):q_low = proj(q) # Project the query into the low-dimensional latent spacescores = q_low @ k_low_cache.T # Low-dimensional scoring; cost drops with dimensiontop_idx = topk(scores, budget) # Select key positions within budget kk_sel = gather(k_cache, top_idx) # Keep only the selected keysv_sel = gather(v_cache, top_idx) # Keep only the selected valuesreturn softmax(q @ k_sel.T) @ v_sel # Compute attention over the subset
Three points matter during implementation. First, k_low_cache must be updated by appending each new token. Otherwise, the index will miss the most recent content. Second, the efficiency of topk and gather directly affects end-to-end speed, so hardware-friendly batched operators should be used. Third, the low-dimensional cache consumes additional GPU memory, which must be included in capacity planning.
5. Evaluation Methods for Engineering Deployment
Before adopting this kind of optimization, verify it with the following steps.
- Time each stage separately. Record the latency of the indexer, the main attention computation, and the other layers to identify the real bottleneck. If indexing accounts for only a small share of the time, optimizing it will yield limited gains.
- Check accuracy against a full-attention baseline. Evaluation tasks should cover retrieval and localization, long-document question answering, code understanding, and multi-turn dialogue. Beyond average scores, track recall of key information, because a small number of missed items can cause noticeable incorrect answers.
- Plot performance across context lengths. Test at multiple lengths, such as 1 million, 3 million, and 10 million tokens, and observe how the speedup changes with length. If the gains appear only within a certain range, determine whether the target workload falls within that range.
- Account for memory and cache overhead. The low-dimensional key cache and index structures both consume memory. In long-context scenarios, this overhead can be considerable.
- Distinguish prefill speed from end-to-end latency. Faster prefill does not mean response time drops by the same proportion. Decoding, network transfer, and batch scheduling all affect the latency users perceive, so measure them together.
6. Applicable Scenarios and Potential Risks
This approach suits scenarios such as retrieval and summarization over very long documents, analysis and question answering over large codebases, and conversational systems that must maintain memory over long periods. These scenarios share a common feature: the context is very long, and only a small number of segments are strongly relevant to the current query.
The limitations also need attention. If the projection dimension is too low, fine-grained semantics are lost, and similar but unrelated positions may be selected by mistake. The indexer’s effectiveness also depends on the distribution of its training data, so results on data from a substantially different domain must be validated again. For tasks that depend on exact global statistics, such as strict counting or full-text alignment, sparse selection itself may not be suitable, and these tasks should be evaluated separately.
7. Summary
- The accuracy foundation of sparse attention is relatively mature. At 10 million tokens, the new bottleneck is the indexing process that finds key tokens, and its cost still grows with context length.
- Sparse selection in latent space can reduce indexing compute, but the projected ranking must remain consistent with the original relevance.
- According to publicly reported research, the prefill speed of a single attention layer at 10 million tokens can reach tens of times that of the original approach, while maintaining accuracy close to the baseline across several task types. These figures come from specific models and hardware, so they must be retested in your own tasks and environment before deployment.
