Sparse Attention at 10 Million Token Contexts: Why Indexing Becomes the New Quadratic Bottleneck
As context windows scale from millions to tens of millions of tokens, the cost center of long-context inference shifts from computation to selection. This article explains why the indexing stage of sparse attention turns quadratic at extreme lengths, how Latent Sparse Attention moves Top-K selection into a low-dimensional latent space to achieve near-linear scaling, reported speedups of over 20x and 70x on prefill for specific layers and models, why accuracy holds up, and how to profile, tune, and regression-test such an optimization in production.
1. Long Contexts Are Shifting the Cost Center from Compute to Selection
Over the past two years, long-context capability has moved quickly from an experimental feature to a default specification. Million-token context is becoming standard in mainstream model serving, and some models now adopt hybrid structures that combine sparse attention with linear attention, so each query attends to only a few of the most relevant positions in the history instead of scanning all of it.
At the million-token scale, this approach has been validated repeatedly: as long as the selection is accurate, attending to a small fraction of tokens is enough to preserve quality. But as context pushes toward ten million tokens, an overhead that once looked negligible starts to spiral.
Take a global attention layer as an example. During prefill at ten million tokens, most of the time is not spent on the matrix math of attention itself, but on the indexing step that decides which tokens to look at. The bottleneck shifts from “how fast can we compute” to “how accurately and how quickly can we find the right tokens.”
That changes the nature of the problem. Sparse attention addresses how much to look at, while extreme-length context is really blocked on how to find that small subset. If locating the candidate set still grows quadratically with context length, no amount of sparsity will bring end-to-end latency down.
2. Why the Indexer Becomes the New Quadratic Bottleneck
To see this, split attention into two stages:
- Selection: score every query and find the Top-K most relevant historical positions.
- Aggregation: compute a weighted sum over only those K positions.
Aggregation is linear in complexity and grows with K, which is typically a constant that is weakly correlated with sequence length, if at all. The real trouble is selection: a naive implementation requires each query to run a similarity evaluation against every historical position, which is quadratic in sequence length. Scale the sequence by 10x and this overhead grows by 100x.
Schematically, the logic looks roughly like this:
# Selection stage of naive sparse attention (simplified illustration)# scores: [seq_len, seq_len], grows quadratically with lengthscores = query @ key.transpose(-1, -2) # quadratic costtopk_idx = scores.topk(k, dim=-1).indices # select K positionsout = weighted_sum(value, topk_idx) # linear cost
At million-token lengths, the quadratic term can still be hidden behind efficient matrix operations and memory bandwidth. At ten million tokens, it consumes both compute and bandwidth and generates massive intermediate write-backs, becoming the dominant source of prefill time. This is also why “sparser” does not automatically mean “faster.”
3. The Core Idea of Latent Sparse Attention
A recent paper from a startup research team in collaboration with universities proposes Latent Sparse Attention. The key is not to make K smaller, but to move the selection step into a low-dimensional latent space.
The idea comes down to three points:
- Compressed representation: map the lengthy history token sequence into a set of latent states far smaller than the original sequence, reducing the candidate pool from “sequence length” to “number of latent states.”
- Scoring in latent space: the query is compared against these compressed representations in latent space, so Top-K selection no longer grows quadratically with the original length.
- Gather and aggregate: once latent states are selected, map them back to the corresponding subset of original tokens and perform weighted aggregation in the original precision space, avoiding accuracy loss from compression.
Intuitively, this is like building a searchable index for a very long history: the query consults the index to locate the relevant chapters, then goes back to the source text to read closely, rather than paging from the first page to the last.
The paper reports that at ten million tokens, prefill for a global attention layer was sped up by more than twentyfold, and another model at a different scale saw more than seventyfold speedup, while accuracy stayed close to the original model across a range of tasks. It is worth stressing that these numbers come from specific models, specific layers, specific hardware, and specific implementations, and should not be extrapolated into a general speedup ratio.
4. Complexity View: From Quadratic to Near-Linear
Let the original sequence length be N, the number of latent states be M, and the number of selected positions be K:
- Selection stage of naive sparse attention: O(N²)
- Selection stage of latent sparse attention: O(N·M + M·K), with aggregation still O(N·K)
Since M is usually far smaller than N (for example, set by a fixed compression ratio or a fixed number of blocks), the overall cost drops from quadratic to near-linear. This also explains an observed pattern: the longer the sequence, the larger the gain. At the million-token scale the improvement may be a single-digit factor; at ten million tokens it can be an order of magnitude.
One engineering caveat: compression is not free. Building latent states requires extra projection and reduction work. If the compression ratio is too low, the savings in selection are eaten by compression overhead; if it is too high, the index becomes too coarse, Top-K localization becomes inaccurate, and accuracy drops. Compression ratio, number of latent states, and K therefore need to be tuned jointly.
5. Why Accuracy Holds Up
Sparse attention works in the first place because attention distributions are highly concentrated: a few positions contribute the vast majority of the weight. The latent-space method adds a coarse localization step on top of that, and as long as the index retains enough information, coarse localization will not miss the positions that truly matter.
Common safeguards in practice include:
- Retaining a local window so neighboring tokens are not flattened by compression;
- Keeping a small number of “anchor” tokens (such as the sequence start or explicit marker positions) out of compression;
- Hybrid layer design, where some layers use global sparse attention and others use local or linear attention;
- Applying normalization and residual connections to the compressed representations to reduce information loss.
6. Implementation Advice and Evaluation
If you are optimizing long-context inference, proceed in this order:
- Locate the bottleneck first: measure the time share of the selection stage and the aggregation stage separately, and confirm whether the quadratic term actually dominates.
- Establish a baseline: record time-to-first-token, prefill throughput, and peak memory at different lengths.
- Replace incrementally: enable latent-space selection in a subset of global attention layers first, then watch accuracy and latency.
- Tune jointly: run a grid search over three dimensions — compression ratio, number of latent states, and K — rather than tuning one in isolation.
- Regression-test accuracy: cover long-document retrieval, multi-hop QA, code-base comprehension, and long-form summarization, instead of relying on perplexity alone.
- Plan capacity: with elastic scaling, log services, and monitoring alerts on your container platform, bring latency and memory metrics into routine observation.
A schematic pseudocode structure looks like this:
# Latent Sparse Attention (structure illustration, not a concrete API)latent = compress(history, ratio=r) # [M, d], M << Nidx = topk(latent @ query.T, k=K) # low-dimensional index lookuptokens = gather(history, map_back(idx)) # gather original tokensout = softmax(qk_norm(tokens)) @ tokens_v # aggregation at original precision
7. Conclusion
At ten million tokens, the central tension of long context has shifted from “too much to compute” to “too much to search.” Latent Sparse Attention answers it by moving selection into a low-dimensional space, trading controlled compression for near-linear localization cost, and then performing exact aggregation in the original space. For developers, the real takeaway is not a particular speedup figure but a reusable analysis framework: quantify the bottleneck first, choose the compression granularity second, and back it up with multi-task accuracy regression last.
