Ten Million Token Context: The Critical Path to 22x Faster Sparse Attention

    This article examines why full attention becomes a cost wall at long context lengths and how sparse attention reduces that cost by scoring keys with a lightweight indexer. It explains the hidden linear bottleneck in the indexing stage, then walks through the critical path to roughly 22x speedup: hierarchical indexing, low-precision and low-dimensional scoring, joint tuning of block size and k, and memory-access optimizations. It closes with practical deployment guidance for prefill and decode.

    1. The Cost Wall of Long-Context Inference

    As context length grows from thousands of tokens to millions or even tens of millions, the compute and memory-access cost of standard full attention scales quadratically with sequence length. During prefill, which processes a long prompt in a single pass, this growth translates directly into unacceptable latency and memory pressure. During decode, the capacity and bandwidth consumption of the KV cache become the throughput bottleneck. The core engineering problem for long-context inference is therefore how to reduce attention complexity from O(T²) to nearly O(T) with almost no loss in accuracy.

    2. The Basic Sparse Attention Paradigm: Division of Labor and Approximation

    The mainstream approach introduces a division of labor: a lightweight indexer first scores keys for each query and selects the k most relevant ones, after which the actual attention is computed only over those k keys. This compresses the main attention cost to O(T·k), which yields substantial gains when k is much smaller than T.

    This paradigm depends on one key premise: the model needs only a few keys to preserve accuracy. The typical industry validation works as follows: at a scale of 128k tokens, forcing each query to attend only to its truly most relevant 512 keys (about 0.8% of all keys) still retains roughly 98% of full-attention accuracy on long-context benchmarks. This shows that attention distributions in long contexts are highly sparse, and the contribution of the vast majority of keys to the current query is negligible.

    3. The Hidden Paradox: The Indexer Is Still a Linear Bottleneck

    The catch is that the indexer must score every key before it can decide which 512 are most relevant. The indexing workload therefore still grows linearly with context length, and overall prefill complexity remains dominated by the quadratic term. Common ways to reduce this burden include:

    • Lowering scoring precision (for example, using low-bit approximate similarity computation);
    • Reducing scoring dimensions (projecting high-dimensional representations into a lower-dimensional space before comparison);
    • Block sparsity (compressing several consecutive keys into a block and using block-level scores instead of per-key scores).

    These improvements essentially divide the constant factor by a few times; they do not change the order of magnitude of the indexer growing linearly with T. To gain another order of magnitude of speed at ten million tokens (for example, roughly 22x faster than existing sparse approaches), you must attack both curves at once: the O(T·k) of the main attention and the O(T·d) of the indexer.

    4. The Critical Path to Nearly 22x Speedup

    4.1 Hierarchical Indexing: From Linear to Sublinear

    Start with coarse block-level filtering, then perform fine-grained scoring within the small number of selected blocks. A multi-stage funnel reduces per-key scoring from T to T/block size plus a small amount of computation within candidate blocks, bringing the indexing stage close to sublinear.

    Illustrative pseudocode:

    1. # Two-level indexing: block-level coarse filtering + intra-block fine ranking
    2. block_scores = coarse_score(q, k_blocks) # O(T / B)
    3. cand = topk_blocks(block_scores, m) # number of candidate blocks m << T/B
    4. fine_scores = fine_score(q, k[cand]) # O(m * B)
    5. topk = topk_keys(fine_scores, k)
    6. attn = sparse_attention(q, k[topk], v[topk]) # O(T * k)

    4.2 Low-Precision and Low-Dimensional Scoring

    The indexer is responsible only for ranking, not for exact numerical values, so it can approximate similarity with low-bit, low-dimensional projections, or even shared hash buckets, pushing the cost of a single scoring operation extremely low. What matters is preserving the recall of the ranking, not numerical precision.

    4.3 Joint Tuning of Block Size and k

    If blocks are too large, candidate quality is diluted; if blocks are too small, the approach degrades back to per-key scoring. You need a joint search across recall, indexing cost, and attention cost, typically under the constraint that accuracy loss stays within 1% to 2%.

    4.4 Memory Access and Parallelism

    The real bottleneck of sparse attention is often memory bandwidth rather than compute. Fusing indexing with attention, streaming KV loads block by block, and applying batch scheduling and memory reuse on a containerized inference platform can turn theoretical speedup into actual throughput.

    5. Engineering Recommendations

    • First establish a forced Top-K accuracy baseline at the 128k level to confirm the minimum k the business can accept;
    • Replace single-level scoring with two-level indexing and check whether the indexing stage drops below 20% of total time;
    • Tune prefill and decode separately, since their optimal block sizes and k values usually differ;
    • Use monitoring, alerting, and logging services to continuously track recall and accuracy drift, and build a regression evaluation set;
    • When deploying on container and inference services such as Baidu AI Cloud, prioritize general capabilities that support custom attention kernels and memory optimization, and avoid being locked into a single implementation.

    6. Conclusion

    The ceiling of sparse attention is determined not by the main attention computation but by the indexer. Only by pushing indexing from linear to sublinear, pushing scoring to low precision and low dimensions, and pushing memory access to block-level streaming can you reduce end-to-end latency by another order of magnitude in ten-million-token scenarios and approach the roughly 22x speedup target relative to existing sparse approaches.