Sparse Attention at 10 Million Tokens: Why the Indexer Becomes the Bottleneck and How Latent Retrieval Delivers Up to 22x Speedup
As context windows scale from millions to tens of millions of tokens, the bottleneck in long-context inference shifts. This article explains why the indexing stage that decides which historical tokens to attend to can consume the vast majority of prefill time, breaks down the three root causes, and walks through Latent Sparse Attention (LSA), which moves candidate retrieval into a low-dimensional latent space. It also covers the engineering rollout path, evaluation methodology, and the boundaries where the reported speedups apply.
When the context grows from millions to tens of millions of tokens, the bottleneck in long-context inference starts to move. We used to assume the cost was in the attention computation itself. In the prefill stage of extremely long sequences, however, the time is often consumed by the indexing step that decides which historical tokens to look at. This article takes a general technical view of the problem: what causes it, how to quantify it, and how a class of methods known as Latent Sparse Attention (LSA) addresses it, along with a practical engineering path and an evaluation approach.
1. The Bottleneck Shifts: From Slow Compute to Slow Lookup
In standard full attention, every query computes a similarity score against all historical keys, so cost grows quadratically with sequence length. Sparse attention starts from a simple observation: since most of the attention mass concentrates on a few positions, keep only the most relevant fraction.
This approach has been validated repeatedly at the million-token scale, with controllable accuracy loss. But as sequences stretch to tens of millions of tokens, an overhead that once seemed negligible begins to spiral. In one global attention layer, for example, roughly 98% of prefill time at the 10-million-token scale is not spent on attention multiplications at all, but on the indexer that selects which tokens enter the candidate set.
The nature of the problem has changed. Sparsification solved “computing too much,” but not “looking up too expensively.” If candidate selection still scores every historical position for every query, its cost remains quadratic, and the constant factor can be even larger once extra sorting and prefix sums are involved.
2. Root Causes: Why the Indexer Becomes a Quadratic Bottleneck
Breaking the indexing cost apart points to three sources.
- High scoring dimensionality: per-token comparison usually runs over the full hidden dimension, so the higher the dimension, the more expensive each dot product.
- Candidate pool grows linearly with length: longer sequences mean a larger pool to compare against, while the number of queries grows too, and the two multiply into a quadratic term.
- Unfriendly memory access and scheduling: candidate scoring often involves irregular reads and sorting, which existing hardware struggles to feed through contiguous access and parallel pipelines.
The conclusion is direct: real speedup requires lowering both the cost of a single scoring operation and the number of objects being compared, not just reducing how many positions end up in the attention computation.
3. The Core Idea: Move Retrieval into Latent Space
One class of methods, known as Latent Sparse Attention, answers this by searching in a low-dimensional latent space first rather than in the original token space.
The structure typically has three parts:
- Compression projection: a lightweight linear mapping compresses each historical key vector into a low-dimensional latent representation, with a dimension that can be significantly smaller than the hidden dimension.
- Cheap scoring and top-k selection: the query side is projected into the same latent space, scored by dot product against the compressed historical representations, and the k highest-scoring positions become the candidates.
- Exact attention: the original attention computation runs only over the candidate set, with numerical precision and masking behavior unchanged.
Illustrative pseudocode (structure only, not a specific implementation):
# Prefill stage: compress the history side once, O(L * d * r)K_lat = K @ W_k # [L, r], r << d# Project queries, then score, O(Lq * L * r)scores = (Q @ W_q) @ K_lat.Tidx = topk(scores, k) # keep only k candidates per queryout = attention(Q, K, V, mask=idx)
The key point is that the quadratic term still exists, but its constant is pushed very low (r is far smaller than d). Combined with blocking and approximate retrieval, the actual time can drop from a dominant term to a negligible one.
4. Why Accuracy Holds Up
Sparse attention works because attention distributions are highly concentrated: most of the mass sits on a few keys. Latent retrieval exploits a second layer of redundancy, since key vectors already preserve the main similarity ordering in a lower-dimensional subspace.
In practice, three safeguards are usually needed:
- Always retain the local window and “attention sink” positions to avoid breaking long-range dependencies;
- Take the union of the candidate sets (local window ∪ global top-k) to cover both neighbors and distant tokens;
- Use blocked approximate retrieval (coarse-filter blocks first, then rank precisely within a block) to push complexity from quadratic to near-linear.
5. Gains and Applicability Boundaries
In one publicly discussed implementation, a single attention layer at the 10-million-token scale achieves roughly 22x faster prefill, with a higher speedup under another attention configuration, while maintaining accuracy close to the original model across a range of long-context tasks. These numbers are specific to particular models, layer types, and hardware conditions, and should not be extrapolated into an end-to-end overall speedup.
The boundaries matter too:
- Gains concentrate in long-sequence prefill; on short sequences, the fixed overhead of compression and indexing may not pay off.
- Retrieval-sensitive tasks (exact multi-hop, long-document retrieval) need focused validation of recall.
- If training and inference use different sparsity strategies, the distributions must stay consistent to avoid accuracy drift.
6. Engineering Checklist
- Profile first: use per-layer timing to confirm the indexer’s share and avoid optimizing the wrong target.
- Layer-specific strategy: enable latent retrieval only on global attention layers, and keep dense computation on local layers.
- Storage and transfer: compressed representations can be cached and reused to avoid repeated projection; in cross-machine settings, prioritize reducing irregular memory access.
- Combine with general cloud capabilities: use a container platform for elastic inference, object storage for long-context caches, and logging and monitoring services to track first-token latency and candidate hit rate.
- Gradual rollout and rollback: roll out in buckets by layer and by sequence length, and keep a dense fallback path.
7. Evaluation Recommendations
- Per-layer metrics: report indexing time, attention time, and end-to-end prefill time separately.
- Recall view: using dense attention as the reference, measure how well the top-k candidates cover the high-weight positions.
- Task view: cover long-document QA, multi-hop retrieval, and repository-level code understanding, and check whether accuracy degrades as length grows.
- Cost view: record the tradeoff curves between compression dimension r, candidate count k, throughput, and memory usage.
Conclusion
At the 10-million-token context scale, the central conflict has shifted from “too slow to compute” to “too expensive to find.” Moving candidate retrieval into a low-dimensional latent space, trading cheap scoring for candidate quality, is currently one of the most cost-effective directions. For developers, the focus of implementation is not chasing a single multiplier, but the engineering discipline of layer-specific enablement, controllable recall, and a fallback path.
