Sparse Attention at 10 Million Tokens: How Latent Retrieval Delivers a 22x Prefill Speedup

    As context windows scale from millions to tens of millions of tokens, the dominant cost of long-context processing shifts. This article examines why the indexer, not the attention math, becomes the bottleneck for sparse attention at extreme lengths, and explains Latent Sparse Attention (LSA): moving relevance retrieval into a low-dimensional latent space, using block summaries instead of per-token scoring, and combining local and global layers. It includes a prefill skeleton, complexity analysis, and practical guidance on profiling, validation, fine-tuning, elastic resources, and recall fallback. You will learn how to push quadratic growth back toward near-linear scaling while preserving accuracy.

    When the context window grows from millions to tens of millions of tokens, the largest overhead in long-text processing quietly changes places. The sparse attention mechanism widely used in the industry lets each query vector attend to only a small set of the most relevant historical segments, and at the million-token scale it has repeatedly proven to balance accuracy and efficiency. But once the window is stretched another tenfold, an originally unremarkable step starts to spiral out of control: the indexing process that decides “which tokens to look at” now costs grows quadratically with context length. Take one global attention layer in a general-purpose large model that supports million-token contexts as an example. During prefill at the 10-million-token scale, roughly 98% of the time is not spent on the numerical computation of attention, but is consumed by filtering and ranking candidate segments.

    The nature of the problem has therefore changed. Sparsification has long proven that “looking at only a small part” is enough to preserve accuracy; the real obstacle is the cost of “finding that small part.” Recently, an overseas research team and a European technical university jointly published a transferable general approach: performing sparse retrieval in latent space (Latent Sparse Attention, or LSA). Their public results show that at the 10-million-token condition, prefill for a single attention layer can be accelerated by more than 20x, with speedups reaching on the order of 70x under certain model architectures, while accuracy on multiple long-text tasks remains close to the original model. This article strips away specific product attributes and abstracts the method into a practical engineering design.

    1. The Bottleneck Shifts: Why the Indexer Takes Center Stage

    The compute and memory overhead of standard attention grows quadratically with sequence length, which is a well-known wall. Sparse attention compresses the visible range of each query to a fixed budget, theoretically bringing complexity back to linear. In engineering practice, however, sparsification splits into two steps: first “select,” then “compute.”

    • Step one: the indexer scores each query, estimating its similarity to all historical positions, then takes the top k.
    • Step two: attention is weighted and summed only over the selected k positions.

    When k is set to a constant, the second step is indeed linear; but if the first step directly reuses the original head dimension for a full dot product, its cost is still quadratic. The longer the context, the larger the constant term, until the first step completely overshadows the second. This is the origin of “98% of the time spent in the indexer.”

    Three common mitigation techniques exist in engineering: reducing the scoring dimension, block-compressing the keys, and mixing a local window with a few global anchors. Each is effective on its own, but only their combination forms a complete solution.

    2. Core Idea: Move Retrieval into Latent Space

    The key abstraction of LSA is that the representation needed for retrieval does not have to be the same as the representation needed for attention computation. Attention requires high-dimensional, multi-head key-values capable of expressing complex patterns; judging relevance only needs a low-dimensional, smooth projection that is easy to search for nearest neighbors.

    The flow then becomes:

    1. # Illustration: prefill skeleton of latent sparse attention
    2. latent = proj_down(k) # high-dimensional keys -> low-dimensional latent representation, with greatly compressed dimension
    3. blocks = chunk(latent, size=256) # chunking; mean/max pooling within each block yields block summaries
    4. scores = q_latent @ blocks.T # queries dot-product only with block summaries, complexity O(n * n/256)
    5. idx = topk(scores, k=2048) # select candidate blocks, then refine to specific positions within blocks
    6. out = attention(q, k[idx], v[idx]) # real attention only over candidates

    Three design points are worth emphasizing. First, the dimensionality-reduction projection is shared and lightweight; it can be jointly trained with the main model or initialized with small-scale distillation. Second, block summaries turn scoring from “per token” into “per block,” dividing the quadratic term by the block size. Third, two-stage retrieval (coarse filtering at the block level, fine ranking within blocks) achieves a controllable tradeoff between recall and overhead.

    3. Hybrid Layout: Local for Accuracy, Global for Recall

    Pure sparsity loses long-range dependencies, while pure global attention returns to quadratic overhead. In practice, a layered layout is more stable: most layers use a sliding window plus a few global anchors, while a few layers retain global attention for cross-segment integration. LSA’s latent retrieval fits precisely into the global layers, replacing their expensive full scoring, while local layers are barely affected. This “few global, many local” structure keeps overall complexity close to linear while preserving the critical global information path.

    4. A Sense of Scale for Complexity and Gains

    Let the sequence length be n, block size b, and candidate budget k:

    • Original global attention: O(n²·d);
    • Naive sparse indexing: O(n²·d) scoring + O(n·k·d) computation;
    • Latent block retrieval: O(n·(n/b)·d’) + O(n·k·d), where d’ is far smaller than d.

    When n=10⁷, b=256, and d’=64, the scoring term shrinks by about two orders of magnitude, which is the source of the 20x-plus speedup; if the model itself has higher head dimensions and more global layers, the speedup amplifies further to tens of times.

    5. Implementation Recommendations

    1. Profile first: use logging services and monitoring alerts to collect per-stage prefill timing, confirm the indexer’s share, and avoid blind optimization.
    2. Validate in small steps: align accuracy on short contexts, then gradually lengthen the window while observing recall and task metrics.
    3. Coordinate training: jointly fine-tune the dimensionality-reduction projection, and use distillation when necessary to stabilize early convergence.
    4. Elastic resources: ultra-long-context prefill is a bursty workload; container platforms with elastic scaling and object storage can carry intermediate results.
    5. Accuracy fallback: set threshold alerts on retrieval recall, and fall back to a larger candidate budget when recall is low.

    In long-context inference scenarios, Baidu AI Cloud typically recommends hosting inference services on a general-purpose container platform and tracking per-stage timing with a monitoring and alerting system, so that optimization is built on observable data rather than guesswork.

    6. Conclusion

    In the era of tens of millions of tokens, the efficiency competition has shifted from “computing fast” to “selecting accurately and cheaply.” Migrating relevance retrieval into a low-dimensional latent space, replacing per-token scoring with block summaries, and using a hybrid layout to preserve accuracy is currently the most cost-effective general path. For developers, the real gain is not just the per-layer speedup multiple, but pushing quadratic growth back to near-linear, taking ultra-long context from demonstration to practical use.