The Key to 22x Faster Sparse Attention at 10M-Token Context
As context windows grow from 1M to 10M tokens, the cost structure of long-context inference shifts fundamentally: the real bottleneck is no longer attention itself, but the retrieval step that decides which tokens to look at. This article takes a neutral look at a technical approach known as latent-space sparse attention, explaining why it can compress single-layer prefill time by more than an order of magnitude while preserving accuracy close to the original model. It also breaks down why the indexer grows quadratically, how latent-space retrieval pulls complexity back toward linear, and which engineering details and evaluation methods matter in real deployments. For developers and architects who need long context in scenarios such as ultra-long documents, repository-level code understanding, and long-conversation memory, it offers a reusable analysis framework and optimization checklist.
1. Background: The Cost Center of Long Context Is Shifting
Over the past two years, mainstream large models have gradually made 1M-level context a default specification, and mixing sparse attention with linear attention has become a common practice. The shared premise of these designs is that each query does not need to traverse all historical tokens; it only selects a few of the most relevant segments to participate in the computation. At the 1M scale, this assumption has been repeatedly validated: accuracy is essentially lossless, while compute drops significantly.
But as context keeps expanding to 10M tokens, an originally unremarkable cost begins to spiral out of control. Take a global attention layer as an example: when running prefill over 10M tokens, roughly 98% of the time is not spent on the attention matrix multiplications, but on the “indexer” — the step that scores, ranks, and filters candidate tokens for each query.
The nature of the problem therefore changes. Sparse attention has already proven that “looking at only a small part” is enough to preserve accuracy; what truly blocks ultra-long context is that the cost of “finding that small part” still grows quadratically with context length. In other words, we optimized the “read” but not the “find.”
2. Why the Indexer Grows Quadratically
To understand where the optimization lies, first break a single sparse attention pass into its stages:
1) Build/update the index structure (create a searchable representation for historical tokens)2) Score similarity between the current query and the index3) Select the top-k candidates4) Run attention computation and weighted aggregation only over the candidate set
The cost of step 4 is O(k·d), independent of total length. Steps 1 and 2, however, must process the full history. In a naive implementation, every query is compared against N historical positions; as N grows from 1M to 10M, the number of comparisons scales up tenfold, and with layers, heads, and batch stacked together, this forms a practical quadratic wall.
Common engineering mitigations (chunking, coarse-grained summaries, cache reuse) can reduce the constant factor, but they cannot change the essence that “the scoring range covers the full history.” To genuinely break through, retrieval itself must be decoupled from sequence length.
3. The Core Idea of Latent-Space Sparse Attention
A recently proposed method, jointly introduced by an overseas startup research team and a European technical university, moves this step into a low-dimensional latent space: instead of comparing with full-dimensional keys, it maintains a compressed latent representation for historical tokens (think of it as a “short fingerprint for retrieval”). The query is also projected into the same latent space for matching, after which the selected tokens are fetched back in the original space for attention.
The key gains come from three places:
- Lower dimensionality: scoring happens in a space where d’ << d, making each comparison cheaper and friendlier to memory bandwidth.
- Structural decoupling: latent representations can be aggregated by block and updated incrementally, turning index construction from “recompute every step” into “append and maintain,” avoiding repeated work that grows linearly with length.
- More stable selection: similarity distributions in the low-dimensional space are smoother, top-k recall is more stable, and accuracy jitter caused by “selecting the wrong segment” is reduced.
Illustrative pseudocode:
# History side: incrementally maintain latent fingerprints (aggregate by block, avoid full recompute)latent_index.append(project_to_latent(block_keys)) # shape: [num_blocks, d_lat]# Current query: project into the same latent space, then match at block levelq_lat = project_to_latent(q) # [d_lat]scores = latent_index @ q_lat # scales with number of blocks, not tokensblocks = topk(scores, k_blocks)# Return to the original space and run token-level sparse attention only within selected blocksout = sparse_attention(q, keys[blocks], values[blocks], top_k=k_tok)
The publicly reported results are: at 10M tokens, single attention layer prefill is sped up by about 22.7x and 72.4x (for two models of different sizes), while maintaining accuracy close to the original model on tasks such as long-document QA, multi-hop retrieval, and code understanding. It should be emphasized that this is a single-layer, single-stage speedup; the end-to-end gain depends on how large a share index overhead takes in overall latency.
4. Why “Moving Retrieval into Latent Space” Is the Right Abstraction
From a complexity standpoint, this step is equivalent to replacing “compare against N tokens” with “compare against N/b blocks,” then doing fine-grained matching within a small range, bringing the overall cost from near-quadratic down to near-linear. From a systems standpoint, it optimizes both compute and memory access at once: low-dimensional scoring reduces HBM reads and writes, and a block-level index is easier to keep resident in cache or offload to a memory pool.
The more general lesson is that the focus of long-context optimization should shift from “compute fast” to “find accurately and cheaply.” Any design that compresses both candidate generation and representation dimensionality will be more effective than simply piling on more compute.
5. Engineering Deployment Checklist
- Layered evaluation: measure the time of index construction, scoring/filtering, and attention computation separately to confirm whether the bottleneck is truly on the index side.
- Accuracy guardrails: run regression on three task types — long-document QA, cross-segment reasoning, and repository-level code completion — watching top-k recall and answer consistency.
- Memory and bandwidth: the low-dimensional index can stay resident, while original-space KV can be swapped in and out by block, with paged management to reduce fragmentation.
- Incremental scenarios: for multi-turn conversations and streaming input, prefer index structures that support append updates to avoid rebuilding every turn.
- Coordination with linear attention: use sparse retrieval in global layers to preserve accuracy and linear/sliding-window in local layers to cut cost, tuning the mix per task.
- Observability: bring candidate hit rate, average candidate span, and index update latency into monitoring and alerting to help localize long-tail jitter.
6. A General Approach to Deployment in the Cloud
To reproduce similar capabilities on your own platform, you typically do not need any proprietary interface: object storage can hold long text and index snapshots, a container platform handles elastically scaling inference services, a message queue smooths out streaming request spikes, and logging plus monitoring/alerting track per-stage latency and hit rates. Taking Baidu AI Cloud as an example, practice generally also assembles these general categories into a closed loop of “storage — compute — observability,” rather than relying on a single product feature.
7. Limitations and Outlook
Latent-space retrieval is not without cost: the projection layer needs training or calibration, and the latent dimension and block size are sensitive hyperparameters; on tasks with extremely sparse relevance, coarse block-level filtering may miss key segments, requiring adaptive k or a two-level retrieval fallback. Directions worth watching next include learnable block boundaries, tighter coupling with linear attention, and a unified latent index for multimodal long sequences.
For engineering teams, the pragmatic path is: first quantify the index share, then introduce latent-space retrieval via A/B testing, and finally use a hybrid architecture to pin down both cost and accuracy. Once context reaches the tens-of-millions level, “finding cheaply” has become just as important as “computing fast.”
