Rúben GalvãoThe Rabbit Hole

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

The KV cache: why LLMs remember every key and value

A model has already read your sentence. Why should writing the next token make it read everything again?

The model reads “the old rabbit” and predicts “slept”.

We add that token to the sentence. Now we want the prediction after “the old rabbit slept”.

Send the sentence through again

We already know how to do this: turn each token into queries, keys and values, run attention, and use the last output to predict the next token.

Each token is a column in the input matrix. Press Add next token and watch the whole growing matrix pass through the same weights again.

Run the growing sentence through attention

Interactive

Each column is a token. The whole matrix passes through the network again.

One new token adds one column. But the next run projects the entire input matrix again, producing whole Q, K and V matrices. The attention map grows by a row and a column.

Watch one earlier token. Did its query, key or value change between runs?

We have calculated this before

Compare the orange outlines below as you add a token. Those earlier projections produce the same vectors again.

What did we calculate again?

Interactive

Orange outlines: calculated again with the same result.

Remember the causal mask from attention. “Rabbit” cannot read “slept” when it arrives later. Adding “slept” therefore cannot change the earlier token representations.

Their queries, keys and values stay the same. So do the earlier rows of attention and their outputs. We have been repeating work to get answers we already had.

What does the new token actually need?

The last token has a new query. It compares that query with every allowed key, then uses the attention weights to sum the values.

The earlier keys and values still take part. But their earlier queries and attention outputs have already served their predictions; this prediction does not need them again.

Could we keep the keys and values between runs, and calculate only the new token’s projections?

Keep the parts we will read again

This time, only the new token column enters the projections. Its key and value are appended to the stored K and V matrices; the green outlines mark the columns we kept.

Keep the keys and values between runs

Interactive

Dashed outlines: kept from the previous run. Only the new token gets new projections.

The new query reads the retained keys. The weighted sum reads the retained values. We add the new key and value to that collection for the next run.

That collection is the KV cache. Each attention layer keeps its own, because each layer has its own projections and token representations.

Reading the initial prompt calculates its keys and values once. As the answer grows, we extend those saved collections instead of sending the whole prefix through every layer again.

Saving work takes space

Every new token adds another entry. A longer conversation leaves more keys and values in memory.

Increase the token count below. The head dimensions and layer count come from the model selected above.

Watch the cache grow

Interactive

KV-cache memory before compression16-bit values

The cache grows along the token axis. More stored coordinates per token, more KV heads, and more layers all multiply the memory it needs.

In multi-head attention, each head keeps its own keys and values. Every layer has different projections, so it needs its own cache too.

Less calculation, more memory

Each new token now needs much less calculation: we reuse the earlier keys and values instead of rebuilding them.

The cost is keeping that work in memory. As the context grows, the KV cache can become enormous—across every head and layer.

Other articles explore how to make that cache smaller and reduce how much attention reads.

Sources
  • Llama reference implementation, for storing earlier keys and values and computing queries for the new positions.
  • Llama 3 report, for a model using per-layer key/value caches.
  • The linked cache techniques have their own primary sources in their article endnotes.

← Browse all subjectsBack to the beginning ↑