Sparse Attention at 10M Tokens: Why Indexing Becomes the Bottleneck
As context windows scale to millions and tens of millions of tokens, sparse attention shifts the main cost from attention computation to Top-K index selection. This article breaks down the indexing and sparse-compute stages, reviews latent-space retrieval approaches and their trade-offs, and provides an evaluation and pre-deployment checklist for long-context sparse attention systems.
1. As Context Grows, the Bottleneck Moves from Attention Compute to Indexing
Model context windows have expanded from a few thousand tokens to millions and are still moving toward tens-of-millions-token scale. The cost structure of attention changes with this shift. Full attention grows quadratically with sequence length, so sparse attention is widely used: each Query selects only a small number of the most relevant Tokens for computation, and the remaining Tokens are excluded.
This approach has been repeatedly validated at million-token scale, where it can substantially reduce computation while preserving model accuracy. The problem lies in the “selection” step. Selection itself requires first computing relevance across all historical Tokens, and this step also grows with context length. The longer the sequence, the more time the selection process takes, while computation over the few selected Tokens is relatively short.
The exact time ratio depends on model architecture, sequence length, hardware, and kernel implementation. Readers should rely on profiling results from their own environment rather than directly applying a public number.
2. Two Stages and Their Corresponding Complexity
Typical sparse attention has two stages.
The first stage is indexing: for the current Query, find the Top-K most relevant Keys in the historical KV Cache. The second stage is sparse computation: compute attention scores only over these K Keys and use them to weight and aggregate the corresponding Values.
The complexity of the two stages must be considered separately. For a single Query, brute-force retrieval computes similarity against N historical Keys one by one, with complexity O(N·d), where d is the vector dimension. The prefill phase must process N Queries, and each Query scans the historical Tokens before it, so the total indexing cost is O(N²·d). In the second stage, each Query processes only K Keys, with total cost O(N·K·d). When K is fixed, the second stage grows linearly with N. The quadratic growth comes from the first stage.
The pseudocode below shows this structure. The complexity in the comments refers to the retrieval cost for a single Query.
def sparse_attention_step(query, history_keys, history_values, top_k):# Indexing stage: compute similarity between one query and all history keys, complexity O(N * d)scores = compute_similarity(query, history_keys)top_k_indices = get_top_k(scores, k=top_k)# Sparse computation stage: process only top_k keys, complexity O(K * d)sparse_keys = gather(history_keys, top_k_indices)sparse_values = gather(history_values, top_k_indices)return scaled_dot_product(query, sparse_keys, sparse_values)
In prefill, calling this function for all N Queries gives an overall indexing cost of O(N²·d).
3. Basic Ideas for Latent-Space Retrieval
One class of ideas for reducing indexing cost avoids retrieving directly in the original high-dimensional Key space. Instead, Keys are first mapped to a lower-dimensional space, and coarse filtering is performed there. Common approaches include the following three points.
- Low-dimensional projection. A trained projection matrix compresses Keys to a lower dimension. In the lower-dimensional space, relevance can be approximated with a cheaper inner product, reducing the cost of each comparison.
- Block-level hierarchical retrieval. Historical Tokens are organized into blocks. A Query first identifies relevant blocks from block-level representations, then performs fine-grained filtering within the selected blocks. The candidate set is therefore greatly reduced, and overall retrieval cost is lower than token-by-token scanning.
- Incremental updates. During prefill, the KV Cache grows continuously, and the index should be updated incrementally as new Tokens are appended, avoiding a full rebuild of the global structure each time.
These ideas come with costs. Projection loses information and may cause truly important Tokens to be missed; this loss usually appears as a drop in recall. The projection matrix requires training, and training data and training cost must be considered. The choice of block size directly affects the balance between recall and retrieval speed. Additional index structures also consume GPU memory.
Therefore, this class of methods is not suitable for every scenario. Its benefit depends on the specific model, sequence-length distribution, and acceptable accuracy loss.
4. How to Evaluate a Sparse Attention Scheme
Evaluation should answer three questions at the same time: how much speed improves, how much accuracy is lost, and what cost is paid.
Speed evaluation should use fixed measurement conditions. Record sequence length, batch size, numeric precision, GPU model, and number of GPUs, and report time to first token (TTFT) and prefill throughput separately. A single speedup multiplier without measurement conditions cannot be reproduced.
Accuracy evaluation should include two types of metrics. The first is comparison with full-attention results, such as comparing the error in attention output on the same input, or measuring the overlap rate between the Top-K selected by the index and the true Top-K. The second is downstream task performance. Use long-context benchmarks with explicit version numbers, such as long-document summarization, key information retrieval, and long-code understanding, and record the specific score for each benchmark.
Performance changes at long sequence lengths should also be checked. Some schemes perform well on short sequences, but recall may gradually degrade as length increases. Evaluation should cover multiple length buckets from short to very long and observe the curve.
5. Pre-Deployment Checklist
Before deploying sparse attention in production, check the following items in order.
First, estimate memory. KV Cache usage can be estimated as follows: the per-Token cost at each layer is approximately 2 × KV heads × head dimension × bytes per element, multiplied by the number of layers and sequence length. The result depends heavily on model architecture; models using grouped-query attention or compressed KV structures can differ substantially. If the estimate exceeds the memory of a single GPU or single node, plan for paged management, offloading to host memory, or cross-GPU partitioning.
Second, plan multi-GPU communication. If indexing requires exchanging Token information across devices, assess network bandwidth and latency. A common practice is to overlap communication with computation so that the retrieval process is not blocked by the network.
Third, design a scheduling policy. Very long requests take longer during prefill, so they should be handled in separate queues from short requests. Consider chunking long prefill execution to avoid blocking other requests.
Fourth, set up fallback and monitoring. Keep full attention or ordinary sparse attention as a fallback path, enabled in short-context scenarios or when recall monitoring metrics fall below a threshold. Also monitor P99 TTFT, throughput, and accuracy-related metrics, and pin model and kernel versions to avoid silent result changes after upgrades.
Conclusion
From full attention to sparse attention, and then to latent-space-based retrieval, long-context evolution has centered on one question: how to pay computation only for the Tokens that are truly needed. When context reaches 10M-token scale, the cost of indexing itself cannot be ignored. When choosing a scheme, first measure the real bottleneck in your own environment, then compare candidate schemes with a unified evaluation method rather than relying only on a single performance multiplier.
