Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Remembering context efficiently

KV sharing across layers: could later layers reuse an earlier cache?

Cross-layer attention (CLA) shares cached keys and values between layers. Could later queries reuse history that an earlier layer already prepared?

The KV cache saves each attention layer from recomputing earlier tokens’ keys and values. But every layer normally makes a different set, so the model keeps many histories of the same conversation.

Grouped-query attention shares keys and values between heads within a layer. Could we also share them between layers?

What must a later layer calculate for itself?

Attention needs a query and some keys and values. The query comes from the current token representation. The keys and values usually come from representations at that same layer, but the calculation does not require that origin.

Suppose an earlier layer supplies K and V. A later layer could make a fresh query from its own input, compare that query with the earlier keys, and collect the earlier values.

Calculated values
Two attention layers read one set of keys and valuesCurrent token at layer 1Earlier context is dimmed; outlined columns are current.Current token computation.Layer 1: make Q, K, VEarlier context is dimmed; outlined columns are current.Current token computation.Q₁One K/V historyEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.KVAttention readEarlier context is dimmed; outlined columns are current.Current token computation.Next layerLayer 2: make Q onlyEarlier context is dimmed; outlined columns are current.Current token computation.Q₂Attention readEarlier context is dimmed; outlined columns are current.Current token computation.Layer 2 result
A schematic pair of sharing layers. Layer 1 produces the K/V history; layer 2 calculates a new query and reads that same history. Neither the queries nor the attention results need to match. Each attention read includes scoring, masking, softmax and value collection. Residual additions, output projections and feed-forward branches between these reads are omitted.

The later layer can ask a different question of the same stored history. Its query and attention weights can differ, so sharing K/V does not force it to return the same result.

Its learned projections must be trained for this arrangement: we cannot simply remove arbitrary K/V projections from an existing model and expect the remaining queries to work with another layer’s cache.

What have we saved?

The borrowing layer no longer needs its own K/V projections or another set of cached entries. The source layer still appends keys and values as tokens arrive. Attention still scores that history for each new query.

In a simple example, give four equal-width attention layers one cache per pair. We keep two K/V histories instead of four. The growing KV storage is halved, while all four layers still perform attention reads.

This is a reduction in copies of the history, not a fixed-size memory. Each full-attention source cache still grows with the conversation. And halving KV storage does not halve total memory or generation time: model weights, attention reads and the rest of the network remain.

Which earlier layer should supply the history?

We do not have to share only between adjacent pairs. Several later layers can borrow from the same source. But each loses the ability to create its own keys and values from its deeper token representations.

Gemma 3n uses a specific arrangement: the final 10 layers in E2B, or 15 in E4B, reuse the K/V states from the last earlier layer of the matching attention type. Local-attention layers borrow from a local source; global-attention layers borrow from a global source. Those later layers still make their own queries.

That matching matters. A shared cache needs compatible head dimensions, position handling and an available history that supports the borrowing layer’s attention mask. Sharing is part of the architecture, not just a choice to point two layers at the same memory allocation.

How does this differ from a hybrid?

In the hybrid article, some layers replaced attention with a fixed-state mixer. Here, the borrowing layers keep attention, but reuse another layer’s keys and values.

Both approaches reduce how many growing caches the model needs. They give up different things: a hybrid layer uses another mixing rule; a sharing layer keeps the attention calculation but gives up its own separately projected K/V history.

Sources and assumptions

The drawing is a conceptual two-layer sharing group, not Gemma 3n’s full layer order. Residual updates, output projections, position transforms, normalisation and feed-forward branches are omitted. The four-layer arithmetic assumes equal KV head counts, head widths, context lengths and storage formats; real models may mix local and global caches. Gemma 3n also passes source K/V states during prompt processing, where a sliding cache alone might no longer hold every position needed for that forward pass. The article does not claim its total memory falls by the simple pair-sharing ratio. Blue/orange cells identify calculated data rather than numerical signs.

← Browse all subjectsBack to the beginning ↑