Sparse Attention at 10 Million Tokens: The Key to a 22x Speedup
As context windows grow from millions to tens of millions of tokens, the main bottleneck in long-context inference shifts from attention computation to retrieval and indexing. This article explains why the cost of finding relevant tokens still grows quadratically, and how latent-space sparse attention moves that search into a low-dimensional space to decouple retrieval cost from sequence length. It covers reported layer-level speedups, four engineering considerations around latent dimensions, index caching, positional encoding and parallelism, a minimal integration example, and guidance on where this approach pays off.
Sparse Attention at 10 Million Tokens: The Key to a 22x Speedup
1. Ultra-Long Context Is Rewriting the Cost Structure
Over the past two years, long context has moved from a selling point to a default specification. Million-token windows are now standard in mainstream model services, and research and engineering teams have begun pushing toward the ten-million-token range. On the surface, this just means enlarging the window by another order of magnitude. In practice, the system’s cost bottleneck has shifted.
At the million-token scale, attention computation itself is the dominant cost. Once the window reaches ten million tokens, an initially unremarkable stage starts to spiral out of control: the retrieval and indexing process that decides which tokens to look at. In one global attention layer of a hybrid-architecture model, roughly 98% of prefill time at the ten-million-token scale is not spent on the attention operator, but on building and querying the index and filtering candidate tokens.
This changes the nature of the problem. The sparse attention approach has long been validated by extensive experiments showing that having each query attend to only a small set of the most relevant historical tokens is enough to preserve accuracy. What actually blocks ultra-long context is not how much you look at, but the act of finding that small subset, whose cost still grows quadratically with context length.
2. The Root of Quadratic Growth: Retrieval Cannot Be Amortized by More Compute
To understand why indexing becomes the bottleneck, break a sparse attention call into three steps:
- Scoring: compute relevance between each query and all historical tokens.
- Selection: pick the top-k candidates from the scores.
- Aggregation: perform attention-weighted summation over the candidate set only.
The third step’s complexity is already determined by k and is largely decoupled from context length. The problem lies in the first two: scoring must traverse all historical tokens, and selection requires a global comparison over an extremely long sequence. Together, they make the cost of finding grow quadratically with length, while growth in GPU compute can only amortize part of it linearly.
Common engineering mitigations each come with tradeoffs. Chunked retrieval narrows the scope of each comparison, but more chunks introduce additional scheduling and memory-access overhead. Multi-level indexing reduces the candidate pool, but adds construction and synchronization costs. Quantized scoring compresses bandwidth, but degrades ranking quality and, in turn, final accuracy. In other words, the industry does not lack faster retrieval; it lacks an approach that decouples retrieval cost from context length.
3. Latent-Space Sparse Attention: Moving the Search into a Low-Dimensional Space
A recent proposal from an overseas research team offers a directional answer. Its core idea can be summarized as latent-space sparse attention: instead of performing full scoring on the original token representations, first map the query and historical tokens into a low-dimensional latent space, complete relevance retrieval in that space, and then return to the original space for attention aggregation.
The key difference is where retrieval happens. Original token dimensions are high and sparsely distributed, making any pairwise vector comparison expensive. The latent space is lower-dimensional and more compact, with semantically similar tokens sitting closer together, so both the number of comparisons and memory accesses required for retrieval drop. Because retrieval cost is mainly determined by the latent dimension and candidate pool size rather than the original sequence length, the overall overhead is largely decoupled from context length.
This idea shares roots with long-standing industry techniques such as low-rank approximation, positional compression, and memory compression, but it applies compression to the index rather than the representation: the model still sees the original tokens, only the way it finds them changes. As a result, it does not require retraining the backbone network and can be integrated on the inference side into existing sparse attention implementations.
Publicly reported results show that at the ten-million-token scale, this approach delivers roughly a 22x speedup for the prefill stage of a single global attention layer, with speedups reaching tens of times on another model architecture, while maintaining accuracy close to the original model across multiple long-context tasks. It is worth emphasizing that these figures are layer-level results under specific models, specific layer types, and specific hardware conditions. They are not equivalent to end-to-end throughput gains; actual benefits depend on the proportion of global layers, the k setting, and memory bandwidth.
4. Engineering Deployment: Four Questions to Settle in Advance
4.1 The Ratio of Latent Dimension to Candidate Count
A latent space that is too small loses ranking quality; one that is too large degrades back into original-space retrieval. In practice, start with a smaller dimension, observe how top-k hit rate and task metrics change, and then increase it gradually. The k value should match how much the task depends on local detail: summarization tasks can use a smaller k, while code and multi-hop reasoning tasks need a larger one.
4.2 Index Construction and Caching Strategy
Build the index once during prefill and update it incrementally during decode. If KV is managed with paged caching, the index must also be maintained per page to avoid rebuilding it at every step. For multi-turn conversations, bind the index lifecycle to the KV lifecycle and reclaim it when the session ends.
4.3 Compatibility with Positional Encoding and Parallel Sharding
Latent-space retrieval changes how tokens are compared, so confirm that positional information is still injected explicitly; otherwise, long-range ordering degrades. Under tensor parallelism, minimize cross-device synchronization during index construction and querying. Under sequence parallelism, ensure the candidate set remains complete at sharding boundaries.
4.4 Evaluation Criteria for Accuracy Regression
Long-context accuracy issues often do not show up in average scores, but in a few tasks such as needle-in-a-haystack retrieval and multi-hop aggregation. A layered evaluation is recommended: first compare numerical consistency at the layer level, then run task-level regression, and finally stress-test end-to-end latency and peak memory usage.
5. How to Integrate It into an Existing Inference Stack
A minimal working sketch is shown below (pseudocode, expressing control flow only):
# Original sparse attention: full scoring in the original dimensionscores = q @ k_all.T # complexity grows quadratically with sequence lengthidx = topk(scores, k)out = softmax(q @ k[idx].T) @ v[idx]# Latent-space sparse attention: retrieval in a low-dimensional spaceql = project_q(q) # project down to the low-dimensional latent spacekl = project_k(k_all)scores = ql @ kl.T # comparison cost drops significantlyidx = topk(scores, k)out = softmax(q @ k[idx].T) @ v[idx] # aggregation still happens in the original space
During deployment, use a switch to control the old and new paths, enable them per layer type, and monitor time-to-first-token, per-step decode latency, and memory levels with alerting. Log candidate hit rate and index construction time in the logging service to help pinpoint where gains fall short of expectations. If inference runs on a container platform, mount the index cache separately from model weights to avoid rebuilding it during scaling.
6. Benefit Boundaries and Selection Guidance
Latent-space sparse attention is not a universal switch. It is best suited to inference workloads with extremely long contexts, a high proportion of global attention layers, and retrieval cost that has already become the primary bottleneck, such as long-document understanding, cross-repository code analysis, and long-session memory. Conversely, if the context is not long to begin with, or the model relies mainly on local window attention, the gains are limited and the added implementation complexity is not worthwhile.
From an architectural evolution perspective, this approach points to a more general principle: once scale crosses a certain threshold, the optimization focus should shift from computing faster to computing less and deciding what to compute more cheaply. Pushing decision cost to decouple from scale is the prerequisite for sustainable contexts at the ten-million-token level and beyond.
