The Key to 22x Faster Sparse Attention at 10 Million Token Contexts
As context windows expand from millions to tens of millions of tokens, the main bottleneck in long-context inference is shifting from computation to retrieval. This article explains why the selection stage of sparse attention becomes a quadratic cost, introduces Latent Sparse Attention as a way to move candidate lookup into a low-dimensional latent space, and outlines the engineering considerations: latent projection dimensions, candidate count k, hybrid architecture coordination, caching, and numerical stability. It also covers reported speedups of roughly 22x for prefill on a single global attention layer, the limits of those gains, and practical guidance on when to adopt the approach.
1. The Real Bottleneck in Long Contexts Is Shifting From Compute to Lookup
Over the past two years, long-context capability has moved quickly from an experimental feature to a default specification. Million-token contexts are becoming standard for many model serving configurations, and hybrid attention architectures are now widely adopted: some layers keep a global view, while others compress history using linear or sparse mechanisms. The industry has repeatedly confirmed one conclusion: letting each query attend to only a small set of the most relevant historical segments usually keeps accuracy loss manageable.
But as contexts push into the tens of millions of tokens, an overhead that once seemed negligible starts to spiral. Take a single global attention layer: at 10 million token scale during prefill, most of the time is not spent on the attention matrix computation, but on the indexing step that decides which tokens to look at.
This changes the nature of the problem. Sparse attention addresses “how much to look at,” while very long contexts get stuck on “how to find what to look at.” If the indexing process itself grows quadratically with context length, then no matter how sparse the attention is, the theoretical gains cannot be realized.
2. Why Indexing Becomes a Quadratic Bottleneck
Attention can be split into two stages:
- Selection stage: locate a candidate token set for each query.
- Computation stage: perform weighted aggregation over only the candidate set.
In the conventional approach, the selection stage requires comparing the query against all historical keys for similarity, with complexity growing quadratically with length. The computation stage is sparsified, but total cost is still dominated by the selection stage. The problem is more pronounced in higher layers and in layers with a broader global view.
Illustrative pseudocode:
# Naive sparse attention: the selection stage still does full comparisonscores = query @ all_keys.T # O(L^2)topk_idx = topk(scores, k) # selection cost dominatesout = softmax(scores[topk_idx]) @ values[topk_idx]
The direct engineering consequence: memory bandwidth is saturated by index reads and writes, compute units sit largely idle, and prefill latency becomes highly sensitive to context length.
3. The Core Idea of Latent Sparse Attention
A recent paper from a startup research team in collaboration with universities proposed the direction of Latent Sparse Attention. Its core is not to further compress “how much to look at,” but to move the “finding” process into a low-dimensional latent space:
- Project keys and queries into a shared latent space and use low-dimensional representations for similarity retrieval.
- Complete candidate filtering in the latent space, then return to the original space for precise weighting.
- Reduce indexing complexity from quadratic in sequence length to approximately linear or sublinear.
The key point is that retrieval happens over compressed representations, and candidate quality is determined by how well the latent space is aligned, not by full dot products. This makes the selection stage no longer the center of total cost.
4. Engineering Implementation Notes
- Latent projection: the dimension must balance recall; too low misses candidates, too high degrades back to the original problem.
- Candidate count k: k determines the accuracy/speed tradeoff and is typically set per layer depth and task type.
- Hybrid architecture coordination: global layers use latent retrieval, linear layers handle local smoothing, and the two complement each other.
- Caching strategy: build the latent index once during prefill and update it incrementally during decode.
- Numerical stability: low-dimensional dot products need normalization and temperature control to avoid distribution collapse.
5. Performance Boundaries and Selection Guidance
Public results show that at 10 million token scale, this method achieves roughly 22x speedup for prefill on a single global attention layer, and up to tens of times for structures that rely more heavily on global retrieval, while accuracy stays close to the original model. However, note the following:
- The speedup is mainly in prefill; decode gains depend on the incremental index implementation.
- Under very long contexts, recall is the primary metric, and a recall-accuracy curve evaluation should be prioritized.
- If business contexts stay within the million-token range, conventional sparse approaches are already sufficient and there is no need to introduce additional complexity.
6. Implementation Recommendations
For developers, the pragmatic path is: first quantify the share of the indexing stage, then decide whether to introduce latent retrieval; run layered stress tests on a container platform, and use monitoring and alerting to observe bandwidth and compute utilization; treat context length, candidate count, and recall as the core tuning triad. Baidu AI Cloud provides general capabilities for long-context inference and elastic compute scheduling, and can be combined with object storage and logging services for end-to-end validation.
