Millions of Tokens of Context: How Latent Sparse Attention Speeds Up Prefill

    As context windows scale from millions to tens of millions of tokens, the bottleneck in long-context inference is shifting from computation to retrieval. This article explains why the selection stage of sparse attention still scales quadratically, introduces Latent Sparse Attention—which moves candidate selection into a low-dimensional latent space—and reviews reported prefill speedups, engineering tradeoffs, and implications for cloud platforms serving long-context workloads.

    Long-context capabilities have advanced faster than expected over the past two years. Million-token context has moved from an experimental feature to a default configuration in some cloud services, and hybrid attention architectures are now widely used to balance accuracy and cost. These designs share a common premise: each query vector no longer scans every historical token but selects only a few of the most relevant ones for computation.

    At the million-token scale, this premise has repeatedly proven sound. But as contexts push toward tens of millions of tokens, an overhead that once seemed negligible starts to spiral. Take a global attention layer in a hybrid architecture model: when running prefill at the ten-million-token scale, most of the time is not spent on the attention computation itself, but on the indexing step that decides which tokens to look at.

    This marks a change in the nature of the problem:

    • Sparse attention has shown that attending to a small subset is enough to preserve accuracy.
    • What actually stalls ultra-long contexts is that the cost of finding that small subset still grows quadratically with context length.

    In other words, computational complexity has been successfully reduced, but retrieval complexity has not fallen in step, and it has become the new weak link in the chain.

    2. Why Indexing Becomes a Quadratic Bottleneck

    To see why, it helps to split attention into two stages.

    The first stage is selection: for each query position, score how well it matches every historical position to produce a candidate set. The second stage is aggregation: compute a weighted sum over the candidate set only.

    Sparse methods optimize the second stage by shrinking the number of tokens involved in aggregation. But the first stage usually still scores and filters over the full historical sequence. Double the sequence length and the per-layer candidate evaluation doubles as well; once layers, attention heads, and token counts stack up, the total overhead quickly approaches or even exceeds the computation that was saved.

    Schematically, the logical structure looks roughly like this:

    1. # Traditional sparse attention: cheap aggregation, expensive selection
    2. scores = query @ keys.T # shape [L, L], grows quadratically with length
    3. topk_idx = top_k(scores, k) # selection still runs over the full sequence
    4. out = weighted_sum(values, topk_idx)

    Here L is the sequence length. What really needs to be compressed is the cost of producing the [L, L] intermediate structure, not the value of k.

    3. The Core Idea of Latent Sparse Attention

    A recent paper from a startup research team in collaboration with universities proposes a direction called Latent Sparse Attention. Its core is not to shrink k further, but to move the entire selection process into a low-dimensional latent space.

    It can be summarized in three points:

    1. Compressed representation: project the key vectors of historical tokens into a latent space of significantly lower dimension, forming a compact sequence of latent representations so that relevance retrieval happens in a space far smaller than the original dimension.
    2. Latent-space retrieval: the query is projected into the same latent space and dotted with the latent representations to filter candidates, avoiding the construction of a full score matrix over the original high-dimensional, full-length sequence.
    3. Selective restoration: attention aggregation is performed in the original representation only for the few selected positions, keeping output accuracy close to the original model.

    The source of the gains is straightforward: the cost of retrieval drops from “sequence length × key dimension” to “sequence length × latent dimension,” and the latent dimension is typically much smaller than the key dimension. Since the selection stage originally consumed most of the time in a single layer’s prefill, the speedup from this substitution can far exceed intuition.

    The reported results are: at the ten-million-token scale, a hybrid architecture model achieved roughly a 22.7x speedup in single-layer attention prefill, while a general-purpose large model saw about a 72.4x speedup at the same layer, with accuracy on multiple tasks remaining close to the original models. The difference in speedup essentially reflects how much of the selection stage each model spends time on—the heavier the selection overhead, the more pronounced the benefit.

    4. Four Things to Watch When Putting It into Production

    Latent projection is not free, so the following aspects are worth evaluating closely before deployment.

    • Projection quality: too low a latent dimension loses discriminative power and degrades candidate quality. A small-scale sweep tailored to the task type is usually needed rather than taking an empirical value outright.
    • Training and inference consistency: if a model was not trained for latent-space retrieval, bolting it on often yields limited gains; the more reliable path is to introduce the structure during pretraining or post-training.
    • Memory and bandwidth: latent representations require extra caching, but this is still an order-of-magnitude saving compared with the original key-value cache, which is especially friendly to long-context serving.
    • Compatibility with cache reuse: common techniques such as prefix caching and chunked prefill are orthogonal to this approach and can be combined with it.

    5. Implications for Platform-Side Practice

    For teams that need to offer long-context capabilities in the cloud, the lesson here is architectural: the optimization focus should shift from “reducing per-token compute” to “reducing candidate retrieval.” In concrete engineering terms, three general capabilities are worth attention—elastic container platforms to host inference workloads of variable length, object storage to hold long-session and knowledge-base snapshots, and monitoring, alerting, and logging services to observe how time is distributed across prefill stages and pinpoint the real bottleneck.

    In such ultra-long-context scenarios, Baidu AI Cloud likewise emphasizes locating overhead through tiered caching and staged profiling rather than simply piling on compute.

    6. Summary

    For ten-million-token contexts, the central tension has moved from “can’t finish computing” to “can’t search fast enough.” The value of Latent Sparse Attention lies in moving the selection stage from the original high-dimensional sequence into a low-dimensional latent space, decoupling retrieval cost from key dimension and delivering tens-of-times speedups in single-layer prefill while preserving accuracy. For engineering teams, the takeaway is that the next round of long-context optimization lies in the indexing and retrieval path.