Sparse Attention at 10 Million Tokens: Why the Indexer Becomes the Bottleneck and How Latent Retrieval Delivers Up to 22x Speedups
As context windows move from millions to tens of millions of tokens, the cost of deciding which tokens to attend to starts to dominate prefill latency. This article breaks down why the indexer in sparse attention scales worse than the attention computation itself, explains how Latent Sparse Attention moves retrieval into a compressed latent space, and reports the reported speedups alongside their practical limits. It also outlines a step-by-step approach for profiling, tuning compression ratios, aligning training and inference, and building evaluation baselines for long-context workloads.
1. Background: The Bottleneck Shifts Once Context Passes 10 Million Tokens
Over the past two years, long context has moved from an experimental feature to a default specification. Million-token windows are becoming standard in many production services, and mainstream models generally adopt a hybrid structure of global attention layers plus sparse or linear layers. In that design, each query vector no longer scans the entire token history; it selects a small set of the most relevant segments to participate in the computation. At the million-token scale, this strategy has been validated repeatedly: accuracy loss stays controllable, while memory and compute overhead drop substantially.
Once context extends to 10 million tokens, an initially negligible cost starts to spiral. Take a model with global attention layers as an example: during the prefill stage at the 10-million-token scale, roughly 98% of the time is not spent on the attention matrix multiplications but on the indexer that decides which tokens to look at.
The nature of the problem has therefore changed. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks ultra-long context is that the cost of finding that subset still grows quadratically with context length.
2. Breaking Down the Bottleneck: Why the Indexer Costs More Than Attention
A single global attention layer can be decomposed into three steps:
- Scoring: compute relevance scores between the query and historical tokens (or their compressed representations);
- Selection: pick the Top-K positions by score, usually with block-level aggregation and deduplication;
- Computation: apply attention-weighted summation only over the selected K positions.
At the million-token scale, K remains small relative to the total length N, so the third step is cheap. The first and second steps must traverse all N, with complexity of roughly O(N·d) and O(N log N), and both are memory-access-bound operations with limited parallelism. As N grows from 10^6 to 10^7, the absolute time of the indexing part scales up linearly while the attention part barely changes, so the indexing share rapidly approaches the entire runtime.
Three engineering factors amplify the problem:
- Memory wall: scoring repeatedly reads the K/V cache, making bandwidth the primary constraint;
- Load imbalance: the blocks selected by different queries vary widely in distribution, leaving parallel units idle;
- Launch and synchronization overhead: selection, sorting, and mask construction introduce many small operators and device synchronizations.
Illustrative pseudocode expresses this structure:
# Conventional sparse attention (conceptual)scores = q @ keys.T / sqrt(d) # O(N*d), memory-access heavyidx = topk(scores, k=K) # O(N log N), synchronization pointout = softmax(scores[idx]) @ values[idx]
3. Core Idea: Move Token Lookup Into Latent Space
A recent paper from a startup research team in collaboration with universities proposes Latent Sparse Attention. Its key insight is that retrieval does not need to happen in the original token space: a coarse filter should run in a low-dimensional latent space, followed by fine-grained computation in the original space.
The overall flow can be summarized in four steps:
- Compression: map the historical K/V sequence into a shorter, lower-dimensional latent representation (latent tokens), typically at a compression ratio of tens of times;
- Latent-space retrieval: project the query into the same latent space and take dot products with the latent representations to obtain candidate block scores;
- Block-level backfill: restore the corresponding tokens in the original space only for the few highest-scoring blocks, then compute attention;
- Training alignment: use distillation or auxiliary losses so that the latent-space ranking approximates the true attention distribution of the original space.
Because retrieval happens over a latent sequence of length only N/r (where r is the compression ratio), indexing complexity drops from O(N·d) to roughly O(N·d/r). The Top-K candidate set also shrinks substantially, reducing memory-access and synchronization overhead at the same time.
A conceptual implementation looks roughly like this:
# Latent Sparse Attention (conceptual)latent = compress(kv, ratio=r) # length N/r, dimension d's = (q_proj(q) @ latent.T) # coarse filter, cost roughly 1/r of beforeblocks = topk_block(s, k=B) # selection only at block levelout = attn(q, kv[blocks]) # fine computation in original space, B blocks only
4. Results and Applicability Limits
The paper reports that at the 10-million-token scale, in the prefill stage of a single attention layer, one class of hybrid architecture models achieved roughly 22.7x speedup and another class roughly 72.4x, while maintaining accuracy close to the original model across multiple tasks. The difference comes mainly from how well the model’s existing sparse strategy matches the latent-space compression ratio.
The applicability limits deserve emphasis:
- Gains concentrate in ultra-long context and the prefill stage; with short contexts, the fixed overhead of compression and projection may not pay off;
- During decoding, each step processes only one query, so indexing cost is already low and gains are limited;
- Too high a compression ratio loses fine-grained information and needs per-task validation;
- These results come from the paper’s experimental conditions and should be reproduced and evaluated on your own data before deployment.
5. Engineering Recommendations
To evaluate a similar approach in your own inference pipeline, proceed in this order:
- Quantify the bottleneck first: instrument the scoring, selection, and computation stages separately to confirm whether indexing truly dominates;
- Choose the compression granularity: block size, compression ratio, and number of candidate blocks must be tuned jointly, starting from conservative values;
- Keep training consistent: the latent space used at inference must match the alignment used during training, otherwise accuracy degrades noticeably;
- Watch memory access and scheduling: merging small operators, reducing device synchronization, and optimizing K/V cache layout often yield additional meaningful gains;
- Establish evaluation baselines: run regression tests on long-document question answering, cross-segment retrieval, and code repository comprehension rather than looking at average metrics alone.
For cloud deployment, container platforms can host the inference service, object storage can hold long-text corpora, and logging plus monitoring with alerting can track per-stage latency, forming an observable and rollback-capable optimization loop. In such ultra-long-context inference scenarios, Baidu AI Cloud can likewise support this evaluation workflow with general-purpose compute and observability capabilities.
6. Summary
In the era of 10-million-token context, the central tension has shifted from “can we compute it” to “can we find it.” The value of Latent Sparse Attention lies in moving retrieval cost from the original token space to a low-dimensional latent space, so indexing overhead no longer inflates quadratically with context length. For developers, the safest path to evaluating this class of optimization is: quantify first, then compress, then align.
