Prefix Cache Hits Don't Guarantee Reuse Under Hybrid Attention Architectures
Prefix caching is a standard optimization in autoregressive LLM inference, but it relies on a hidden assumption: that a matched prefix maps to one complete, reusable state. Hybrid attention architectures break this assumption. This article explains why cache hits can be unsafe under mixed attention forms, the five types of state that must be tracked explicitly, how allocators need to change, and the four validation layers required before a cached prefix can safely continue computation. Includes a reference code example for state-aware prefix matching.
1. The Premise of Prefix Caching: One Prefix, One State
In autoregressive inference, generating each token requires reading the Keys and Values of all previous tokens. These intermediate results are collectively known as the KV cache. When the same prefix (such as a system prompt or multi-turn conversation history) appears across different requests, it produces exactly the same KV data, so inference systems can cache it and reuse it directly in later requests, eliminating redundant computation.
In practice, the token sequence is typically divided into fixed-size blocks, logical blocks are mapped to physical blocks in GPU memory via a paging mechanism, and prefix hashes are used as indexes. On lookup, the system finds the longest matching prefix by hash and reuses it if found. This design carries an implicit assumption: if a block hits, all layer data within that block can be used directly.
2. Hybrid Attention Breaks This Assumption
When different layers of a model use different attention forms, the assumption no longer holds. Common forms include:
- Full-history compression: earlier history is compressed into shorter representations to preserve global context;
- Sliding window: only the KV data within the most recent window is retained;
- Sparse routing: each query accesses only a selected subset of tokens, relying on an index structure to determine which positions to visit.
The same sequence of tokens leaves behind different data shapes at different layers. Even if a prefix’s hash matches perfectly, its compressed representations, window states, or index information may be incomplete. For example, the uncompressed portion at the tail may not have finished computing, or the data may have been offloaded to external storage.
Long contexts make the problem worse. The longer the context, the larger the KV footprint. Once it exceeds a single GPU’s memory, the system migrates part of the cache to host memory or disk and loads it back on demand. Cross-request reuse then carries these states into more business scenarios, multiplying the combinations of states involved.
3. Five Types of State That Must Be Tracked Explicitly
A reusable prefix can be broken down into five parts. Each has a different lifecycle, recovery path, and hit criterion.
- Compressed KV: representations produced by full-history compression. The recovery condition is that compression has covered the corresponding range.
- Uncompressed tail: the most recent tokens that have not yet filled a compression granularity. They exist only in raw form and must be fully preserved and restored as-is.
- SWA window: the KV data covered by the sliding window. Data outside the window can be discarded; data inside must be complete.
- Indexer state: the index structure required for sparse routing. If it is missing, queries cannot determine which positions to access.
- On-disk prefix: portions already migrated to external storage. After a hit, the data must be loaded back to GPU memory first; it cannot participate in computation until the load completes.
To determine whether a prefix can be directly reused, all five states must satisfy their recovery conditions. If any one is missing, the prefix should be treated as a miss—or reused only partially.
4. Allocators Must Track Data Shape, Not Just Location
A traditional allocator answers a single question: where in GPU memory does this block live. With heterogeneous caches, it must also answer a second question: what state is the data in this block currently in.
Two operations need clearly separated responsibilities:
- Allocation: pick a free block for new tokens, write the data, and register the block’s state type;
- Prefix matching: look up the longest matching prefix by hash, and check the state records for each segment.
If a hit is reported based on hash alone, without checking state, this can happen: the hashes match, but the tail or window data is missing at read time, and generation produces incorrect results. The following example code shows how to check each state item during matching.
from dataclasses import dataclass@dataclassclass SegmentState:compressed_kv: bool # whether the compressed representation is readytail_kv: bool # whether the uncompressed tail is completeswa_window: bool # whether the sliding window data is completeindexer: bool # whether the indexer state is readyon_disk: bool # whether the data resides in external storagedef is_reusable(state: SegmentState) -> bool:# Segments in external storage must be loaded back first;# they are not considered reusable until thenif state.on_disk:return Falsereturn all([state.compressed_kv, state.tail_kv,state.swa_window, state.indexer])def longest_reusable_prefix(hashes, lookup):"""hashes: block hashes in orderlookup: returns (physical block location, SegmentState) on hit, else None"""hit = 0for i, h in enumerate(hashes):entry = lookup(h)if entry is None or not is_reusable(entry[1]):breakhit = i + 1return hit
This example only illustrates the decision logic; a real implementation must be adapted to the specific framework’s data structures and scheduling strategy.
5. Four Validation Layers Determine Whether Computation Can Continue
Even when both the hash match and the state records check out, the prefix may still not be safe to continue from directly. Four additional validation layers are typically required:
- Block alignment: different cache groups may use different block sizes, so alignment must be done at the least common multiple. Otherwise, data at boundaries becomes misaligned.
- Kernel interface constraints: compute kernels impose fixed requirements on input layout, strides, and data types, and the prefix data must satisfy them.
- Intersection of multi-cache-group states: the longest common prefix where every cache group hits is the usable prefix. If any group lags behind, it limits the overall result.
- Byte-level hardware alignment: storage addresses and lengths must satisfy hardware access requirements. When they don’t, memory access efficiency drops and errors may even be triggered.
If any of the four layers fails, the reusable prefix should be shortened rather than risking a continuation.
6. Four Rules the Allocator State Machine Must Follow
- Admission cap: a new request’s cache demand must not exceed available capacity. When it does, the request should be queued or degraded to avoid mid-run failures.
- Separate handling for the five states: each state type has its own recovery path and cannot share a single generic logic.
- Race between lookup and allocation: there is a time gap between a lookup result and the actual allocation. If a block is reclaimed or migrated in between, the lookup result becomes invalid. Critical steps must therefore be re-validated, and data should be referenced through state pointers rather than raw addresses.
- Static topology: use GPU graph capture.
