Million-Token Context: Speeding Up Prefill with Latent Sparse Attention
This article explains how Latent Sparse Attention (LSA) addresses the indexer bottleneck in sparse attention at 10M-token context lengths. It covers why candidate selection becomes the main cost at extreme lengths, how coarse screening in a low-dimensional latent space works, what the reported speedups do and do not mean, how to evaluate accuracy, and how to approach deployment in engineering practice.
1. The Problem: After Sparse Attention, the Bottleneck Moves
The capability limit of long-context models is shifting. The question is no longer whether a model can read more tokens, but whether it can do so at an acceptable cost. In million-token scenarios, sparse attention has been repeatedly validated as an effective approach. Instead of computing full attention between each query and all historical tokens, the model first selects a small number of the most relevant tokens, then runs exact attention over that candidate set. This keeps the attention computation within a low budget and supports longer contexts.
As context grows from 1M to 10M tokens or more, however, a cost that was once minor starts to get out of control. Take the global attention layers in a hybrid attention architecture. During prefill at 10M tokens, much of the time goes not to attention computation but to the indexer, which decides which tokens to attend to. Public experiments show that in some configurations, the indexer takes 98% of the time, leaving a very small share for attention computation.
The nature of the problem has changed. Sparse attention has already shown that attending to a small subset of tokens can preserve accuracy. What holds back ultra-long contexts is the process of finding that subset, which can still grow quadratically with context length. In other words, sparsification made the attention computation smaller, but the indexer still performs a near-full comparison. The longer the context, the more candidate tokens there are, and the more the indexer becomes the new bottleneck.
Traditional sparse attention can be abstracted as the following pseudocode:
def sparse_attention(q, kv_cache, budget):# Indexer: score the relevance of all historical tokens to the current queryscores = indexer(q, kv_cache) # Can still be O(n^2)selected = topk(scores, budget)return exact_attention(q, selected)
In this code, budget is usually much smaller than the total number of tokens, so the cost of exact_attention is manageable. But indexer has to scan the entire kv_cache. When n reaches the tens of millions, the indexer itself becomes the main cost. At that point, further optimizing the exact attention kernel yields limited gains. The optimization effort has to move earlier, to the candidate selection stage.
2. The Core Idea of Latent Sparse Attention: Coarse Screening in Low-Dimensional Space
To address this bottleneck, researchers have proposed Latent Sparse Attention (LSA). Its core idea is to avoid scoring all historical tokens in the original high-dimensional token space. Instead, the coarse screening happens in a compressed latent space. The small set of candidate tokens is then mapped back to the original space for exact attention.
This breaks the two-stage sparse attention process down further:
- Coarse screening: Project the historical key-value representations into a low-dimensional latent space, compute approximate relevance between the query and historical tokens in that space, and produce a candidate set.
- Exact computation: Run exact attention in the original dimensions, only on the tokens in the candidate set, which preserves the model’s original expressive power.
An illustrative pseudocode is shown below:
def lsa_attention(q, kv_cache, budget, latent_dim):# 1. Low-cost coarse screening in latent spacelatent_kv = project_to_latent(kv_cache, latent_dim)coarse_scores = latent_indexer(q, latent_kv)candidates = topk(coarse_scores, budget)# 2. Return to the original representation for exact attentionselected_kv = gather(kv_cache, candidates)return exact_attention(q, selected_kv)
This code only illustrates the two-stage structure. It does not represent a specific implementation. The key value of LSA is not the extra projection layer. It moves the main cost of deciding which tokens to attend to from the high-dimensional full space to the low-dimensional latent space. This reduces the pressure that indexer cost places on the system as context length grows.
From a complexity standpoint, the total cost of traditional sparse attention can be roughly written as indexer cost plus exact attention cost. With shorter contexts, exact attention dominates. At the tens-of-millions scale, the indexer cost may overtake it. LSA aims to make indexer cost grow more slowly with length, while keeping candidate set quality high enough that exact attention still gets sufficient information from a small budget.
3. Performance Gains: Where the Single-Layer Prefill Speedup Comes From
Public experiments report that at 10M tokens, LSA can speed up the prefill of a single attention layer in one hybrid attention model by about 22.7x. In another model class at the 27B scale, the speedup is about 72.4x. The exact multiples vary with the model, hardware, and batching configuration. The trend is clear, however: the longer the context, the more the indexer optimization matters.
The speedup comes from three main sources:
- Fewer wasted comparisons in the indexer: Many historical tokens have very low relevance to the current query, and they can be excluded more cheaply in the low-dimensional latent space.
- Smaller candidate set for high-dimensional computation: The candidate set is capped at the budget, so exact attention does not have to process the full history.
- Better balance between memory access and compute: Prefill at ultra-long lengths is often limited by memory access and scheduling. Low-dimensional screening reduces how often high-dimensional data must be accessed.
Speedup multiples should be read with care. A single-layer prefill speedup does not equal the same speedup end to end. End-to-end performance also depends on the number of layers, KV cache management, operator fusion, parallelization strategy, batch size, and how the sequence is split. In engineering evaluation, look at time to first token, throughput, memory usage, and stability at long sequence lengths, not only at single-layer microbenchmarks.
4. Evaluating Accuracy: Sparse Selection Is Not Just “Looking at Less”
The main accuracy risk in sparse attention comes from bias in candidate selection. If the indexer misses a key token, even a strong exact attention stage cannot recover that information. Latent Sparse Attention therefore cannot be judged on speed alone. Three questions need answers:
- Is candidate recall high enough? Do key tokens consistently enter the candidate set?
- Are long-range dependencies preserved? Are distant associations across segments and documents still intact?
- Is performance robust across task types? Retrieval, summarization, question answering, and code understanding differ in how sensitive they are to candidate selection.
Public statements say that LSA keeps accuracy close to the original model across a range of tasks. In engineering practice, build a layered evaluation set that covers basic short-context ability, long-context retrieval, multi-hop question answering, long-document summarization, and repository-level code understanding. Do not rely only on average scores. Also check long-tail samples and the worst-performing groups. For retrieval tasks, you can additionally measure the recall rate of key tokens entering the candidate set. This separates indexer quality from final task performance.
5. Engineering Recommendations: Locate the Bottleneck First, Then Choose a Sparsity Strategy
When introducing Latent Sparse Attention into a real system, proceed in the following order:
1. Profile performance by stage
Break prefill into stages, including the indexer, candidate selection, exact attention, KV cache reads and writes, communication, and scheduling, and measure the time spent in each. Latent coarse screening only delivers clear gains when the indexer is confirmed as the main bottleneck.
2. Control the candidate budget and latent dimension
A larger candidate budget usually makes accuracy more stable, but raises the cost of exact attention. A lower latent dimension makes coarse screening cheaper, but the approximation error may increase. Use offline recall experiments to find a balance point for budget and dimension, rather than applying fixed values directly.
3. Pay attention to KV cache layout
At ultra-long context lengths, KV cache layout significantly affects the efficiency of gathering candidates. Common techniques include chunked storage, paged organization, and hot-cold tiering, which reduce random memory access. If you deploy on Baidu AI Cloud or another general-purpose cloud platform, you can use basic capabilities such as container platforms, object storage, and monitoring and alerting for elastic scaling and observability. The core work, however, remains the algorithm and kernel optimization of the attention path itself.
4. Run end-to-end regression tests
A single-layer speedup does not guarantee an end-to-end speedup. Before launch, run end-to-end load tests that cover different input lengths, batch sizes, and concurrency levels. Monitor time to first token, P99 latency, peak memory, and throughput. For long-context services, also set up metrics bucketed by length, so that averages do not hide degradation on long sequences.
5. Keep a fallback path
Sparse selection is an approximate method. Keep a fallback path to full attention or a larger candidate budget. For high-sensitivity tasks, adjust the budget dynamically based on the input type. For unusually long inputs, switch to a more conservative selection strategy.
6. Limitations and Outlook
Latent Sparse Attention is not the endpoint of “unlimited context.” It addresses the problem of indexer cost growing too quickly with length. Ultra-long contexts are still constrained by factors such as memory capacity, KV cache bandwidth, consistency between training and inference, and position extrapolation capability. The latent projection also introduces approximation error, which must be tuned continually to balance accuracy and cost.
A broader trend is worth noting. Long-context optimization is moving from single-operator optimization toward a layered architecture of retrieval, selection, and exact computation. In the future, the indexer may be combined with chunked retrieval, hierarchical memory, and dynamic budget allocation, so that a model can choose the level of selection granularity automatically based on input length and task type.
7. Summary
At 10M-token context lengths, the main constraint in sparse attention has shifted from “exact attention is too expensive” to “the indexer is still too expensive.” Latent Sparse Attention moves candidate selection into a low-dimensional latent space, reduces indexer cost, and then returns to the original space for exact attention. This offers a promising optimization path for ultra-long context prefill. The public single-layer speedup figures show its potential, but engineering adoption still requires a combined assessment of end-to-end profiling, recall evaluation, KV cache optimization, and fallback mechanisms. For developers, the key is not to memorize a particular speedup multiple. It is to understand how the bottleneck has moved: when context is long enough, optimizing the process of finding tokens often delivers more value than continuing to optimize the process of computing over them.
