Indexer Cache Compression with Weighted Pooling for Sparse Attention Acceleration

    This article explains how long-context LLM serving shifts the performance bottleneck from the attention matrix to the indexer cache used by sparse attention, and how weighted pooling can compress four cache vectors into one. It covers the memory and bandwidth costs of indexer caches, a reference IndexPool implementation, capacity estimates, engineering pitfalls such as position alignment and train/inference consistency, and a practical validation plan based on isolated indexer timing, recall quality checks, and tiered load testing.

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

    At million-token context lengths, the compute and memory cost of full attention grows quadratically with sequence length. This is why the industry commonly adopts a hybrid architecture of linear attention plus sparse attention: the linear branch compresses history recurrently and captures local dependencies, while the sparse branch relies on a lightweight indexer to retrieve a small number of relevant segments from the global long context and applies exact attention only to those segments.

    A common misconception is that computing only a few segments automatically reduces the overhead. In practice, the indexer itself must maintain a set of cache vectors for every historical position (think of them as multiple parallel retrieval keys). At a 1M context, the capacity and access count of this cache become a new bottleneck:

    • Memory footprint: cache capacity ≈ sequence length × vectors per position × vector dimension × bytes per value × concurrent batch size. Scaling the length by 1,000× quickly pushes this term close to the available GPU memory.
    • Memory access latency: at every step the indexer scans the entire cache to score and retrieve candidates. This is a typical memory-bound workload with low compute utilization.
    • Scheduling jitter: the larger the cache, the more frequent paging and swap-in/swap-out become, and tail latency (P99) looks worse than the average.

    So optimizing long context is not only about computing less attention; it is also about making the indexer read less.

    2. Core Idea: Collapsing Four Cache Streams into One with Weighted Pooling

    IndexPool takes a direct approach: add a pooling layer on the indexer cache side that merges every 4 consecutive cache vectors into 1 via weighted pooling. This shrinks the indexer cache to one quarter of its original size, reducing both latency and memory overhead in long-context scenarios.

    The “weighted” part is the key. A naive approach takes the arithmetic mean of the 4 vectors, but different positions do not contribute equally to retrieval, and equal-weight averaging dilutes the useful signal. Weighted pooling introduces a set of learnable weights (or normalizes the weights) so the model itself decides which vectors to preserve more, keeping recall quality as high as possible at the same compression ratio.

    Illustrative implementation (semantics only, not tied to a specific framework API):

    1. class IndexPool(nn.Module):
    2. def __init__(self, dim, group=4):
    3. super().__init__()
    4. self.group = group
    5. # Learnable weights with softmax normalization for weighted, not uniform, averaging
    6. self.score = nn.Linear(dim, 1)
    7. def forward(self, cache):
    8. # cache: [B, L, D], L must be divisible by group
    9. B, L, D = cache.shape
    10. x = cache.view(B, L // self.group, self.group, D)
    11. w = torch.softmax(self.score(x), dim=2) # [B, L/g, g, 1]
    12. return (x * w).sum(dim=2) # [B, L/g, D]

    Three engineering points are worth emphasizing:

    1. Align group boundaries with positional encoding. Compression changes how much original text a single slot represents, so position indices and masks must be adjusted accordingly; otherwise retrieval offsets appear.
    2. Keep training and inference consistent. If pooling is enabled at inference but not during training, the index distribution drifts and recall quality drops noticeably. Enable compression during training, and if needed use a “train first, then distill to align” transition.
    3. Cooperate with paged caches. After compression, fewer slots are scanned per step, which together with paged cache management improves both bandwidth pressure and fragmentation.

    3. Where the Gains Come From: A Reusable Capacity and Benefit Estimate

    Assume 4 cache vectors per position, vector dimension d, and fp16 precision (2 bytes). The indexer cache is then about 4 × d × 2 bytes per token, dropping to d × 2 bytes per token after 4-to-1 compression. With d=128:

    • Before compression: about 1KB/token, roughly 1GB for a 1M context (single request, single replica);
    • After compression: about 256B/token, roughly 256MB for a 1M context;
    • At 32 concurrent streams, the savings are on the order of 24GB of GPU memory.

    Note that these are illustrative estimates derived from the formulas above, meant to build capacity intuition; they are not measured data from any specific implementation. Real gains depend on dimension, precision, concurrency, and kernel implementation, so always rely on your own load tests.

    The benefits break down into three chains: lower memory usage allows larger batches and longer contexts; less data scanned directly reduces latency in the memory-bound stage; and a smaller cache means fewer page swaps, flattening P99 tail latency.

    4. Implementation and Validation Checklist

    • Establish a baseline first: fix input length, concurrency, and precision, and record the indexer stage time in isolation rather than only end-to-end time, otherwise the gains get masked by other stages.
    • Watch recall quality: evaluate with both “overlap of retrieved segments before and after compression” and “downstream task metrics” so that chasing latency alone does not cause a quality regression.
    • Cover the long tail in load tests: measure at 128K / 512K / 1M separately and check whether latency grows linearly rather than quadratically with length.
    • Provide a fallback switch: keep a configuration option to disable pooling for canary rollout and rollback.
    • Observability: log cache hit rate, compression ratio, and indexer time, and feed them into monitoring and alerting to form capacity warnings.

    A load-test skeleton in pseudocode:

    1. for L in (128_000, 512_000, 1_000_000):
    2. for pool in (False, True):
    3. model.set_index_pool(pool)
    4. t = benchmark(model, seq_len=L, batch=8, warmup=3, iters=20)
    5. print(L, pool, t.p50_ms, t.p99_ms, model.index_cache_bytes())

    5. Applicability and Common Pitfalls

    • A higher compression ratio is not always better. The 4-to-1 merge is a tradeoff between saving significant memory and losing little information; pushing the ratio further requires re-evaluating recall quality.
    • Limited gains for short contexts. When sequences are short, the indexer is not the bottleneck, and enabling compression only adds a small amount of computation.
    • Do not confuse this with full attention. The optimization targets the indexer cache and does not change the fact that the sparse branch computes only a few segments.
    • Tune the hybrid architecture as a whole. The state capacity of the linear branch, the retrieval budget of the sparse branch, and the pooling group size should be tuned jointly.

    6. Summary

    The cost center of long context has shifted from the attention matrix to the indexer cache. Compressing four vectors into one with weighted pooling is a simple optimization path with quantifiable gains: the cache drops to one quarter, memory and memory-access pressure fall together, and train/inference consistency, position alignment, and a canary switch keep it controllable. For acceptance, measure indexer time in isolation, evaluate recall quality on a second track, and run tiered load tests. To serve this kind of long-context inference, general cloud capabilities such as container platforms, object storage, and monitoring and alerting can support elastic scaling and capacity governance.