Latent Sparse Attention: Speeding Up Long-Context Inference at 10M Tokens
As context windows grow toward millions of tokens, the real bottleneck in sparse attention shifts from the attention computation itself to the indexer that selects which keys to attend to. This article explains how Latent Sparse Attention (LSA) restructures indexing into a two-stage retrieve-then-rerank pipeline in a low-dimensional latent space, analyzes the cost structure of full versus sparse attention, and outlines seven practical engineering details—from recall monitoring to KV cache layout and fallback paths—for teams building 10M-token context systems.
Latent Sparse Attention: Speeding Up Long-Context Inference at 10M Tokens
1. The Cost Center Shifts as Context Gets Longer
The first phase of the long-context race was about whether models could stretch their context windows at all. Now that million-token contexts are becoming a common configuration, the industry is validating a different question: does every query really need to attend to every historical token? Extensive experiments show that in long-context tasks, the truly critical keys often make up only a tiny fraction of the total. Sparse attention has consequently become core infrastructure for ultra-long contexts: a lightweight indexer first picks a small set of candidate keys for each query, and attention computation happens only over those candidates.
The cost structure of standard full attention is straightforward. With a context length of T, the prefill phase requires every query to compute relevance against all keys, for a total cost of roughly O(T²); decoding costs O(T) per generated token as it interacts with historical keys. When context grows from 1 million to 10 million tokens, the theoretical attention compute in prefill increases about 100-fold. Sparse attention aims to compress the actual attention computation to O(Tk), where k is the number of keys retained per query—typically far smaller than T.
But there’s a hidden bill that’s easy to overlook: the indexer itself must decide which tokens to look at. If the indexer still has to score every key for every query, its cost remains O(T²). Techniques like block sparsity, low-dimensional projection, and multi-key merging can reduce constant factors, but they hardly change the quadratic scaling trend. Public experiments offer a striking illustration: during prefill of a single global attention layer in a hybrid attention model at the 10-million-token scale, roughly 98% of the time was spent not on attention computation, but on the indexer. The nature of the problem has changed. Sparse attention has already proven that “looking at only a small subset” preserves accuracy; what actually blocks 10-million-token contexts is that “finding that small subset” is still too expensive.
2. The Sparse Attention Paradox: The Sparser You Get, the More the Indexer Matters
The sparse approach works only if the model truly needs few keys. Public benchmarks show that at a 128K-token context length, letting each query attend to only its roughly 512 most relevant keys—about 0.8% of all keys—retains approximately 98% of full attention’s accuracy on long-context benchmarks. This means sparsity itself is not the main source of accuracy loss; whether the indexer can reliably recall the truly important keys is.
This creates a paradox: to know which 512 keys matter most, the traditional approach is to score every key first. Lowering indexer precision, reducing scoring dimensions, and compressing multiple keys into block-level representations are all common optimizations—but they mostly reduce per-unit cost rather than eliminating the exhaustive candidate search. The longer the context, the more the indexer becomes a new bottleneck.
So the next generation of ultra-long-context attention must solve not “whether to be sparse” but “how to find sparse candidates with sub-quadratic complexity.” The core value of Latent Sparse Attention (LSA) lies in moving the indexing process from pairwise scoring in the original high-dimensional token space to coarse candidate generation in a low-dimensional latent space, followed by fine-grained reranking over a small set of candidates.
3. LSA’s Core Idea: Splitting “Score Everything” into “Coarse Filter + Fine Rerank”
LSA doesn’t eliminate the indexer—it restructures how the indexer works. A practical implementation path has three layers:
- Compressed representation: Project keys into a low-dimensional latent space to obtain compact representations that preserve the principal directional information. The goal of compression is not to replace the original keys, but to provide a low-cost summary for candidate generation.
- Coarse candidate generation: Maintain block-level or cluster-level centers in the latent space. Queries first interact with a small number of centers to select candidate buckets or regions. This stage no longer scores every key pairwise.
- Fine-grained reranking: Use the original keys to compute relevance only within candidate regions, select the final top-k, and run attention over them.
Indexing cost thus changes from “score all keys for every query” to “coarse-filter against a few candidate centers, then rerank candidate keys.” When the number of candidate centers and the key count per bucket are kept constant or grow slowly, the indexer as a whole approaches linear or sub-quadratic complexity instead of continuing to scale with T².
Pseudocode illustration:
def lsa_prefill(Q, K, V, top_k, candidate_buckets):# 1. Low-dimensional latent projection to produce low-cost summariesK_latent = project_latent(K)Q_latent = project_latent(Q)# 2. Block-level/cluster-level centers with incremental maintenancecenters = build_or_update_centers(K_latent)# 3. Each query coarsely filters against only a few centers to get candidate bucketsbucket_ids = top_candidates(Q_latent, centers, candidate_buckets)# 4. Rerank with original keys only within candidate bucketscandidate_keys = gather_keys(K, bucket_ids)scores = Q @ candidate_keys.Tselected = top_k(scores, top_k)# 5. Sparse attention runs only on the selected keysreturn softmax(scores[selected]) @ gather_values(V, selected)
The point of this pseudocode isn’t the specific interfaces but the cost boundaries: the coarse-filter stage’s relationship to token count is controlled by the compressed representation and a fixed candidate count; the rerank stage handles only a small set of candidates; and the final attention computation happens only over the top-k. Only when all three combine can the indexer avoid becoming a quadratic bottleneck again at the 10-million-token scale.
4. Seven Engineering Details You Must Get Right
First, candidate recall matters more than single-point latency. If the coarse filter misses a truly critical key, no amount of reranking speed can recover the accuracy loss. In practice, monitor candidate bucket recall and top-k overlap rates, and run regression tests on long-context evaluation sets.
Second, combine block-level and token-level indexing. Pure block-level indexing is cheap but carries more intra-block noise; pure token-level indexing is more accurate but expensive. A common approach is block-level coarse filtering followed by token-level reranking, while preserving a local window and a few global anchor tokens to keep critical information from being fragmented across block boundaries.
Third, calibrate the latent dimension. Too low a dimension loses discriminative power; too high drives coarse-filter costs back up. Evaluate offline against your task distribution, key vector dimensions, and candidate bucket counts—not just single-layer latency.
Fourth, optimize prefill and decode separately. Prefill can build candidate centers in batches and is suited to throughput optimization. Decode queries the index for every generated token, so it should reuse block-level summaries and candidate bucket caches to reduce redundant computation.
Fifth, align KV cache management with sparse candidates. Physically lay out candidate buckets contiguously to enable batched gathers, and maintain stable caching strategies for hot buckets, local windows, and global tokens to reduce random memory access overhead.
Sixth, cover the indexer in training objectives. If the indexer is swapped in only at inference time, training and inference distributions can diverge. Use distillation, auxiliary losses, or recall supervision so the indexer learns to approximate the important-key distribution under full attention.
Seventh, keep a fallback path. For accuracy-sensitive tasks, set a candidate recall threshold; when coarse-filter confidence is insufficient, expand the candidate range or fall back to a denser attention path, trading controlled latency for stability.
5. How to Read the Published Speedup Numbers
Public experiment reports show that in a single-layer prefill scenario at the 10-million-token scale, different architectures achieved speedups of roughly 22x and 72x respectively, while maintaining accuracy close to the original models across a range of tasks. These numbers need careful interpretation: they represent local single-layer prefill speedups, not end-to-end inference throughput gains, and the magnitude depends heavily on model structure, layer type, candidate bucket settings, and hardware batch sizes.
The truly valuable takeaway is the trend: as contexts enter the tens of millions of tokens, the optimization focus must expand from “sparse attention computation” to “sparse candidate search.” Compressing attention itself to O(Tk) is not enough—the indexer must also move to sub-quadratic complexity. LSA represents exactly this direction: latent-space compression to lower coarse-filter cost, candidate generation to avoid exhaustive scoring, and reranking to guarantee recall quality.
For teams building ultra-long-context capabilities, the practical path comes down to three steps: first, verify that your tasks genuinely need only a small set of critical keys; second, establish a recall and accuracy regression framework; third, incorporate indexer cost into your end-to-end latency breakdown and keep iterating on coarse filtering, reranking, caching, and fallback strategies. In the 10-million-token era, the competition in sparse attention is no longer just about “looking at less”—it’s about “finding the right things, fast.”
