Sparse Attention at 10 Million Tokens: The Key to a 22x Prefill Speedup
As context windows scale toward 10 million tokens, the bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article explains why naive indexing costs grow quadratically with sequence length, how Latent Sparse Attention moves similarity search into a low-dimensional latent space to cut indexing cost, why reported speedups vary across model architectures (about 22.7x for one global attention layer and about 72.4x for another structure at 10 million tokens during prefill), and how to profile, prototype, and roll out similar optimizations in your own inference stack.
1. Long Context Is Spending the Compute You Saved
Over the past two years, long-context capability has moved from an experimental feature to a default specification. Million-token context windows are now standard in many general-purpose LLM services. Going further, the industry is exploring inference scenarios at the 10-million-token scale, including whole-repository code understanding, correlation analysis over very long logs and on-chain records, and joint question answering across multi-quarter document sets.
The common technical choice behind these models is to stop having each query scan every historical token and instead let it attend to only a few of the most relevant tokens. This approach has been validated repeatedly at the million-token scale: accuracy is largely preserved, and performance on long-text retrieval, summarization, and multi-hop QA remains stable.
But as the context grows to the 10-million-token scale, an overhead that once seemed minor starts to spiral. In one model with a global attention layer, roughly 98% of prefill time at 10 million tokens is not spent on attention computation itself, but on the indexer that decides which tokens to look at.
This changes the nature of the problem. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks ultra-long context is that the cost of finding that subset still grows quadratically with context length.
2. Where the Quadratic Indexing Cost Comes From
To understand this overhead, split attention into two stages:
- Selection (indexing/routing): score each query against candidate tokens and pick the top-k.
- Computation (attention compute): take a weighted sum over only the selected k tokens.
The benefit of sparsification applies only to the second stage. If the first stage uses a naive implementation that scores a query against every historical token, its complexity remains O(n²·d), where n is the sequence length. The result is an awkward scissors effect: the larger n gets, the harder the compute stage is compressed, the higher the indexing stage’s share climbs, and the overall speedup is ultimately capped by indexing cost.
Schematically, the time for a single layer can be written as:
# Illustrates complexity structure only; not a real implementationt_total = t_index(n, d) + t_compute(n, k, d)# Naive indexing: t_index ∝ n^2 * d# Sparse compute: t_compute ∝ n * k * d, k << n# As n grows, t_index becomes the dominant term
Common engineering mitigations include block-wise scoring, local window constraints, and per-page or per-segment pre-aggregation. But these only reduce constant factors or move part of the indexing into a cacheable stage; they do not change the quadratic growth with length. That is precisely the target of a newer class of methods.
3. Latent Sparse Attention: Moving the Search into a Low-Dimensional Space
A recent paper from a startup research team and university collaborators proposes Latent Sparse Attention. Its core claim can be summarized in one sentence: do not perform exhaustive matching in the original token space; instead, project both the query and candidate tokens into a low-dimensional latent space, and perform similarity retrieval and top-k selection there.
The reasoning has three parts:
- Dimensionality reduction reduces cost. Original scoring requires a dot product between the query and the d-dimensional representation of every token. After projecting to r dimensions (r much smaller than d), the cost per score drops to r/d, significantly compressing the overall magnitude of the indexing stage.
- Retrieval and computation are decoupled. The indexing stage only locates a candidate set in the low-dimensional space; the computation stage can still run on the original representations, preventing accuracy loss from dimensionality reduction from propagating directly to the output.
- Candidate quality is controllable. Training aligns the distance structure of the latent space with the true attention distribution, preserving top-k recall so that approximate retrieval does not sacrifice exact computation.
The paper reports that at the 10-million-token scale, this approach speeds up the prefill of a single global attention layer by about 22.7x, and speeds up another model structure by about 72.4x, while maintaining accuracy close to the original model across multiple tasks. Note that these are single-layer, prefill-stage speedups; end-to-end gains depend on the proportion of sparse layers among all model layers, the indexer implementation, and hardware utilization.
4. Why Gains Differ So Much Across Model Architectures
The gap between 22x and 72x highlights an important engineering fact: the speedup is not an intrinsic constant of the method but is strongly coupled to model architecture.
- Sparse layer ratio. If most layers use global attention, indexing accounts for a larger share and the ceiling on gains is higher.
- Head count and dimension configuration. In multi-head settings, indexing may be shared or independent, which directly determines how much dimensionality reduction can be reused.
- Mixed linear attention. In hybrid architectures, linear layers already have linear complexity, which dilutes the overall speedup.
- Indexer granularity. Token-level, block-level, and page-level retrieval have different marginal benefits and costs for top-k.
So when evaluating this kind of approach, watch the leading indicator of “indexing-stage share” rather than only the end-to-end number.
5. Adoption Path and Engineering Considerations
To borrow this idea in your own inference pipeline, proceed in this order:
- Profile first. Measure the share of indexing versus compute time at the target length to confirm the bottleneck is indexing.
- Define the latent space. Choose the projection dimension r and decide whether projection is shared or grouped per head.
- Align the training objective. Use distillation or an auxiliary loss to make latent-space distances approximate the true attention distribution.
- Preserve compute precision. Use low dimensions for indexing and return to the original dimensions for computation to avoid error accumulation.
- Roll out in stages. Run A/B tests on long-context tasks such as long-document summarization and code-base QA, and watch accuracy and time to first token.
Illustrative pseudocode:
# Indexing happens in the low-dimensional latent space; computation stays in the original spaceq_lat = proj_q(q) # [n, r]k_lat = proj_k(k) # [n, r]scores = q_lat @ k_lat.T # Low-dimensional scoring at much lower costidx = topk(scores, k) # Select candidatesout = attention(q, k[idx], v[idx]) # Exact computation in the original dimensions
On the deployment side, this kind of optimization is typically used alongside containerized inference services, model and cache distribution over object storage, and logging, monitoring, and alerting systems, so that time to first token, throughput, and recall can be observed continuously. On cloud platforms such as Baidu AI Cloud, staged rollouts and rollbacks can be handled with general-purpose container platforms, object storage, and monitoring and alerting capabilities.
6. Conclusion
In the 10-million-token era, the core tension has shifted from “can’t compute” to “can’t find.” The value of Latent Sparse Attention is that it brings the indexing stage into complexity governance, so the benefits of sparsification are no longer swallowed by quadratic retrieval. For developers, the real action item is to quantify indexing share first, then decide whether to rebuild the retrieval path in a low-dimensional space. That matters more to the final cost of an ultra-long-context pipeline than chasing a single speedup number.
