Sparse Attention at 10M Tokens: Why the Indexer, Not the Compute, Is the Real Bottleneck

    As context windows scale toward 10 million tokens, the dominant cost of sparse attention shifts from attention math to token selection. This article explains why naive indexing degrades to O(n²), how Latent Sparse Attention moves selection into a compressed latent space to restore near-linear scaling, the reported layer-level speedups of about 72x and 22x at 10M tokens, and the four engineering concerns—latent construction, two-stage index consistency, hybrid architecture compatibility, and memory/operator efficiency—plus a layered evaluation approach and a general cloud deployment path.

    Over the past two years, competition in long-context capability has centered on window size: from hundreds of thousands of tokens to a million, mainstream model services have gradually made 1M context a default configuration, and architectures commonly adopt a hybrid of sparse and linear attention. The shared premise of these designs is that each query does not need to scan the entire history of tokens; it only needs to select a few of the most relevant segments to participate in the computation.

    At the 1M scale, this premise has been validated repeatedly. But as the context advances to 10M tokens, the cost structure changes qualitatively. Take a certain class of global attention layers as an example: at the 10M-token scale, roughly 98% of prefill time is not spent in the attention operator itself, but in the indexer that “decides which tokens to look at.”

    This means the optimization focus has to move. Sparse attention answers the question “is looking at only a small subset enough,” and the answer is largely settled. What remains is “how to find that small subset at low cost,” and its complexity still grows quadratically with context length, making it the real roadblock.

    2. Why the Indexer Becomes a Quadratic Bottleneck

    Attention can be split into two stages:

    1. Selection: score, rank, and filter the top-k key/value entries for each query.
    2. Computation: perform weighted aggregation only over the selected subset.

    The complexity of the computation stage is determined by k, and k is usually far smaller than the sequence length n, so it is approximately linear and manageable. If the selection stage uses naive per-token scoring, it must evaluate all n candidates for every query, and the overall cost returns to the order of O(n²).

    Illustrative pseudocode makes this more concrete:

    1. # Naive indexing: complexity grows quadratically with n
    2. scores = q @ K.T # [n, n], pressure on both memory and compute
    3. idx = topk(scores, k) # the selection stage becomes the main cost
    4. out = sparse_attend(q, K, V, idx)

    At 10M tokens, an intermediate tensor of size [n, n] neither fits in memory nor can be computed. Even with blockwise processing and streaming statistics to relieve memory pressure, the selection stage remains the dominant cost.

    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 core idea is to move “selection” from the original token space into a compressed latent space:

    • First build a low-dimensional, low-resolution representation in the latent space, using a small number of latent variables to summarize the semantic distribution of a long sequence.
    • The query performs coarse-grained matching in the latent space, locating a few “potentially relevant” regions.
    • Fine-grained re-ranking and attention computation are then performed within the original token blocks corresponding to those regions.

    This is a typical two-stage retrieval structure of “coarse filtering first, fine computation second,” methodologically akin to index scans in databases and recall-plus-ranking in recommendation systems. The key point is that the cost of the coarse-filtering stage is determined by the latent-space length, which can be far smaller than the original sequence length, pulling overall complexity back from quadratic to close to linear.

    The reported results are: at 10M tokens, for the prefill stage of a single attention layer, one class of 27B-scale models is sped up by about 72x, and another model with a hybrid attention structure is sped up by about 22x, while maintaining accuracy close to the original model across multiple tasks. It should be emphasized that these figures are layer-level results for specific models, specific layer types, and specific hardware conditions; they are not equivalent to end-to-end throughput gains, and engineering evaluations should be based on your own workload.

    4. Four Things Engineering Deployment Must Handle

    4.1 Building and Updating the Latent Space

    The latent representation must be built incrementally during prefill and support streaming appends. A common approach is blockwise pooling plus a learnable projection, compressing the key/value of each block into a fixed number of latent slots. Too high a compression rate loses detail; too low a rate loses the speedup, so a grid search is usually needed in the 1% to 10% range.

    4.2 Consistency of the Two-Stage Index

    Missed recall in coarse filtering is the main source of accuracy loss. In practice, a “forced window” (the most recent tokens are always selected) and “task-critical blocks” (such as the blocks containing instructions or retrieval fragments) are kept as a fallback to avoid semantic loss caused by locally optimal retrieval.

    4.3 Compatibility with Hybrid Architectures

    The linear attention branch does not depend on top-k selection; only the sparse branch needs an indexer. In a hybrid structure, the indexer should act only on global attention layers, while local window layers go directly through the dense path, reducing unnecessary scheduling overhead.

    4.4 Memory and Operator Efficiency

    Whether the gains of the selection stage materialize depends on whether large matrix multiplications and memory round trips are genuinely reduced. If coarse filtering still produces a large scoring matrix of [n, m] (where m is the latent-space length), the overhead must be controlled through blockwise pipelining and low-precision accumulation.

    5. How to Verify Whether the Gains Are Real

    A layered evaluation is recommended in the following order:

    • Layer level: measure the prefill latency and peak memory of the target attention layer alone, and confirm whether the indexer’s share drops significantly.
    • Model level: compare time-to-first-token and overall prefill throughput, noting that the decode stage may not benefit.
    • Task level: cover four scenarios—long-document retrieval, multi-hop question answering, codebase understanding, and long-text summarization—and observe accuracy curves at different context lengths.
    • Stability: verify performance and accuracy sensitivity under different compression rates and different k values, and form a reproducible configuration baseline.

    6. A General Path for Cloud Deployment

    To serve 10M-scale context in production, you can build a validation pipeline on general cloud capabilities: use a container platform to host inference services and load-testing jobs, object storage to hold long-text corpora and evaluation sets, a message queue for request peak shaving and asynchronous batching, and logging and monitoring with alerting to track time-to-first-token, memory usage, and error rates. The general product categories offered by Baidu AI Cloud in such scenarios can serve as a reference foundation for building an elastic long-context inference pipeline.

    7. Conclusion

    In the era of 10M tokens, the core conflict is retrieval cost, not compute cost. Moving the selection process into the latent space and replacing full scoring with coarse filtering is currently one of the most engineering-feasible directions; combined with a forced-window fallback, division of labor in hybrid architectures, and layered evaluation, it is possible to raise speed while holding the accuracy line.