Long-Context Sparse Attention: Index Cache Pooling Compression in Practice

    This article explains index cache pooling compression, a technique for reducing the memory and latency overhead of the lightweight indexer used in sparse attention. It covers why indexer caches grow linearly with sequence length, how weighted four-to-one pooling merges cache vectors, the engineering details required for index mapping and causal masking, and how to evaluate the trade-off between compression ratio and recall quality in million-token context serving.

    1. Background: Long Contexts Shift the Cost from the Attention Matrix to the Indexer

    As million-token context windows become a mainstream configuration, the compute and memory-access pressure of attention is no longer concentrated in the main attention itself. Hybrid attention architectures offer a middle path: linear attention (or state-space style recurrent structures) captures short-range dependencies with a constant-size state, while sparse attention selects a small number of key positions from the global range to participate in computation.

    The key premise of sparse attention is that the selection must be accurate, and the selection is usually performed by a lightweight indexer. The indexer maintains several cached vectors for each historical position, used for scoring and Top-K recall.

    The problem is that although the indexer is lightweight, its cache grows linearly with sequence length. When the context reaches the 1M scale, both the capacity and the number of reads and writes of the indexer cache become a bottleneck that cannot be ignored: on one hand, it takes up GPU memory budget that could otherwise go to the KV cache; on the other hand, it introduces additional memory-access latency during decoding. A common approach in the industry is worth noting: weighted pooling can compress the indexer cache four-to-one, merging 4 cached vectors into 1 and reducing indexer cache usage to one quarter, which lowers both the latency and the memory overhead of the indexer under long contexts. This article abstracts that approach into a general technical pattern called index pooling.

    2. Why the Indexer Cache Deserves Separate Optimization

    1. Growth Is Linear and Unavoidable

    The KV cache of the main attention can be compressed with techniques such as grouped-query attention, quantization, and eviction policies. The indexer cache, however, carries the responsibility of global visibility, and any compression must guarantee that recall quality does not collapse, so it has long been treated as a fixed cost.

    2. Decoding Is Memory-Bandwidth Bound

    Autoregressive decoding processes only one token per step, so compute utilization is low and the bottleneck falls on bandwidth. The smaller the indexer cache, the fewer bytes need to be moved per step, and latency drops almost linearly.

    3. The GPU Memory Budget Is Zero-Sum

    In long-context serving, GPU memory is shared among weights, the KV cache, the indexer cache, activations, and communication buffers. Space freed by compressing the indexer cache can be converted directly into a larger batch size or a longer usable context.

    3. The Core Idea of Weighted Four-to-One Pooling

    The naive approach is average pooling: take the mean of every 4 consecutive index vectors. This is too coarse in terms of information retention, because different positions contribute very differently to recall. The improvement of weighted pooling is to introduce learnable weights (or heuristic weights based on scores or frequency) and to normalize before merging, so that the signal from important positions is not diluted.

    A sketch implementation follows (it only illustrates the idea and is not a specific product interface):

    1. import torch
    2. def index_pool(views, weights, group=4):
    3. # views: [B, L, D] indexer cache; L must be divisible by group
    4. B, L, D = views.shape
    5. v = views.view(B, L // group, group, D)
    6. w = weights.view(B, L // group, group, 1).softmax(dim=2)
    7. return (v * w).sum(dim=2) # output [B, L/4, D]
    8. def topk_recall(pooled, q, k=2048):
    9. scores = (q @ pooled.transpose(-1, -2)) / pooled.size(-1) ** 0.5
    10. idx = scores.topk(k, dim=-1).indices
    11. return idx # then map back to the original position granularity

    Three things need extra handling in engineering. First, alignment between group boundaries and sequence length (padding must be divisible). Second, index mapping: a pooled position must be restorable to the original token interval. Third, compatibility with positional encoding and causal masks, to avoid information leakage across groups.

    4. A Rational Assessment of Benefits and Costs

    The benefits are clear: cache capacity drops to one quarter, memory access drops accordingly, and both decoding latency and GPU memory pressure under long contexts are eased. The costs must also be faced honestly: pooling is lossy compression, and recall granularity changes from a single point to an interval. On tasks that require precise retrieval of fine-grained facts (such as a single string in a long document or a code line number), recall drift may occur.

    The recommended evaluation framework observes three dimensions jointly: recall quality (accuracy on long-document retrieval and multi-hop question answering), system metrics (time to first token, per-step decoding latency, peak GPU memory), and throughput (maximum concurrency and context length under a fixed memory budget). Only when the quality regression stays within an acceptable threshold does the compression ratio have engineering value. If quality is not acceptable, you can try a coarser grouping (for example, eight-to-one instead of four-to-one), keep one original vector as a residual, or apply temperature adjustment to the weights.

    5. Implementation Advice and Cloud Support

    A three-step rollout is recommended: first validate recall quality on an offline evaluation set, then run single-GPU memory and latency benchmarks, and finally roll out gradually and compare service quality before and after compression. Supporting engineering capabilities can rely on general cloud components: a container platform for elastic scaling of the inference service, object storage for archiving evaluation sets and model artifacts, and logging plus monitoring and alerting to track latency and memory curves. Baidu AI Cloud can provide the corresponding compute, storage, and operations support for these general capabilities, helping teams turn attention-structure optimizations into observable service metrics.

    In summary, index cache pooling compression offers a low-intrusion optimization path: without changing the model backbone, simply merging the indexer cache with weights yields predictable resource gains in million-token context scenarios. The key is to weigh compression ratio and recall quality on the same evaluation sheet, and to let data rather than intuition decide the final configuration.