Heterogeneous KV Cache for Hybrid Attention LLMs: A Practical Guide
This article explains why traditional prefix reuse breaks in hybrid attention LLMs and how to make KV cache management heterogeneous. It covers the five cache object types and their hit conditions, allocator changes that track segment form, four layers of prefix hit validation, and consistency rules for safe reuse in long-context inference.
1. Why Prefix Reuse Rules Break Down
In long-context inference serving, paged attention caching is a widely adopted general-purpose approach. Each request’s token history is split into fixed-size pages, and a page table maps logical pages to physical GPU memory blocks. Requests with identical prefixes can share existing blocks directly, avoiding redundant prefilling. This mechanism relies on an implicit assumption: the attention semantics of every layer are homogeneous, so “identical prefix tokens” is equivalent to “identical reusable cache content.”
Hybrid attention architectures break that assumption. In these designs, different layers mix different forms of attention: some layers produce a compressed representation over the full history, some retain raw keys and values only within a sliding window, and others use sparse routing to access only selected historical positions. Once these three semantics are combined, the reusable artifacts left by the same prefix differ completely across layers, and the rule “a hash hit means computation can resume directly” no longer holds.
The difference is similar to a change in the library book-retrieval process. In a homogeneous model, finding the shelf number is enough to retrieve the book. In a heterogeneous cache, after finding the number you must confirm five things: whether the compressed copy is on the shelf, whether the latest pages are bound, whether the borrowing window is still valid, whether the index catalog is synchronized, and whether an offline copy has yet to be returned. Only when all five conditions are met can reading truly continue.
2. Five Cache Object Types: Lifecycles and Hit Conditions
In engineering practice, the objects that must be tracked explicitly fall into five types. Their lifecycles, recovery conditions, and hit conditions are independent of one another.
- Compressed keys and values: produced by full-history compression layers, with a lifecycle spanning the entire context; once generated, they can be reused for a long time. Recovery condition: the compressed block is complete and the corresponding compression parameters match.
- Uncompressed tail: raw keys and values for the most recent tokens, appended continuously as generation proceeds, with a very short lifecycle. Recovery condition: the tail length and its page-boundary alignment are clearly defined.
- Sliding window cache: retains keys and values only within the window; old data becomes invalid naturally as the window slides. Recovery condition: the window position matches the current step.
- Index state: the directory and selection results that sparse routing layers depend on, updated incrementally as history grows. Recovery condition: the index version matches the key-value version.
- Offloaded prefix: historical prefixes swapped out to external storage due to insufficient GPU memory, with the longest lifecycle but the highest recovery cost. Recovery condition: the swapped-out block can be read back and passes validation.
These five object types correspond to three typical trigger scenarios: mixed layer semantics, context length exceeding single-GPU memory capacity, and prefix sharing across requests. Any of these scenarios can push cache state into a situation that a single rule cannot cover.
3. Allocator Changes: From Picking Free Blocks to Recording Form
A traditional allocation action only picks a free GPU memory block and writes new tokens into it. Once cache objects become heterogeneous, the allocator must additionally record the current form of each token segment; otherwise a mismatch appears in which the matching stage reports a hit but the reading stage lacks data. Downstream, a hash-based prefix hit is declared, yet at execution time the tail, the window, or the routing information is missing, and generation results become incorrect.
A schematic metadata structure can be expressed as follows:
@dataclassclass SegmentMeta:seg_id: intform: str # compressed / tail / swa / index / offloadpage_ids: list[int]version: intvalid_range: tuple[int, int]aligned: bool
Allocation and matching must be designed as a pair. The matching stage should output not just a boolean hit, but the hit length and a per-segment form list. The scheduler then decides which segments can be reused directly, which must be recomputed, and which must be read back first.
4. Four Layers of Hit Validation
For a prefix hit to be genuinely usable, it must pass four layers of validation in sequence:
- Page alignment validation: round the hit length down to whole pages to avoid partial reuse across page boundaries.
- Compute kernel interface consistency validation: the operator signatures and parameter layouts for different attention forms must match, otherwise reused data will be misinterpreted.
- Multi-cache-group state intersection validation: hybrid architectures usually maintain multiple cache groups, and a hit counts only when all groups are simultaneously valid.
- Byte-level hardware alignment validation: data types, strides, and address alignment must satisfy low-level memory access requirements.
5. Five Consistency Rules That Must Not Be Violated
- Admission limit: set a resource cap when swap-in and prefilling run concurrently, to prevent GPU memory from being saturated instantly.
- State routing: route recovery through separate paths for each of the five object types; a unified fallback logic is prohibited.
- Temporal consistency: keep the state snapshot consistent between hit determination and data reading, to avoid the state being modified after the determination.
- Static topology constraints: in graph-capture mode, addresses and shapes are fixed, so dynamic swap-in and swap-out must go through an explicit update process.
6. Practical Recommendations
For implementation, first separate cache metadata from the data itself and link them with version numbers. Then instrument each of the five object types to track hit rate and recovery time. Finally, compare the quality difference between “strict validation” and “loose reuse” on a small traffic share. On general-purpose capabilities such as containers and monitoring alerts offered by cloud platforms like Baidu AI Cloud, these metrics can be turned into an observability dashboard to continuously calibrate the reuse strategy, balancing throughput and generation correctness in long-context scenarios.
