The Key to 22x Faster Sparse Attention at 10 Million Token Contexts
As context windows scale toward 10 million tokens, the bottleneck in long-context inference shifts from compute to selection. This article explains why the indexing stage of sparse attention becomes a new quadratic cost, introduces Latent Sparse Attention (LSA) as a two-stage coarse-then-fine approach that moves selection into a low-dimensional latent space, and provides pseudocode plus an engineering checklist covering hybrid layer design, latent state caching, adaptive quotas, accuracy regression, and observability.
1. Long Contexts Are Shifting the Bottleneck from Compute to Search
Over the past two years, long-context capability has moved quickly from an experimental feature to a default specification. Million-token context is becoming standard in mainstream model serving, and some models now combine million-token context with hybrid architectures that mix sparse attention and linear attention, trading a smaller set of “only the most relevant history fragments per query” for acceptable memory and latency.
This approach has been validated repeatedly at the million-token scale: the number of key-value pairs that actually participate in computation is sharply reduced, with limited accuracy loss. But as context pushes into the tens of millions of tokens, an overhead that once seemed negligible starts to spiral. In one class of global attention layers, roughly 98% of prefill time at the 10-million-token scale is not spent on the attention matrix multiplications at all, but on the indexing step that “decides which tokens to look at.”
The nature of the problem therefore changes. Sparse attention has already proven that “looking at only a small fraction” is enough to preserve accuracy; what blocks ultra-long context is that the cost of “finding that small fraction” still grows quadratically with context length. In other words, the computation has been cut down, but the selection cost has not been cut in step with it, and it has even become the dominant term.
2. Why the Indexer Becomes the New Quadratic Bottleneck
A single global attention layer can be broken into three steps:
- Scoring: estimate the similarity between each query and all historical positions;
- Selection: pick the top-k positions;
- Aggregation: compute an attention-weighted sum over only the selected keys and values.
Sparsification usually optimizes only the third step, while the first two are inherently on the order of O(n²): each of the n queries must be compared against n positions. Multiply the context by ten and the selection cost grows a hundredfold. When n reaches the tens of millions, the memory-access and scheduling overhead of top-k sorting and threshold checks far exceeds the efficiency of dense matrix multiplication on dedicated accelerator units.
Two engineering factors amplify this further. First, memory access in the indexing stage is irregular and hard to saturate bandwidth. Second, at long sequence lengths the intermediate score volume must be processed in a tiled, streaming fashion, or it will not fit in memory; tiling in turn introduces repeated reads and synchronization overhead. The result is “idle compute, saturated indexing.”
3. The Core Idea of Latent Sparse Attention
A recent paper from a startup research team and a university proposes Latent Sparse Attention (LSA). Its core idea is not to keep doing smarter filtering in the original token space, but to move the “search” into a low-dimensional latent space.
It can be summarized in three points:
- Compressed representation: compress the keys of a long sequence into a small number of latent states, each carrying aggregated information for a contiguous span of positions;
- Latent-space scoring: the query first computes dot products against the small set of latent states to obtain a coarse relevance distribution;
- Two-stage selection: first select a few latent states, then do fine-grained top-k within the original intervals they cover, and finally aggregate.
Because the scoring targets drop from n positions to m latent states (m far smaller than n), the complexity of the selection stage drops approximately from O(n²) to O(n·m + n·k), which is nearly linear growth at the 10-million-token scale. The paper reports that at the 10-million-token scale, the prefill stage of a single attention layer is about 22x faster than an existing sparse attention approach, with speedups of tens of times achievable on another model architecture, while maintaining accuracy close to the original model across a range of long-text tasks.
It is worth stressing that this kind of speedup is a gain in “intra-layer prefill” and does not mean end-to-end throughput improves by the same ratio; the decoding stage, fully connected layers, vocabulary projection, and others still account for a substantial share.
4. Illustrative Implementation
The pseudocode below only expresses control flow and does not depend on any specific framework interface:
def lsa_attention(q, k, v, m=1024, k_fine=2048, block=4096):# 1) Precompute: compress long-sequence keys into m latent states (cacheable and reusable)latent = compress_kv(k, v, num_latent=m) # [m, d]out = []for qs in chunk(q, block): # Stream queries in blocks# 2) Coarse filtering in latent space: cost O(len(qs) * m)scores = qs @ latent.T # [b, m]cand = topk(scores, k=m // 4) # First select a few latent states# 3) Fine filtering within intervals + aggregationfor idx in cand:lo, hi = span_of(idx) # Original interval covered by this latent stateks, vs = k[lo:hi], v[lo:hi]s = qs @ ks.Tsel = topk(s, k=k_fine)out.append(softmax_with(qs, ks[sel], vs[sel]))return merge(out)
There are three key implementation points: how latent states are constructed (mean pooling, a learnable projection, or convolutional aggregation) determines recall quality; the quota of the two-stage top-k determines the balance between accuracy and speed; and the tiling and caching strategy determines whether the indexing stage can be turned into a bandwidth-friendly operation.
5. Engineering Deployment Checklist
- Layered hybrid: use global sparse attention in only some layers, and local windows or linear attention in the rest, to further compress selection cost;
- Cache latent states: latent states built during prefill can be reused across requests, and combined with prefix caching this can significantly reduce repeated indexing;
- Adaptive quotas: dynamically adjust k based on query entropy or the attention distribution, giving a larger quota to flat distributions and tightening it for sharp ones;
- Accuracy regression: run A/B tests on three task types, long-document retrieval, multi-hop question answering, and long-text summarization, with particular attention to recall of mid-section information;
- Observability: bring indexing latency, selection rate, and latent state hit rate into monitoring, alerting, and logging services, to avoid “speedups that only hold in load tests.”
6. Conclusion
The real adversary at the 10-million-token scale is not floating-point compute, but the quadratic selection cost. Moving filtering into a low-dimensional latent space and replacing global scoring with a two-stage “coarse plus fine” structure is currently a cost-effective general solution. For developers, the focus of deployment is not the algorithm formula but three engineering details: latent state construction, quota strategy, and cache reuse; only by closing the loop on load testing and monitoring on top of container platforms and object storage can long-context capability be delivered reliably.
