The Key to 22x Faster Sparse Attention at 10 Million Token Contexts
As context windows scale from thousands to millions of tokens, the bottleneck in sparse attention shifts from computing attention to finding which tokens matter. This article breaks down why the indexer dominates prefill time at 10 million token scale and introduces Latent Sparse Attention, a method that moves retrieval into a compressed low-dimensional space. It covers the two-stage retrieval workflow, engineering considerations such as compression block size, positional encoding, caching, and accuracy guardrails, plus a practical three-step adoption path for engineering teams.
1. The Cost Structure of Long Contexts Has Changed
Over the past two years, long context has moved from an optional feature to a default specification. Million-token context is becoming a standard configuration in mainstream model serving, and some newly released models already mix sparse attention with linear attention to form a layered structure of “local precision plus global overview.” These approaches share the same premise: instead of having every query vector scan the entire token history, only a few of the most relevant positions participate in the computation.
This premise has been repeatedly validated at the million-token scale. But as context pushes toward 10 million tokens, an overhead that once seemed negligible starts to spiral. Take one global attention layer in a 10-million-token context model: during prefill (processing the entire prompt in one pass), roughly 98% of the time is not spent on the attention matrix multiplications, but on the indexer that decides which tokens to look at.
In other words, the nature of the problem has changed. Sparse attention has long proven that “looking at only a small subset” is enough to preserve accuracy. What actually blocks ultra-long context is the act of “finding that small subset,” which still grows quadratically with context length.
2. Why the Bottleneck Lands on the Indexer
Breaking a single global sparse attention operation into three steps makes the cost shift clear:
- Scoring: compute relevance between each query and all historical positions, with complexity O(N²·d);
- Selection: take the Top-k indices, then deduplicate, merge, and align to blocks;
- Aggregation: apply attention weighting only to the selected k positions, with complexity O(N·k·d).
Between 4K and 128K, k is kept relatively tight, so the N·k term in step three is on the same order as the N² term in step one, and the indexing overhead stays hidden. Once you reach the million-token range, k must grow with length to preserve recall (a typical approach ties k to √N or some small power of N). Step three stays constrained, but step one balloons quadratically. At 10 million tokens, steps one and two completely dominate runtime, and memory bandwidth is saturated by “reading all keys and values plus writing all scores.”
The conclusion is direct: further optimizing how attention is computed yields little. The complexity order of how relevant positions are located must change.
3. Latent Sparse Attention: Moving Retrieval into a Low-Dimensional Space
A recent paper from a startup research team in collaboration with universities proposes Latent Sparse Attention. Its core idea is not a smarter scoring function, but a different space and scale for scoring:
- Compressed representation: the keys and values of a long sequence are compressed into a number of latent slots far smaller than N, where each latent slot carries aggregated information for a contiguous span of tokens, forming a coarse-grained map.
- Low-dimensional retrieval: queries are projected into the same low-dimensional latent space and dotted with the latent slots, reducing retrieval cost from O(N²·d) to O(N·M·d’), where M is the number of latent slots and d’ is the compressed dimension, with M ≪ N and d’ ≪ d.
- Two-stage localization: first select a few “regions of interest” in the latent space, then perform fine-grained scoring within the original token blocks corresponding to those regions, keeping the quadratic term bounded by a constant block size.
- Complementary to the linear path: global information lost to compression is carried by a linear attention/state-space path with constant state, offsetting the recall loss from the coarse view.
The reported results: at 10 million token scale, a single attention layer’s prefill achieves roughly 22.7x and 72.4x speedups relative to existing sparse attention methods while maintaining comparable accuracy. The difference in speedup comes from the different indexer implementations and k-value strategies of each model, not from any preference of the method itself for particular models.
A schematic of the flow:
# Pseudocode: two-stage latent retrievallatent = compress_kv(k, v, block=256) # N -> M, M << Nscores = q @ latent.keys().T # low-dimensional coarse ranking, O(N*M*d')regions = topk_regions(scores, r=8) # select a few blocksidx = refine_within(q, k, regions, topk=k) # fine ranking within blocks, controlled costout = attention(q, k, v, idx) # aggregate only selected positions
4. Engineering Implementation Notes
- Compression block size tradeoff: too small and M approaches N, degrading retrieval; too large and localization becomes coarse, hurting recall. In practice, start from 128–512 and tune using recall on a validation set rather than loss alone.
- Coordination with positional encoding: compression breaks the original relative position relationships, so position summaries must be preserved on the latent slots, or relative offsets must be restored during the fine-ranking stage.
- Caching strategy: latent slots built during prefill can be persisted alongside the key-value cache; incremental decoding only needs to append latent slots for new blocks, avoiding repeated compression.
- Hit rate and long tail: workloads with strong locality (codebases, logs, long-document QA) benefit most; when queries require long-range jumps across the full context, increase r and k.
- Accuracy guardrails: establish a long-context recall benchmark, using needle-in-a-haystack, multi-hop QA, and long-document summarization as regression tests. Any speedup change must pass this gate first.
- Serving architecture: use elastic scaling on a container platform, use object storage to hold long-context caches, and combine logging services with monitoring and alerting to observe time-to-first-token, prefill throughput, and cache hit rate. Baidu AI Cloud’s general capabilities can serve as a reference architecture for hosting such inference services.
5. Assessment and Recommendations
The next step in the 10-million-token context competition is not widening the window further, but reducing the complexity of “locating relevant tokens” from quadratic to near-linear. Latent retrieval offers a reusable abstraction: use a cheap low-dimensional representation for coarse filtering, then do fine computation in the original space. The practical recommendation for engineering teams is a three-step approach: first quantify the indexer’s share of prefill time to confirm the bottleneck; then introduce a switchable coarse-filtering layer for A/B testing, observing accuracy and latency curves; finally, solidify caching and monitoring to turn the speedup into a sustainable service metric.
