Rúben GalvãoThe Rabbit Hole

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

Multi-head latent attention (MLA): caching a compressed latent

The heads need keys and values. Do we have to store them in that form?

The KV cache saved us from calculating the same keys and values over and over. But it grows with every token, in every layer.

MQA and GQA made it smaller by sharing K and V. That also meant fewer separate key and value projections for the heads to learn.

Could we cache something else instead?

What about the input?

Every head made its keys and values from the same input token vector.

We could keep that vector once. Each head could then use its own weights to make the K and V it needs, though it would have to remake them for every cached token, at every step.

Keep the input once

Interactive
Stored contextNew token

But the input vector is wide. Keeping one for every token, in every layer, still adds up.

What if we learned a smaller vector to keep instead?

Learn what to keep

Put a learned matrix before the cache. It turns the input into a smaller vector, called a latent.

Press Compress the input below. Each head then learns its own projections from the latent to its content key and value.

Learn a smaller input to keep

Interactive
Stored contextNew token

All these weights train together. The model learns what the latent must preserve for the heads to do their work.

The smaller starting point also makes the heads’ K/V projections narrower. We keep separate head projections while reducing their weights and projection work.

Do the same for queries

The query projections also start from the same wide input.

We can give them their own shared, smaller starting point too. One projection makes a query latent, then each head turns it into its own query.

A smaller starting point for queries

Interactive
Stored contextNew token

This latent is for the current token. It does not go into the KV cache.

Can we do less work for the next token?

We have cut the memory. But we no longer cache the keys and values themselves. What does that cost us?

Look at the highlighted parts below. For every new token, each head expands the whole latent cache into keys and values.

That is a lot of work, just to get scores and a weighted sum. Could we change the order of the multiplications?

Move the projections, keep the result

Interactive
Stored contextNew token

Highlighted: expand every cached token again

Press Move the key weights. They now act on one query, which compares directly with the cached latents. The scores stay the same.

Now press Move the value weights. Sum the latents first, then apply the value weights to that one result. The output stays the same too.

Both changes replace a projection over the whole cache with a projection of one vector. We no longer rebuild all the keys and values for each new token.

Give position its own path

There is one more piece: RoPE rotates queries and keys according to position. Those changing rotations would stop us from moving the whole key projection as we just did.

Keep position separate. Another projection makes a single rotary key per token, shared by every head, as in MQA. We cache it alongside the content latent. Each head still gets its own rotary query.

The two parts of the cache

Interactive
Stored contextNew token

Each query gets a content score and a position score for every cached token. Add them, then apply softmax as before.

The attention weights tell us how to sum the content latents.

Together, these changes are multi-head latent attention, or MLA.

The complete latent-attention head

Here is how the pieces fit together during decoding. The content-query weights include the moved key projection; the output weights include the value projection.

One head reads the shared latent and position caches, calculates attention, and adds its projected update to the residual stream.

How does it compare?

Keep the context length and head sizes the same, then change what we cache.

How much do we save?

Interactive

MLA keeps much less data than separate K/V for every head. A single shared K/V pair can be smaller still, but gives every head the same key and value projections.

Smaller entries, still a growing sentence

We have reduced what we store per token and avoided rebuilding the expanded K/V during decoding. Unlike MQA and GQA, we did it without sharing K and V: every head still learns its own projections to its content keys and values.

A longer sentence still leaves more cache entries to read.

Can we choose which entries need the full attention calculation?
Sources

← Browse all subjectsBack to the beginning ↑