Sparse Attention at 10M Token Context: Why the Indexer Becomes the Bottleneck and How to Speed Up Prefill by 22x
As context windows move from millions to tens of millions of tokens, the main cost of sparse attention shifts from computing attention to finding which tokens to attend to. This article explains why naive top-k selection keeps quadratic complexity, how latent sparse attention moves selection into a low-dimensional latent space to cut that cost, and what engineering and evaluation practices matter when prefill needs to be more than an order of magnitude faster.
1. Background: The Bottleneck Shifts Once Context Passes 10M Tokens
Over the past two years, long context has moved from an optional feature to a default specification. Million-token context is already standard in many production services, and some models combine sparse and linear attention in hybrid architectures so that each query does not scan the entire history but focuses on a few of the most relevant segments.
This approach has been repeatedly validated at the million-token scale: as long as the selection is accurate, attending to a small fraction of tokens is enough to preserve accuracy. But as context keeps growing toward 10M tokens, an initially negligible overhead starts to spiral out of control.
Take a global attention layer as an example. When prefilling at 10M tokens, most of the time is not spent on the attention matrix multiplications. It is spent on the indexer that decides which tokens to look at. In other words, what really blocks ultra-long context is not “computing attention” but “finding attention.”
The nature of the problem therefore changes:
- Sparse attention solves “how much to look at”;
- It does not solve “how to find it”;
- And the cost of “finding” still grows quadratically with context length.
2. Why the Indexer Becomes the New Quadratic Bottleneck
The selection logic of naive sparse attention is usually straightforward: for each query, score relevance against all historical keys, then take the top-k. Let the sequence length be n. The selection stage is still O(n²·d), the same order as dense attention. The only difference is that “multiply-accumulate” is replaced by “score plus sort”: the constant factor shrinks, but the order of magnitude does not.
At the million-token scale, this constant is still acceptable. At 10M tokens, the n² term dominates completely:
n = 1e6 -> n² = 1e12n = 1e7 -> n² = 1e14 # selection overhead grows by roughly 100x
This produces a counterintuitive result: attention itself becomes extremely cheap after sparsification, but the indexer that serves sparsification becomes the main cost. In engineering terms, this shows up as low GPU utilization, memory-bound access, frequent kernel launches, and large amounts of compute sitting idle.
3. Core Idea: Move Selection into Latent Space
One validated direction is latent sparse attention. Its key abstraction is to avoid exhaustive comparison in the original token dimension. Instead, keys are first compressed into a small number of latent slots. Queries are scored only against these slots, and the slot selection is then mapped back to original token blocks.
It can be understood as a three-stage funnel:
- Compression: aggregate n keys into m latent representations, where m is much smaller than n (for example, m ≈ √n or a fixed constant);
- Coarse selection: score the query against the m latent representations, reducing complexity to O(n·m·d);
- Fine selection: perform exact top-k only within the token blocks corresponding to the few hit latent slots.
Illustrative pseudocode:
# n: sequence length, m: number of latent slots, k: number of tokens finally keptlatent = compress(keys, m) # O(n) one-time compressionscore = query @ latent.T # O(m*d), decoupled from nblocks = topk(score, block_budget) # coarse block selectionsparse = gather(keys, blocks) # fetch only candidate blocksattn = softmax(query @ sparse.T) @ values(sparse)
When m is on the order of √n, the selection stage drops from O(n²) to roughly O(n^1.5). When m is a constant, the selection stage is nearly linear and the overall prefill can be compressed substantially. This is the source of more than an order-of-magnitude speedup for single-layer prefill at 10M tokens.
4. Why Accuracy Holds Up
The premise for sparse attention to preserve accuracy is accurate selection. The latent-space approach compensates in two ways:
- Coarse recall: latent slots are produced by learnable compression and can cover clusters of semantically similar tokens, avoiding the noise of per-token scoring;
- Fine compensation: exact attention is still performed within the hit blocks, preserving local detail and making up for the information loss caused by compression.
In practice, three supporting designs are usually required: keep positional information consistent before and after compression, align block boundaries with the causal mask, and process ultra-long sequences in a chunked, streaming manner to control memory usage.
5. Engineering Implementation Notes
- Complexity and memory: selection-stage memory drops from O(n²) to O(n·m), which is what makes 10M tokens feasible;
- Memory-access optimization: fuse compression and gather into a small number of kernels to reduce launch overhead and wasted bandwidth;
- Compatibility with decoding: use latent-space coarse selection during prefill, and cache latent slots during decode to avoid repeated compression;
- Layered strategy: use denser sparsity in shallow layers and more aggressive compression in deep layers to balance accuracy and speed;
- Observability: add an “indexer time share” metric to the monitoring and alerting system to locate shifts in the bottleneck.
6. Evaluation and Selection Recommendations
Evaluating this class of solution should not look at peak speedup alone. It should also report:
- Prefill and decode latency at different context lengths (1M / 4M / 10M);
- Whether indexer time share grows linearly rather than quadratically with length;
- Accuracy retention on long-document retrieval, multi-hop question answering, and repository-level code understanding;
- Peak memory usage and batch throughput.
7. Conclusion
In the 10M-token era, the real opponent is not attention computation but finding the part that should be looked at. Moving the selection process into a low-dimensional latent space and replacing per-token exhaustive comparison with one compact scoring pass is currently one of the most engineering-valuable directions. For platform providers, whoever can compress index overhead to near-linear can turn ultra-long context from “able to run” into “usable.”
