Sparse Attention at 10M Token Context: Key Practices Behind a 22x Prefill Speedup
As context windows grow from 1M to 10M tokens, the bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down where the quadratic indexing cost comes from, explains how Latent Sparse Attention (LSA) moves retrieval into a low-dimensional latent space with a two-stage recall-and-rerank design, and shows why hybrid architectures benefit most. It also covers four practical engineering points—candidate pool sizing, projection initialization, separate prefill and decode strategies, and cache/batching coordination—plus how to validate gains at the operator, model, and serving layers.
Sparse Attention at 10M Token Context: Key Practices Behind a 22x Prefill Speedup
1. Long Context Is Turning Attention into a Retrieval Problem
Long-context capability has advanced faster than most people expected over the past two years. Million-token windows have steadily become the default spec for mainstream model serving, and some platforms now ship 1M context as a standard configuration. Once you push into the 10M token range, the engineering question is no longer whether the model can read that much, but what it costs to read it.
The quadratic growth of compute and memory in dense attention as sequence length increases has been discussed repeatedly. The mainstream response is sparsification: instead of having every query scan the entire history, each query selects a small number of the most relevant segments to attend to. This approach has already been proven at the 1M scale, with controllable accuracy loss.
But as context keeps scaling to 10M tokens, an overhead that once looked negligible starts to spiral. Take a global attention layer as an example: at 10M tokens, roughly 98% of prefill time is not spent on the attention computation itself, but on the indexer that decides which tokens to look at.
That changes the nature of the problem. Sparse attention solved “computing too much,” but it did not solve “searching too slowly.” The indexing step still grows quadratically with context length and has become the dominant cost.
2. Where the Quadratic Cost Actually Comes From
To understand the value of LSA (Latent Sparse Attention), you first need to break down the cost of the indexing stage. A typical sparse attention flow has three steps:
- Scoring: compute similarity between the query and all historical keys to produce candidate scores;
- Selection: pick the top-k positions from those scores;
- Aggregation: compute a weighted sum over only the selected k tokens.
The cost of the third step is already bounded by k. What actually explodes is the first and second steps. Scoring must evaluate all N keys one by one, with complexity O(N·d); selection must maintain a candidate set, at a cost that also grows with N. When N goes from 1M to 10M, indexing cost scales up tenfold, and since it already accounted for a large share of total time, it completely overshadows the attention computation.
There is an easily overlooked implication here: in long-context scenarios, the optimization target should not be “compute faster” but “search more cheaply.” Any approach that still requires the query to explicitly interact with the full history will hit the same wall.
3. The Core of LSA: Moving Retrieval into Latent Space
LSA can be summarized in one sentence: don’t retrieve in the original token space—first compress the query and keys into a low-dimensional latent space, locate candidates there, then return to the original space for exact aggregation.
The structure breaks into four modules:
- Latent projection: introduce a set of learnable low-dimensional projection matrices for the query and keys, compressing d-dimensional representations down to d’ dimensions (d’ much smaller than d);
- Low-dimensional scoring: compute similarity in the d’ space to produce candidate scores;
- Coarse filtering and reranking: first take a larger candidate pool in the low-dimensional space, then run a second check using a small number of original dimensions;
- Sparse aggregation: run attention only over the final k selected tokens.
The key gain is a change in the complexity structure: scoring drops from O(N·d) to O(N·d’), and the low-dimensional space is more memory-access friendly with higher cache hit rates. Since d’ is a constant-level configuration, the growth of indexing cost with N is significantly flattened—the quadratic curve is effectively straightened out.
Illustrative pseudocode:
# Low-dimensional latent retrieval + reranking in the original spaceq_lat = q @ Wq_lat # [B, H, L, d']k_lat = k @ Wk_lat # [B, H, N, d']scores_lat = q_lat @ k_lat.transpose(-1, -2) # low-dimensional scoring, much cheapercand = scores_lat.topk(k_pool, dim=-1).indices # coarse candidate pool# Rerank in the original dimensions over candidates onlyk_sel = gather(k, cand) # [B, H, L, k_pool, d]scores = (q.unsqueeze(-2) @ k_sel.transpose(-1, -2)).squeeze(-2)idx = scores.topk(k, dim=-1).indicesout = sparse_attention(q, k, v, idx) # compute over k tokens only
It is worth stressing that LSA is not just another term for approximate attention. It preserves a two-stage structure: the low dimension handles recall, and the original dimensions handle the decision. The recall stage tolerates some error, while the decision stage guarantees selection quality. This division of labor is the fundamental reason accuracy is preserved.
4. Why a Hybrid Architecture Is a Prerequisite
A single sparse mechanism struggles to cover both local detail and global information. Current long-context models generally adopt a hybrid architecture: some layers use global attention for cross-segment integration, while others use linear attention or local windows for low-cost modeling.
The gains from LSA are amplified under this architecture, for three reasons:
- Global layers are the bottleneck. Global layers must model the full history, so their indexing cost is the highest—and that is exactly where LSA’s speedup is most pronounced;
- Linear layers are unaffected. Linear attention does not perform full retrieval, so introducing LSA does not change its complexity;
- The division of labor between layers becomes clearer. Global layers focus on “finding accurately,” linear layers focus on “computing cheaply,” and overall throughput is determined by the slowest global layer—so optimizing it optimizes the whole.
For production, the recommendation is to configure per layer: enable latent retrieval only on global attention layers and leave the rest unchanged, avoiding unnecessary projection overhead.
5. Four Key Engineering Points for Production
1) Setting the candidate pool size k_pool
Too small a k_pool loses recall; too large and it degrades back to the original approach. In practice it is usually set to 4–8x k, and you should observe the recall curve on a validation set to find the inflection point rather than picking a value by intuition.
2) Initializing and training the projection matrices
Orthogonal initialization is recommended for the low-dimensional projections, and the original attention path should remain a fallback early in training (for example, taking the dense branch with some probability) to avoid unstable gradients in the early phase.
3) Different strategies for prefill and decode
The prefill stage has long sequences and is parallelizable, making it suitable for full low-dimensional scoring; the decode stage adds only one query per step, so you can cache the latent representation of keys and reduce per-step retrieval cost to constant time. The KV cache needs to store both the original and latent representations—trading memory for time.
4) Coordinating with caching and batching
Latent representations are small and suitable for higher-tier caches; under continuous batching, request lengths vary widely, so scheduling by length buckets is recommended to reduce padding waste.
6. Gain Boundaries and Validation Methods
The speedup of LSA-style approaches depends heavily on model structure: the higher the share of global layers and the more aggressive the latent dimension compression, the more pronounced the gain. The magnitude given in the source material is a 22x to 70x prefill speedup for a single attention layer at 10M tokens; such numbers should be read as an upper-bound reference for a specific structure, not a universal promise.
Validation is recommended at three levels:
- Operator level: compare the share of time spent in the indexing stage to confirm whether the quadratic term has been flattened;
- Model level: compare accuracy on tasks such as long-document retrieval, multi-hop QA, and code base comprehension, with attention to long-tail samples;
- Service level: observe time to first token and throughput to confirm the optimization holds under real traffic.
7. Conclusion
At 10M token context, the core tension has shifted from “can’t compute” to “can’t find.” Migrating retrieval from the original token space to a low-dimensional latent space, and trading a two-stage recall-and-rerank structure for lower complexity, is currently one of the most cost-effective directions. For developers, the real practical point is not to copy a particular speedup number, but to first locate the indexing bottleneck in your own pipeline, then decide on the latent dimension, candidate pool, and caching strategy. If you need to serve long-context inference, you can combine general cloud capabilities such as container platforms, object storage, and monitoring and alerting to handle deployment and observability, and surface the optimization effect consistently in production metrics.
