Sparse Attention at 10M Tokens: Moving Token Selection into Latent Space for Up to 22x Prefill Speedups

    As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computing attention to choosing which tokens to attend to. This article explains why the indexer becomes a quadratic cost during prefill, how latent-space sparse attention replaces full-resolution comparison with low-dimensional coarse filtering, and what accuracy and engineering practices keep recall high. It includes a conceptual pseudocode flow, guidance on combining with hybrid architectures, and observability metrics for production.

    1. Background: From Million to 10M Tokens, the Bottleneck Has Moved

    Over the past two years, long context has become a baseline capability of large language models. Million-token context has moved from an optional feature to the default configuration of mainstream services, and hybrid attention architectures—stacking global attention layers alternately with linear or sparse layers—are widely adopted. The shared premise is that a query vector does not need to scan every historical token; it only needs to select a small set of the most relevant segments to participate in the computation.

    This approach has been validated repeatedly at the million-token scale. But as context pushes toward 10M tokens, an originally negligible cost starts to spiral: during prefill, in a given global attention layer, most of the time is not spent on the matrix multiplications of attention itself, but on the indexing and scoring work that decides which tokens to look at.

    The nature of the problem has therefore changed. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks ultra-long context is that the process of finding that small subset still grows quadratically with context length. The indexer must evaluate similarity between every query and all historical tokens, so the complexity remains O(N²); only the constant has shifted from attention computation to selection computation.

    2. Root Cause: Why the Indexer Becomes the Quadratic Bottleneck

    Breaking down prefill cost yields three categories:

    • Attention computation: constrained by sparsity, it depends only on the small number of selected tokens and grows roughly linearly;
    • Indexing and scoring: requires full comparison and grows strictly quadratically;
    • Memory access and scheduling: bounded by bandwidth and parallelism, and likewise amplified at long sequence lengths.

    Once sequence length crosses a certain threshold (typically beyond the million-token range), the second category rapidly becomes dominant. Worse, indexers are often hard to parallelize and involve irregular memory access, so hardware utilization falls far below that of dense matrix multiplication—same nominal complexity, worse actual latency.

    The optimization direction is therefore not to reduce sparsity further, but to change the computational space and complexity order of the selection step itself.

    3. Core Idea: Move Selection into Latent Space

    The key insight of latent-space sparse attention is that deciding whether two tokens are related does not have to be done dimension by dimension in the original high-resolution key space. A coarse filter can run in a low-dimensional, learnable latent space, after which fine-grained selection returns to the original space.

    The flow can be abstracted into four steps:

    1. Dimensionality reduction projection: project queries and keys separately into a latent space whose dimension is far smaller than the head dimension;
    2. Latent-space scoring: evaluate similarity inside the latent space to obtain a candidate set;
    3. Candidate completion: map back to the original tokens via the index and fill in the necessary positional and structural information;
    4. Sparse attention: run attention computation only on the candidate tokens.

    Because scoring happens in a low-dimensional space, complexity drops from O(N²·d) to O(N²·d’), where d’ is far smaller than d. Combined with hierarchical coarse filtering (block level first, then within blocks), it can approach near-linear behavior. Illustrative pseudocode:

    1. # Illustrative flow: latent-space coarse filtering + original-space fine computation
    2. q_lat = proj_q_to_latent(q) # project to low-dimensional latent space
    3. k_lat = proj_k_to_latent(k)
    4. block_scores = latent_pooling(q_lat, k_lat) # block-level coarse filtering
    5. cand = topk_blocks(block_scores, budget) # select candidate blocks
    6. idx = refine_within_blocks(q, k, cand) # fine ranking within blocks
    7. out = sparse_attention(q, k, v, idx) # compute only on candidates

    The payoff is twofold: it reduces the computation of the selection stage, and because candidates are more focused, it improves memory access locality and noticeably raises hardware utilization.

    4. Results and Accuracy: Why It Is Faster Without Losing Quality

    At the 10M-token scale, this class of methods can achieve tens of times speedup in the prefill stage of a single global attention layer, with the speedup ratio varying across models and sparsity budgets—the higher the sparsity and the heavier the original indexer, the larger the gain.

    Accuracy retention relies on three engineering constraints:

    • Recall first: the coarse filtering stage should keep more candidates rather than risk missing key segments;
    • Hierarchical verification: block-level and within-block filtering correct each other;
    • Position and structure compensation: enforce visibility for local windows and task-critical regions to prevent loss of long-range information.

    The practical recommendation is to first validate recall and task accuracy at the million-token scale, then scale up gradually to 10M tokens while monitoring candidate hit rate and the share of time spent on indexing.

    5. Engineering Implementation Notes

    • Cooperate with hybrid architectures: linear layers provide low-cost global coverage, while sparse global layers handle precise retrieval, with a clear division of labor;
    • Balance memory and bandwidth: keep the extra memory of latent-space projection under control, and make candidate indices as contiguous as possible to improve bandwidth utilization;
    • Observability: bring “share of indexing time” and “candidate hit rate” into monitoring and alerting as a basis for capacity planning;
    • Cloud implementation: inference services can run on a container platform, long-context caches can be managed with object storage, and log services plus monitoring and alerting can help locate long-tail latency.