Sparse Attention at 10 Million Tokens: Why Fixing the Index Delivers a 22x Speedup
As context windows scale from hundreds of thousands toward 10 million tokens, sparse attention has proven that attending to only a small subset of keys preserves accuracy. The remaining bottleneck is the selection step itself: scoring every key for every query still grows quadratically with context length. This article breaks down the cost structure of sparse attention, explains how moving the scoring stage into a fixed-dimensional latent space decouples selection cost from window length, and covers why ranking consistency rather than numerical precision is what protects accuracy, along with practical deployment notes and common pitfalls.
1. The First-Order Cost of Long Context: From Computing Attention to Finding Attention
Over the past two years, the race for long-context capability has revolved around a single number: the usable window moving from hundreds of thousands of tokens to millions, and now pushing toward tens of millions. Once windows reached the million-token scale, engineering consensus settled on one idea: instead of having every query scan the entire token history, select only the few most relevant pieces to participate in the computation. This sparsification route has been validated repeatedly at the million-token scale, with controllable accuracy loss and significant gains.
But when the window grows another ten or a hundred times, an originally negligible overhead starts to spiral out of control. Take a global attention layer as an example: when running prefill at the scale of 10 million tokens, the majority of the time is not spent on attention itself, but on the indexing stage that decides which tokens to look at.
The nature of the problem therefore changes. Sparse attention has long proven that “looking at only a small part” is enough to preserve accuracy; what actually blocks ultra-long context is that the cost of “finding that small part” still grows quadratically with context length.
This article does not center on any specific model or product. Instead, it abstracts the phenomenon into a general technical problem: how to free the selection stage of sparse attention from quadratic complexity and achieve genuinely subquadratic attention for ultra-long context.
2. Cost Structure Breakdown: Why Sparse Attention Is Still Not “Cheap”
The cost structure of standard full attention is well known: with T queries, the prefill stage costs roughly O(T²) in computation, and decoding each token costs roughly O(T). Growing the window from one million to 10 million tokens inflates attention computation by about a hundred times.
The division of labor in sparse attention works like this: a lightweight indexer first picks the k most relevant keys for each query, then the actual attention computes only over those k keys, compressing attention proper to O(Tk).
This approach rests on one premise: the model really does need only a few keys. Validation in the industry at the scale of a few hundred thousand tokens has been fairly consistent: letting each query attend only to the few hundred keys most relevant to it (typically less than one percent of all keys) still retains about 98% of full-attention accuracy on long-context benchmarks.
But a paradox hides here: for the indexer to know which few hundred keys are most relevant, it must first score every key. Naive scoring means every query performs a similarity evaluation against all historical keys, and the cost is once again O(T²). So sparse attention pushes down the bill for “computing attention” but leaves behind the bill for “finding attention”, and at the 10-million-token scale that bill becomes the dominant term by far.
Existing engineering mitigations fall into roughly three categories, but all of them only shrink the constant without changing the order of magnitude:
- Lower scoring precision: use low-bit or low-precision representations for similarity estimation to reduce the per-score cost;
- Compress key dimensions: project the representation used for scoring into a lower-dimensional space;
- Block sparsity: aggregate several adjacent keys into a block, score and select at block granularity, cutting the number of scoring operations to a fraction of the original.
These methods help, but the multiplicative relationship between the number of scoring operations and T remains, so the curve stays steep as the window keeps growing.
3. The Core Idea: Move Scoring into Latent Space
A more thorough approach is to not perform selection in the original token space, but instead construct a low-dimensional latent space where matching between queries and keys happens, and whose size does not grow linearly with context length.
The key design can be abstracted into three steps:
- Compressed representation: map the key at each historical position into a latent vector of fixed dimension, with dimensionality independent of window length;
- Latent-space scoring: the query performs a dot product against these fixed-dimension representations in latent space to obtain a relevance estimate;
- Top-k selection: keep only the small number of highest-scoring positions and hand them to attention proper.
Because the latent vector dimension is fixed, scoring cost is decoupled from window length, and the selection stage drops from O(T²) to roughly O(T) (viewable as subquadratic under blocked and parallel implementations). Overall complexity shifts from “quadratically dominated” to “linearly dominated”. This is exactly what “subquadratic ultra-long-context attention” means.
The structure can be expressed with illustrative pseudocode (showing logic only, not a specific implementation):
# Illustrative: latent-space index + top-k sparse attention# h: historical latent vectors, shape [T, d_latent], d_latent independent of T# q_lat: current query projected into latent space, shape [d_latent]# k, v: original keys and values, shape [T, d_head]scores = h @ q_lat # [T], scoring decoupled from window lengthidx = topk(scores, k) # select the k most relevant positionsout = softmax(q @ k[idx].T) @ v[idx] # attention computed over only k keys
It is worth emphasizing that latent space is responsible only for “filtering”; the real attention is still completed in the original representation, so compression does not directly cost attention accuracy. Whether accuracy holds depends on the ranking consistency between latent-space scoring and true relevance.
4. Why Accuracy Holds: Ranking Consistency, Not Numerical Consistency
A common misconception is that compressed representations necessarily reduce accuracy. In fact, the selection stage needs only “correct ranking”, not “numerically exact values”. As long as latent-space scoring ranks the truly most relevant positions at the front, the subsequent attention computation in the original space is lossless.
Common engineering practices for improving ranking consistency include:
- Let latent vectors and query projections share the training signal, so the scoring objective aligns with the true attention distribution;
- When using block-level aggregation, preserve within-block representativeness so that strongly relevant positions inside a block are not averaged away;
- For tasks with strong locality, retain a small number of “must-see” positions (such as the recent window or special marker positions) and merge them with the latent-space selection result;
- Introduce selection-consistency constraints during training or fine-tuning to reduce distribution shift between training and inference.
5. Complexity and Gains: Order-of-Magnitude Change Matters More Than Constant Tuning
Placing the two schemes side by side makes the difference obvious:
- Naive sparse attention: attention O(Tk), indexing O(T²), with the overall order dominated by T²;
- Latent-space sparse attention: attention O(Tk), indexing roughly O(T), with the overall order dominated by T.
At the million-token scale, the gap between the two may be only a constant factor; at the 10-million-token scale, the order-of-magnitude difference is amplified into a difference of orders of magnitude. This is also why the same idea shows nonlinear growth in its “speedup multiple” across different window scales: the larger the window, the higher the share of the eliminated quadratic term.
The direct implication for engineering practice is that optimizing the index structure yields more than continuing to tune the attention kernel, and the benefit becomes more pronounced as the window grows.
6. Deployment Notes and Common Pitfalls
- Blocking and parallelism: latent-space scoring is naturally suited to blocked, streaming processing; prefill can be blocked and parallelized, but global top-k correctness must be preserved when merging across blocks;
- Memory and bandwidth: latent vectors must stay resident, so watch their memory footprint and access pattern to avoid the scoring stage becoming a bandwidth bottleneck;
- Decoding stage: single-token decoding already has low indexing cost, so the main gains from the latent-space approach are in prefill and first-token latency on long sequences;
- Working with positional encoding: at ultra-long windows, positional extrapolation error is amplified, so the selection mechanism must be evaluated together with the positional scheme;
- Evaluation metrics: report prefill latency, decode throughput, and long-context benchmark accuracy together, avoiding misleading single-point metrics;
- Accuracy regression: always regress separately in the two ranges of “lossless at short windows” and “controllable at long windows” to prevent long-window optimization from hurting common scenarios.
7. Conclusion
The core tension of 10-million-token context has shifted from “being able to compute it” to “being able to find it”. Migrating the selection stage from the original token space to a latent space decoupled from window length is currently a clear path for pushing sparse attention toward subquadratic complexity. For developers, the real takeaway is not any specific implementation but a judgment: as the window keeps growing, restructuring the invisible bill of “indexing” first often has more leverage than optimizing attention proper.
