Cache Management and Tuning Practices for Hybrid Attention LLM Inference
Inference for hybrid attention large language models (LLMs) presents a unique challenge: a single request simultaneously holds multiple forms of caches, while the scheduler aims to manage them uniformly. This article breaks down the differences in cache forms and systematically details token coordinate mapping, tiered cache pools, prefix reuse evaluation, incremental compression, and pre-launch checklists. Specific performance metrics are omitted; parameters should align with model configurations and actual stress testing.
1. The Core Problem: Contradiction Between Multiple Cache Forms and Unified Scheduling
In traditional Transformer inference, the KV Cache shape is consistent across all layers, and the cache grows linearly with sequence length. The scheduler simply allocates contiguous or paged memory blocks for each request, making management relatively straightforward.
Hybrid attention breaks this consistency. The model typically contains two types of layers simultaneously:
- Sliding Window Attention (SWA) layers: Only attend to the most recent W tokens, where W is determined by model configuration. The cache length has a fixed upper limit.
- Compression Attention layers: Compress historical tokens into block representations by step size s. The cache grows with context but at a much slower rate than raw KV. The specific step size and block size depend on model configuration; exact numbers are not provided in this article.
Compression attention is further divided into two modes: the fine-step mode preserves finer historical information, while the coarse-step mode (step size denoted as S) covers more distant history. These two modes are distributed across different layers. This distribution is determined during training by the model structure and written into the model configuration. The runtime scheduler does not dynamically switch layer modes. Therefore, the number of cache pools, the shape of each layer, and the block size are fixed upon model loading, and the scheduler simply queries by layer index.
Let n be the number of tokens processed for a request. The characteristics of the three cache types are as follows:
| Cache Type | Shape | Growth Method | Release Condition | Prefix Reusability |
|---|---|---|---|---|
| SWA layer | Ring buffer, length W | Overwrites oldest position when limit reached | Token slides out of window | Only window state is valid |
| Fine-step compression layer | Appended by block, blocks ~ n/s | Adds one block per s tokens | Request ends or is evicted | Reusable only at complete block boundaries |
| Coarse-step compression layer | Appended by block, blocks ~ n/S | Adds one block per S tokens | Request ends or is evicted | Reusable only at complete block boundaries |
Additionally, each compression layer has a tail state that has not yet filled a complete block, referred to here as the pending state.
The server needs to answer four questions. First, how does the same token correspond across different caches? Second, when should old data be released once the window cache is full? Third, where should the pending state be stored? Fourth, upon a prefix cache hit, which pools can be reused and which must be recomputed? The following two sections provide the answers sequentially.
2. Unified Token Coordinates and Tiered Cache Pools
The server defines a logical position p for each request, representing the token sequence number within the request, starting from 0. All pools derive their physical positions from p; this serves as the unified coordinate.
The coordinate mapping rules are as follows:
- SWA pool: The physical slot is p mod W. Writing overwrites the oldest slot.
- Compression pool: The block number is floor(p / s), and the intra-block offset is p mod s. A compressed representation is generated and written to the corresponding block number only when a block is full.
- Pending pool: Saves token states that have not yet filled a complete block. This pool is exclusive to each request and does not participate in prefix sharing.
Under these rules, Question 1 has a clear answer: the correspondence of position p across various pools is directly derived by the formulas above, so the scheduler does not need to maintain an additional mapping table.
The answer to Question 2 depends on the pool type. SWA slots can be reclaimed after a token slides out of the window, but if the position is still referenced by prefix caching, it can only be released once the reference count reaches zero. Compression blocks are released when the request ends or is evicted.
The answer to Question 3 is to store it in the pending pool. This pool does not enter the prefix cache because its content depends on unfinished blocks; sharing it would cause different requests to read incomplete states.
Regarding pool design, the SWA, compression, and pending pools each use independent block sizes, allocators, and reference counts. Mixing blocks of different sizes into a single allocator easily causes memory fragmentation and makes troubleshooting more difficult.
3. Prefix Reuse and Sparse Retrieval
Let L be the prefix hit length. Reuse evaluation is performed per pool:
- Fine-step compression pool: The reusable length is floor(L / s) × s, meaning only complete blocks are taken. Remaining tokens within the block require recomputation or restoration from the pending state. The restoration method depends on whether the implementation saved the tail state.
- Coarse-step compression pool: The rule is the same as above, with block boundaries at integer multiples of S.
- SWA pool: Only the states of the most recent W tokens prior to L are valid. If the window state is saved in the cache, it can be reused; otherwise, the last W tokens must be recomputed.
The reusable length for the entire request is the minimum reusable length across all pools. A prefix hit is valid only if the conditions for all pools are satisfied.
The key to sparse retrieval is index alignment. When performing retrieval selection on compression blocks, the retrieved block numbers must map back to physical positions in the pool, and the position information corresponding to the blocks must match the original sequence. Number misalignment will cause retrieval results to point to incorrect historical content, and such errors do not necessarily trigger exceptions.
4. Incremental Compression
During the decoding phase, every newly added token is first written to the pending pool. When the number of tokens in the pending state reaches s, a compression operation is executed to generate a block representation, which is written to the compression pool, and the pending state is then cleared. The period for coarse-step layers is S, and the processing method is identical.
The prefill phase can process an entire input segment at once. If the input length is not a multiple of s, the tail r = length mod s tokens enter the pending state. Subsequent decoding must correctly continue from this tail state; otherwise, the generated results will deviate at the concatenation point.
Tuning steps are as follows:
- Collect statistics on the length distribution of the pending state after prefill to ensure the tail restoration logic covers all length scenarios.
- Observe whether compression triggers concentrate on the same scheduling step. If they do, this will create compute spikes; staggering the compression moments of different requests can mitigate this.
- After adjusting the block size, conduct stress tests under batch sizes close to production environments. Do not base judgments solely on single-request results.
This article does not promise specific acceleration ratios brought by compression; actual gains must be measured in the target environment.
5. Disaggregated Deployment and Pre-Launch Checklist
When prefill and decode are deployed on separate machines, caches must be transmitted across machines. The transmission includes the states of all pools as well as the pending state. The transmission size for a single request can be estimated by the following formula: the sum of bytes per token across all pools multiplied by the token count, plus the size of the pending state. The SWA pool has a fixed upper limit, while the compression pool grows slowly with the context. Therefore, transmission growth under long contexts primarily comes from the compression pool.
Pre-launch checklist items are as follows:
- Cache hit rate: Track statistics per pool. Do not look only at the overall hit rate, otherwise a failure in a specific pool may be masked.
- Pool usage and watermark: Set alert thresholds individually for each pool to prevent one pool from filling up and dragging down the performance of others.
