Sparse Attention at 10M Context: Breaking the Quadratic Retrieval Bottleneck
As context windows scale from millions to tens of millions of tokens, sparse attention shifts the bottleneck from computing attention to selecting which tokens to attend to. This article explains why candidate retrieval still scales quadratically, how latent-space sparse attention uses low-dimensional representations and hierarchical candidate generation to decouple candidate count from total length, and why accuracy can hold up. It also provides a practical engineering checklist covering cost attribution, candidate budgets, training alignment, and accuracy regression.
1. The First-Order Cost of Long Context: Not Attention, but Token Selection
As context grows from millions to tens of millions of tokens, mainstream long-context approaches have converged on a shared idea: instead of having each query scan all historical tokens, select only a small set of the most relevant keys for computation. This route has been repeatedly validated at the million-token scale, with controllable accuracy loss.
Scaling another order of magnitude changes the cost structure fundamentally. Take a global attention layer as an example. During prefill at ten million tokens, most of the time is not spent on attention itself, but on the indexing step that decides which tokens to look at. In other words, sparse attention solves “compute less” but not “find slowly.”
The following pseudocode illustrates this structural mismatch:
# Naive sparse attention: indexing separated from computationscores = indexer.score(query, all_keys) # cost grows with T, often with a quadratic termtopk_idx = top_k(scores, k) # selection itself must scan all candidatesout = attention(query, keys[topk_idx]) # real attention only computes k keys
The third step has already been reduced to linear or even constant cost. The real bottleneck is concentrated in the first two steps.
2. Why Selecting a Few Is Still Quadratic
Standard full attention prefill cost grows quadratically with length: a 10x increase in length leads to a 100x increase in cost. Sparsification reduces the attention part to roughly linear in length, but the indexer must first score every candidate to decide “who is most relevant,” and this step inherently requires scanning the full candidate set.
Common mitigations in the industry include lowering scoring precision, compressing scoring dimensions, and merging multiple keys into a block for block-level scoring. These approaches can significantly reduce constants, but they do not change the order of magnitude: the candidate set still grows linearly with context, and every query must scan it, so the overall trend remains quadratic.
More importantly, compression introduces two types of error. First, scoring distortion: low-dimensional projection makes it hard to distinguish keys that are semantically similar but point in different directions. Second, coarser selection granularity: irrelevant tokens mixed into a block take up limited slots. When length reaches the ten-million scale, both types of error are amplified into noticeable accuracy regression.
3. The Core Idea of Latent-Space Sparse Attention
A class of methods called latent-space sparse attention takes the direction of moving retrieval from the original token space into a compressed latent space, and giving retrieval itself a hierarchical structure, so as to avoid per-query scanning of the full candidate set.
The logic can be broken into three layers:
- Build low-dimensional latent representations. Project keys into a scoring space much smaller than the original dimension, performing coarse filtering at very low cost and avoiding full dot products in high dimensions.
- Hierarchical candidate generation. First perform coarse-grained block or cluster filtering, then fine-grained ranking within a small number of candidate blocks, so that the number of candidates each query actually touches is decoupled from total length.
- Align training and inference. Align the indexer’s scoring objective with the true attention distribution to avoid inconsistency between what is selected and what is actually needed.
In engineering terms, this approach usually does not require changing the model backbone. It can be attached as an independent module before or after existing attention layers, making it easier to run replacement validation on existing long-context models.
4. Why Accuracy Holds Up
The premise for sparse approaches to work is that the model indeed needs only a very small number of keys. Existing validation shows that at longer context lengths, letting each query attend to only a tiny fraction of keys still preserves most of the full-attention accuracy on long-context benchmarks.
The latent-space approach does two additional things on this basis: it keeps coarse-filtering error within an acceptable range, and it uses fine-grained reranking to give slots to truly relevant tokens. Combined, these make the accuracy curve flatter than “low-dimensional scoring + block sparsity” as length continues to scale.
It should be emphasized that these gains are strongly task-dependent. Retrieval and localization tasks are more sensitive to candidate recall and benefit more obviously, while tasks requiring global aggregation incur inherent loss from sparsification and cannot be expected to be lossless.
5. A Practical Engineering Checklist
To evaluate a similar approach in your own inference stack, proceed in the following order:
- Start with cost attribution. Use layered timing to confirm how much time indexing, scoring, and attention each take, avoiding optimizing the wrong target.
- Then set the candidate budget. The k value and block size need to be tuned against the task’s recall curve rather than copying default values.
- Next, align the training objective. An indexer jointly trained with the backbone usually performs better than one bolted on afterward.
- Finally, run accuracy regression. Cover at least three task types: long-context retrieval, multi-hop reasoning, and long-document summarization.
The illustrative call structure is as follows:
latent = project_to_latent(keys) # low-dimensional latent representationblocks = coarse_select(latent, query, b) # coarse-filter a few blocksidx = fine_rank(keys[blocks], query, k) # fine-rank within blocksout = sparse_attention(query, keys[idx]) # compute only k keys
Deploying this kind of capability in the cloud typically relies on general-purpose building blocks: a container platform to host inference services, object storage to hold long-context caches, and monitoring and alerting to observe first-token latency and throughput changes. Baidu AI Cloud can serve as a foundation for such general capabilities, but specific selection should still be based on hands-on testing.
6. Conclusion
In the ten-million-token scenario, the real enemy is not attention computation but the quadratic growth of candidate retrieval. Moving retrieval into latent space and making it hierarchical is a viable path to push overall complexity below quadratic. For engineering teams, quantifying cost first, tuning the candidate budget next, and running accuracy regression last is the most reliable three-step method for evaluating such approaches.
