Sparse Attention at 10 Million Tokens: 22x Speedup by Moving Retrieval to Latent Space
As context windows scale from millions to tens of millions of tokens, sparse attention still works—what breaks is how it selects tokens. This article explains why the bottleneck shifts from attention compute to the indexing step, with a global attention layer in an open-source million-token model spending roughly 98% of prefill time on the indexer at 10M tokens. It then walks through Latent Sparse Attention, which compresses history into a low-dimensional latent space, runs approximate nearest-neighbor search there, and hands only the matched tokens to attention. You will find the pseudocode, an explanation of why speed and accuracy can be gained together, a practical deployment checklist, and the boundaries and risks of the approach.
When the context grows from millions to tens of millions of tokens, sparse attention does not fail. What fails is its way of picking tokens. The common industry approach is to stop each query from scanning the entire history and instead let it select a few of the most relevant segments for computation. At the million-token scale, this has been validated repeatedly: both accuracy and efficiency hold up. But push the length ten times further and the nature of the problem changes. What is expensive is no longer attention itself, but the indexing process that decides which tokens are worth looking at. Take one global attention layer in an open-source large model that supports million-token context. During prefill at 10 million tokens, roughly 98% of the time is spent in the indexer, while the attention computation accounts for only a small fraction. This means that as long as indexing still relies on pairwise comparison over the full history, its complexity grows quadratically with length and eventually consumes all the gains that sparsity was supposed to deliver.
1. The Bottleneck Moves: From a Compute Problem to a Retrieval Problem
The benefit of traditional sparse attention comes from two things: reducing the number of key-value pairs that participate in computation, and bringing complexity down from quadratic to nearly linear. At tens of millions of tokens, the second benefit covers only the attention matrix multiplications. It does not cover scoring and selection. The indexer has to evaluate a large number of candidates for every query, and the candidate pool itself grows linearly with context length, while the number of queries also grows with length. Multiply the two and you get quadratic behavior. The result is counterintuitive: the more cleverly a model computes only a small subset, the more the index becomes the absolute bottleneck. In engineering terms, this shows up as low GPU utilization, memory-bound access, and frequent kernel launches and synchronization. No matter how expensive the compute card is, it cannot be kept busy.
2. The Core Idea: Move the “Search” into Latent Space
One general solution that has been proposed is Latent Sparse Attention. Instead of retrieving directly in the original token space, it first compresses the history into a low-dimensional latent representation, then performs similarity matching in that latent space, and finally passes only the few matching original tokens to attention. Three points matter here. First, the latent representation has a much smaller dimension than the original one, so comparison cost drops sharply. Second, retrieval and computation are decoupled, so indexing can be built as an approximate nearest-neighbor structure and avoid full pairwise scoring. Third, the compression is learnable: the training objective makes the latent space retain information about whether something is relevant, rather than reconstructing the text verbatim. Illustrative pseudocode:
# Conceptual illustration, not a specific implementationlatent = compress(kv_history) # compress history into a low-dimensional latent spaceidx = ann_search(latent, query) # approximate search, near-linear complexityselected = kv_history[idx] # take only a few original tokensout = attention(query, selected) # standard sparse attention
3. Why It Delivers Both Speed and Accuracy
The speed comes from a multiplicative split of complexity: retrieval drops from quadratic to near-linear, and attention still runs on a fixed budget. The accuracy comes from the two-stage design: the latent space handles recall, and the original space handles precise computation. The recall stage tolerates slight error, while the precise stage keeps the original numerical path. Compared with a hard truncation performed directly in the original space, this approach is less likely to miss critical segments, so it is more stable on long-document question answering, cross-passage reasoning, and code-base understanding. It is worth stressing that these gains depend on adaptation during training and cannot simply be applied to any model. In engineering practice, start with small-scale validation and then scale the length up gradually.
4. Engineering Deployment Checklist
- Layered strategy: keep recent details in local window layers, and route global layers through latent-space retrieval, balancing recency effects against long-range dependencies.
- Controllable budget: set a fixed number of candidates per query so latency stays predictable and capacity planning is easier.
- Cache friendly: latent representations can be updated incrementally, and combined with prefix caching this reduces repeated compression.
- Observability: use logging services and monitoring alerts to track recall rate, hit distribution, and tail latency, avoiding a situation where the average is fast but the long tail is slow.
- Progressive rollout: first align accuracy on an offline evaluation set, then run a small-traffic canary, and finally go to full traffic. If you rely on a container platform and a message queue for asynchronous prefetching, you can further smooth out prefill jitter.
5. Applicability Boundaries and Risks
Latent-space retrieval is not a universal fix. When a task requires verbatim exact matching, such as very long log retrieval, compression may lose information. When the distribution drifts sharply, the latent space needs continuous calibration. When the candidate budget is too small, insufficient recall will directly hurt accuracy. The reasonable approach is to treat retrieval budget, compression dimension, and local window as one group of tunable parameters, and run a grid search over a unified evaluation set rather than deciding by intuition.
6. Conclusion
In the era of tens of millions of tokens, the real competitive point is not whether you can compute, but whether you can quickly find the part that needs computing. Moving retrieval into latent space rearranges the complexity structure without changing the model backbone. For developers, this means supporting longer contexts at a more controllable cost. For enterprises, it means long documents, long conversations, and code-base-level understanding become engineering-feasible scenarios. Baidu AI Cloud can provide stable support for such long-context solutions through foundational capabilities such as general compute, container platforms, and logging and monitoring, helping teams turn “theoretical speedup” into “an operable service”.
