Sparse Attention Index Cache Compression with Weighted Pooling

    Weighted pooling reduces the memory and scoring cost of sparse attention indexes by merging adjacent index vectors into a single representative per time bucket. This article explains how to combine pooled retrieval with within-bucket reranking, manage recall errors, and measure whether a smaller index improves long-context inference latency without sacrificing task quality.

    Why compress the index cache?

    In long-context inference, full attention incurs roughly quadratic compute as sequence length grows, while the key-value (KV) cache grows linearly. A common engineering trade-off is to split the workload into two paths: a recurrent or linear-state path tracks nearby dependencies with predictable compute and a small cache, while a sparse attention path retrieves distant evidence on demand. Rather than scoring the entire history with full attention, the sparse path maintains a lightweight cache of low-dimensional index vectors. It uses those vectors to identify promising positions and passes only the best candidates to the attention computation.

    The indexer filters before attention, but filtering has a cost of its own. At million-token sequence lengths, the number of cached entries, the vectors scanned per query, and the bandwidth required to read them can all become bottlenecks. Even with a smaller main model and lower theoretical complexity for sparse attention, the indexer can still account for substantial latency and memory use. That makes the index cache a better first target for compression than the already sparse attention computation.

    Pool adjacent index vectors into time buckets

    Weighted pooling groups consecutive index vectors and replaces each group with a weighted average. With non-overlapping groups of four, the compressed cache has roughly one-quarter as many entries, and each query scans roughly one-quarter as many vectors. The index scoring matrix shrinks accordingly, and scoring reads less index data. This is not lossless compression: each pooled vector represents a time bucket rather than an individual position.

    Weighting matters because index vectors carry both direction and salience. A simple average gives strong evidence and weak background equal influence, while a max-based approach can let a noise spike stand in for the whole segment. Weighted pooling lets more recent tokens, higher-norm vectors, or positions with stronger gate scores contribute more to the representative. The weights can come from the indexer’s scoring head, a time-decay function, or a small scalar gate. Computing them should cost substantially less than a full attention calculation.

    Use pooled retrieval before within-bucket reranking

    The local and sparse paths have distinct responsibilities. The recurrent state captures order and fine-grained dependencies within the local window without relying on the compressed index. The indexer answers a narrower question: which distant segments are likely to be relevant? Pooling belongs in the index cache’s write or periodic compaction stage, not in the recurrent state update.

    At query time, scan the compressed index to retrieve candidate buckets. Then access the finer-grained keys and values or original positions only in those buckets for a second scoring pass. The global scan stays small, and fine-grained scoring is limited to the retrieved subset.

    Treat the index cache as a tensor with shape [time, index_dim]. When four entries have accumulated, reduce them to one vector:

    1. def weighted_pool(cache_group, weights):
    2. # cache_group: [4, dim]; weights: [4], normalized
    3. w = weights / (weights.sum() + 1e-6)
    4. return (cache_group * w[:, None]).sum(axis=0)

    Append the result to the compressed cache. The four original index vectors can be discarded or retained briefly in the hot window. On the query side, use the same index projection to produce a query vector, score the pooled cache with a dot product or cosine similarity, and select the top K buckets. Map those buckets back to their original position ranges. The mapping requires only a start and end position per bucket and takes much less space than the vectors themselves.

    Manage the main sources of retrieval error

    • Bucket boundaries. If key evidence spans two adjacent buckets, neither representative may match the query well enough. Overlapping buckets, or including a high-weight position in a neighboring bucket, can help. Overlap slightly reduces the compression ratio and is most useful when topics change frequently.
    • Vector norm shrinkage. Weighted averaging can reduce vector norms and shift the score scale relative to the uncompressed index. Applying the same normalization at cache write time as during training, or storing the maximum norm in each group as an extra scalar, can help keep score thresholds stable.
    • Training–inference mismatch. If training retrieves from the original index but inference uses pooled vectors, the retrieval distribution can shift. Include pooling in the training graph, or use distillation to align the compressed index’s top-K results with those of the teacher index.

    Measure recall, task quality, and latency together

    Compression ratio alone does not tell you whether the method works. Track index-stage latency, index cache size in bytes, end-to-end latency per token, and retrieval hit rate: whether the buckets selected by the compressed index cover the reference positions identified by the uncompressed index. Also measure downstream quality on tasks such as long-document question answering or summarization.

    If hit rate barely drops but task quality falls noticeably, the compression may be losing evidence that scores relatively low but is essential for reasoning. Try smaller groups, more overlap, or more candidates in the second scoring pass. If hit rate holds steady but latency does not improve, check whether bandwidth is still dominated by the uncompressed KV cache or repeated host–device transfers. Index compression does not remove those costs.

    Tune the retrieval budget and cache layout

    Start with four controls: group size, overlap, top K, and the reranking range. Groups of four are a straightforward way to reduce the index to one-quarter of its original entry count when vectors are already low-dimensional and meaning is relatively stable within each segment. For dialogue with frequent shifts or code with frequent jumps, consider groups of two with overlap. Set top K in terms of compressed buckets, not original tokens, so that an overly broad reranking pass does not erase the scan savings. Keep reranking within the selected buckets plus a few neighboring tokens rather than scanning the entire sequence again.

    Store pooled vectors contiguously so they can be scored in one vectorized operation, and keep their start and end positions in a parallel integer array. Align eviction with the serving context window: when an old bucket falls outside the maximum context, evict it as a unit rather than rewriting its pooled vector in place. If the system already uses prefix caching or chunked prefill, align buckets with chunk boundaries so each chunk contains a whole number of buckets and no pooling group is split across chunks.

    Know when pooling is a poor fit

    Weighted pooling reduces the indexer’s cache footprint and scoring cost. It does not make sparse attention less sensitive to missed evidence, and it does not replace the local recurrent path’s modeling of nearby dependencies. The method assumes adjacent positions can share a useful retrieval representative. If any individual token could independently contain the answer, use smaller groups or pool only lower-importance regions.

    Measure compression, coarse retrieval, and within-bucket reranking as one pipeline, and use regression tests to verify the effect on downstream tasks. The goal is to trade index overhead for a measured, acceptable loss in retrieval quality—not just to divide the cache entry count by four.