Latent-Space Sparse Attention: About 22x Faster at 10 Million Tokens

    As context windows grow from millions to tens of millions of tokens, the main cost in sparse attention shifts from computing attention to selecting which tokens to attend to. Latent-space sparse attention moves this selection step into a low-dimensional, block-level space, routes each query to its most relevant blocks, and then runs exact attention on those blocks. This article covers the complexity trade-offs, the accuracy risks, and the engineering practices for deploying the method, including the reported speedups.

    1. Problem Restatement: The Bottleneck Has Shifted from Computing Attention to Selecting Tokens

    As context length grows from millions to tens of millions of tokens, the challenges for sparse attention change in nature. The core assumption of sparse attention is that each query does not need to attend to every historical token. It only needs to focus on a small number of the most relevant segments, which significantly reduces computation with controllable loss in accuracy. This assumption has been validated extensively at the million-token scale. As sequence length keeps growing, however, a cost that was once minor grows quickly: the indexing and routing process that decides which tokens to look at.

    In a typical global attention layer, the prefill stage has two parts. First, the indexer scores the relevance of historical tokens or historical blocks for each query. Second, exact attention is computed only over the small set of selected tokens. Public analysis indicates that at the scale of 10 million tokens, about 98% of the prefill time in a certain type of global attention layer is spent not on exact attention but on the indexer. The reason is straightforward. Even if only a few tokens are ultimately computed, the indexer may still need to score each query against all historical tokens or blocks. As sequence length grows, this full comparison grows quadratically.

    Therefore, what constrains ultra-long context is no longer whether attending to a small portion is sufficient. It is how to find that small portion at an acceptable cost. This is the core problem latent-space sparse attention aims to solve. It moves the expensive indexing process from the original high-dimensional token space to a low-dimensional, block-level latent space. This preserves recall as much as possible while substantially reducing the comparison width and the computational constant of the indexer.

    2. Core Idea: Route with Low-Dimensional Block Summaries, Then Run Exact Attention in the Original Space

    The key of latent-space sparse attention is not to change how exact attention is computed. It changes how the tokens that take part in exact attention are selected. The method typically has three layers.

    1. Block summarization: Queries, keys, and values are split into blocks of fixed or adaptive size, and each block gets a low-dimensional summary. The summary can come from the block mean, pooling, a low-rank projection, or a lightweight encoder. The goal is to represent the semantics of a block with a vector much smaller than the original dimension.

    2. Low-dimensional routing: Relevance between query blocks and historical key blocks is computed in the latent space. This produces block-level scores, and the top-k most relevant blocks are selected. Because the latent dimension is far smaller than the original attention dimension, and the comparison unit changes from tokens to blocks, both the computation and the memory access of the indexer drop noticeably.

    3. Exact fallback: For the selected blocks, exact attention is still computed with the original keys and values to preserve output quality. The latent space only handles routing and does not replace the final attention values. Because of this, the indexer and the exact computation can be decoupled in the engineering design.

    Compared with high-dimensional sparse selection at the token level, this approach moves the quadratic term of the indexer from high-dimensional token-level comparison to low-dimensional block-level comparison. A larger block size means fewer blocks and fewer routing comparisons. A lower latent dimension means each comparison is cheaper. Together, these effects greatly reduce the indexing overhead in 10-million-token prefill. Public experiments show that at this scale, prefill of a single global attention layer can achieve about a 22.7x speedup. On a smaller model, the speedup can reach about 72.4x. These figures suggest that once the bottleneck shifts, optimizing the indexer may yield more direct gains than further optimizing exact attention.

    3. Complexity Analysis: Reduce the Dimension and Constant of the Quadratic Term, Not Just Eliminate It

    Let N be the sequence length, d the original attention dimension, B the block size, M = N/B the number of blocks, r the latent dimension (with r much smaller than d), and k the number of blocks selected by routing. The typical cost of a traditional token-level indexer can be approximated as O(N²·d_i), where d_i is the dimension used for index scoring. The main costs of latent-space sparse attention break down as follows:

    • Block summary generation: roughly O(N·r) to O(N·d·r), depending on the projection implementation;
    • Block-level routing scores: roughly O(M²·r) = O((N/B)²·r);
    • Exact attention: computed only over the top-k blocks, roughly O(N·k·B·d).

    Routing scores still form a quadratic term in the number of blocks M. Since B is usually much greater than 1, however, M² is far smaller than N². At the same time, r is much smaller than the original dimension, so each score is cheaper. Adding hierarchical indexing, two-level block routing, or a local window fallback can reduce the cost of distant block comparisons further.

    A more precise statement is therefore this: latent-space sparse attention does not turn the indexer into strictly linear complexity in every implementation. It moves the quadratic growth from high-dimensional token-level comparison to low-dimensional block-level comparison. It then controls the constant through block size, latent dimension, and the top-k budget. At the 10-million-token level, this constant reduction is often what determines whether the approach is practical.

    4. Illustrative Pseudocode: Decoupling Routing from Exact Computation

    The following engineering-oriented pseudocode focuses on the data flow rather than specific interfaces.

    ```text
    def latent_sparse_attention_prefill(q, k, v, block_size, low_rank_proj, topk_blocks):

    1. # q/k/v: [seq_len, dim]
    2. q_blocks = split_blocks(q, block_size)
    3. k_blocks = split_blocks(k, block_size)
    4. v_blocks = split_blocks(v, block_size)
    5. # 1. Generate low-dimensional block-level summaries; the latent dimension is much smaller than the original dimension
    6. q_summary = low_rank_proj(block_pool(q_blocks))
    7. k_summary = low_rank_proj(block_pool(k_blocks))
    8. # 2. Route at the block level in the latent space to get the top-k key blocks each query block attends to
    9. route_scores = matmul(q_summary, transpose(k_summary))
    10. selected_idx = topk(route_scores, topk_blocks, axis=-1)
    11. # 3. Run exact attention only on the selected blocks
    12. out_blocks = []
    13. for i in range(num_blocks(q_blocks)):
    14. selected_k = gather_blocks(k_blocks, selected_idx[i])
    15. selected_v = gather_blocks(v_blocks, selected_idx[i