Sparse Attention at 10 Million Tokens: The Key to a 22x Speedup Is Cheaper Indexing

    As context windows scale from millions to tens of millions of tokens, the cost structure of long-context inference shifts. This article explains why sparse attention alone is not enough at extreme lengths: the indexing stage that decides which tokens to attend to can consume roughly 98% of prefill time in a global attention layer. It introduces Latent Sparse Attention, a method that performs candidate scoring in a compressed latent space, reports the published speedups of about 22.7x and 72.4x with comparable accuracy, and walks through the complexity analysis, engineering considerations, and evaluation practices needed to turn those gains into production results.

    1. The Cost Structure of Long Context Has Changed

    Over the past two years, long-context capability has moved from an experimental feature to a default specification. Million-token context is becoming a standard configuration for mainstream model serving, and architectures that mix sparse attention with linear attention are widely adopted. These designs share the same premise: each query does not need to scan the entire token history, and only needs to select a small set of the most relevant segments for computation.

    At the million-token scale, this premise works well. But as the context pushes toward 10 million tokens, an overhead that once seemed negligible starts to spiral. Take a global attention layer as an example: at 10 million tokens, roughly 98% of prefill time is not spent on the attention computation itself, but on the indexing step that “decides which tokens to look at.”

    This changes the nature of the problem. Sparse attention has already shown that attending to only a small fraction is enough to preserve accuracy. What actually blocks extreme-length context is that the process of finding that small fraction still grows quadratically with context length.

    2. Why the Bottleneck Lands on the Indexer

    Attention can be split into two stages:

    1. Selection stage: locate a candidate token set for each query.
    2. Computation stage: perform weighted aggregation over the candidate set.

    Sparsification only compresses the second stage; the first stage still has to score and rank the full history. When sequence length grows tenfold, the total work for candidate scoring grows roughly a hundredfold, so the indexer consumes almost all of the time. It is also memory-bandwidth intensive: candidate scoring repeatedly reads a large key cache, so bandwidth rather than compute becomes the ceiling.

    1. # Pseudocode: the two-layer structure of naive sparse attention
    2. scores = indexer(query, all_keys) # O(L^2): grows quadratically with length
    3. idx = topk(scores, k) # select the k most relevant tokens
    4. out = softmax(score(query, keys[idx])) # O(L*k): already sparsified

    The conclusion is straightforward: further optimizing the second stage yields limited returns, so the selection itself must be made cheaper.

    3. The Core Idea of Latent Sparse Attention

    A recent paper from a startup research team in collaboration with universities proposes Latent Sparse Attention. Its key insight is that selection does not have to happen in the original token space.

    The method compresses the key cache into a low-dimensional latent space, and the indexer performs candidate scoring and filtering in that latent space. Attention computation at original precision is only performed on the small number of selected positions. The reported results are speedups of about 22.7x and 72.4x for prefill of a single attention layer at 10 million tokens, while maintaining accuracy close to the original model across multiple tasks.

    Why it works comes down to three points:

    • Lower dimensionality: the cost of index scoring is tied to the compressed dimension, and the latent dimension is far smaller than the original head dimension.
    • Reduced memory traffic: the latent cache is small and more likely to hit high-speed cache, easing the bandwidth bottleneck.
    • Controllable selection quality: compression serves ranking, so as long as the recall of the candidate set is high enough, final accuracy is guaranteed by the second stage.

    4. Where the Gains Come From: A Complexity View

    Let the sequence length be L, the original head dimension be d, the latent dimension be r (r much smaller than d), and the number of candidates be k:

    • Original indexing cost is on the order of L²·d.
    • Latent-space indexing cost is on the order of L²·r, plus a one-time projection overhead of L·d.
    • The computation stage remains L·k·d.

    When L reaches the tens of millions, the L² term dominates total time, and the cost ratio converges approximately to d/r. This explains two observations: the longer the sequence, the higher the speedup; and different models yield different speedups due to differences in head dimension and structure (the gap between 22x and 72x comes from this).

    5. Engineering Implementation Notes

    • Cache tiering: the latent cache and the original key cache need block-based management, supporting incremental appends and block-level eviction.
    • Blocking and parallelism: split prefill into blocks, complete latent-space scoring within each block, run blocks in parallel, and pay attention to candidate backfilling at block boundaries.
    • Recall validation: on small-scale samples, compare “candidates selected in latent space” against “candidates selected by full scoring,” and monitor recall rather than only end-to-end accuracy.
    • Numerical stability: use higher-precision accumulation for the compression projection to avoid error accumulation over long sequences affecting ranking.
    • Mixing with linear attention: using sparse attention for global layers and linear attention for local layers is an effective combination for controlling total cost.
    • Observability: bring index time, candidate hit rate, and cache bandwidth into monitoring, alerting, and logging services to locate tail latency.

    6. Practical Recommendations

    When evaluating this type of approach, fix three variables: context length, candidate count k, and latent dimension r, then observe the curves of throughput and task accuracy as each changes. For a quick validation, reproduce the phenomenon of “indexing share rising with length” using a small model on a container platform, then gradually scale up the sequence length. Object storage is suitable for hosting long-context evaluation corpora, and message queues can be used to split prefill tasks.

    The core conclusion is a single sentence: the optimization focus of the 10-million-token era has shifted from “compute less” to “find cheaply.” Moving selection into latent space is the most direct path today; when deploying on platforms such as Baidu AI Cloud, pairing it with block-based caching and a complete monitoring system is what turns the paper’s speedups into stable production gains.