The Key to 22x Faster Sparse Attention at 10 Million Token Contexts

    As context windows scale from millions to tens of millions of tokens, the cost structure of long-context inference flips: the real bottleneck is no longer attention itself, but the step that decides which tokens to look at. This article breaks down the core idea of Latent Sparse Attention (LSA) from a general technical perspective—moving candidate token retrieval from the original token space into a low-dimensional latent space, reducing the selection overhead that grows quadratically with sequence length to near-linear. It includes a complexity comparison, illustrative pseudocode, an analysis of why accuracy is preserved, and engineering guidance on chunked prefill, cache tiering, and batch scheduling, so developers can achieve both throughput and accuracy in ultra-long context scenarios.

    1. Background: Million-Token Contexts Are Already Standard, and 10 Million Is Coming

    Over the past year, long context has shifted from a selling point to a default specification. Mainstream large model services now offer million-token context as standard, and the capability boundaries of both open-source and closed-source models continue to expand. For application developers, this means feeding an entire codebase, a full manual, a year of logs, or the transcript of a long video in a single request has moved from a demo to a production requirement.

    But once length increases tenfold, the numbers no longer scale linearly. A clear “superlinear inflection point” appears. Understanding that inflection point is the prerequisite for understanding the method in this article.

    2. The Cost Structure Flips: 98% of the Time Goes to “Selecting Tokens,” Not “Computing Attention”

    Consider a typical global attention layer in a hybrid architecture. When performing prefill at the scale of 10 million tokens, most of the time is not spent on attention matrix multiplications. It is spent on the indexer—the scoring and selection logic that picks the most relevant historical tokens for each query.

    The reason is straightforward. Let the sequence length be n:

    • Attention computation itself: complexity is roughly O(n²·d), but it is a highly parallel dense matrix multiplication with very high hardware utilization;
    • Sparse selection: the naive approach scores each query against all historical keys for relevance, then takes the top-k. The complexity is also on the order of O(n²), but it consists of many small-granularity, memory-access-intensive operations that are hard to parallelize fully.

    When n grows from 10⁵ to 10⁷, dense matrix multiplication can rely on compute power and parallelism to push through, while the selection stage is constrained by memory bandwidth and scheduling overhead and deteriorates almost linearly. The nature of the problem changes: sparse attention has long proven that “looking at only a small fraction” is enough to preserve accuracy, but what truly blocks ultra-long context is that “finding that small fraction” still grows quadratically with length.

    3. The Core Idea: Move Retrieval into Latent Space

    The key abstraction of Latent Sparse Attention (LSA) is this: do not define the candidate set over the original token sequence. Instead, first compress the key/value into a low-dimensional latent space, then perform similarity retrieval and aggregation in that latent space.

    It can be broken into three steps:

    1. Dimensionality reduction projection: use a learnable linear mapping to compress the key at each position into dimension d’ (d’ ≪ d), producing a compact latent representation;
    2. Latent-space scoring: project the query into the same space and take a dot product with the latent representations to obtain candidate scores;
    3. Sparse aggregation: take the top-k latent slots and perform attention-weighted summation only at those positions.

    Illustrative pseudocode:

    1. # Prefill stage: build the latent-space index in one pass
    2. Z = W_k @ K # [n, d'] low-dimensional latent representation
    3. q_lat = W_q @ Q # [n, d']
    4. # Process in chunks to avoid materializing the n x n score matrix at once
    5. for q_block in chunks(q_lat, size=B):
    6. scores = q_block @ Z.T # [B, n] low-dimensional dot product
    7. idx = topk(scores, k) # keep only k candidates per query
    8. out[q_block] = attn(q_block, K[idx], V[idx])

    In terms of complexity, latent-space scoring drops from O(n²·d) to O(n²·d’), where d’ is typically only a few hundredths of the original dimension. Combined with chunking and approximate nearest neighbor retrieval, the overall selection overhead can be reduced to a near-linear order of roughly O(n·k·d’). This is the source of the order-of-magnitude speedup.

    4. Why Accuracy Does Not Drop: Three Reasons

    1. The information bottleneck acts as regularization. The low-dimensional projection forces the model to compress “relevance” into the most essential subspace, filtering out redundant noise dimensions;
    2. The semantics of top-k are unchanged. It is still the case that “a few most relevant positions dominate the output”; only the coordinate system used for candidate scoring becomes cheaper;
    3. The projection matrices are learnable. W_k and W_q are trained jointly with the backbone, so the latent space spontaneously aligns to the similarity structure the task actually cares about, rather than a manually specified heuristic.

    It is worth emphasizing that this is an abstract description at the general method level. In concrete implementations, the chunk size, the value of k, and the projection dimension must be tuned empirically against the specific model and hardware; there is no one-size-fits-all fixed configuration.

    5. Engineering Implementation Points

    • Chunked prefill: split the sequence into chunks, build the latent index chunk by chunk, and aggregate in a streaming fashion, avoiding peak memory being blown up by the n×n intermediate matrix;
    • Cache tiering: maintain a two-level cache for latent representations and the original KV, keeping hot data resident in GPU memory and pushing cold data down to object storage or a distributed cache, with monitoring and alerting to pinpoint bandwidth bottlenecks;
    • Batch scheduling: when mixing requests of different lengths in a batch, bucket them by length to reduce padding waste;
    • Accuracy regression: before launch, run A/B tests on tasks such as long-document QA, cross-section retrieval, and codebase completion to confirm that top-k truncation has not hurt critical recall.

    6. Applicability Boundaries and Selection Guidance

    Latent Sparse Attention is not a universal solution. When the context is only a few thousand tokens, dense attention is simpler and more efficient. The gains are concentrated in the prefill and long-sequence inference range from hundreds of thousands to tens of millions of tokens. If a workload is dominated by short requests, prioritize optimizing batching and caching. Only when the cost of “selecting tokens” begins to outweigh “computing attention” is this method truly worth the investment. For teams that need to balance throughput and accuracy under ultra-long contexts, the recommendation is to first validate recall loss on an offline evaluation set, then roll out gradually, continuously calibrating k and the projection dimension with observable metrics.