Rúben GalvãoThe Rabbit Hole

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

Multi-query attention (MQA): one key and value for every head

Our cache grows with every token. How much of it does each head need to keep for itself?

Every time we add a token, we add its key and value to the cache. And we do that again for every attention head.

More heads, more keys and values to keep.

Could they share?

What can we move outside the head?

Look at Wk and Wv below. Each head has its own weights for making keys and values.

Suppose we learned just one Wk and one Wv for all the heads. We would only need to calculate and cache one set of keys and values.

Press Share K and V to move that work outside the heads.

Move K and V out of the heads

Interactive
Multi-head attention
KV cache

The queries stay inside. Each head still learns its own question.

This is multi-query attention, or MQA.

Would every head get the same result?

Think back to “the old rabbit”. One head might ask about an adjective; another might ask about an action.

They can ask different questions of the same keys. That gives them different attention weights.

Those weights determine how much each head takes from each token’s value. So sharing V still lets the heads produce different outputs.

What did we trade?

We now keep one K/V pair per token, per layer. The cache is smaller, and each new token needs less cached data to be read.

But the heads can no longer learn separate ways to represent keys and values. In the original MQA experiments, that restriction caused a small drop in quality.

Could we keep some of those separate representations without bringing back the whole cache?

Next: let smaller groups of heads share K and V.

Sources

← Browse all subjectsBack to the beginning ↑