Sparse Attention at 10M Token Context: A 22x Prefill Speedup with Latent Retrieval

    Long-context models have made 1M token windows a standard feature, but pushing to 10M tokens exposes a new bottleneck: the selection step that decides which tokens to attend to now dominates prefill time. This article breaks a global attention layer's prefill into scoring and indexing, KV gathering, and attention computation, and explains why the quadratic indexing cost overwhelms the rest at 10M scale. It then introduces Latent Sparse Attention (LSA), which moves retrieval from the original token space into a compressed latent space, combined with hierarchical indexing and asynchronous prefetching. You will find the design details, sample pseudocode, reported speedups of roughly 22x and up to about 72x, why approximate retrieval preserves accuracy, and a practical engineering checklist covering memory budget, KV cache alignment, dynamic k, and observability.

    Sparse Attention at 10M Token Context: A 22x Prefill Speedup with Latent Retrieval

    1. 1M Context Is Now Standard; 10M Is the Real Dividing Line

    Over the past two years, long context has shifted from a selling point to a default specification. Context windows at the 1M level are already standard in several mainstream model services, and architectures that mix sparse attention with linear attention have shipped in flagship models. The industry has reached a consensus: instead of having every query scan the entire token history, selecting only a few of the most relevant segments to participate in the computation is a path that has been repeatedly validated at the 1M scale.

    But once the context grows to 10 million tokens, an overhead that once looked negligible starts to spiral out of control. Take one global attention layer in a model that supports million-level context. Running the prefill stage at 10M tokens, the time actually spent on the attention matrix computation accounts for only about 2%; the remaining 98% is consumed by the indexer that “decides which tokens to look at.”

    This means the nature of the problem has changed. Sparse attention has long proven that “looking at only a small portion” is enough to preserve accuracy. What actually blocks ultra-long context is that “finding that small portion” still grows quadratically with context length.

    2. Where the Quadratic Bottleneck Sits: Prefill in Three Stages

    To see where the speedup potential lies, break the prefill of a single global attention layer into three stages:

    1. Scoring and indexing (selection): finding the Top-k candidates for each query across the entire token history; a naive implementation needs O(N²) similarity computations.
    2. Candidate gathers and KV movement: pulling the selected K/V from high-bandwidth on-chip cache or remote storage to the compute units.
    3. The attention computation itself: weighted aggregation over only the k candidates, with complexity O(N·k).

    At the 1M scale, the third stage still takes the largest share. At the 10M scale, the N² term in the first stage completely overwhelms the other two. Common engineering optimizations (block-wise scoring, quantized indexing, cache reuse) can only shrink the constant factor; they cannot change the order of magnitude.

    The breakthrough point of Latent Sparse Attention (LSA) is straightforward: do not retrieve in the original token space; move retrieval into a compressed latent space.

    3. Three Key Designs of LSA

    1. Latent Compression: Squeezing N Tokens into M “Summary Slots”

    A lightweight encoder compresses each block of consecutive tokens into a low-dimensional latent vector, forming M summary slots (with M far smaller than N, for example one slot per 256 tokens). The query is projected into the same latent space, and retrieval happens only over the M slots, reducing complexity from O(N²) to O(M·N) or even O(M²). When M/N is on the order of one percent, the indexing overhead drops by three to four orders of magnitude.

    Illustrative pseudocode:

    1. # Prefill stage: build the latent index
    2. slots = latent_encoder(kv_blocks) # [M, d_latent]
    3. index = build_ann_index(slots) # approximate nearest neighbor index
    4. # Retrieval for each query
    5. q_latent = project(query) # [d_latent]
    6. top_slots = index.search(q_latent, k=K) # search only over M slots
    7. kv = gather(kv_blocks, top_slots) # pull back the original K/V
    8. out = sparse_attention(query, kv)

    2. Hierarchical Indexing: Coarse-Filter Slots, Fine-Filter Tokens

    After latent retrieval returns candidate slots, a fine-grained score is computed inside each slot, forming a two-level coarse-to-fine funnel. The first level shrinks the candidate range by roughly a hundredfold; the second performs an exact Top-k only within a very small range, balancing recall and precision.

    3. Asynchronous Prefetch: Overlapping Movement with Computation

    Index results are usually determined several layers ahead, so K/V movement can be built into an asynchronous prefetch pipeline. This overlaps the third-stage attention computation with the second and first stages, further hiding memory access latency. This step does not change the complexity, but it can significantly improve measured wall-clock time.

    4. Results and Accuracy: Why Approximate Retrieval Does Not Hurt Precision

    Public results show that at the 10M token scale, this class of methods achieves roughly a 22x speedup on the prefill of a single global attention layer, and up to about 72x on models where the indexing overhead takes an even larger share; at the same time, accuracy stays close to the original model across a range of long-context tasks.

    There are three reasons accuracy does not collapse:

    • Retrieval is approximate but recall is controllable: the coarse-filter stage of the hierarchical funnel uses low-dimensional latent vectors, and the probability of false recall is tunable via the dimensionality and the number of slots.
    • Only “who to look at” changes, not “how to compute”: the selected tokens still go through the original attention operator, so the numerical path is unchanged.
    • Training/fine-tuning alignment: aligning the indexer with a small number of long samples can make the query’s latent projection match the distribution of the summary slots.

    5. Engineering Checklist

    • Memory and bandwidth budget: the summary slots and index structures stay resident in memory, so reserve space for M×d_latent; K/V movement should use a high-bandwidth channel.
    • Coordination with paged/blocked KV cache: slot boundaries should align with KV block boundaries to avoid cross-block gathers.
    • Dynamic k strategy: use a small k for short contexts to preserve precision, and moderately increase k for long contexts to trade for recall.
    • Observability: monitor the index hit rate, coarse-filter recall, and the share of prefill time spent in each stage to locate where the bottleneck moves.
    • Cloud deployment shape: inference services can be hosted on a container platform, long samples stored in object storage, and a logging service plus monitoring and alerting used to track hit rate and latency percentiles.

    6. Conclusion

    For contexts of tens of millions of tokens, the core tension has shifted from “can we compute it” to “can we find it.” Moving retrieval from the original token space into a latent space, controlling recall with a hierarchical funnel, and hiding memory access with asynchronous prefetch is the key path to driving down quadratic indexing overhead without sacrificing accuracy. For teams planning ultra-long context capabilities, the recommendation is to first profile indexing time in the 1M–10M range, then decide whether to adopt latent retrieval and hierarchical indexing.