Sparse Attention at 10M Token Context: How Latent-Space Indexing Delivers 22x Prefill Speedup
As context windows scale from 1M toward 10M tokens, the cost structure of long-context inference changes fundamentally. Sparse attention limits each query to a few historical tokens, but the indexing step that decides which tokens to attend to still grows quadratically with sequence length. This article examines that bottleneck, citing a study on ultra-long context in which roughly 98% of prefill time for a global attention layer at 10M tokens was spent in the indexer rather than in the attention computation itself. It then introduces Latent Sparse Attention, an approach that moves token selection into a low-dimensional latent space to reduce indexing complexity, reporting more than 22x speedup for single-layer prefill at 10M tokens, higher ratios under some model configurations, and accuracy close to the original model. Complexity comparisons, applicability boundaries, engineering integration guidance, and evaluation methods are included for architects and inference engine developers.
When the context length grows from 1M to 10M, the cost structure of long-context inference changes fundamentally. A sparse attention scheme that is widely used in the industry allows each query to attend to only a small number of historical tokens, but the indexing step that “decides which tokens to attend to” still grows quadratically with length. In a study targeting ultra-long context, a global attention layer performing prefill at 10M tokens spent about 98% of its time in the indexer rather than in the attention computation itself. This article abstracts that problem and introduces a new idea called Latent Sparse Attention: moving the selection process into a low-dimensional latent space, which significantly lowers indexing complexity. In measured results, single-layer prefill at 10M tokens achieved more than 22x speedup, with higher speedup ratios under some model configurations, while accuracy remained close to the original model. The article further provides a complexity comparison, applicability boundaries, key engineering integration points, and evaluation methods for architects and inference engine developers.
1. Background: Longer Context Rewrites the Cost Structure
Over the past two years, mainstream large models have gradually made 1M context a default specification and then explored even longer ranges. One of the key technologies supporting this trend is allowing each query to stop scanning all historical tokens and instead select only a few of the most relevant segments to participate in the computation. This is the basic idea of sparse attention. It has been repeatedly validated at the million-token scale: accuracy loss is controllable and throughput improves significantly.
But as the context advances to the tens-of-millions scale, an overhead that once seemed negligible starts to spiral out of control. Take a certain global attention layer as an example: when doing prefill at 10M tokens, most of the time is not spent on the attention matrix multiplications, but on the indexer that decides “which tokens to look at.” In other words, sparse attention solved the problem of “computing too much” but did not solve the problem of “finding too slowly.”
2. Root Cause: The Indexer’s Quadratic Growth
To understand where the speedup comes from, you first need to see clearly where the indexer’s complexity originates. A common approach can be abstracted as the following pseudocode:
# Pseudocode: indexing stage of traditional sparse attention# q: [B, H, L, D] k: [B, H, L, D]scores = einsum('bhld,bhmd->bhlm', q, k) # O(L^2 * D)topk_idx = topk(scores, k=64, dim=-1) # O(L^2)out = gather_and_attend(q, k, topk_idx) # only 64 neighbors are computed
The key issue is that even if only 64 neighbors are computed afterward, the preceding scoring and topk still have to traverse all L historical positions, giving a complexity of O(L^2·D). At L=10M, this term completely overwhelms the actual attention computation, forming an “indexing wall.”
3. Core Idea: Move the Selection into Latent Space
The core of Latent Sparse Attention is to move the task of “who to select” from the original token space into a low-dimensional latent space. Its main points can be summarized in three steps:
- Compression: project the key/value of the long sequence into a latent representation whose dimension is far smaller than D, reducing the number of multiply-adds in the scoring stage;
- Coarse filtering: perform similarity evaluation and topk selection within the latent space, reducing complexity from O(L^2·D) to close to O(L^2·d) with d≪D, and with blocking/sampling it can be pushed further toward linear;
- Fine computation: perform exact attention only on the small number of selected tokens in the original space to preserve accuracy.
A schematic implementation is as follows:
# Pseudocode: latent-space coarse filtering + original-space fine computationz = proj_latent(k) # [B,H,L,d], d << Ds = einsum('bhld,bhmd->bhlm', q_latent, z)idx = topk(s, k=64, dim=-1) # coarse filteringout = precise_attend(q, k, idx) # fine computation
The benefits are twofold: in the coarse filtering stage, the dimension of each multiply-add drops substantially; and the number of tokens that go through fine computation stays constant and does not grow with L.
4. Gains and Boundaries: Where the 22x Speedup Comes From
At the 10M token scale, this scheme achieved about 22.7x speedup for the prefill of a single attention layer, and reached 72.4x on another type of model configuration, with accuracy close to the original model across a series of tasks. The reason the speedup ratio can exceed 20x is that what gets compressed away is precisely the indexing time that accounts for about 98% of the total.
The boundaries need to be emphasized: this kind of speedup is mainly reflected in the prefill stage and in ultra-long context; under short context, the indexing overhead is not large to begin with, so the gains are limited; the latent dimension d and the topk value k need to be tuned per task—too small loses accuracy, while too large weakens the gains.
5. Engineering Implementation Recommendations
- Layered strategy: enable latent-space coarse filtering only for global attention layers, and keep local layers as they are;
- Blocking and caching: precompute and reuse the latent representations in blocks to amortize the projection cost;
- Accuracy guardrails: before going live, run regression tests with long-text retrieval, summarization, and multi-hop question answering;
- Observability: instrument indexing time and fine-computation time separately in log services and monitoring alerts to locate where the bottleneck has shifted.
6. Evaluation Framework
It is recommended to evaluate with a “three-layer metric” approach: first, end-to-end prefill latency and throughput; second, the proportion of time spent on single-layer indexing versus fine computation; third, task accuracy (retrieval, long-document summarization, code understanding). Only when all three meet the bar simultaneously does the speedup have production value. If an enterprise already has an inference pipeline running on a container platform with object storage, the instrumentation above can be plugged directly into the existing monitoring system to enable canary rollout and rollback.
In summary, in the era of tens-of-millions of tokens, the core conflict has shifted from “unable to finish computing” to “unable to find.” Moving the selection process into a low-dimensional latent space is currently the most cost-effective path to break through, and a key direction for the next evolution of inference engines.
