Sparse Attention for Ultra-Long Contexts: Removing the Hidden Cost of Indexing
Sparse attention is now standard for long-context language models, but at ten million tokens the indexer that selects which tokens to attend to can take most of the prefill time. This article explains why indexing cost still grows quadratically with context length, and how Latent Sparse Attention (LSA) moves token selection into a low-dimensional latent space. It covers the reported speedups, their limits, and practical lessons for teams building long-context inference systems.
Background: Sparse Attention Has Become Standard for Long Contexts
The context windows of large language models have grown faster than expected. Models that once offered a few tens of thousands of tokens by default now treat a context of one million tokens as standard across their services. Some architectures also combine sparse attention with linear attention. These developments share one technical choice: instead of having each query attend to every past token, each query picks out only the few most relevant tokens.
This approach has been repeatedly shown to work at the scale of one million tokens. As contexts grow toward ten million tokens, however, an overhead that once seemed minor starts to get out of control. Take the prefill pass of a single global attention layer at ten million tokens. Most of the time goes not to the attention computation itself but to the indexer that decides which tokens to look at. The problem has therefore changed. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What blocks ultra-long contexts now is that the process of finding that small subset still grows quadratically with context length.
A recent study from a team of research institutions and universities proposes Latent Sparse Attention (LSA). At ten million tokens, LSA speeds up the prefill of a single attention layer by tens of times, while keeping accuracy close to the original model across a range of long-context tasks. This article walks through the technical background of the method.
1. Understanding the Cost: How Attention Scales
In standard full attention, each query computes a relevance score against every previous key. With a context length of T, prefill attention costs O(T²), and each decoded token costs O(T). Growing the context from one million to ten million tokens increases the prefill attention computation by 100 times. This is the most direct cost pressure of ultra-long contexts.
Sparse attention divides the work. A lightweight indexer first selects the k most relevant keys for each query. Then the actual attention runs only over those k keys. The attention cost drops to O(Tk). Since k is usually much smaller than T, the savings are large.
This approach depends on one premise: the model needs only a small number of keys. Related experiments show that at context lengths of around 100,000 tokens, letting each query attend only to the few hundred keys most relevant to it (less than 1% of all keys) retains about 98% of full attention’s accuracy on long-context evaluation benchmarks. In other words, attending to a small portion holds up in terms of accuracy.
2. The Paradox: The Indexer Also Has to Compute Attention
A paradox hides here. To know which k keys are most relevant, the indexer must score every key. Scoring requires at least an approximate relevance computation between the query and all keys. Mainstream sparse methods reduce this cost in different ways: lowering scoring precision, compressing the representation dimension, or merging multiple keys into blocks before scoring. These techniques reduce the constant factor, but they do not change the core problem. Indexing still costs on the order of O(T²).
When the context is short, the indexer is a light module, and its share of total cost is negligible. At ten million tokens, however, this quadratic term quickly exceeds the cost of the sparse main attention (O(Tk)) and becomes the bulk of prefill time. The savings from sparse attention are then quietly consumed by the indexer. This is the “hidden bill.”
3. The Core Idea of LSA: Moving Key Search into a Low-Dimensional Latent Space
LSA starts from one observation. Index scoring is itself a form of attention computation, so optimizing it at the engineering level is not enough. The method changes it at the mathematical level. LSA works in three steps.
Project into a low-dimensional latent space. Keys and queries are each mapped by small learnable projection matrices into a latent space whose dimension is much lower than the original hidden size. Relevance scoring happens in this latent space. The cost of each score drops from O(d) to O(d’), where d’ is much smaller than d.
Run top-k retrieval in the latent space. The scoring matrix still has O(T²) entries, but the cost of generating each entry is greatly reduced. More importantly, once the latent dimension is low enough, efficient approximate nearest neighbor search structures, such as block-wise maintained key indexes, can replace full scoring. This reduces top-k selection from quadratic to near sub-linear.
Run main attention only over the k selected keys in the original space. After retrieving candidate keys, the method returns to the original representation space for exact attention. The final output is therefore unaffected by the latent-space approximation. Approximation happens only during selection, not during computation.
The key difference from traditional sparse methods is this. Traditional methods share one representation space between the indexer and main attention, so their indexing cost has a natural lower bound. LSA decouples indexing from main attention. The indexer only needs to find the right keys quickly, not to compute scores precisely.
4. Results and Trade-offs
In prefill tests at the ten-million-token scale, the method speeds up a single global attention layer by more than 20 times in a model with a hybrid architecture. For a full-attention model with a larger parameter count, the single-layer speedup reaches more than 70 times. Long-context evaluation accuracy stays essentially on par with the original models. This supports the core claim: the bottleneck of ultra-long contexts is not computing attention but finding the tokens worth attending to.
The approach has limits. First, the gains mainly appear in the prefill stage. The retrieval pattern during decoding differs, so it requires a separate design. Second, the latent projection matrices need training or fine-tuning. Applying them directly to a trained model may require a small amount of calibration data. Third, approximate latent-space retrieval has a recall problem. Redundancy across blocks and expanded candidate sets are needed to keep critical tokens from being missed.
5. Lessons for Engineering Practice
For teams deploying long-context systems, the approach offers several transferable lessons:
- Profile the cost first. Before adopting sparse attention, measure the time share of indexing versus computation. Indexing overhead is negligible for short contexts. Once the context reaches ten million tokens, the indexer becomes the main bottleneck.
- Decouple retrieval from computation. Letting retrieval use lower precision, lower dimensions, and approximate methods, while reserving exact computation for the final attention, is a general pattern for balancing speed and accuracy.
- Use the block structure of the KV cache. Latent-space indexes can be updated incrementally by block. This fits naturally with block-based KV cache management and can be combined with existing paged cache mechanisms in inference frameworks.
Conclusion
Sparse attention addressed the cost of attending to many tokens. LSA and similar work start to address the cost of finding the right tokens. As context lengths keep growing toward ten million tokens and beyond, making the indexing step sub-linear may become a key battleground for long-context inference. For developers building their own long-context services, understanding this route of decoupling retrieval from computation can support better architecture decisions.
