Sparse Attention in Latent Space: How Indexing Speeds Up 10M-Token Contexts

    As context windows grow from millions to tens of millions of tokens, the bottleneck of sparse attention shifts from computing attention to finding which tokens matter. This article breaks down the cost structure of long-context prefilling, explains how latent-space sparse attention moves candidate selection into a compressed representation to achieve near-linear indexing, and covers why the two-stage design preserves accuracy, along with deployment and evaluation practices for inference services.

    1. The Problem: When Contexts Get Longer, the Bill Moves

    Over the past two years, long context has shifted from an optional capability to a default specification. Million-token context is now standard in many general-purpose model services, while 10-million-token context is entering validation on research platforms and some cloud inference services. One key technology behind this shift lets each query avoid scanning the entire history and instead select only a few of the most relevant segments for computation.

    But as lengths grow by another order of magnitude, the cost structure shifts. Take a global attention layer as an example: at 10 million tokens, prefilling spends most of its time not on the attention matrix multiplications, but on the indexing step that decides which tokens to look at. In other words, sparse attention solved “computing too much” but not “searching too expensively.”

    2. Cost Breakdown: The Quadratic Overhead Has Not Disappeared

    The cost of standard full attention is easy to understand: with sequence length T, prefilling costs roughly O(T²), and each decoding step costs roughly O(T). Going from one million to ten million tokens increases attention computation by about 100x.

    The common approach to sparse attention is “select first, compute later”: a lightweight indexer scores each query, picks the k most relevant keys, and computes attention only over those k keys, reducing the attention itself to O(Tk). This approach rests on a repeatedly validated premise: in sufficiently long contexts, the keys actually used by the model make up only a tiny fraction, and keeping a few hundred of the most relevant keys maintains accuracy close to full attention.

    The problem is that the indexer still needs to evaluate relevance for every key to decide “who is most relevant.” If that evaluation happens in the original token dimension, its complexity still carries an O(T²) shadow. Common mitigations in the industry include lowering scoring precision, compressing dimensions, and block aggregation, but these approaches often trade off between “cheaper” and “more accurate.”

    3. The Core Idea: Move “Selection” into Latent Space

    An approach called latent-space sparse attention proposes moving candidate filtering from the original token space to a compressed latent representation space. Its basic logic can be summarized in three steps:

    1. Compress: Map historical keys and values into lower-dimensional, more compact latent representations, significantly reducing the cost of a single relevance evaluation.
    2. Coarse filtering: Perform candidate scoring and ranking in latent space, selecting a small number of candidate blocks or positions.
    3. Fine computation: Execute attention only on the selected original keys, ensuring numerical precision is not polluted by the compression process.

    This division of labor—coarse filtering in latent space, fine computation in the original space—makes the indexing stage grow nearly linearly, while the attention itself remains O(Tk). The overall shift from “quadratic-dominated” to “near-linear-dominated” is the most critical property change at the 10-million-token scale.

    Illustrative pseudocode:

    1. # z: latent representations of historical keys, shape [T, d_latent], d_latent << d_head
    2. # q: current query, shape [d_head]
    3. # k, v: original keys and values, shape [T, d_head]
    4. scores = z @ proj(q) # latent-space coarse filtering, cost ~ O(T * d_latent)
    5. idx = topk(scores, k) # select k candidates
    6. attn = softmax(q @ k[idx].T) # fine computation on candidates only, cost ~ O(k * d_head)
    7. out = attn @ v[idx]

    4. Complexity and Gains: Why It Can Be an Order of Magnitude Faster

    Writing out the two-stage costs makes the source of gains clear:

    • Traditional sparse indexing: O(T² · c_index), where c_index is the per-score constant;
    • Latent-space indexing: O(T · d_latent) + O(Tk), approximately linear when d_latent is much smaller than T;
    • At 10 million tokens, the gap between the linear and quadratic terms is amplified to an order of magnitude.

    Published research points to the same conclusion: in 10-million-token prefilling scenarios, a single attention layer can be sped up by tens of times, with clearly different gains across model sizes—the larger the model and the more expensive the original indexing, the higher the relative gain. In engineering terms, this means the long-context inference bottleneck shifts from “compute” to “memory access and scheduling,” and optimization focus should shift accordingly.

    5. Why Accuracy Holds: The Robustness of the Two-Stage Design

    The biggest concern with compression is that information loss leads to selecting the wrong candidates. The two-stage design isolates the risk by “recall only in latent space, fine computation in original space”: even if coarse filtering has minor errors, as long as the target key enters the candidate set, the fine computation stage still produces numerical results consistent with full attention. This is more robust than “computing attention directly on the compressed representation.”

    Several common protection strategies can be layered on in engineering:

    • Keep a small number of global anchor keys to avoid missing long-range information;
    • Add moderate redundancy to the candidate set (k slightly larger than the theoretical minimum);
    • Mix scheduling of local windows and global candidates to balance nearby and long-distance dependencies.

    6. Deployment: From Paper to Inference Service

    To evaluate this type of approach in your own inference pipeline, focus on the following:

    1. The degree of fusion between the indexer and the attention kernel, which determines whether memory access becomes the new bottleneck;
    2. Caching strategy for latent representations, optimized separately for prefilling and decoding;
    3. Joint tuning of block size and candidate redundancy, which directly affects the throughput-accuracy balance;
    4. Compatibility with common inference optimizations such as paged caching and continuous batching.

    For cloud implementation, you can rely on container platforms to host inference services, object storage to save model and cache snapshots, and monitoring and alerting to track latency distributions of long-context requests. These common capabilities are enough to support an observable, rollback-capable canary release process.

    7. Evaluation: Don’t Just Look at Average Scores

    Long-context capability evaluation is prone to “average scores masking the long tail.” At minimum, cover three types of metrics:

    • Retrieval: locating sparse facts within long text;
    • Aggregation: statistics and summarization across multiple paragraphs;
    • Reasoning: tasks requiring multi-hop dependencies.

    Also record latency curves across different length tiers and watch for inflection points after length crosses a certain threshold. The value of latent-space sparse attention is precisely in significantly pushing this inflection point back.

    8. Conclusion

    The core tension of 10-million-token context has shifted from “can’t finish computing” to “can’t find it.” Moving the selection process into latent space and replacing quadratic scoring with near-linear indexing is a currently validated technical path. For developers, what truly needs to be mastered is not a specific implementation, but the design paradigm of “separating coarse filtering from fine computation” and the evaluation and tuning system built around it.