Sparse Attention at 10M Token Context: Why Latent-Space Indexing Delivers 22x Prefill Speedup

    As context windows scale from 1M to 10M tokens, the bottleneck in sparse attention shifts from compute to indexing. This article explains why the indexer overtakes attention matmuls at extreme sequence lengths, how Latent Sparse Attention moves selection into a low-dimensional latent space for over 20x single-layer prefill speedup, why block-level coarse filtering preserves accuracy, and what to consider when implementing it in a long-context inference stack—including profiling, compression ratios, batching, caching, and recall-based regression baselines.

    When the context length grows from 1M to 10M tokens, the nature of the sparse attention problem changes fundamentally: computation is no longer the bottleneck. What actually runs out of control is the indexing step—the logic that decides which tokens to look at. A class of approaches now referred to as Latent Sparse Attention addresses this by moving the “selection” task out of the original token space and into a low-dimensional latent space. At the 10M-token scale, this can deliver more than 20x speedup for single-layer prefill while keeping accuracy loss within an acceptable range. This article does not focus on any specific model or product. Instead, it abstracts the general principles, failure boundaries, and engineering considerations of this technical route for developers building long-context inference systems.

    1. Background: The Cost Structure of Long Context Has Changed

    Over the past two years, mainstream large models have treated 1M context as the default specification for flagship services, and hybrid sparse plus linear attention has gradually become a common architectural choice. The shared premise is that each query does not need to traverse the entire history; it only needs to pick out a few of the most relevant segments to participate in computation. This premise has been validated repeatedly at the million-token scale.

    But as the context grows to tens of millions of tokens, an overhead that used to be negligible starts to dominate total latency. Take a global attention layer as an example: at 10M tokens, roughly 98% of prefill time is not spent on the attention matmul at all, but on the indexer—the logic for scoring, sorting, and selecting the top-k.

    So the nature of the bottleneck has changed. Sparse attention proved that “looking at only a small part” is enough to preserve accuracy, but the cost of “finding that small part” still grows quadratically with sequence length. This creates an awkward situation: we saved on computation, but shifted the cost to selection.

    2. The Source of Quadratic Growth: Why the Indexer Costs More Than Attention

    To understand this, break prefill latency into three parts:

    1. Scoring: evaluating similarity between the query and all historical keys, at a cost of O(N²), where N is the sequence length.
    2. Selection: globally sorting or threshold-filtering the scores to take the top-k, involving O(N log N) comparisons and a large amount of irregular memory access.
    3. Aggregation: running attention only over the selected k positions, at a cost of O(N·k).

    The first two are masked by the third when N is small, but k is typically capped at a few thousand and stays roughly constant, while the first two grow quadratically with N. When N goes from 1M to 10M, the first two terms scale up by 100x while the third barely changes, so the proportions inevitably invert.

    Memory access patterns make it worse. The attention matmul is regular work that tensor cores can absorb efficiently, whereas top-k selection is data-dependent, branch-heavy, and random in its memory access, with very low hardware utilization. For the same FLOPs, the indexer may take several times as long.

    3. The Core Idea: Move Selection into Latent Space

    The key insight behind Latent Sparse Attention is that we do not need to “select on the original tokens.” We only need to “select a set of tokens that is good enough.” Since selection is just a means to an end, it can be performed on a low-dimensional, compressed representation, with the result then mapped back to the original space.

    The general flow can be summarized in four steps:

    1. # Pseudocode: single-layer forward pass of latent sparse attention
    2. # 1. Compress: reduce the long sequence to a few latent representations (block-level/slot-level)
    3. latent = compress(K, V, block_size=B) # shape: [N/B, d_latent]
    4. # 2. Score in latent space: cost drops from O(N^2) to O((N/B)^2)
    5. score = q @ latent.T # [1, N/B]
    6. # 3. Select blocks instead of points: top-k operates at block granularity
    7. blocks = topk(score, k_blocks) # irregularity of indexing drops sharply
    8. # 4. Backfill and refine: run original-precision attention only inside selected blocks
    9. idx = expand_to_token_indices(blocks, B)
    10. out = sparse_attention(q, K[idx], V[idx])

    There are three key design points here. First, the compression ratio B determines how much the indexing cost drops; the quadratic relationship means B=64 can reduce scoring cost by roughly three orders of magnitude. Second, the selection granularity changes from token to block, memory access becomes contiguous, the randomness of top-k drops significantly, and hardware utilization improves. Third, in-block refinement preserves accuracy—coarse filtering is allowed to make mistakes, and as long as the correct tokens fall inside a selected block, the final result is still exact.

    4. Why Accuracy Holds Up

    The accuracy of sparse attention relies on an empirical fact: attention mass is highly concentrated, with a few positions contributing the vast majority of the weight. The latent-space approach adds a layer of fault tolerance on top of this:

    • Recall of the coarse filter matters more than precision. As long as the target block enters the candidate set, exact computation inside the block corrects the ranking error.
    • The compressed representation retains block-level statistics (mean, extremes, direction), which is enough to distinguish coarse-grained differences between “relevant” and “irrelevant.”
    • In multi-layer structures, shallow layers can use more aggressive compression while deeper layers tighten k, forming a funnel from coarse to fine.

    In practice, it is worth evaluating recall specifically rather than looking only at end-to-end task scores. A practical approach is to measure the overlap ratio between exact top-k and approximate top-k, and treat it as a tunable knob to trade off against the latency budget on a Pareto curve.

    5. Engineering Implementation Checklist

    To implement similar capabilities in your own inference stack, proceed in this order:

    1. Profile first and confirm the bottleneck. Use sampling-based performance profiling to separate the time spent on matmuls from the time spent on indexing logic, so you do not optimize the wrong layer.
    2. Design compression ratios per layer. Use a large B for global layers and keep dense or sliding-window attention for local layers; mixing them is often more cost-effective than a uniform setting across all layers.
    3. Make the selection operator batchable. Convert top-k over variable-length sequences into fixed-shape block scoring to reduce kernel launch and synchronization overhead.
    4. Cache latent representations. The compressed representations built during prefill can be reused across requests (in scenarios where prefix sharing is allowed), amortizing repeated indexing cost.
    5. Establish an accuracy regression baseline. Use tasks such as long-document retrieval, multi-hop question answering, and code-base-level completion as controls, and monitor drift introduced by approximate selection.
    6. Combine with general cloud capabilities. Use a container platform for elastic scaling, object storage to hold long-context caches, and logging plus monitoring and alerting services to track P99 latency and recall metrics.

    6. Boundaries and Caveats

    Latent Sparse Attention does not apply unconditionally. When a task requires exact single-point retrieval (for example, locating a unique identifier in a long document), too large a B hurts recall. When the sequence is short, the indexer is not the bottleneck anyway, and introducing compression only adds complexity. If compression is done differently during training and inference, it causes distribution shift. The reasonable approach is to make the compression ratio, block size, and k value all configurable, and tune them separately for different business workloads.

    Conclusion

    At 10M-token context, the core tension has shifted from “unable to compute” to “unable to find accurately and find fast.” Migrating the selection process into a low-dimensional latent space, coarse-filtering at block granularity, and then returning to the original space for refinement is a proven general path. The 20x-plus single-layer prefill speedup does not come from stronger hardware, but from redrawing the cost structure. For developers, the real payoff is this: measure first, locate the bottleneck, and then trade approximate selection for an order-of-magnitude latency reduction while using recall metrics to hold the accuracy line.