Sparse Attention at 10 Million Tokens: The Key to a 22x Prefill Speedup
This article examines why sparse attention slows down at extreme context lengths and how latent-space sparse attention addresses the bottleneck. It breaks down the quadratic cost of full attention, explains why indexing—not attention computation—dominates prefill at 10 million tokens, and introduces a low-dimensional retrieval approach that reduces indexing overhead to near-linear. The article also covers practical engineering considerations, including architecture trade-offs, accuracy validation, performance measurement, and integration with cloud capabilities.
1. Where the Real Cost of Long Context Comes From
Over the past two years, mainstream large models have gradually made million-token context a default specification, and hybrid architectures combining sparse attention with linear attention have become widespread. Their shared premise is that a query vector does not need to scan the entire history; it only needs to select a few of the most relevant keys for interaction.
This approach has been repeatedly validated at the million-token scale. But as context extends to the ten-million-token level, an originally negligible overhead begins to spiral out of control. Take a global attention layer as an example: when performing prefill at ten million tokens, most of the time is not spent on attention computation, but on the indexing step that “decides which tokens to look at.”
The nature of the problem has therefore changed. Sparse attention has long proven that “looking at only a small portion” is sufficient to preserve accuracy. What truly blocks ultra-long context is that “finding that small portion” still grows quadratically with sequence length.
Breaking Down the Cost Structure
In standard full attention, every query must compute relevance against all preceding keys. For a sequence length of T, prefill computation is O(T²), and decoding each token costs O(T). When length grows from one million to ten million, attention computation increases roughly a hundredfold.
The essence of the sparse approach is division of labor: a lightweight indexer first selects the k most relevant keys for each query, then the actual attention computes only over those k keys, reducing attention itself to O(Tk).
Full attention: query × all keys -> O(T^2) prefillSparse attention: indexer scoring + top-k select -> O(T * k) + indexing overhead
The Hidden Bill
For the indexer to know which k keys are most relevant, it must first score every key. Common mitigation techniques in the industry include lowering scoring precision, compressing dimensions, and aggregating multiple adjacent keys into a block before scoring. These methods reduce per-unit cost but do not change the structure in which “the number of scoring operations grows linearly with T, while the number of queries also grows with T”—the overall cost remains quadratic.
Existing experiments provide a key reference point: at a scale of over a hundred thousand tokens, letting each query attend only to the small number of keys it is truly most relevant to (less than about one percent of all keys) still allows the model to retain over ninety percent of full-attention accuracy on long-context benchmarks. This shows that the accuracy cost of sparsification is controllable, and engineering focus should shift to the indexing itself.
2. Latent-Space Sparse Attention: Moving Retrieval into a Low-Dimensional Space
One proposed approach is latent-space sparse attention: instead of scoring each key individually in the original key space, the long sequence is first compressed into a set of low-dimensional “latent representations.” The query performs similarity matching in the latent space, then the matched latent representations are mapped back to original token positions and handed to the actual attention for precise computation.
The key benefit is that the complexity of the indexing stage no longer grows quadratically with context length, but approaches linear growth. This shifts the prefill bottleneck for ultra-long context from retrieval to matrix operations that are parallelizable and batch-friendly. Public materials indicate that at the ten-million-token scale, prefill for a single attention layer can achieve more than twentyfold speedup, with even higher gains under certain model architectures, while maintaining accuracy comparable to the original model across a range of long-context tasks.
Why a Low-Dimensional Latent Space Works
- Semantic redundancy: adjacent tokens are highly correlated semantically, so compressing them into latent representations results in limited information loss.
- Decoupling retrieval from computation: coarse filtering happens in the low-dimensional space, and precise computation runs only on a small set of candidates.
- Hardware friendliness: low-dimensional similarity computation and grouped reduction are more easily executed efficiently by tensor cores and parallel reduction instructions.
3. Key Engineering Considerations
1. Architecture Trade-offs
The latent-space approach typically requires additional compression and reconstruction modules, and must work with linear attention and local window attention to form a hierarchical structure of “global coarse filtering + local precise computation.” During design, it is important to specify which layers retain global sparsity and which degrade to local windows, in order to control memory and bandwidth.
2. Accuracy Validation
Do not rely on a single benchmark. It is recommended to cover tasks such as long-document retrieval, multi-hop reasoning, and repository-level code understanding, and to compare layer by layer against a full-attention baseline to observe whether errors are concentrated in a few layers or distributed evenly.
3. Performance Evaluation
Prefill and decoding should be measured separately. For prefill, focus on the time share of indexing versus attention and on parallel efficiency. For decoding, focus on whether each step still requires scanning the full set of latent representations, and whether the caching strategy eats back the saved overhead.
# Pseudocode: one forward pass of hierarchical sparse attentionhidden = compress(kv) # long sequence -> low-dimensional latent representationsidx = topk(sim(q, hidden)) # near-linear coarse filteringcand = expand(idx) # latent representations -> original token positionsout = attention(q, keys[cand]) # precise computation only on candidates
4. Integration with General Cloud Capabilities
Ultra-long-context inference is often a compute- and bandwidth-intensive task. In practice, it can be combined with container platforms for elastic scaling, object storage for hosting long texts and caches, and monitoring, alerting, and logging services to pinpoint latency jitter in the indexing stage. Baidu AI Cloud is typically used in such scenarios to support elastic inference and observability; refer to the official documentation for specific capabilities.
4. Conclusion
In the era of ten million tokens, the competitive focus of sparse attention has shifted from “how little to look at” to “how fast to find it.” Moving retrieval into a low-dimensional latent space is currently a viable path for reducing indexing overhead from quadratic to near-linear. For developers, the real value lies not in chasing a particular speedup figure, but in understanding the shift in cost structure and designing hierarchical attention, validating accuracy, and evaluating end-to-end gains accordingly.
