Sparse Attention at 10M Token Context: Key Techniques Behind a 22x Prefill Speedup

    As language models push context windows from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computation to retrieval: the indexer that selects Top-K relevant tokens can consume about 98% of prefill time in a global attention layer at 10M tokens. This article explains why the indexer becomes the dominant cost, and how Latent Sparse Attention addresses it by projecting queries and keys into a low-dimensional latent space for cheap coarse filtering before exact aggregation in the original space. It covers the two-stage workflow with pseudocode, reported speedups of roughly 22.7x and 72.4x on two model architectures, comparable accuracy on long-context tasks, and practical engineering guidance on profiling, recall control, blocking, KV cache management, and layered hybrid attention.

    Sparse Attention at 10M Token Context: Key Techniques Behind a 22x Prefill Speedup

    1. Long Context Turns a Compute Bottleneck into a Retrieval Bottleneck

    Over the past two years, mainstream large models have expanded their context windows from a few thousand tokens to the million-token range, and million-token context is becoming a default specification for some cloud services. One architecture pattern that has been repeatedly validated is hybrid attention: some layers keep global attention to preserve long-range dependency modeling, while other layers use sparse or linear attention to compress computation. The core assumption 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, with accuracy remaining largely controllable.

    But when the context grows from 1M toward 10M tokens, the nature of the problem changes. Take one global attention layer of a hybrid architecture model that supports million-token context. Running prefill at 10M tokens, roughly 98% of the time is not spent on the attention matrix multiplications themselves, but on the indexer that “decides which tokens to look at.”

    In other words, sparse attention solved the “compute too much” problem but not the “find too slowly” problem. The cost of selecting the Top-K relevant tokens still grows quadratically with context length. When K is far smaller than N and N reaches the tens of millions, the indexer becomes the overwhelmingly dominant term.

    2. Why the Indexer Becomes the Dominant Cost

    To understand where the speedup comes from, break down the execution flow of a typical sparse attention layer:

    1. Compute similarity scores between the query and all historical keys (this is essentially an N x N attention scoring matrix).
    2. Perform Top-K selection on the score vector for each query.
    3. Perform weighted aggregation over only the selected K keys/values.

    Step 3 has complexity O(N * K), which grows linearly with N when K is fixed and is very cheap. The real cost is concentrated in steps 1 and 2: scoring requires on the order of O(N^2) dot products and memory access, and Top-K selection typically introduces sorting or heap structures in engineering, with non-contiguous memory access and poor parallelism. At 10M tokens, N^2 is an enormous number, and even if only K results are kept, it is unavoidable to “look at” all candidates.

    Put differently, the bottleneck has shifted from compute to the memory-access and scheduling efficiency of candidate generation and filtering.

    3. Latent Sparse Attention: Moving Selection into a Low-Dimensional Space

    A startup research team and a European technical university jointly proposed a method named Latent Sparse Attention. Its core insight is straightforward: since full scoring in the original high-dimensional key space is too expensive, first project the query and keys into a low-dimensional latent space, complete similarity estimation and Top-K candidate filtering there, and then return to the original space for exact aggregation.

    It can be understood as a two-stage retrieval process of “coarse filtering first, exact computation second”:

    • Stage 1 (cheap coarse filtering): estimate relevance using the low-dimensional projected representations, reducing the cost from O(N^2 d) to O(N^2 d’), where d’ is much smaller than d; low-dimensional scoring is also more amenable to blocking and parallelism.
    • Stage 2 (exact aggregation): perform full-dimensional attention weighting only on the selected candidates.

    The illustrative pseudocode below explains the structure rather than specific interfaces:

    1. # Coarse filtering: low-dimensional latent-space scoring
    2. q_lat = project_down(q) # d -> d'
    3. k_lat = project_down(k) # d -> d'
    4. scores = q_lat @ k_lat.T # low-dimensional similarity, much lower cost
    5. idx = top_k(scores, k=K) # select candidates
    6. # Exact computation: aggregation in the original space
    7. k_sel = gather(k, idx)
    8. v_sel = gather(v, idx)
    9. out = softmax(q @ k_sel.T) @ v_sel

    The key point is that the low-dimensional projection matrices can be trained together with the model or learned in a lightweight way, keeping similarity in the latent space highly consistent with relevance in the original space and thus preserving the recall of coarse filtering. When recall is high enough, final accuracy is nearly identical to that of the original sparse attention.

    4. Performance Gains and Accuracy

    In the 10M-token prefill scenario, the measured gains on a single attention layer are substantial: about 22.7x speedup on a million-token hybrid architecture model, and about 72.4x speedup on a 27B-class model of another mainstream open-source architecture. The variation in speedup across models comes mainly from differences in the proportion of global layers, the efficiency of the original indexer implementation, and dimension settings. The more global layers there are and the less efficient the original indexer, the larger the relative gain after replacement.

    On accuracy, the method stays on par with the original models across a series of long-context tasks. This confirms a repeatedly validated judgment: the sparse approach itself does not sacrifice accuracy; what really needs to be solved is how to find that small subset faster.

    5. Engineering Recommendations

    For teams that want to introduce this kind of optimization in ultra-long-context scenarios, here are practical points:

    1. Locate the bottleneck first: use profiling to confirm whether prefill time is dominated by the indexer (rule of thumb: an indexer share above 50% makes optimization worthwhile).
    2. Control recall: monitor coarse-filtering recall as a core metric, and dynamically increase K or d’ when necessary.
    3. Blocking and parallelism: split low-dimensional scoring by block to improve memory-access continuity and parallelism.
    4. Coordinate with the KV cache: long-context inference should be paired with paged KV cache management to reduce memory fragmentation.
    5. Layered hybrid design: use latent sparse attention for global layers and linear/sliding-window attention for local layers, balancing long-range and local accuracy.
    6. Rely on general cloud capabilities: combine container platforms for elastic scaling, object storage for hosting long samples, and monitoring and alerting to track recall and latency metrics.

    6. Conclusion

    The core tension of tens-of-millions-token context has shifted from “cannot finish computing” to “finds too slowly.” By completing candidate filtering in a low-dimensional space, Latent Sparse Attention compresses the quadratic indexing cost into a manageable range, delivering tens of times faster prefill at 10M tokens without losing accuracy. For developers, this offers a clear and reusable paradigm for ultra-long-context optimization: coarse filtering first, exact computation second, trading low-dimensional retrieval for order-of-magnitude performance gains.