Sparse Attention at 10M Token Context: Why the Indexer Becomes the Bottleneck and How Latent Retrieval Delivers Up to 22x Speedup
As context windows scale from 128K to 1M and beyond, sparse attention shifts the cost of long-context inference from attention computation to token selection. This article explains why the indexer becomes a new quadratic bottleneck at 10M tokens, how Latent Sparse Attention moves relevance scoring into a low-dimensional latent space with approximate nearest neighbor retrieval, and the engineering practices needed to deploy it, including index and KV cache coordination, chunked pipelining, recall monitoring, and hybrid linear attention. Teams running prefill on ultra-long contexts will learn why profiling indexer overhead often pays off more than shrinking k.}
1. Ultra-Long Context Is Spending the Compute It Saved
Over the past two years, long-context capability has moved from 128K to 1M and has gradually become the default specification for mainstream model services. One of the key designs behind this progress is that each query no longer scans every historical token. Instead, it selects only a few of the most relevant tokens to participate in the computation. This is the basic idea of sparse attention. At the 1M scale, it has been repeatedly validated: accuracy stays largely under control, and tasks such as long-document retrieval and cross-section reasoning perform reliably.
But when the context keeps growing to 10 million tokens, the math changes. Take a model with global attention layers as an example. When running prefill at the 10M token scale, most of the time is not spent on the attention matrix multiplications. It is spent on the indexing step that decides which tokens to look at. In other words, sparse attention successfully compressed the computation, but it shifted the cost to retrieval and ranking, and that overhead itself grows quadratically with context length.
The nature of the problem has therefore changed. The real bottleneck is no longer “how much to look at,” but “how to find the small portion that needs to be looked at.”
2. Why the Indexer Becomes the New Quadratic Bottleneck
The naive sparse attention flow can be broken into three steps:
- For each query, compute relevance scores against all historical keys;
- Select the top-k keys;
- Apply attention weighting only over those k keys.
The cost of the third step is linear and manageable. The real cost is concentrated in the first and second steps. When the sequence length is N, full scoring is O(N) per query, or O(N²) overall; top-k selection also requires additional sorting or heap maintenance. At the 1M scale, this part is still acceptable. At the 10M scale, the N² term pushes the indexer into the dominant position.
Common engineering mitigations include block-wise scoring, a local window plus a small number of global anchors, and approximate retrieval based on hashing or quantization. They can reduce the constant factor, but they can hardly change the essence that the scoring range expands linearly with N. So a more fundamental idea emerges: do not perform retrieval in the original token space.
3. Latent Sparse Attention: Moving Retrieval into a Low-Dimensional Space
The core of Latent Sparse Attention is to maintain an additional low-dimensional “index vector” for each token, so that retrieval happens only in the low-dimensional space. The logic chain is as follows:
- Dimensionality reduction: project the key of dimension d into a latent space of dimension r, where r is much smaller than d (for example, r is a few dozen to a bit over a hundred);
- Approximate retrieval: compute the similarity between the query and the index vectors in the low-dimensional space, and organize the index with an approximate nearest neighbor structure to avoid a full scan;
- Exact recomputation: only for the selected top-k tokens, go back to the original dimension for exact attention computation.
The benefits are twofold: each comparison in the scoring stage is cheaper, and the candidate set can be greatly reduced with the help of the approximate retrieval structure; meanwhile, exact computation happens only on a very small k, so the accuracy loss is limited to the single factor of “selecting the wrong candidates.” Public research results show that at the 10M token scale, prefill for a single global attention layer can achieve several tens of times speedup, with some model configurations showing even higher speedup ratios, while accuracy on long-document and multi-hop tasks stays close to the original model.
Illustrative pseudocode:
# 1) Precompute low-dimensional indices (done once before prefill)idx = project_to_latent(keys) # [N, r], r << dindex = build_ann_index(idx) # approximate nearest neighbor structure# 2) Each query goes through "coarse filtering + exact computation"cand = index.search(project_to_latent(q), topk=k * gamma) # enlarge the candidate multipliercand = rerank(q, keys[cand])[:k] # go back to the original dimension for exact rerankingout = attention(q, keys[cand], values[cand])
Here gamma is the candidate enlargement factor, used to trade off recall against overhead; the reranking step is the key gate that controls accuracy loss.
4. Engineering Implementation Notes
- Index and KV cache coordination: the low-dimensional index needs to be persisted together with the KV cache. Watch the trade-off between GPU memory and bandwidth; the index itself should be far smaller than the original KV.
- Chunking and pipelining: split prefill into chunks so that index construction and attention computation can be pipelined and overlapped, hiding part of the latency.
- Recall monitoring: in production, monitor the top-k hit rate and the reranking elimination rate. An unusual increase usually signals distribution drift.
- Hybrid with linear attention: local linear attention handles the near field, while latent sparse attention handles far-field retrieval. The two are complementary and avoid the weaknesses of a single mechanism.
- Cloud deployment: inference services can be hosted on a container platform, index snapshots can be stored in object storage, and log services plus monitoring alerts can support observability for long-context scenarios.
5. Conclusion
In the 10M token scenario, the main contradiction of sparse attention has shifted from “too much computation” to “too expensive retrieval.” Moving relevance judgment into a low-dimensional latent space, using approximate retrieval to shrink the candidates and exact recomputation to hold accuracy, is currently the most cost-effective path for this kind of改造. For teams that need to run prefill on ultra-long contexts, evaluating the indexer’s share of overhead first often pays off more than continuing to shrink the k value.
