Sparse Attention at 10 Million Tokens: A 22x Prefill Speedup by Fixing the Indexer

    As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computing attention to deciding which tokens to attend to. This article breaks down why naive full-history scoring makes the indexer a new quadratic bottleneck, explains how latent-space retrieval reduces index complexity from quadratic to near-linear, and reports the resulting prefill speedups of roughly 22x at the 10-million-token scale. It also covers practical engineering guidance on block size, latent dimensionality, hybrid linear-attention designs, caching, observability, and a three-layer evaluation approach.

    1. The Problem: As Context Grows from Millions to Tens of Millions, the Bottleneck Moves

    Over the past two years, long context has become a default expectation for LLM serving. Million-token windows have moved from a flagship capability to a common spec, and hybrid architectures combining sparse and linear attention are now widely adopted. The shared premise is that each query does not need to scan the entire history: selecting a few of the most relevant segments is enough to balance accuracy against cost.

    Once context pushes past a million tokens toward tens of millions, the nature of the problem changes. Take a single global attention layer at the 10-million-token scale during prefill: roughly 98% of the time is not spent on attention computation at all, but on the indexing step that decides which tokens to look at. Sparsification solved the “compute too much” problem; it did not solve the “find too slowly” problem.

    It helps to think in terms of two separate costs:

    • Compute cost: scales with the number of selected tokens, is roughly linear after sparsification, and has been effectively contained.
    • Index cost: scales with the full history length, and grows quadratically with context length under naive full scoring and ranking, eventually becoming the dominant term.

    So the key question in the era of tens of millions of tokens is no longer whether to be sparse, but how to locate the positions worth sparsifying at a lower cost.

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

    The indexing pipeline in naive sparse attention usually has three steps: score every historical token for relevance, globally rank the scores or apply a threshold filter, then gather keys and values by index. The first two steps both have to touch the full history, so the complexity inherently carries a quadratic term.

    At a million tokens this overhead is tolerable. At ten million it is amplified by two orders of magnitude and triggers three knock-on effects:

    1. Memory and bandwidth pressure: full scoring repeatedly reads and writes keys and values, making memory access the primary bottleneck.
    2. Reduced scheduling and parallelism efficiency: irregular indexing makes it hard for operators to parallelize fully, so hardware utilization drops.
    3. Uncontrollable end-to-end latency: prefill is stretched out by indexing, and time-to-first-token degrades noticeably.

    The conclusion: continuing to optimize engineering within the “score everything” paradigm yields diminishing returns. The search space of the index has to be reduced at the representation level.

    3. The Core Idea: Retrieve in Latent Space, Not in Raw Token Space

    One approach that has been validated is latent-space sparse attention. The point is not to change the “attend to only part of the sequence” principle, but to change where the search happens:

    • Compressed representation: map historical keys and values into a low-dimensional latent space to form compact semantic summaries.
    • Coarse localization: run similarity retrieval in latent space to lock onto a small number of candidate blocks first.
    • Fine-grained selection: score and aggregate at original precision only within those candidate blocks.

    The indexing stage thus changes from “score ten million tokens one by one” to “retrieve over a small number of block summaries,” cutting complexity substantially, while the keys and values that ultimately participate in attention remain at original precision, keeping accuracy loss within an acceptable range.

    Illustrative pseudocode below; it only demonstrates the flow and does not correspond to any specific API:

    1. # 1. Compress historical keys/values into block-level latent representations
    2. block_latent = compress(kv_history, block_size=256)
    3. # 2. Run coarse retrieval in latent space to obtain candidate blocks
    4. cand_blocks = latent_search(query_latent, block_latent, topk_blocks=8)
    5. # 3. Apply fine-grained sparse attention only within candidate blocks
    6. out = sparse_attention(query, kv_history[cand_blocks])

    4. Results: Order-of-Magnitude Prefill Acceleration

    In public technical discussions, this class of methods achieves roughly 22x speedup on the prefill stage of a single attention layer at the 10-million-token scale, with speedups reaching tens of times on another model architecture, while maintaining accuracy close to the original model across a range of tasks. The gains come from:

    • Index complexity dropping from quadratic to near-linear;
    • Memory access growing with the number of candidate blocks rather than the full history;
    • Significantly lower prefill latency, improving first-response experience on long documents.

    Note that the magnitude of the speedup depends heavily on model architecture, block size, and the number of candidate blocks. Engineering teams should determine the right configuration through small-scale benchmarks rather than copying numbers directly.

    5. Engineering Implementation Notes

    1. Block size trade-off: blocks that are too small reduce the indexing gain, while blocks that are too large inflate the fine-grained selection space; sweeping the 128 to 512 range is a reasonable starting point.
    2. Latent dimensionality: too low loses semantic separability, too high weakens the compression gain, so tune it jointly with retrieval accuracy.
    3. Coordination with linear attention: in a hybrid architecture, use latent-space sparse retrieval for global layers and keep the linear-complexity path for local layers.
    4. Caching strategy: block-level latent representations can be updated incrementally as the session grows, avoiding repeated compression.
    5. Observability: instrument index time and attention time separately in the monitoring and alerting stack to locate where the bottleneck has migrated.

    6. Evaluation: Don’t Look at Average Accuracy Alone

    A three-layer evaluation is recommended:

    • Task accuracy: long-document QA, multi-hop retrieval, code base comprehension, and similar tasks.
    • System metrics: prefill latency, time-to-first-token, peak memory, throughput.
    • Stability: whether the performance curve stays smooth across different context lengths, and whether there is a cliff at a particular length.

    7. Conclusion

    The competition at tens of millions of tokens has shifted from whether sparsity is possible to whether sparsity can be located cheaply. Latent-space retrieval moves indexing from raw token space into a compressed representation space, and is a representative solution to that shift. For teams that need to balance cost and experience in long-context scenarios, reworking the indexing path often has more leverage than continuing to optimize the attention operators.