Overcoming the Indexing Bottleneck in Ultra-Long Context Sparse Attention with Latent Space Compression
This article explains why indexing, rather than attention itself, becomes the main cost of sparse attention at ten-million-token context lengths. It introduces Latent Sparse Attention (LSA), which runs token retrieval in a compressed latent space so that indexing cost grows subquadratically with context length. The article also summarizes single-layer prefill speedups and accuracy results, and it offers deployment considerations for teams running long-context inference.
Million-token context windows have moved from a selling point to a baseline. Over the past year, several vendors have set one million tokens as the default specification for their flagship services, and some new models use hybrid architectures that combine sparse attention with linear attention. These models share one engineering choice: instead of having every query attend to the full history, they select a small set of the most relevant keys for each query.
This approach has been validated repeatedly at the million-token scale. As context grows toward ten million tokens, however, a cost that once looked minor starts to escalate. In a typical global attention layer, most of the prefill time over ten million tokens is not spent on attention computation. It goes to the indexing step that decides which tokens to look at.
The nature of the problem has therefore changed. Sparse attention has already shown that attending to a small fraction of tokens is enough to preserve accuracy. What now limits ultra-long contexts is that the cost of finding that small fraction still grows quadratically with context length.
2. The Cost Structure of Sparse Attention
Start with the cost of standard full attention. Each query must compute relevance against all previous keys. With context length T, prefill complexity is O(T²), and decoding each token costs O(T). Growing the context from one million to ten million tokens inflates the attention computation in prefill by roughly 100 times.
Sparse attention divides the work. A lightweight indexer first selects the k most relevant keys for each query. The actual attention then runs only over those k keys, which reduces the attention cost itself to O(Tk).
This approach depends on one premise: the model needs only a small number of keys. Related experiments show that at a 128k-token scale, when each query attends only to its 512 most relevant keys (about 0.8% of all keys), the model still retains about 98% of full-attention accuracy on standard long-context benchmarks. Sparsification itself is therefore not the accuracy bottleneck.
The indexer, however, has its own hidden cost. To select the top-k keys, it usually still has to score the relevance between each query and every key at the raw token level, which means computing a score over a length-T sequence at every position. Scaling the context tenfold enlarges the indexer’s score matrix a hundredfold. At ten-million-token scale, the indexer can account for close to 98% of total runtime. The attention computation has been optimized, but the bottleneck has moved to a different place.
3. Latent Sparse Attention: Moving Retrieval to a Lower Dimension
Latent Sparse Attention (LSA) takes a different approach. Instead of retrieving in the raw token space, it first compresses the history into a low-dimensional latent space and then searches for relevant keys within that space.
The core mechanism has three steps:
- Latent compression. The model introduces a set of latent tokens, far fewer than the sequence length. Through cross-attention, all historical tokens pass their information to these latent vectors. If there are M latent vectors and M is much smaller than T, the compression cost is O(TM).
- Latent scoring. Each query no longer scores against all T keys. It scores only against the M latent vectors, producing a coarse relevance distribution. The cost drops from O(T) to O(M).
- Block mapping and selection. The relevance scores in latent space are mapped back to blocks of the original tokens. The highest-scoring blocks are selected, and standard attention runs only on the tokens inside those blocks.
The overall complexity becomes O(TM + Tk). When M is a constant or grows slowly with T, the indexing cost no longer depends on T². This makes subquadratic attention possible for ultra-long contexts.
A simplified pseudocode sketch follows:
# Latent sparse attention sketchlatents = learnable_latents(M) # M << Tlatents = cross_attn(latents, kv) # compress history into latent space, O(TM)scores = q @ latents.T # query scores only against latents, O(M)top_blocks = topk(scores, k_blocks) # select high-relevance blockskv_sel = gather(kv, top_blocks) # map back to original token blocksout = attn(q, kv_sel) # standard attention, O(Tk)
4. Benchmark Results and Accuracy
In single-layer prefill benchmarks at ten-million-token scale, LSA delivered about a 22x speedup for one class of models that use global sparse attention layers. For another class of models with dense attention architectures, the speedup exceeded 70x. More importantly, across a range of long-context tasks, the models with LSA kept accuracy close to that of the original models. This suggests that latent-space retrieval does not significantly harm information recall.
The takeaway applies more broadly. When indexing cost dominates total overhead, the optimization target shifts from reducing the number of tokens that participate in attention to lowering the resolution of the selection process itself. This two-stage structure, with coarse retrieval followed by fine computation, parallels the recall-then-rank pattern used in information retrieval.
5. Deployment Recommendations
Teams that want to apply this kind of technique in large model inference services should pay attention to the following points:
- Diagnose bottlenecks layer by layer. Use profiling to determine how prefill time is split between the indexer and attention computation before optimizing, so effort is not spent on the wrong component. Cloud logging and monitoring services can help establish latency baselines for long-sequence inference.
- Tune block size and the number of latent vectors together. Blocks that are too large add redundant computation, while blocks that are too small push retrieval overhead back up. The number of latent vectors sets the balance between recall and indexing cost. A grid search on long-context benchmark tasks is a practical way to find that balance.
- Watch KV cache pressure. At ten-million-token context lengths, KV cache memory use is also very large. LSA can be combined with KV compression and paged caching. At the deployment level, container platforms with elastic scaling can help absorb compute spikes from very long requests.
- Do not skip accuracy regression testing. Any sparsification change should be validated on multiple task types, such as math reasoning, retrieval-augmented generation, and code understanding, rather than on a single benchmark.
Conclusion
Competition in long-context inference is shifting from whether models can support ultra-long contexts to whether they can support them affordably. LSA shows that retrieval can also be sparsified and reduced in dimension. This opens a new path for controlling inference cost in the ten-million-token era and offers a useful pattern for co-designing training and inference.
