Latent Sparse Attention: Keeping Indexing from Breaking Ten-Million-Token Contexts
As LLM context windows scale toward ten million tokens, the indexing stage of sparse attention—not attention computation itself—becomes the dominant cost, consuming roughly 98% of prefill time in some block-sparse models. This article explains why quadratic scoring costs in the indexer overwhelm constant-factor optimizations, introduces latent sparse attention—which compresses keys into a low-dimensional latent space before retrieval—and summarizes its speedups (22x to over 70x per layer), accuracy retention, and practical engineering guidance for building ultra-long-context inference services.
Latent Sparse Attention: Keeping Indexing from Breaking Ten-Million-Token Contexts
1. Background: Ultra-Long Context Is Now Standard, but the Cost Structure Is Shifting
Over the past year, the context length of large language models has climbed from hundreds of thousands of tokens to the million-token range, with some flagship services making a million tokens the default. Leading models have largely converged on the same design choice: instead of letting every query attend to the full history, they select only a small set of the most relevant keys for the attention computation—in other words, the sparse attention route.
This approach has been repeatedly validated at million-token scale. However, as contexts push toward ten million tokens, a previously negligible cost starts to spiral out of control: in a certain class of models using block-sparse schemes, prefilling a single global attention layer over ten million tokens spends roughly 98% of its time not on attention computation itself, but on the indexer that decides which tokens to look at.
This means the nature of the problem has changed. Sparse attention long ago proved that “looking at only a small fraction” is enough to preserve accuracy. What truly blocks ultra-long context is that “finding that small fraction” still grows quadratically with context length.
2. The Cost Accounting of Sparse Attention: The Overlooked “Hidden Bill”
Start with the cost structure of standard full attention. With a context length of T, attention computation during prefill is O(T²), and decoding each token costs O(T). Scaling context from one million to ten million tokens multiplies prefill attention computation by 100.
Sparse attention splits the work:
- The indexer: a lightweight scoring module selects the k most relevant candidate keys from the full history for each query;
- The sparse attention core: the actual attention is computed only over those k keys, reducing complexity to O(Tk).
This works provided the model genuinely needs only a small number of keys. Prior research validated this at 128k length: keeping only the 512 truly most relevant keys per query (about 0.8% of all keys) preserves roughly 98% of full-attention accuracy on long-context benchmarks. There is no dispute that “sparsity is sufficient.”
But the paradox hides one step earlier: to know which 512 keys are most relevant, the indexer must first score every key. Whether you lower scoring precision, compress the scoring dimension, or aggregate adjacent keys into blocks to reduce the number of scoring operations, all of these merely multiply the O(T²) scoring process by a smaller constant. As T moves from hundreds of thousands toward ten million, the quadratic term eventually overwhelms every optimization—the indexer turns from an auxiliary module into the new performance bottleneck.
3. The Core Idea: Move Indexing into a Low-Dimensional Latent Space
To address this bottleneck, a new technical route—latent sparse attention—proposes a key shift: instead of scoring relevance at the raw token granularity, first compress all keys into a latent space whose length is far smaller than T, and perform indexing in that compact space.
The design breaks down into three steps:
- Hierarchical compression: learnable compression modules (e.g., strided pooling or cross-attention) progressively aggregate the T keys into T/r latent vectors, where the compression ratio r can be in the tens or even hundreds.
- Latent-space scoring: the query no longer computes relevance against all T keys, but only against the T/r latent vectors, cutting indexing cost from O(T²) to O(T²/r). When r grows on the same order as T (for example, keeping the number of latent vectors near a fixed cap), overall complexity approaches sub-quadratic.
- Coarse-to-fine two-stage retrieval: high-scoring regions are first selected in latent space; fine-grained exact scoring is then applied only to the raw tokens in those regions, and the final top-k keys are handed to the attention core. Coarse filtering ensures efficiency; fine filtering ensures accuracy.
This differs fundamentally from conventional block sparsity: block sparsity is a fixed, weakly content-aware aggregation, whereas latent-space compression is learnable—the compression is trained jointly with the model and can actively preserve the information most useful for retrieval.
4. Results and Implications
In prefill tests at ten-million-token scale, this approach delivers over 22x single-layer speedup on mainstream sparse-attention architectures, and more than 70x single-layer speedup on another model without sparse optimization, while maintaining accuracy comparable to the original models across multiple long-context tasks. More importantly, indexing cost no longer grows uncontrollably with context length, turning “ten-million-token context” from a theoretical spec into an affordable engineering capability.
For the industry, this points to a more general lesson: in long-context systems, the optimization focus should shift from “reducing compute” to “re-examining the asymptotic complexity of retrieval and indexing.” Any auxiliary module that grows quadratically with input length will eventually become the new bottleneck.
5. Engineering Recommendations
Teams building ultra-long-context inference services can start from the following:
- Profile before optimizing: don’t assume the bottleneck lies in attention FLOPs. Break down prefill latency layer by layer and confirm the shares of indexing, KV reads/writes, and compression modules.
- Design indexing and retrieval in tiers: adopt a multi-stage “coarse filter + fine rerank” structure. Use low-dimensional, low-precision representations for coarse filtering to control cost, and small-scope exact computation for fine ranking to preserve recall.
- Train compression modules jointly: fixed-rule aggregation (average pooling, block representatives) is simple to implement, but learnable compression delivers noticeably more stable recall quality at the same compression ratio.
- Focus on end-to-end rather than per-layer metrics: translate single-layer speedups into full-model and real workload latency, and verify accuracy regression on tasks like long-document retrieval and multi-hop reasoning.
- Leverage infrastructure capabilities: KV caches and intermediate results at ultra-long context are massive. Combine cloud object storage, high-speed caching, and elastic compute scheduling for tiered storage and on-demand loading, so VRAM doesn’t become the new ceiling. For example, elastic compute and storage services such as those from Baidu AI Cloud can dynamically allocate inference resources based on context scale, helping control costs in long-text scenarios.
6. Conclusion
Sparse attention solved the problem of “looking at less”; latent sparse attention goes further and solves “finding it fast.” As context lengths push toward ten million tokens and beyond, real competitiveness lies not in stacking ever-longer windows, but in ensuring every stage’s complexity holds up under scaling. Moving indexing into a learnable, low-dimensional latent space is a key step toward sub-quadratic ultra-long context.
