KV Cache State Consistency Under Hybrid Attention
Hybrid attention breaks the assumption that an identical prefix is enough to reuse KV cache. This article explains why prefix hits can still be incomplete, models five independent cache objects and their recovery rules, and describes how an allocator state machine, four validation layers, and four consistency boundaries keep reuse correct without silent generation errors.
1. Background: The Hidden Assumption Behind Prefix Cache Reuse Is Failing
In a traditional PagedAttention inference architecture, the KV cache is split into fixed-size logical pages and mapped to physical GPU memory blocks through a page table. When two requests share the same prefix token sequence, a later request can reuse the physical blocks already cached by an earlier one and skip repeated prefill. This mechanism has worked for a long time in the open-source inference community, and its correctness rests on a simple assumption:
The same prefix means the cached content is complete and consistent, so computation can continue directly.
With hybrid attention, different layers use entirely different attention patterns. Some compress the full history, some attend only to a recent window, and others selectively access historical tokens through sparse routing. In that setting, “the same prefix” is no longer sufficient to decide that the cache is usable. A prefix that appears to hit may be missing its tail, its window, or its routing index, or critical data may still be on disk and not yet loaded. If the inference framework reuses the cache solely because a hash matches, generation can fail silently—one of the hardest classes of production inference bugs to diagnose.
2. Five Cache Objects: Lifecycles and Recovery Rules
The first step toward handling heterogeneous caches is to abandon the old mental model of the KV cache as one contiguous byte region. The cache should instead be modeled explicitly as five independent objects:
- Compressed KV: A coarse-grained representation produced by full-history compression layers. Its lifecycle is bound to the compression algorithm. Recovery usually means running compression again; it cannot be rebuilt token by token from the original KV.
- Uncompressed tail: Exact KV for the most recent tokens, used to preserve precision for recent context. It is appended on every decode step, and a hit requires only prefix-token alignment.
- SWA window: The valid window for sliding-window attention layers. The window moves as the sequence advances, so historical blocks that are still in GPU memory may still be unusable because the window has expired.
- Indexer state: The retrieval index maintained by sparse-routing layers. The index must stay synchronized with the indexed KV. If a block is evicted or rewritten and the index is not updated, routing points to the wrong location.
- On-disk prefix: Prefix blocks swapped to disk storage for long-context workloads. A hit is usable only after the data has been read back into GPU memory and the read-back does not conflict with writes.
A library analogy makes the difference clear. In a traditional model, finding a shelf number is enough to take the book. In a hybrid-attention model, the shelf number is only the start. You still have to confirm that the book is on the shelf (compressed KV), that the latest pages are bound (the tail), that the lending card has not expired (the SWA window), that the catalog index is in sync (indexer state), and that the book has not been left in an off-site warehouse (on-disk prefix). Only when all five checks pass can reading actually continue.
These five objects have different lifecycles, recovery conditions, and hit logic. They cannot share one eviction and matching policy; each needs its own metadata.
3. The Allocator Becomes a State Machine, Not Just a Free-Block Finder
In a traditional implementation, the allocator has one job: pick a free GPU memory block for a new token. Once cache objects are heterogeneous, that job has to expand:
- The cache for every token span must record its current form: compressed, raw, windowed, indexed, or stored on disk.
- Prefix matching by hash in the cache manager must be decoupled from reuse. A hit means reusable data may exist; it does not mean the data is usable right now.
- On the generation path, the allocator must drive each object through its own recovery flow as a state machine.
A schematic state-management interface looks like this (pseudocode):
class HybridKVAllocator:def try_reuse(self, prefix_hash) -> ReusePlan:entry = self.prefix_table.lookup(prefix_hash)if entry is None:return ReusePlan.NEED_PREFILLactions = []for obj in entry.objects: # check each of the five objectsstate = obj.check(self.seq_progress)if state is ObjState.EVICTED:return ReusePlan.NEED_PREFILLelif state is ObjState.ON_DISK:actions.append(obj.stage_back_to_gpu())elif state is ObjState.STALE_WINDOW:actions.append(obj.recompute_window())elif state is ObjState.INDEX_OUT_OF_SYNC:actions.append(obj.rebuild_index())return ReusePlan(partial_prefill=entry.tail_len, actions=actions)
The core rule is to separate hit detection from reuse eligibility. A hit only starts a recovery plan. Computation may begin only after recovery finishes.
4. Four Validation Layers: Confirming a Hit Can Actually Continue
Even after all five objects recover successfully, four validation layers are still required before the cache is attached to computation:
- LCM whole-block check: Attention patterns require different token block sizes. Alignment must use the least common multiple of those block sizes so a partial block is never reused.
- Kernel ABI check: Hybrid attention introduces multiple attention kernels. Each kernel has specific conventions for cache layout, data type, and head dimension. Layout compatibility must be confirmed before reuse.
- Intersection of multiple cache-group states: One hit may involve several cache groups—compressed, tail, window, and index. Reuse is allowed only when every group is usable and their intersection is non-empty and consistent.
- Byte-level hardware alignment: GPU kernels have hard address-alignment requirements. The reused block’s base address, size, and stride must all satisfy those constraints. Otherwise the runtime can crash or produce undefined behavior.
The cache manager should run all four checks in one place. A failed check must fall back safely to partial prefill rather than fail the request.
5. Four Consistency Boundaries
Whatever the implementation becomes, these four consistency rules must not be broken:
- Admission cap: The number and size of objects admitted to the cache must stay within the GPU memory budget, so recovery itself does not trigger unexpected eviction.
- Separate paths for five states: Matching, eviction, and recovery for the five objects must be explicitly separated. A single generic path must not guess the state.
- TOCTOU and state_data_ptrs: There is a time window between check and use. Data that was in GPU memory at check time may have been moved or freed by use time. Stable pointers or version numbers are required.
- Static CUDA Graph topology: With static graph optimization, the cache address topology must not change implicitly after capture. Any reallocation must be coordinated with the graph’s static topology.
6. Engineering Practices
- Metadata first: Keep a separate metadata table for each of the five cache objects, recording version, location, dependencies, and recovery cost. Configure eviction per object type.
- Observable recovery: Record whether each of the five checks passed on every hit. Track hit rate, partial-hit rate, and fallback rate so performance hits can be distinguished from correctness hits.
- Fall back rather than fail: If any check fails, fall back to partial prefill and keep correctness first.
- Tiered storage for long context: When cold prefixes are written to disk, retain index and validation information, and control concurrency and consistency on read-back.
- Test state combinations: Build differential tests for failure combinations across all five objects, and compare hybrid-attention and dense-attention outputs on the same inputs.
7. Summary
Hybrid attention turns the KV cache from homogeneous paging into a set of heterogeneous states. A prefix hit is no longer a simple condition; it is a multi-object recovery process that needs an explicit state machine. The sound engineering path is to model lifecycles around the five objects—compressed KV, uncompressed tail, SWA window, indexer state, and on-disk prefix—let the allocator track state, run the four validation layers after a hit, and keep the four consistency boundaries intact. For inference-engine developers, this state model removes silent generation errors under hybrid attention and is a foundation for serving million-token contexts.
