KV Cache State Recovery in Hybrid Attention Models: Prefix Reuse Essentials
A prefix hash hit in a hybrid attention model only indicates that cached blocks may exist; it does not guarantee safe reuse. This article explains the five heterogeneous cache object types that must be tracked, the four-layer validation required before reporting a prefix hit, the allocator fields and responsibilities, and the consistency checks that prevent stale or torn data from being read during KV cache recovery.
1. Key Conclusion
In hybrid attention models, a prefix hash hit only means that the blocks for that prefix may exist. Reusing that prefix depends on the state of five cache object types and must pass four layers of validation. Prefix Hit should mean “this prefix can directly participate in computation,” not “the block pointer was found.”
The content below follows a general design. Field names, block sizes, and specific behavior depend on the inference framework and version. Consult the framework documentation and run tests before relying on any detail.
2. Why an Identical Prefix Is Not Enough
PagedAttention maps token history to physical KV blocks through a page table. Requests with the same prefix can share those blocks. That mechanism works for full-history attention layers when prefix hashes, block sizes, and prefix caching configuration remain consistent across requests.
Hybrid attention models use different attention modes in different layers. Full-history compressed layers need compressed historical data. Sliding-window layers only retain KV inside the window, and blocks outside the window may already be evicted. Sparse routing layers rely on an index structure. Therefore, the block numbers for a prefix may be present, but the window state, index state, or tail data may be missing. Reusing the prefix directly can read missing or stale data; output can deviate from expectations, or raise errors in severe cases.
3. Five Heterogeneous Cache Object Types
The system should separately track the five object types below. Each has independent recovery conditions and validation rules.
| Object | Lifecycle | Hit Prerequisite | Recovery Steps | Failure Behavior | Implementation Notes |
|---|---|---|---|---|---|
| Compressed KV | Stored after compressing early token history; serves ultra-long context | Compressed blocks exist; metadata needed for decompression (parameters, version) is complete | Read compressed data by block and decompress into the layout required for computation | Missing metadata causes attention to read incorrect values | Write compression parameters into block metadata; treat a version mismatch as a miss |
| Uncompressed Tail | Recently generated uncompressed tokens; frequent read/write | Tail pages are contiguous, page table is complete, no page has an unfinished write | Validate page-table contiguity and write sequence, then map physical blocks | Half-written page is reused; torn data or out-of-bounds read | Set a visibility flag only after writes finish; expose only completed pages |
| SWA Window | Historical KV inside the sliding window; data outside window is evicted | The context range required by the request is fully inside the retained window | Calculate required window start/end positions and compare with the retained range | Window boundary is one token short; model loses recent local information | Record window range with request; window length matches model configuration |
| Indexer State | Index structure used by sparse routing; determines sparse attention pattern | Index version matches the KV block version | Restore index and corresponding KV blocks together, then compare versions | Index lags behind KV; routing points to wrong positions; output quality degrades | Update index and its KV blocks as one atomic unit |
| On-disk Prefix | Prefix swapped to host memory or distributed object storage | Remote data exists, checksum passes, transfer completed | Load asynchronously to compute device; wait for completion event before participation | Compute without waiting for transfer; stale or empty data is read | Set a load-completion event; fall back to recomputation on timeout |
A prefix is usable only when the prerequisites of all five object types are satisfied. If any one is not satisfied, a hit should not be reported.
4. Allocator Responsibilities
A conventional allocator is only responsible for picking free blocks for new tokens. In a hybrid architecture, the allocator must also record the form and state of each token segment. At least these fields should be maintained:
object_type: one of the five object typesversion: version at write time; used to judge whether data is staledevice: GPU memory, host memory, or remote storagetoken_range: token start/end positions coveredcontiguous: whether tail pages are contiguous and completeindex_version: whether index state is on the same version as KV blockspending_io: whether there is an unfinished data transferref_count: current number of requests referencing this object
The allocator interacts with prefix matching in the following order:
def match_prefix(request):candidate = hash_lookup(request.tokens) # 1. hash gives a candidate prefixsafe_len = candidate.lengthfor obj in FIVE_OBJECTS: # 2. query usable length for each object typesafe_len = min(safe_len, obj.usable_length(request))safe_len = align_to_lcm(safe_len) # 3. align to whole blocks; see Section 5if safe_len == 0:return 0start_async_load_if_needed(safe_len) # 4. mark objects being transferred as pendingif not all_ready(safe_len):return 0 # 5. do not report a hit this time if not readyreturn safe_len
5. Four Layers of Validation
Prefix Hit must pass the following four validation layers.
LCM block-alignment validation: Different cache groups may have different block sizes, for example, sliding-window group and full-history group. Use the least common multiple (LCM) of the group block sizes as the unified reuse granularity; the hit length must be an integer multiple of it. Validate when calculating
safe_len. On failure, round down to the nearest integer multiple of the LCM; do not reuse half blocks.Kernel ABI validation: Check whether dtype, memory layout (contiguous or paged), stride, and head dimensions match the signature of the current attention operator. Validate before reporting a hit. On failure, use a repacking path or abandon reuse.
State intersection of multiple cache groups: Each cache group reports its own usable length; take the minimum of all groups as the final length. Validate after all groups have finished queries. On failure, truncate to the intersection; do not handle the remainder.
Byte-level alignment validation: The block start address must satisfy the alignment requirements for vectorized reads in the operator. The common requirement is 16 bytes or higher, depending on the operator documentation. Validate when allocating or mapping. On failure, do not reuse that block; copy it to an aligned address.
Checklist before going live:
- LCM of each group’s block size divides the hit length
- dtype, layout, stride, and operator signature are consistent
- usable length from each cache group is reduced to the minimum
- block start address meets operator alignment requirements
6. Consistency Checks
The system needs consistency checks to prevent the state machine from entering invalid states.
Admission cap: Set memory quotas per object type to prevent one object type from occupying all GPU memory. Quotas are determined by the deployment owner from load tests. For example, the cap for one object type could be 40% of memory; this is only an example value. Monitor rejection rate and alert when it stays above the configured threshold.
Five-way state routing: Every write carries an object-type label; the write pipeline routes by label. If the type does not match the pipeline, fail the assertion immediately to avoid state contamination.
TOCTOU protection and state data pointers: There is a race between check time and use time. For example, request A checks that a sliding-window block is valid; an eviction thread then reclaims the block and assigns it to request B; when request A reads the block again, it sees new data. Protection steps:
- Step 1: Check block state
- Step 2: Increment reference count (pin) to prevent eviction
- Step 3: Check version number again to confirm state has not changed
- Step 4: Use the data
- Step 5: Decrement reference count (unpin)
If Step 3 fails, roll back the reference count.
