Sparse Attention Indexing at 10M-Token Contexts: How to Measure It

    This article explains why indexing becomes the bottleneck in long-context sparse attention, derives the complexity boundary between indexing and attention computation in prefill and decode, reviews latent-space filtering and its constraints, and provides a practical measurement checklist for evaluating sparse attention indexing at context lengths up to millions of tokens.

    1. Why Indexing Becomes the New Bottleneck for Long Contexts

    In standard attention, every query computes relevance against all historical keys, resulting in O(n²) computation, where n is the context length. Sparse attention keeps only the k most relevant keys for each query, reducing attention computation to O(n·k).

    Sparse attention has two stages. The first is the indexing stage, which determines which keys each query should attend to. The second is the computation stage, which performs the actual attention operation over the selected k keys.

    The cost of the computation stage is proportional to k. The indexing stage must evaluate potentially relevant keys for each query, and its cost depends on the scoring method. When context expands to the ten-million-token scale, the growth rate of indexing may exceed that of computation. The two stages therefore need to be measured and optimized separately.

    2. Complexity Boundaries of Indexing Overhead

    Define the symbols as follows. n_q is the number of queries, n_k is the number of keys, d is the original attention dimension, d’ is the dimension of each key summary vector, and k is the number of tokens retained per query. The following discussion assumes that every query is scored against all summaries, and then top-k is applied to the scores.

    Prefill Stage

    During prefill, both the number of queries and the number of keys equal the context length n. Summary scoring costs O(n_q·n_k·d’) = O(n²·d’). Top-k selection filters n_k scores for each query, costing O(n_q·n_k) = O(n²). The constant factor of the selection stage depends heavily on the implementation; heaps, bucketing, and partial sorting have very different overheads.

    The computation stage costs O(n·k·d). The ratio of indexing to computation is approximately (n·d’)/(k·d). When n is greater than k·d/d’, indexing overhead exceeds computation overhead. The values of k and d’ determine where this crossover occurs.

    Decode Stage

    During decode, there is only one new query per step. Full scoring costs O(n_k·d’) and grows linearly with context length. The main pressure in decode is usually memory bandwidth for reading the KV cache and summaries, as well as KV cache occupancy, rather than quadratic indexing computation. Prefill and decode therefore need to be evaluated separately; a single number cannot represent both.

    Fixed Patterns vs. Content-Based Indexing

    Fixed patterns such as sliding windows and fixed blocks do not require content-based indexing and do not incur the scoring overhead described above. The complexity conclusions in this section apply only to sparse attention that performs per-query, content-based filtering.

    Recall and Approximation Error

    Summary scoring is an approximate computation. Compression into d’ loses information, and approximate filtering may miss keys that would have received high scores or rank low-scoring keys too highly. The cost of missed recalls does not appear in the complexity formula; it can only be measured through recall rate and output error.

    k and d’ need to be tuned jointly. Increasing d’ improves accuracy but raises indexing cost. Increasing k improves recall but raises computation cost.

    3. Latent-Space Filtering: Approach and Limitations

    The basic approach of latent-space filtering is to store a low-dimensional summary for each key. The summary can be a linear projection or block-level statistics such as the mean or maximum within a block. The query is first compared with summaries to select candidate blocks or candidate keys, followed by exact computation.

    Applicable conditions:

    • The ordering of attention scores is mainly determined by a small number of directions, and a low-dimensional projection can preserve those directions.
    • The query and key distributions during training and inference are close.
    • The memory and bandwidth budgets can accommodate the summary cache.

    Main limitations:

    1. Recall risk. Low-dimensional projections change score ordering, and critical tokens outside top-k may be discarded. Recall rate should be measured against full attention as a reference.
    2. Distribution drift. Model fine-tuning, switching to data from a different domain, or changing positional encoding can invalidate the original projection, making previous measurements no longer applicable.
    3. Update cost. During decode, each newly generated token requires a summary to be computed and appended to the index. Block summaries also need to handle partially filled blocks and update or dynamically maintain them when blocks become full.
    4. Granularity loss. Block summaries reduce n_k, but differences among tokens within a block are averaged out, making fine-grained local dependencies easy to lose.
    5. Storage overhead. The extra memory for summaries is approximately n·d’ elements per layer per KV head multiplied by bytes per element. At long context lengths, this occupancy cannot be ignored.

    4. Measurement Checklist

    Fix the conditions before comparing numbers. Every experiment should record the following.

    • Model: number of layers, number of attention heads, number of KV heads, d, and sparse configuration.
    • Runtime parameters: precision (such as bf16 or fp8), batch size, sequence length, whether fused kernels are enabled, and whether tensor-parallel communication is included.
    • Runtime environment: GPU model, driver version, CUDA version, and framework version.

    Measurement steps:

    1. Segment timing. Split attention into three parts: summary scoring, top-k selection, and sparse attention computation. Time each part with GPU events. Synchronize before and after timing, warm up first, repeat multiple times, and take the median.
    2. Measure prefill and decode separately. For prefill, record the total time for the entire input. For decode, record per-step time and track how it changes with generated length.
    3. Sweep context length. Suggested points are 1k, 16k, 128k, and 1M. For each point, record n_q, n_k, d’, k, and the top-k implementation. Plot indexing time against n on a log-log scale; a slope close to 2 indicates that the quadratic term dominates, while a slope close to 1 indicates that the linear term dominates.
    4. Record memory. Separately record peak memory, KV cache usage, and summary usage.