Sparse Attention at 10M Token Context: The Key to a 22x Speedup Is Finding the Right Tokens

    As context windows grow from 1M toward 10M tokens, sparse attention solves how much each query looks at, but not how it finds the relevant tokens. At this scale, the indexing stage of score, filter, and locate becomes the dominant cost, growing quadratically even when the attention budget stays fixed. This article explains Latent Sparse Attention, which moves retrieval into a low-dimensional latent space using latent keys, coarse-grained block indexing, and a fixed-budget refinement step. It covers why accuracy is preserved through representation alignment, recall fallbacks, and train-inference consistency; how to amortize the quadratic cost with precomputation, caching, sharding, and tiered offloading; and how to evaluate efficiency, recall, accuracy, and stability rather than relying on a single average speedup number.

    1. The Problem: Sparse Attention Solves “How Much to Read,” Not “How to Find It”

    As context expands from 1M to 10M tokens, a common design in mainstream long-context models is to stop having every query scan the full history. Instead, each query selects a small set of the most relevant tokens to participate in the computation. At the 1M scale, this approach has been validated repeatedly, with controllable accuracy loss.

    Push the length up by another order of magnitude, however, and the cost structure shifts. Take a global attention layer doing prefill over 10M tokens: most of the time is not spent on the attention matrix multiplications, but on the indexing work of scoring, filtering, and locating. The reason is straightforward:

    • Attention compute grows quadratically with length, but sparsification caps it at a fixed budget, flattening the actual compute footprint.
    • The indexing stage still performs similarity evaluation and top-k selection over the full candidate set, so its cost keeps inflating quadratically with length.
    • When the sparse budget is far smaller than the context length, indexing goes from a supporting role to the main event.

    So what actually blocks ultra-long context is not the conclusion “only look at a small part,” but the process of “finding that small part.”

    2. The Core Idea: Move Retrieval into Latent Space

    The key abstraction of Latent Sparse Attention is this: do not perform “find relevant tokens” in the original token dimension. Instead, first construct a low-dimensional, reusable latent representation, then do candidate filtering inside it.

    It has three components:

    1. Latent space projection. Generate low-dimensional keys (latent keys) for history tokens, with a dimension far smaller than the original head dimension, sharply reducing the constant factor of similarity evaluation.
    2. Coarse-grained block indexing. Aggregate latent keys into blocks (for example, mean pooling or a learnable compression) to form a multi-level index structure: filter at the block level first, then rank precisely within blocks, avoiding a token-by-token scan.
    3. Fixed-budget refinement. Run full attention only on the few selected blocks or tokens, keeping compute from growing linearly with length.

    A schematic flow (pseudocode, structure only):

    1. # Preprocessing stage: cacheable and reusable
    2. latent_keys = project_to_latent(history_keys) # [N, d_lat], d_lat << d_head
    3. block_index = pool_blocks(latent_keys, block=256) # [N/256, d_lat]
    4. # Per query step
    5. scores = block_index @ latent_query # Block-level coarse filtering, very cheap
    6. blocks = topk(scores, k_blocks)
    7. cand = gather_blocks(history_keys, blocks) # Fetch candidate blocks only
    8. idx = topk(cand @ query, k_tokens) # Fine ranking within blocks
    9. out = attention(query, history_values[idx]) # Fixed-budget refinement

    The gains come from two places. First, low-dimensional dot products replace high-dimensional ones, lowering the constant factor. Second, hierarchical filtering turns “compare against everything” into “a few comparisons plus local comparisons,” so the asymptotic complexity is significantly better than the original quadratic scan.

    3. Why Accuracy Holds Up

    Sparse attention works because the attention distribution is itself highly concentrated: a few tokens carry most of the weight. Latent space retrieval does not change that fact; it only changes where scoring happens. To preserve accuracy, three engineering points matter:

    • Representation alignment. Latent keys and original keys must be aligned in the same semantic space, typically via a shared projection or a distillation-style training objective, so that “retrieved as relevant” does not diverge from “considered relevant by the model.”
    • Recall fallback. For tasks with strong locality, such as code or structured documents, add a fixed window or neighboring blocks to prevent long-tail critical information from being missed by top-k.
    • Train-inference consistency. If inference uses a latent space index, training should adapt the attention distribution to that index; otherwise distribution drift appears.

    4. Engineering: Shift the Quadratic Cost Forward and Amortize It

    The biggest engineering value of a latent space index is turning the expensive part from “repeated every step” into “built once, reused many times”:

    • Precomputation and caching. Latent keys and block indexes for history tokens can be generated once during prefill and cached, then reused directly during decode, avoiding repeated scans.
    • Blocking and parallelism. Block-level indexes are naturally suited to parallel reduction and sharding, making horizontal scaling on a container platform straightforward; block size is the tradeoff parameter between latency and recall.
    • Memory and bandwidth. Low-dimensional latent keys substantially compress the resident index memory, and tiered offloading (hot blocks resident, cold blocks fetched on demand) can further reduce bandwidth pressure.
    • Observability. It is worth building monitoring and alerting for candidate recall rate, block hit rate, and indexing time share, using a logging service and metrics dashboards to locate bottlenecks rather than looking only at end-to-end latency.

    A practical tuning order: fix the token budget first and adjust block size until recall meets the target; then reduce the latent dimension to compress the index; finally evaluate whether a multi-level index is needed.

    5. Evaluation: Do Not Look Only at the Average Speedup

    To evaluate this kind of approach, cover at least four dimensions:

    1. Efficiency: report prefill and decode latency separately, the indexing time share, and the slope of the curve as length grows.
    2. Recall: using full attention as the reference, measure top-k hit rate and coverage of key tokens.
    3. Accuracy: long-document QA, multi-hop retrieval, code completion, and structured extraction, with attention on the length extrapolation range.
    4. Stability: sensitivity across different block sizes and latent dimensions, avoiding a situation where “the average looks great but the long tail breaks.”

    It is worth stressing that public claims of “tens of times speedup in a single attention layer” are usually intra-layer metrics; end-to-end gains are diluted by other layers and I/O, so re-measure against your own business distribution before deploying.

    6. Conclusion

    In the 10M token regime, the bottleneck has shifted from “can we compute it” to “can we find it.” The value of Latent Sparse Attention is that low-dimensional representations and hierarchical indexing convert retrieval cost from quadratic inflation into a fixed overhead that is cacheable, parallelizable, and amortizable. For teams doing retrieval augmentation, code base understanding, or long-document analysis over ultra-long contexts, this is an optimization path worth validating first; on Baidu AI Cloud, it can be stress-tested and rolled out using general capabilities such as the container platform, object storage, and monitoring and alerting.