Sparse Attention at 10 Million Token Contexts: Why a 22x Speedup Comes Down to Fixing the Indexer

    As models move from million-token to ten-million-token contexts, the bottleneck in sparse attention shifts from computing attention to finding which tokens to attend to. This article breaks down the three cost stages of sparse attention—scoring, selection, and aggregation—explains why the indexer can consume up to 98% of runtime, and introduces Latent Sparse Attention: compressing key representations into a low-dimensional latent space, running a coarse-to-fine two-stage retrieval, and splitting work between global and linear attention layers. It also covers accuracy safeguards, evaluation tasks that expose missed retrievals, engineering rollout steps, and the context-length thresholds where this approach pays off.

    Over the past two years, mainstream large models have turned million-token context windows from an experimental feature into a default specification. Hybrid attention architectures—stacking global attention layers alternately with linear attention layers—have become a common design: global layers handle precise lookback, while linear layers carry long-range information at low cost. This approach has been validated repeatedly at the million-token scale. The problems appear once you push it another order of magnitude.

    Imagine a single global attention layer performing prefill over ten million tokens. Intuitively, sparsification should make the computation trivial. In practice, the opposite happens: most of the time is not spent on the attention matrix multiplications at all, but on the indexing step that decides which tokens to look at. The reason is straightforward—the candidate set grows linearly with context length, and scoring and filtering must happen over that candidate set, so the overall complexity remains quadratic.

    This changes the nature of the problem. Sparse attention has already proven that attending to a small subset is enough to preserve accuracy. What actually blocks extreme context lengths is that finding that small subset has not become any cheaper.

    2. Breaking Down the Bottleneck: Why the Indexer Eats All the Time

    A single sparse attention call can be split into three cost components:

    • Scoring: estimating the relevance of every historical position for each query.
    • Selection: picking the top-k from the scores, which involves sorting or partial sorting.
    • Aggregation: computing the attention-weighted sum over only the selected k positions.

    Sparsification compresses only the third stage. The first stage is inherently the number of queries multiplied by the context length. The second adds the extra constants and memory-access overhead of sorting. At ten million tokens, the first two stages together dominate absolutely—this is where the “98% of time spent in the indexer” figure comes from.

    Memory access makes things worse. Scoring requires reading the full key-side representations, so bandwidth pressure grows linearly with length. Meanwhile, top-k selection in the second stage suffers from insufficient parallelism under small batches and long sequences, easily creating pipeline bubbles that leave the accelerator idle.

    3. The Core Idea: Move the Search into Latent Space

    A class of methods recently proposed by a startup research team in collaboration with universities can be collectively called Latent Sparse Attention. The starting point is simple: if retrieving over the full token representations is too expensive, don’t retrieve over the full representations.

    It involves three key moves.

    First, compress the candidate representations. Project the key side into a low-dimensional latent space, keeping only a small number of channels per position. Lowering the dimension reduces both the arithmetic intensity and the memory traffic of the scoring stage, and low-dimensional dot products are more likely to hit on-chip caches.

    Second, two-stage retrieval. First run a coarse filter in latent space, reducing candidates from ten million to a few thousand; then rerank those thousand candidates on the original representations. The coarse filter is cheap but rough, the fine ranking is expensive but accurate. Chaining them brings the total cost to roughly “linear + small-scale quadratic,” an order of magnitude below direct full fine ranking.

    Third, divide labor with linear attention layers. Global layers use the retrieval path above to guarantee precise lookback, linear layers handle long-range compressed memory, and the two alternate by layer so that not every layer pays the retrieval cost.

    Illustrative pseudocode below—the point is the structure, not the exact interfaces:

    1. # Coarse filter: scoring in low-dimensional latent space
    2. z = project_to_latent(keys) # [S, d_latent], d_latent << d_head
    3. score = query_latent @ z.T # [Q, S], one cheap matmul
    4. cand = topk(score, k_coarse) # thousand-scale candidates
    5. # Fine ranking: original-dimension attention on candidates only
    6. k_sel = keys[cand] # small-scale gather
    7. attn = softmax(query @ k_sel.T) @ values[cand]

    Note that the latent projection and the k value for the coarse filter are design points that need tuning per task; there is no universally correct setting. In practice, start with a smaller k and gradually increase it based on validation-set accuracy.

    4. Why the Accuracy Holds Up

    The most common failure mode of sparse retrieval is a miss: the truly critical evidence happens not to be selected, producing factual errors or broken long-range coreference in the output. The latent-space approach preserves accuracy by relying on three conditions:

    • Semantics remain separable after compression. The low-dimensional projection is not random dimensionality reduction; it is trained jointly with the main task so that relevance remains rankable in the low-dimensional space.
    • The coarse filter leaves enough redundancy. k_coarse is significantly larger than the final k used, leaving room for the fine ranking to correct mistakes.
    • Layered fallback. Linear attention layers retain global summary information, so even if a global layer misses something, later layers still have a chance to recover.

    For evaluation, don’t look at perplexity alone. It is recommended to cover four task types: single-hop retrieval (key information appears exactly once in a long document), multi-hop reasoning (evidence is scattered and must be combined), long-range coreference resolution, and coverage in long-document summarization. Perplexity is insensitive to misses, while these tasks expose problems quickly.

    5. Engineering Implementation Notes

    To validate a similar approach on your own inference stack, proceed in this order:

    1. Profile the baseline first. Use monitoring and tracing to confirm whether time goes to scoring, selection, or aggregation, so you don’t optimize the wrong target.
    2. Start on the inference side. The gains in the prefill stage far outweigh those in decode, so pilot in scenarios like long-document batch processing and codebase understanding.
    3. Control peak memory. The coarse filter introduces extra intermediate tensors; if necessary, materialize candidate indices in batches, or use container platforms with elastic scaling to handle bursty long requests.
    4. Establish regression gates. Include accuracy metrics alongside first-token latency and throughput in continuous integration, so quality is never sacrificed for speed.
    5. Separate logging from alerting. Push index hit rates and candidate-set size distributions into a logging service, and set thresholds through monitoring and alerting to catch distribution drift early.

    6. Benefit Boundaries and Selection Guidance

    This kind of method is not free. It introduces new training objectives and extra operators, raising engineering complexity. In short-context scenarios (within a few thousand tokens), index overhead is not significant anyway, and the gains are offset by the complexity. So the criterion is clear: when context length enters the million-to-ten-million range and index time exceeds half of total compute time, latent-space retrieval offers the best return on investment.

    The practical takeaway for developers is to locate the bottleneck before optimizing long contexts. If the bottleneck is in attention computation, the direction is sparsification and linearization. If the bottleneck has already shifted to indexing, you must reduce the dimensionality and add hierarchy to the retrieval itself—otherwise even the most refined sparse strategy will be swallowed by the cost of finding. When deploying in the cloud, you can combine object storage for long documents, message queues for peak shaving, container platforms for elastic scaling, and monitoring and alerting into a closed loop, turning long-context capability into a sustainable engineering capability rather than a one-off stack of parameters.