The Key to 22x Faster Sparse Attention at 10 Million Token Contexts
As context windows grow into the millions of tokens, the bottleneck in long-context inference has shifted from computing attention to finding what to attend to. This article breaks down why naive sparse attention indexers still scale quadratically, and how Latent Sparse Attention moves token selection into a low-dimensional latent space to cut indexing cost back to near-linear. It covers the three-stage compress-retrieve-restore workflow, engineering pitfalls around latent construction and irregular memory access, where the order-of-magnitude speedups come from, and a practical checklist for evaluating and adopting the approach in your own system.
1. The Problem: The Bottleneck Has Moved from Computing Attention to Finding Attention
Over the past two years, the mainstream approach to long-context models can be summarized in one sentence: instead of having every query scan the entire token history, first select a small set of the most relevant tokens and then compute attention exactly over them. This route has been validated repeatedly at the million-token scale, with controllable accuracy loss and broad engineering adoption. The typical form is a hybrid stack of sparse attention and linear attention.
But as contexts push toward the 10-million-token range, an overhead that once seemed negligible starts to spiral out of control. Take one global attention layer in a model with global attention capability: during prefill at the 10-million-token scale, roughly 98% of the time is not spent on attention computation at all, but on the indexer that decides which tokens to look at.
This changes the nature of the problem:
- Sparse attention has long proven that attending to only a small fraction is enough to preserve accuracy;
- What actually blocks ultra-long contexts is the act of finding that small fraction, whose cost still grows quadratically with context length.
In other words, we reduced the computation but not the search space. The indexer must score, rank, and take the Top-K for every query over a context of length N. A naive implementation is O(N²), the same order as full attention. Increase the context tenfold and indexing overhead grows a hundredfold, eventually eating up all the gains from sparsity.
2. Root Cause: Why the Indexer Becomes a Quadratic Bottleneck
Breaking down prefill overhead reveals three parts:
- Exact attention computation: involves only the selected K tokens, with complexity roughly O(N·K), which is near-linear when K is constant;
- Index scoring: computing relevance for every query against all historical keys, with complexity O(N²·d);
- Memory access and scheduling: irregular index results lead to random gather operations, low cache hit rates, and operators that are hard to fuse.
The second part is the main cause; the third is an amplifier. Common engineering techniques for “optimizing the indexer”—quantized scoring, block-wise approximation, cache reuse—mostly work on constant factors and cannot change the order of magnitude. Solving the problem at its root requires reducing the dimension or length at which indexing operates, not tuning the scoring formula.
3. The Core Idea: Move Selection into Latent Space
The key insight of Latent Sparse Attention is that selecting tokens does not have to happen over the original token representations.
The approach can be abstracted into three steps:
- Compress: project the key/value of a long sequence into a low-dimensional latent space, forming a small number of “latent state” representations, reducing the length from N to M (M ≪ N);
- Retrieve: the query performs similarity scoring and Top-K selection in latent space, reducing the cost to O(N·M) or lower;
- Restore: apply exact attention only to the small number of selected original tokens, keeping the output isomorphic to the original model.
Intuitively, this is like first building a “low-resolution map” of the entire history, locating the general region on the map, and then going back to the original text to read closely. The map is small, so locating is cheap; the close-reading range is small, so computation is cheap.
Illustrative pseudocode below—the point is the structure, not the specific interfaces:
# 1) Precompute stage: build the latent-space index (can be done offline together with model weights)latent = compress(kv_hidden) # [N, d] -> [M, d_latent], M << Nindex = build_index(latent) # low-dimensional, cacheable, reusable# 2) Prefill stage: select in latent space first, then compute over the original tokensscores = query @ index.T # [B, N] but with a very low dimensiontopk = top_k(scores, k=K) # keep only K candidates per queryout = exact_attention(q, k[topk], v[topk]) # compute only the selected part
Three engineering details here are easy to overlook:
- The construction of latent space must be consistent with the training objective; otherwise the “map” and the “original text” won’t align, and selection quality will collapse;
- The value of M is the core knob balancing accuracy and speed: too small misses key tokens, too large degrades back to the original problem;
- Irregular memory access during the gather stage needs chunking and operator fusion as a fallback, otherwise the theoretical complexity reduction will be consumed by actual throughput.
4. Gains and Costs: Where the Order-of-Magnitude Improvement Comes From
At the 10-million-token scale, this class of methods can deliver tens of times speedup on prefill for a single global attention layer, with clear differences across model sizes and architectures. Smaller models often benefit more, because their non-attention overhead accounts for a higher share to begin with. Meanwhile, across a range of long-context tasks, accuracy stays close to the original model.
The sources of the gains can be attributed precisely:
- The index dimension drops from d to d_latent, and scoring cost decreases nearly linearly;
- The index length drops from N to M, sharply shrinking the Top-K search space;
- Exact computation still covers only K tokens, so accuracy is not sacrificed.
The costs are equally clear: extra compression and index-construction overhead, extra memory usage, and the need for the model to learn “how to select in latent space” during training or fine-tuning. It is therefore better suited to prefill-heavy scenarios with extremely long contexts and reusable indexes, such as long-document understanding, code-base-level retrieval, and long-conversation memory.
5. Implementation Advice: How to Evaluate and Integrate It in Your Own System
If you are making architecture choices for ultra-long contexts, proceed in this order:
- Quantify the bottleneck first: separately measure the time share of attention computation, index scoring, and memory-access scheduling to confirm whether indexing is really the main cause;
- Validate accuracy at small scale: run ablations over an M/K grid to find the accuracy inflection point rather than blindly taking extremes;
- Reuse the index: cache the latent-space index together with the weights to avoid rebuilding it on every request;
- Layer and mix: use sparse retrieval for global layers and keep linear or sliding-window attention for local layers, balancing long-range and local detail;
- Add observability: use monitoring, alerting, and logging services to track prefill latency, index hit rate, and memory usage, and leave capacity planning to container platforms and elastic scaling.
For cloud deployment, you can decouple model serving, vector retrieval, object storage, and message queues, using general-purpose capabilities to handle index caching and batch scheduling, avoiding coupling to any specific vendor’s interfaces.
6. Conclusion
The core tension of 10-million-token contexts has shifted from “can’t compute” to “can’t find.” The value of Latent Sparse Attention lies in moving the selection process into a low-dimensional latent space, bringing indexing overhead back from quadratic to near-linear, and thereby speeding up single-layer prefill by tens of times while preserving accuracy. For developers, the real action item is not chasing a particular model, but first quantifying the index share in your own system and then deciding whether to retrieve in latent space—this determines whether your long-context solution goes from “it runs” to “it runs cost-effectively.”
