Ten-Million-Token Context: How Latent Sparse Attention Speeds Up Prefill

    This article explains why sparse attention still carries a hidden indexing cost at extreme context lengths, and how Latent Sparse Attention moves relevance scoring into a low-dimensional latent space. It covers the cost breakdown, core method with pseudocode, engineering considerations, and reported speedups and evaluation practices for 10M-token contexts.

    1. The New Bottleneck After Long Context Became Standard

    Over the past year, million-token context windows have become commonplace. Most mainstream model services now treat 1M context as the default specification, and some newer architectures combine sparse attention with linear attention. The industry consensus is straightforward: instead of having every query scan all historical tokens, each query should select a small set of the most relevant keys.

    This approach has been validated repeatedly at the 1M scale. But as context grows toward 10 million tokens, an initially negligible overhead starts to spiral: during prefill at extreme context lengths, a global attention layer spends roughly 98% of its time not on attention computation itself, but on the indexer that decides which tokens to look at.

    The nature of the problem has changed. Sparse attention has already shown that attending to a small subset is enough to preserve accuracy. What actually blocks extreme-length context is that the cost of finding that subset still grows quadratically with context length.

    2. Cost Breakdown: Why Sparse Attention Still Has a Hidden Bill

    In standard full attention, every query computes relevance against all previous keys. With context length T, prefill cost is O(T²) and each decoded token costs O(T). Going from 1M to 10M tokens increases attention computation by roughly 100x.

    Sparse attention splits the work: a lightweight indexer first selects the k most relevant keys for each query, then the actual attention runs only over those k keys, reducing attention cost to O(Tk). This holds only if the model genuinely needs very few keys. Related validation shows that at 128k tokens, letting each query attend to only the 512 truly most relevant keys (about 0.8% of all keys) still retains roughly 98% of full-attention accuracy on long-context benchmarks.

    The paradox: to decide which 512 keys are most relevant, the indexer must score every key. If scoring is of the same order as full attention, then O(T²) has simply moved from attention to the indexer, and total complexity has not dropped. That is the hidden bill at extreme context lengths.

    3. Core Idea: Move Relevance Scoring Into Latent Space

    The key idea behind Latent Sparse Attention is to move scoring from the original high-dimensional representations into a lower-dimensional latent representation:

    • Use low-rank projections to compress query/key into dimension d (d much smaller than the head dimension), and compute similarity in latent space;
    • Scoring complexity drops from O(T²·D) to O(T²·d), and combined with chunking, quantized indexing, and approximate retrieval, the overall cost approaches O(T·k) or near-linear;
    • After selecting top-k, return to the original representation for exact attention, preserving numerical stability and accuracy.

    Illustrative pseudocode:

    1. # Low-dimensional scoring in latent space, exact attention in the original space
    2. q_lat = proj_q(q) # [T, d]
    3. k_lat = proj_k(k) # [T, d]
    4. scores = q_lat @ k_lat.T # low-dimensional similarity, much cheaper
    5. idx = topk(scores, k) # each query selects k keys
    6. out = exact_attention(q, k, v, idx) # computed only on selected keys

    4. Engineering Implementation Notes

    • Chunking and streaming: split the context into chunks, build and query the latent-space index per chunk to reduce peak GPU memory;
    • Choosing the low-rank dimension: too small loses accuracy, too large yields insufficient gains; tune on a validation set;
    • Hybrid with linear attention: use sparse indexing for global layers and linear/sliding-window for local layers, covering both long-range and local patterns;
    • Decode-stage optimization: build the latent-space index during prefill, then perform only incremental top-k during decode to avoid repeated scoring;
    • Accuracy safeguards: keep a small number of “anchor” tokens forcibly visible so critical information is not missed.

    5. Results and Evaluation

    At 10M tokens, this method speeds up prefill for a single attention layer by about 22.7x (one open-source model) to 72.4x (another model), while maintaining accuracy close to the original model across multiple long-context tasks. Recommended evaluation coverage: long-document retrieval, multi-hop question answering, and repository-level code understanding, reporting accuracy alongside throughput and memory curves.

    6. Conclusion

    The real bottleneck in extreme-length context has shifted from computing attention to finding attention. By combining low-dimensional scoring with exact recomputation, Latent Sparse Attention compresses quadratic indexing cost to near-linear, offering a practical engineering path for ten-million-token context. If you deploy this kind of capability on a cloud platform, you can combine general capabilities such as container platforms, object storage, and monitoring and alerting to build elastic scaling and observability.