State Cache Rebuilding for Hybrid LLMs: Prefix Reuse in Mixed Attention and Linear Architectures
This article examines why prefix caching becomes harder in hybrid large language models that alternate full attention layers with linear or state space layers. It explains the difference between key-value caches and recurrent states, identifies three root causes of prefix reuse failure, and presents a general design that treats state as a first-class citizen through a layered state store, checkpoint-aligned snapshots, consistency-first eviction, tiered caching, and prefill batch scheduling. It also covers the sources of performance gains and key engineering considerations for production deployment.
1. Background: Why Cache Management Gets Harder When Hybrid Architectures Go Mainstream
Recent open-source LLM designs increasingly introduce linear attention and state space layers—such as various gated incremental update mechanisms and structured state recurrence layers—stacked alternately with full attention layers into a hybrid structure. The motivation is clear: full attention layers handle precise global information retrieval, while linear layers carry long-range history in a fixed-size state. This reduces per-step computation during decoding and compresses cache usage from linear growth with context length to near-constant.
The catch is that the benefit shows up most clearly in single-request decoding, whereas real workloads are often multi-turn, long-context scenarios with heavily overlapping prefixes, such as intelligent coding tools and document assistants. In these cases the bottleneck shifts from decoding compute to prefix reuse efficiency, and cache reuse in hybrid architectures is significantly harder than in pure attention models.
2. The Fundamental Difference Between the Two Cache Types
To understand where the optimization room is, start by separating two kinds of state:
- Key-value cache: every attention head in every layer keeps the keys and values of historical tokens. Capacity grows linearly with sequence length, but it can be truncated, concatenated, and reused at arbitrary token granularity.
- Recurrent state: linear layers compress history into a fixed-size matrix or vector. Capacity is constant, but the state is a product of fully coupled history and cannot be simply split by token.
This leads to a key conclusion: a key-value cache can be sliced on demand, while a recurrent state can only be computed from the beginning or replaced as a whole. Prefix reuse in hybrid models is therefore not a simple memory copy problem, but a problem of state consistency and splittability.
3. Three Root Causes of Prefix Reuse Failure in Hybrid Models
- Granularity mismatch. Prefix caches are usually managed in blocks, but recurrent states have no natural block boundaries, so block-level hits cannot map directly to state snapshots.
- Snapshot cost. Supporting reuse at arbitrary positions requires periodically persisting state snapshots as the sequence advances, and snapshot frequency conflicts directly with storage overhead.
- Eviction policy mismatch. Pure attention models can safely drop unmatched trailing blocks; a hybrid model that drops some key-value blocks while keeping the old state creates an inconsistency between context and state, degrading output quality in ways that are hard to diagnose.
4. A General Design: Treat State as a First-Class Citizen
A reusable design approach is to abstract the cache as a layered state store, so the scheduler only faces a unified interface:
class StateStore:def lookup(self, prefix_hash) -> StateHandle: ... # return a reusable handle on a hitdef snapshot(self, state, seq_pos) -> None: ... # periodic snapshotdef evict(self, policy) -> None: ... # unified evictiondef restore(self, handle) -> State: ... # restore key-value and recurrent state
On top of this, there are four practical points:
- Align snapshots with checkpoints: use a fixed interval (for example, persisting state every few layers) to get a predictable storage upper bound, downgrading “reuse at any position” to “reuse at the most recent checkpoint plus a small amount of recomputation.”
- Consistency-first eviction: use “key-value blocks and their corresponding state snapshots” as the minimum reclamation unit to avoid partial hits.
- Tiered caching: keep hot prefixes in GPU memory, and use host memory and object storage for warm and cold data, forming a gradient of capacity and latency.
- Prefill batch scheduling: schedule requests that hit a cached prefix separately from requests that need full prefill, reducing queueing pressure that long sequences impose on short requests and improving tail latency.
5. Where the Gains Come From and Engineering Notes
In public research, similar approaches replaying real coding tool traces on high-end accelerator cards achieve several times the prefill throughput of general-purpose inference frameworks, with a substantial drop in p90 time-to-first-token; the gains hold after adding host memory caching at the hundreds-of-gigabytes scale. The source of these numbers is not a single algorithm, but the combination of “less repeated prefill + higher cache hit rate + better queueing.”
For production, watch the serialization cost of state snapshots, cross-node transfer bandwidth, cache consistency checks, and the fact that hit determination must include a model version and configuration fingerprint to avoid silent errors from version drift. For cloud deployment, an elastic cache layer can be built from general capabilities such as container platforms, object storage, message queues, and monitoring and alerting. In long-context inference scenarios like these, Baidu AI Cloud likewise needs to plan capacity around core metrics such as cache hit rate, time-to-first-token, and tail latency.
