Sparse Attention at 10M Token Context: How Latent Indexing Delivers Up to 22x Prefill Speedup
As large language models scale from 1M to 10M token contexts, sparse attention solves the "compute too much" problem but leaves a new bottleneck: the indexer that decides which tokens to attend to. This article breaks down why indexing cost grows quadratically and can consume roughly 98% of prefill time, then explains how Latent Sparse Attention (LSA) moves relevance scoring into a low-dimensional latent space to cut both computation and memory traffic. It covers the core design, complexity analysis behind the reported ~22.7x speedup (and ~72.4x on another model), how LSA complements rather than replaces existing attention strategies, and a practical checklist for adopting the approach in production.
1. The Bottleneck Shifts as Context Grows from Millions to Tens of Millions
Over the past two years, mainstream large language models have gradually made 1M token context a default specification, and are now pushing rapidly toward 10M tokens. One of the key technologies enabling this trend is sparse attention: instead of scanning every historical token, each query selects only a small set of the most relevant tokens for computation. Practice has repeatedly shown that “looking at only a small fraction” is enough to preserve accuracy on most tasks.
But when context length scales up by another order of magnitude, an originally negligible overhead starts to spiral out of control. Take a hybrid attention architecture (global attention layers plus linear attention layers) as an example: at 10M tokens, during the prefill phase, roughly 98% of the time is not spent on the numerical computation of attention, but on the indexer that “decides which tokens to look at.”
This means the nature of the problem has changed:
- Sparse attention solved the “compute too much” problem;
- But it did not solve the “find too slowly” problem;
- And the cost of “finding” still grows quadratically with context length.
Latent Sparse Attention (LSA), proposed by a research team, targets exactly this “find too slowly” stage with a structural optimization: at 10M tokens, a single attention layer’s prefill can achieve roughly 22.7x speedup (and about 72.4x on another model of a different scale), while maintaining accuracy close to the original model across a range of tasks. The following sections provide a neutral, generalized breakdown of its principles and engineering essentials.
2. Why Indexing Becomes the Main Cost: The Source of Quadratic Growth
To understand the value of LSA, first break down the cost structure of the prefill phase. A prefill pass through a global attention layer roughly involves three stages:
- Scoring/indexing: compute the relevance between each query and all historical tokens to obtain top-k candidates;
- Sparse attention computation: perform QK^T, softmax, and weighted summation only on the top-k candidates;
- Memory access and scheduling: move the KV of candidate tokens into on-chip cache and organize them into blocks that can be computed efficiently.
In a naive implementation, step 1 has complexity O(N^2·d) (N is context length, d is head dimension), and step 2 is O(N·k·d). When N grows from 1M to 10M, the N^2 term grows 100x, while k is usually constant or grows very slowly, so step 1 quickly becomes the dominant term. This is the root cause of “98% of time spent on the indexer.”
Looking further, the indexer is slow not only because of the floating-point operations, but more critically because of memory access:
- Scoring requires reading all of K, creating enormous bandwidth pressure;
- Top-k selection introduces irregular memory access and sorting overhead;
- Candidate gathering causes fragmented reads with low cache hit rates.
Therefore, optimizing the indexer must reduce both “computation volume” and “memory traffic” at the same time; otherwise, it merely shifts the bottleneck from compute to bandwidth.
3. The Core Idea of LSA: Moving the “Find” into a Low-Dimensional Latent Space
The key abstraction of LSA is: do not perform “relevance judgment” on the original high-dimensional token representations. Instead, first construct a low-dimensional, higher-information-density latent representation, then perform scoring and top-k selection in that latent space. The logic chain is as follows:
- Dimensionality reduction projection: use a lightweight linear mapping to compress K (and even Q) into an r-dimensional latent space (r much smaller than d). The projection requires only one matrix multiplication and can be fused with subsequent operators;
- Latent-space scoring: compute similarity in r dimensions, reducing complexity to O(N^2·r). Since r ≪ d, the computation volume drops significantly;
- Low-precision/blockwise selection: latent-space scoring can use lower precision (such as fp16/bf16 or even int8) and blockwise approximate top-k, reducing sorting and irregular memory access;
- Precise computation back in the original space: perform exact attention only on the selected top-k in the original d dimensions to guarantee accuracy.
Expressed in illustrative pseudocode (for meaning only, not a specific interface):
# Notation: N context length, d head dimension, r latent dimension, k number selected# 1) Precompute: project K into latent space (can be fused with KV cache writes)K_lat = proj_k(K) # [N, r]# 2) Blockwise approximate scoring + top-k (low precision, block-level reduction)scores = approx_score(Q_lat, K_lat) # [N, N] -> streamed in blocks, not materializedidx = topk_blockwise(scores, k) # irregular access is blocked# 3) Perform exact attention on candidates only in the original spaceK_sel = gather(K, idx) # [N, k, d]V_sel = gather(V, idx)out = sparse_attention(Q, K_sel, V_sel)
There are three engineering essentials here:
- Fused projection: the generation of K_lat is merged with KV cache writes to avoid extra reads and writes;
- Scoring without materialization: blockwise streaming computation plus block-level max/approximate selection avoids O(N^2) memory usage;
- Blocked selection: converging the irregularity of top-k to block granularity improves cache and parallelism efficiency.
4. Complexity and Gains: Why It Is an Order of Magnitude, Not a Percentage
Putting the costs of the three stages together (ignoring constants):
- Original: O(N^2·d) + O(N·k·d)
- LSA: O(N^2·r) + O(N·k·d) + O(N·d) (projection)
When r ≪ d and N is large, the dominant term drops from N^2·d to N^2·r. Combined with the memory-access gains from low precision and blocking, an order-of-magnitude end-to-end speedup is not surprising. The ~22.7x at 10M tokens (and ~72.4x on another model) reported in the original text falls right in this range: the speedup ratio increases as N grows, because the more the quadratic term is flattened, the more pronounced the gain.
It is worth emphasizing that LSA does not reduce the k of the final exact attention, nor does it change the model weight semantics. It optimizes “how to find top-k faster and more efficiently.” Therefore, accuracy close to the original model is a structural outcome, not tuning luck.
5. Differences from Existing Approaches: Not a Replacement, but a Complement
Common industry solutions each have their own focus:
- Sliding-window/local attention: simple to implement, but weak on long-range dependencies;
- Global + local hybrid: balances local detail and global recall, but the indexing cost of global layers grows as N^2;
- Linear attention/state space: reduces complexity to linear, but expression and recall are limited on some tasks;
- Block sparse/routing-based sparse: engineering-mature, but routing and block selection themselves still incur overhead.
LSA’s positioning is to “fill the gap in the global layer indexer”: it does not overturn the above approaches, but makes global sparse layers affordable again at tens of millions of tokens. It is especially friendly to hybrid architectures—linear layers handle cheap global information compression, global layers handle exact recall, and LSA keeps the “finding” of global layers from becoming a drag.
6. Engineering Implementation Checklist: From Prototype to Production
If you want to borrow the LSA idea in your own system, it is recommended to proceed according to the following checklist:
- First profile the cost structure: at target lengths (such as 1M/5M/10M), separately measure the proportions of scoring, attention, and memory access to confirm whether the indexer is truly the dominant term;
- Choose the latent dimension r: start ablation from 1/8 to 1/4 of d, and observe the inflection point of the accuracy-speed curve;
- Precision strategy: use bf16/fp16 for latent-space scoring, and int8 when necessary, combined with block-level reduction to control error;
- Fusion and streaming: fuse projection with KV writes; stream scoring in blocks, prohibiting materialization of N×N;
- Blocked top-k: perform approximate selection and gather at block granularity to reduce fragmented memory access;
- Coordination with KV cache: persist latent-space projection results with the cache to avoid recomputation;
- Monitoring and fallback: perform online monitoring of “recall hit rate” and “top-k overlap rate,” and fall back to exact indexing on anomalies.
Illustrative pseudo-switch (expresses strategy only, not bound to any interface):
if seq_len >= LSA_THRESHOLD and layer.type == "global":use_lsa_indexer = Truer = max(64, head_dim // 4)else:use_lsa_indexer = False
7. Practical Recommendations on Baidu AI Cloud
If you plan to implement such ultra-long-context inference optimization in a Baidu AI Cloud environment, you can combine general cloud product categories: use a container platform to host inference services and elastic scaling; use object storage to persist long-text corpora and intermediate artifacts; use a message queue for request peak shaving and task orchestration; use logging and monitoring/alerting services to build dashboards and threshold alerts for prefill latency, recall hit rate, and memory usage. The above are all general capability categories; specific selection should be based on actual business load testing.
8. Conclusion
The core contradiction of tens-of-millions-token context has shifted from “can’t compute” to “can’t find.” The value of LSA lies in using a low-dimensional latent space to free indexing and selection from the quadratic dominant term, allowing sparse attention to deliver its due cost-performance at ultra-long context. For developers, the real takeaway is not a specific number, but a reusable methodology: first locate the main cost, then reduce dimensions, lower precision, block, and fuse, and finally safeguard the accuracy baseline with monitoring and fallback.
