Indexing Bottlenecks and Optimization for Sparse Attention at Ten Million Tokens
At the million-token scale, hybrid architectures combining sparse and linear attention are widely adopted, but at ten million tokens the indexing stage that selects Top-K tokens can become the dominant cost before attention computation itself. This article breaks down where indexing overhead comes from in both prefill and decoding phases, compares four common approaches to reducing it (full scoring with Top-K, block-based indexing, approximate nearest neighbor, and latent-space sparse attention), and walks through the core idea of LSA with illustrative pseudocode and its associated trade-offs.
1. From Millions to Tens of Millions: Where the Problem Shifts
At the million-token scale, hybrid architectures combining sparse attention and linear attention are already widely adopted in mainstream models. The idea is to let each query attend only to a small set of the most relevant tokens, thereby reducing attention computation. This approach has been well validated at the million-token level, but it rests on one premise: the overhead of the indexing stage must be small enough.
When the context grows to ten million tokens, the situation changes. Attention computation itself can scale linearly with length, while the indexing process that selects the Top-K tokens still has to score every historical key. Indexing overhead becomes the dominant cost before attention computation does. This is the starting point of this article.
The original source cited a figure claiming that “at 10M tokens, 98% of the time is spent on the indexer,” but it did not specify the model, hardware, batch size, Top-K value, or measurement methodology. That figure cannot be verified, so this article does not rely on it. Readers should base their conclusions on measurements taken with their own models and hardware.
2. Where Does Indexing Overhead Come From?
Take a single attention layer as an example. Let the number of queries be Q, the number of historical keys be N, and the head dimension be d. Full scoring produces a Q×N similarity matrix, with a computational cost of roughly O(Q·N·d).
During the prefill phase, Q and N are of the same magnitude, so the per-layer indexing cost is approximately O(N²·d). This is the quadratic growth people often refer to.
During the decoding phase, each step has only one new query, so the per-step indexing cost is about O(N·d), which grows linearly with length. However, the cumulative cost over the entire generation still rises quickly as the context gets longer. At the same time, the volume of KV cache reads grows in parallel, so the decoding phase also faces memory-access pressure.
Therefore, whether indexing becomes the bottleneck depends on the combination of phase, length, batch size, and Top-K. A conclusion that holds under one configuration may not hold under another. When making design decisions or writing reports, prefill and decoding data should be recorded separately.
3. Four Approaches to Reducing Indexing Overhead
Common approaches today fall into four categories.
| Approach | How It Works | Indexing Complexity (Approximate) | Main Risks |
|---|---|---|---|
| Full scoring + Top-K | Compute similarity against all keys, then take the top K | O(Q·N·d) | Cost grows the fastest; used as the baseline |
| Block-based indexing | Summarize keys into blocks, select blocks first, then tokens | Depends on block count; usually well below full scoring | Block summaries lose detail; recall drops at block boundaries |
| Approximate nearest neighbor (e.g., graph index or clustering) | Build an approximate index; queries visit only part of the nodes or clusters | Extra time and memory for construction; queries are typically sublinear | High index construction and update costs; sensitive to parameters |
| Latent-space sparse attention (LSA) | Project into a lower-dimensional space, retrieve via hashing or clustering, then re-rank in the original space | Depends on projection dimension r, hashing implementation, and candidate count C | Projection loses information; recall depends on data distribution |
None of these methods is universally optimal. Block-based indexing is simple to implement and works well as a first step. Approximate nearest neighbor performs well when the data distribution is stable, but requires maintaining the index. The value of LSA lies in performing retrieval in a low-dimensional space, but its recall and output quality must be validated separately.
4. The Core Idea of LSA with Pseudocode
LSA consists of two steps. First, a projection matrix maps the d-dimensional queries and keys into an r-dimensional latent space, where r is significantly smaller than d. Second, locality-sensitive hashing or clustering is used in the latent space to find a candidate set, and the candidates are then scored precisely in the original space.
The following pseudocode is only intended to illustrate the data flow and is not production-ready. Details such as batching, parallelism, boundary conditions, and the hashing implementation are omitted.
# Full-scan indexing (baseline)# query: [B, H, Q, d], keys: [B, H, N, d]# Score matrix [B, H, Q, N]; per-layer cost approx. O(B·H·Q·N·d)def full_scan_topk(query, keys, top_k):scores = query @ keys.transpose(-1, -2) # [B, H, Q, N]return scores.topk(top_k, dim=-1).indices # [B, H, Q, K]
# Latent-space indexing (illustrative, not runnable code)# r is the latent-space dimension, with r < d# C is the number of candidates, typically a few times top_k;# it should be determined through recall experimentsdef latent
