Sparse Attention at 10 Million Tokens: How Latent Sparse Attention Delivers Up to 22x Prefill Speedup

    As context windows scale from 1 million to 10 million tokens, the bottleneck in long-context inference shifts from computing attention to finding which tokens to attend to. This article examines why naive sparse attention indexing still grows quadratically, and introduces Latent Sparse Attention (LSA), a technique that moves token selection into a low-dimensional latent space. It covers the root cause of the indexing overhead, the three-step compress-retrieve-read-back method, illustrative pseudocode, reported single-layer prefill speedups of roughly 22.7x and 72.4x, practical deployment guidance, and the accuracy and length boundaries that determine when this approach is worth adopting.

    When the context length grows from 1 million tokens toward 10 million, the cost structure of long-context inference changes fundamentally. Sparse attention has already shown that having each query attend to only a small set of the most relevant historical tokens is enough to preserve accuracy. But at the 10-million-token scale, the real drag on the system is no longer the attention computation itself; it is the indexing step that decides which tokens to look at. This article abstracts that problem from a neutral perspective and introduces a class of techniques known as Latent Sparse Attention (LSA): move the selection process into a low-dimensional latent space, so that indexing overhead drops from growing quadratically with length to growing roughly linearly. The sections below break down the cause of the problem, the method’s principles, an illustrative implementation, how to put it into production, accuracy validation, and its applicable boundaries, so you can make a more informed technology choice for ultra-long context scenarios.

    1. Problem Restatement: At 10 Million Tokens, the Bottleneck Is “Finding,” Not “Computing”

    With million-token contexts now a mainstream configuration, long-context capability has gone from a differentiating feature to a baseline specification. The common way to support it is to shift attention from “reviewing everything” to “retrieving on demand”: each query no longer scans every historical token but selects a small set of the most relevant ones to participate in the computation. This idea has been validated repeatedly at the 1M scale, with controllable accuracy loss.

    But when the length grows by another order of magnitude, an originally unremarkable cost starts to spiral out of control. Take a global attention layer as an example: doing a prefill over 10 million tokens, most of the time is not spent on the attention matrix multiplications, but on the indexer, meaning the logic that scores, ranks, and filters candidate tokens.

    The reason is an asymmetry in complexity:

    • After sparsification, the attention computation itself depends only on the number of selected tokens and grows roughly independently of the total length.
    • If the indexing process uses the naive approach of comparing the query against every historical token one by one, its cost still grows quadratically with context length.

    So the nature of the problem has changed: sparse attention solved “looking at less” long ago, but “finding accurately” remains a quadratic burden. At the 10M level, the quadratic term completely dominates overall latency, and the attention computation becomes a rounding error.

    2. Method Principles: Moving “Selection” into the Latent Space

    The core claim of Latent Sparse Attention (LSA) is a single sentence: do not perform exhaustive comparison in the original token space; first compress the query and history into a low-dimensional latent space, then complete retrieval in that latent space.

    It can be understood in three steps:

    1. Compress: project the key vectors of historical tokens into low-dimensional latent representations, forming a set of “summary entries” far smaller than the original length.
    2. Retrieve: compute dot-product scores between the query’s low-dimensional latent vector and the summary entries, and select the top-k candidates.
    3. Read-back: perform attention computation in the original space only on the small number of selected tokens.

    The key benefit comes from the simultaneous reduction in dimension and scale in the second step: the scoring targets change from “tens of millions of original tokens” to “a small number of latent entries,” and the dimension of each dot product is also lower. An indexing cost originally on the order of O(n²·d) is compressed to a linear magnitude close to O(n·k·d’), where d’ is far smaller than d.

    It is worth emphasizing that this is not a stack of “approximate nearest neighbor” engineering tricks, but the introduction of learnability into the selection path: the compression and projection matrices can be trained or fine-tuned alongside the model, making similarity in the latent space better aligned with the language modeling objective, so that it “selects correctly” while “selecting less.”

    Illustrative pseudocode (structure only, not any specific API):

    1. # History side: build latent summaries offline/incrementally
    2. latent_history = project_to_latent(k_history) # [n, d'] n is the history length
    3. # Query side: low-dimensional retrieval
    4. q_latent = project_to_latent(q) # [d']
    5. scores = q_latent @ latent_history.T # [n] linear cost
    6. idx = topk(scores, k) # keep only k candidates
    7. # Read-back: attention in the original space
    8. attn = softmax(q @ k_history[idx].T) @ v_history[idx]

    3. Why It Can Deliver Speedups on the Order of 20x and 70x

    The two model classes mentioned in the source achieved roughly 22.7x and 72.4x single-layer prefill speedups respectively. The difference is not accidental but strongly correlated with each model’s own attention structure:

    • The higher the proportion of global attention layers and the more of them there are, the more clearly the indexing overhead is amortized, and the speedup typically lands in the tens-of-times range.
    • If a model uses a large amount of linear attention or local windows, its indexing burden is already small, leaving limited room for compression, and the speedup falls back.
    • The latent dimension d’ and the candidate count k are the main control knobs: the smaller d’ and k are, the faster the indexing, but the higher the accuracy risk.

    What is more worth attention in engineering is that this kind of optimization acts directly on the prefill stage. For scenarios that “read in a large amount first, then ask a few questions,” such as long-document summarization, repository-level code understanding, and multi-turn ultra-long conversations, prefill latency often determines time to first token, so the benefit is significantly amplified.

    4. Deployment Essentials: From Prototype to a Usable System

    1. Use a layered hybrid rather than a full replacement. Keeping a small number of global layers to handle long-range dependencies while using latent-space sparse retrieval for the rest is a common balance point between accuracy and speed.
    2. Maintain latent summaries incrementally. In streaming scenarios, history summaries should support appending and sliding eviction, avoiding a new quadratic cost from rebuilding every round.
    3. Leave redundancy in the candidate set. Beyond top-k, you can add a fixed local window of tokens and a guaranteed channel for “important tokens” to prevent key evidence from being missed.
    4. Coordinate with caching. Latent summaries can stay resident together with the key-value cache, using tiered storage to push cold data down to object storage while keeping hot data in memory or on local high-speed media.
    5. Prioritize observability. Only by bringing indexing time, candidate hit rate, and truncation ratio into logging services and monitoring alerts can you pinpoint “whether it is slow to compute or slow to find.”

    5. Accuracy Validation and Applicable Boundaries

    To evaluate this kind of solution, you cannot look at single-layer latency alone; you need to look at three curves together:

    • Quality curve: score changes on tasks such as long-document question answering, multi-hop retrieval, code retrieval, and long-text summarization.
    • Length curve: whether latency growth from 1M to 10M truly returns to linear.
    • Candidate curve: where the trade-off inflection point between quality and speed lies as k and d’ change.

    The boundaries are equally clear: when a task strongly depends on a “globally unique evidence point” and the evidence is extremely sparsely distributed, the information loss from compression is more sensitive; in this case you should increase k, keep global layers, or switch to hierarchical retrieval. If the context itself is short, the indexing overhead is not enough to dominate latency, and the cost-effectiveness of the transformation drops noticeably.

    6. Conclusion

    In the era of 10-million-token contexts, the focus of long-context optimization has shifted from “computing less” to “finding faster.” The value of Latent Sparse Attention lies in using learnable low-dimensional retrieval to pull indexing complexity back from quadratic to linear, making prefill latency controllable again while keeping accuracy close to that of the original model. For developers, the pragmatic path is: first do latency attribution to confirm that indexing is indeed the main cause; then replace incrementally with a hybrid architecture; and finally use the quality, length, and candidate curves to continuously calibrate parameters. Running on Baidu AI Cloud’s container platform and monitoring and alerting system, this kind of transformation can be rolled out and measured more smoothly and controllably through canary releases.