Rúben GalvãoThe Rabbit Hole

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

Shared K/V: could one entry do both jobs?

MQA already shares the cache across query heads. Could tying K and V reduce its memory further?

Multi-query attention (MQA) already saves memory by letting every query head read the same keys and values.

Could we use even less memory?

One shared cache still stores two vectors

MQA shares entries between heads, but it still makes a key to score and a value to collect. With 512 coordinates in each, that is 1,024 stored numbers per position.

The key and value have different jobs. But do those jobs require different vectors? We could learn one projection and use its result for both:

Separate key and value projections compared with one shared projectionMQA: separate K and VEarlier context is dimmed; outlined columns are current.Current token computation.MQA + tied K/VEarlier context is dimmed; outlined columns are current.Current token computation.WkEarlier context is dimmed; outlined columns are current.Current token computation.KWvEarlier context is dimmed; outlined columns are current.Current token computation.VWkvEarlier context is dimmed; outlined columns are current.Current token computation.K = VStored per position: 2 × 128Stored per position: 1287,1687,1687,1681281287,1681281287,168128128
Both designs share their caches across query heads. Tying the K/V projection additionally replaces two stored vectors per position with one.

We would store 512 numbers per position instead of 1,024. At the same vector widths and precision, that halves the stored K/V data on top of the saving from MQA.

Read the same vector in two ways

The attention operation already has the two uses we need. Compare the query with the stored entry to get its attention weight, then use that weight to collect information from the same entry:

The same cached entries supply keys and values; only the newest attention row is computedQueriesShared entries for K and VEarlier context is dimmed; outlined columns are current.Current token computation.Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.CLCLAs KAttention mapQuery–key dot products, scaling, causal masking and softmax. Only the current query's last row is new; earlier rows are dimmed for context.CLCLSame VThe highlighted attention row weights every available shared entry as a value.×Earlier context is dimmed; outlined columns are current.Current token computation.CLCollected values192128128
The same shared entries provide K for the attention map and V for the weighted sum. Earlier context is dimmed; the outlined last column is new. Only the last attention row and collected output are highlighted for the current query.

The trade-off is in what the model can learn. Separate projections let matching and content use different features. Tying them requires one representation to work for both scoring and supplying information to the result.

This is the shared K/V representation used by DeepSeek-V4. One learned Wkv produces the entry; the label Wk = Wv in the worked diagrams describes this tying.

Keep the cache shared across query heads

We keep the head-sharing arrangement from MQA. The tied entries stay outside the head stack. Each head still has its own query, its own attention weights and its own collected result:

The input still makes a different query for each head. Each head’s attention map weights the shared entries as values, then its output projection makes an update for the residual stream:

Complete attention path with one shared key-value projection and cacheToken representationsEarlier context is dimmed; outlined columns are current.Current token computation.CLResidual streamWkvShared entries (K = V)Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.CLWq / headQEarlier context is dimmed; outlined columns are current.Current token computation.CLAttention mapEach head forms its own attention map. Only the last query row is new.CLCLSame entries as V×CollectedEarlier context is dimmed; outlined columns are current.Current token computation.CLWo / headHead updateEarlier context is dimmed; outlined columns are current.Current token computation.CL+Sum head updates+Earlier context is dimmed; outlined columns are current.Current token computation.CLUpdated token representations7,1687,168128128128 heads7,1681921921281287,1687,1687,168
The complete attention path with MQA and tied K/V. Wkv and the cache are shared outside the head stack; Wq and Wo belong to each head. Earlier context is dimmed, and the new columns and last attention row are highlighted.

The optimisations act on different parts of the memory cost. MQA shares the cache across query heads. Tying K and V shares the vector within each stored position. Using both leaves one set of entries, with one vector per position.

The cache still adds an entry for every token. Even this smaller shared history keeps growing. Could we keep recent tokens individually and compress the older information?

Sources and assumptions

The comparison begins with ordinary MQA, which shares distinct key and value caches across query heads. The final diagram shows tied-KV MQA with the usual dense per-head query and output projections, to isolate the cache change. DeepSeek-V4’s factorised query path, normalisation, positional operations and grouped output projections are expanded in the final series article. The storage comparison counts cached K/V scalars at equal widths and precision; it does not estimate model weights or temporary workspaces. Full-resolution history isolates the representation choice before the next article introduces sequence compression.

← Browse all subjectsBack to the beginning ↑