Latent Sparse Attention for Ten-Million-Token Context: ~22x Prefill Speedup

    As long-context inference moves from million-token to ten-million-token scales, the bottleneck shifts from attention computation itself to the indexer that decides which tokens to attend to. This article explains why sparse-attention routing can grow quadratically with context length, introduces Latent Sparse Attention (LSA) as a low-dimensional hierarchical routing approach, and discusses recall safeguards, evaluation metrics, and engineering practices for balancing speed, accuracy, throughput, and tail latency.

    Latent Sparse Attention for Ten-Million-Token Context: ~22x Prefill Speedup

    1. At Ten-Million-Token Scale, the Bottleneck Moves

    Long-context capability is shifting from a selling point for a few models to a default specification for inference services. As official services make million-token context a default configuration, hybrid architectures that combine sparse attention and linear attention are also becoming common. The core idea is straightforward: instead of applying full attention between every query and all historical tokens, the model first selects a small set of the most relevant historical tokens and then performs attention over that candidate set. This reduces the main attention computation from quadratic growth with length to approximately linear growth with length.

    This approach has been validated repeatedly at million-token context lengths. But as context is pushed to the ten-million-token level, an overhead that was previously easy to overlook starts to get out of control: the indexer that determines which tokens should be attended to. Under specific model structures and experimental settings, during ten-million-token prefill, a very high share of time within a single global attention layer is spent not on attention computation but on indexing and routing. Public research reports a single-layer prefill speedup of roughly 22x; models with different structures may see larger or smaller gains. This phenomenon shows that the real scalability bottleneck for sparse attention has moved from “whether to attend” to “whether the right tokens can be found.”

    2. Why the Indexer Grows Quadratically with Context Length

    Typical sparse attention has two stages:

    1. Routing stage: compute the relevance between each query and historical tokens or historical blocks.
    2. Attention stage: perform full-dimensional attention only on the top-k candidate tokens.

    The cost of the attention stage can usually be written as (O(n \cdot k \cdot d)), where (n) is the context length, (k) is the number of candidates, and (d) is the head dimension. Because (k) is much smaller than (n), this stage appears scalable. The problem lies in the routing stage: if every query must be scored against all historical blocks or all tokens, the cost approaches (O(n^2 \cdot d_r)). Even when (d_r) is a low-dimensional routing dimension, it remains substantial at ten-million-token scale.

    More seriously, the indexer is not only expensive in compute; it also introduces additional memory access, candidate gathering, and sorting costs. During prefill, batches are large and sequences are long, so the indexer often needs to frequently read and write block summaries, score matrices, and top-k intermediate results. Actual runtime can therefore be much higher than theoretical FLOPs suggest. This leads to a counterintuitive phenomenon: attention computation has already been sparsified, but “choosing which tokens participate in attention” remains a heavy operation.

    3. Core Idea of Latent Sparse Attention

    The key to Latent Sparse Attention (LSA) is not simply reducing top-k. Rather, it moves the indexing process into a low-dimensional latent space and reduces the number of candidate comparisons through hierarchical routing. Its basic flow can be summarized as follows:

    • Project queries and keys into a low-dimensional latent space.
    • Divide historical tokens into blocks and generate block-level summaries or cluster centers.
    • Have the query first perform coarse-grained routing against block summaries in the latent space to select a small number of candidate blocks.
    • Perform token-level reranking within candidate blocks to obtain the final top-k.
    • Apply full-dimensional attention only to the final candidate tokens.
    • Optionally combine a local window or a linear attention branch to provide a fallback path for missed candidates.

    The essence of this design is to move the quadratic term of indexing from the original high-dimensional space into a low-dimensional space, and to use a two-level structure of “block-level coarse filtering + token-level reranking” so that most queries do not need to be compared directly against all tokens. It does not reduce indexing cost to zero, but it can compress indexing from the main bottleneck into a controllable overhead.

    An illustrative pseudocode example is as follows:

    1. function latent_sparse_prefill(Q, K, V, block_size, topk_block, topk_token):
    2. Q_lat = project_q(Q)
    3. block_summary = summarize_blocks(K, block_size)
    4. for each query chunk:
    5. block_score = Q_lat_chunk @ block_summary.T
    6. candidate_blocks = topk(block_score, topk_block)
    7. for each query q in chunk:
    8. candidates = gather_tokens(K, candidate_blocks[q])
    9. token_score = q @ candidates.T / sqrt(d)
    10. selected = topk(token_score, topk_token)
    11. output[q] = softmax(token_score[selected]) @ V[candidates[selected]]
    12. return output

    The pseudocode above expresses structure only and does not represent a specific interface. Engineering implementations also need to handle multi-head routing, block boundaries, causal masks, batching, and cache reuse.

    4. Accuracy Safeguards: Index Recall Matters More Than a Single Speedup Figure

    The risk of sparse attention is never simply “computing too slowly”; it is “missing the right candidates.” If key information does not enter the candidate set, no amount of precision in subsequent attention can compensate. Therefore, LSA-style approaches need to focus on candidate recall, not only on end-to-end latency.

    Common safeguards include:

    • Joint optimization of routing and main attention: let the low-dimensional projection learn relevance that is useful for downstream tasks, rather than performing only unsupervised dimensionality reduction.
    • Multi-level candidates: keep a wider candidate set during block-level coarse filtering, then narrow it down through token-level reranking.
    • Local-window fallback: always retain recent tokens and adjacent blocks so that local dependencies are not missed by routing.
    • Independent multi-head routing: different attention heads focus on different semantic subspaces, reducing single-route bias.
    • Training-inference consistency: simulate top-k candidate selection during training to avoid distribution shift at inference time.

    Evaluation should not rely only on average accuracy. It should also examine tasks such as long-document question answering, multi-hop retrieval, and repository-level code dependency tracking. Candidate recall, accuracy degradation across different context lengths, and the share of time spent on indexing versus attention should be measured separately. A solution has engineering value only when “acceptable recall” and “significant latency reduction” both hold.

    5. Engineering Deployment Recommendations

    First, profile the time breakdown first. Split prefill into projection, block summarization, routing scoring, top-k, candidate gathering, attention computation, and output writeback, then confirm whether the bottleneck is compute, memory bandwidth, or sorting. Second, cache block summaries. Historical block summaries can be reused across multi-turn requests to avoid recomputing them on every prefill. Third, control the number of candidate blocks. Too few top-k blocks can hurt recall, while too many can erase the speedup. The number should be adaptive to the task type. Fourth, treat prefill and decoding differently. Indexing overhead is more prominent during prefill, while decoding is more focused on KV cache access and candidate reuse; the two need different optimization strategies. Fifth, use monitoring and alerting to observe tail latency in long-context services, so that average latency looks normal while individual ultra-long requests drag down overall throughput.

    6. Conclusion

    Ten-million-token context exposes the contradiction in sparse attention more fully: attention computation can be sparsified, but the indexer can still grow quadratically with length. The value of Latent Sparse Attention is that it does not stop at “reducing top-k.” Instead, it restructures the indexing path: low-dimensional projection lowers the cost of each comparison, block routing reduces the number of comparisons, and candidate reranking preserves accuracy. For teams building ultra-long-context inference capabilities, the metrics that truly matter are not a single speedup number, but the balance among index recall, end-to-end throughput, tail latency, and accuracy degradation.