Sparse Attention at 10 Million Token Context: 22x Faster Prefill with Latent-Space Retrieval

    As context windows grow from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computing attention scores to finding which tokens to attend to. This article breaks down why the indexer becomes the new quadratic cost, how Latent Sparse Attention moves retrieval into a compressed latent space to cut that cost to near-linear, and what the reported 22x+ prefill speedup does and does not mean. It also covers the boundaries of the approach and a practical rollout path for engineering teams.

    1. Background: The Bottleneck Shifts Once Context Length Crosses a Threshold

    Over the past two years, long context has moved from an optional feature to a default specification. Million-token context is becoming a standard configuration for many model services, and hybrid attention architectures, which interleave global attention layers with linear or local attention layers, are widely adopted to balance accuracy and throughput.

    The shared assumption behind this design is that a query vector does not need to scan the entire token history; it only needs to select a small set of the most relevant tokens for computation. At the million-token scale, this assumption has been validated repeatedly. But as context pushes toward ten million tokens, an initially unremarkable cost starts to spiral.

    Take a single global attention layer. During prefill at the ten-million-token scale, most of the time is not spent on the matrix multiplications of attention itself, but on the indexing step that decides which tokens to look at. In other words, the system’s time profile shifts from “slow to compute” to “slow to find.”

    The nature of the problem therefore changes. 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 subset still grows quadratically with context length.

    2. Complexity View: Why the Indexer Becomes the New Bottleneck

    A simplified complexity breakdown makes this clear:

    1. Standard attention: O(N^2 * d)
    2. Sparse attention (naive): compute O(N * k * d) + index O(N^2 * d_s)
    3. where N is context length, k is the number of tokens kept per query, d is the head dimension

    As N grows from 10^6 to 10^7, the compute term grows only linearly because k is fixed, while the index term still expands quadratically. On top of that, indexing typically involves a large amount of irregular memory access, sorting, or top-k selection. These operations are far less efficient on hardware than dense matrix multiplication, so their actual share of runtime is significantly higher than their share of theoretical operation counts.

    The conclusion is clear: to make ten-million-token context genuinely usable, the optimization focus must shift from “compute less” to “find what to compute cheaply.”

    3. Core Idea: Move Retrieval into Latent Space

    A class of methods known as Latent Sparse Attention offers a new answer. The key insight is that expensive similarity matching does not have to happen in the original token space. Instead, you can first build a low-dimensional, compressed latent representation, perform candidate selection in that latent space, and then map back to the original tokens to run sparse attention.

    The flow breaks into four steps:

    1. Compression: project the key vectors of historical tokens into a low-dimensional latent space, forming a compact “index view” with lower memory footprint and bandwidth demand.
    2. Latent-space matching: compute similarity between the query vector and the compressed representations in the same latent space to select a candidate set.
    3. Threshold and size control: constrain the retrieval range per query with a configurable candidate count or similarity threshold, keeping overhead bounded and predictable.
    4. Map-back computation: run attention aggregation over the small set of selected tokens only in the original precision space.

    Illustrative pseudocode:

    1. # Prefill: build the latent-space index first, then run sparse aggregation
    2. latent = project_to_latent(keys) # [N, d_latent], d_latent << d
    3. scores = query_latent @ latent.T # [Nq, N], low-dimensional matching
    4. idx = topk(scores, k) # candidate token indices
    5. out = sparse_attention(query, keys, values, idx)

    There are three reasons this delivers one to two orders of magnitude of speedup: low-dimensional matching sharply reduces the arithmetic intensity and memory traffic of the indexing stage; a fixed candidate size turns indexing cost from quadratic into near-linear; and irregular memory access is confined to a much smaller candidate set, which noticeably improves hardware utilization.

    4. Results and Boundaries: Where the Gains Come From and Where They Stop

    In public descriptions, this class of methods achieves more than 20x speedup for prefill of a single global attention layer at the ten-million-token scale, with even higher speedup ratios under some model architectures, while maintaining accuracy close to the original model across a range of long-context tasks. It is worth stressing that these figures are layer-level results under specific models, specific layer types, and specific hardware conditions, and cannot be equated directly with end-to-end throughput gains.

    Because the gains come mainly from compressing the indexing stage, the applicability boundaries are also clear:

    • The longer the context and the higher the share of global attention layers, the more pronounced the benefit.
    • In short-context scenarios, the fixed overhead of compression and index construction may offset the gains.
    • Retrieval quality depends on the expressive power of the latent space; too low a compression dimension leads to accuracy loss.
    • During decoding, each step processes a single query, so the benefit shows up mainly in prefill and long-sequence batching.

    5. Engineering Recommendations

    To validate a similar approach in your own system, proceed in this order:

    1. Start with time attribution. Use logging services and monitoring alerts to profile the prefill stage in segments and confirm the indexer’s share, so you do not optimize in the wrong direction.
    2. Design the index by layer. Keep local window layers dense or linear, introduce latent-space retrieval in global layers, and evaluate accuracy sensitivity layer by layer.
    3. Parameterize candidate size. Make candidate count, compression dimension, and threshold configuration items so you can trade off under different workloads.
    4. Make accuracy regression routine. Build a regression set from retrieval-style question answering, long-document summarization, and multi-hop reasoning tasks, and watch for degradation caused by compression.
    5. Coordinate on the resource side. Combine elastic scaling on a container platform with object storage for long-text corpora, and turn memory savings into concurrency capacity.

    6. Summary

    The core tension of ten-million-token context has shifted from “too slow to compute” to “too slow to find.” The value of Latent Sparse Attention is that it compresses retrieval cost from quadratic to near-linear, keeping the sparse approach viable at longer sequence lengths. For developers, the real point is not to copy a particular speedup number, but to first quantify the indexer’s share in your own workload, then introduce latent-space retrieval incrementally in a configurable and regression-tested way, and ultimately reach a sustainable balance among accuracy, memory, and throughput.