Sparse Attention at 10M Token Context: 22x Prefill Speedup by Fixing the Indexer
As context windows scale from millions to tens of millions of tokens, the bottleneck in sparse attention shifts from computation to selection. This article breaks down why the indexer becomes a new quadratic bottleneck, explains how Latent Sparse Attention moves the selection stage into a low-dimensional latent space to reduce complexity from O(n²) to linear, and provides a practical engineering checklist covering compression tuning, recall thresholds, kernel fusion, load balancing, and observability. Readers will learn where the reported 22.7x and 72.4x prefill speedups come from, which pitfalls to avoid, and how to reproduce and measure the gains before scaling to production traffic.
1. Background: How the Cost Structure Inverts at Scale
Over the past two years, long context has shifted from a selling point to a default specification. Million-token context is now standard in mainstream model serving, and some models have adopted hybrid designs that combine sparse attention with linear attention. These approaches share the same premise: each query vector does not need to scan the entire token history, but instead selects a few of the most relevant segments to participate in the computation.
At the million-token scale, this premise has been validated repeatedly. But as context pushes toward 10 million tokens, the cost structure inverts. Take a global attention layer as an example: at 10M tokens, roughly 98% of prefill time is not spent on the attention matrix multiplications themselves, but on the indexing step that decides which tokens to look at.
This means the nature of the bottleneck has changed. Sparse attention has already proven that “looking at only a small subset” is enough to preserve accuracy. What actually blocks ultra-long context is the task of finding that small subset, whose cost still grows quadratically with context length. In other words, we successfully compressed the computation but not the search.
2. Root Cause: Why the Indexer Becomes the New Quadratic Bottleneck
Splitting attention into two stages makes the problem clearer:
- Selection stage: score, rank, and filter the top-k key/value pairs for each query.
- Computation stage: perform weighted aggregation over only the selected k positions.
The computation stage has complexity O(n·k). Since k is a constant, it grows linearly with n. The selection stage, if implemented naively as “score every token, then sort globally,” has complexity O(n²). When n grows from 10⁶ to 10⁷, the quadratic term grows 100x while the linear term grows only 10x, so the indexer inevitably becomes the dominant cost.
Three engineering factors amplify the problem:
- Memory access amplification: scoring repeatedly reads key representations, so memory bandwidth saturates before compute does.
- Insufficient parallelism: filtering and sorting involve many branches and strong dependencies, making it hard to saturate compute units the way matrix multiplication does.
- Load imbalance: different queries hit very different segments, producing a pronounced long tail during chunked execution.
Illustrative pseudocode shows this structural problem:
# Naive implementation: explicitly materialize an n x n score matrixscores = q @ k.transpose(-1, -2) # [n, n], quadratic in both memory and computeidx = scores.topk(k, dim=-1).indices # global selectionout = gather_and_aggregate(v, idx)
3. Core Idea: Move Selection into Latent Space
A class of methods known as Latent Sparse Attention offers this answer: do not search in the original token space. First compress the long sequence into a low-dimensional latent representation, then perform coarse filtering in latent space, and finally return to the original space for fine-grained computation.
It breaks down into three steps:
- Compress: aggregate the key/value of each contiguous block into a small number of latent vectors; block size and the number of latent vectors are controlled by the compression ratio.
- Coarse filter: the query is dotted only with the latent vectors to select candidate blocks, reducing complexity to O(n·r), where r is the number of latent vectors and r ≪ n.
- Fine compute: expand within the candidate blocks back to token granularity and run standard sparse attention.
The key point is that quadratic search is replaced by “linear compression plus small-scale retrieval.” Public results show that at the 10M token scale, prefill for a single attention layer achieves speedups on the order of 22.7x and 72.4x (for models of different parameter sizes), while maintaining accuracy close to the original model across multiple tasks.
4. Complexity and Gains: Where the Order-of-Magnitude Difference Comes From
Let n be the sequence length, b the number of tokens per block, r the number of latent vectors per block, and m the number of blocks finally retained:
- Original indexing: O(n²)
- Latent-space indexing: O(n·r + m·b·k)
Since both r and m are hyperparameters independent of n, the overall complexity drops from quadratic to linear. The speedup varies noticeably across models (22x vs 72x), typically due to three factors: different numbers of attention heads and dimensions, different indexing implementation efficiency in the native sparse strategy, and different degrees of fit between compression ratio and block size. This also suggests that the gains are not a free lunch and require tuning for the specific model architecture.
5. Engineering Implementation Checklist
- Compression ratio tuning: blocks that are too small produce too many latent vectors and shrink the gains; blocks that are too large hurt coarse-filter recall. Start with a grid search from b=64~128 and r=1~4.
- Recall guardrail: use “whether candidate blocks cover the original top-k” as the metric; recall should be no lower than 0.95, or accuracy will drop noticeably.
- Kernel fusion: fuse compression, scoring, and filtering into a single kernel to avoid materializing intermediate tensors in memory.
- Load balancing: bucket queries by the number of candidate blocks to mitigate idle compute caused by the long tail.
- Layered strategy: use a higher compression ratio in shallow layers for speed, and a lower compression ratio in deep layers to preserve accuracy.
- Mixed deployment with linear attention: use latent sparse attention in global layers and linear attention in local layers, balancing long-range dependencies with local detail.
- Observability: instrument the share of indexing time, recall, and per-step token throughput in your monitoring and alerting system, and retain long-tail samples in a logging service for later review.
6. Common Pitfalls
- Pitfall 1: assuming the speedup comes from “computing less.” In fact, computation is no longer the bottleneck; the gains come mainly from compressing the search space.
- Pitfall 2: blindly pursuing a high compression ratio. Over-compression causes the coarse filter to miss critical segments, and the accuracy loss usually shows up first in long-text reasoning and retrieval tasks.
- Pitfall 3: evaluating only on short sequences. The indexer’s quadratic term is not obvious below the million-token scale; you must stress-test at the 10M token scale to see the real gains.
7. Conclusion
The next threshold for ultra-long context is not “can we compute it” but “can we quickly find the part that needs computing.” Migrating the selection process into a low-dimensional latent space and replacing quadratic cost with linear cost is currently the most engineering-feasible path. For enterprise users who need to support long-document analysis, codebase understanding, and multi-turn memory on their own platforms, it is advisable to first reproduce the approach at small scale on a container platform, quantify recall and latency gains with monitoring and logging, and then gradually scale up to production traffic.
