> For the complete documentation index, see [llms.txt](https://theshank.gitbook.io/ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://theshank.gitbook.io/ai/transformers/multi-head-latent-attention.md).

# Multi-head Latent Attention

How to reduce KV cache size compared to alternative methods such as group-query attention or multi-query attention.&#x20;

<figure><img src="https://1877261540-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LFDuA0A2VRqmT31Blrq%2Fuploads%2FXMlRSnUvCJJ7MrRoprxe%2Fimage.png?alt=media&amp;token=71951802-89f6-4dd2-aa96-7f7703d72cb9" alt=""><figcaption></figcaption></figure>

**In Multi-query attention,** basically there's single key value shared across all the attention heads, instead of key value for each of the head.&#x20;

In this way, we have have to cache smaller size of key-value. But this leads to to compromising on the model performance as we are reducing the parameters of the model.&#x20;

## Multi Head Latent Attention

**How do we get key and value**

Let $$x$$ be the input, and how we can get key and value $$k,v$$, is by using a full-connected layer i.e multiplying by dense matrixes $$W\_k \in \mathcal{R}^{(n\_h \times d\_{head}) \times d\_{model}}$$ and $$W\_v \in \mathcal{R}^{(n\_h \times d\_{head}) \times d\_{model}}$$, where $$n\_h, d\_{head}, d\_{model}$$ are number of heads, vector dim in each head, model dimension respectively.&#x20;

$$
k = W\_kx\\
v = W\_vx
$$

**DeepSeek's Trick**

Force this input vector transformation to key-values to be low rank. i.e &#x20;

Instead of going from $$d\_{model} \rightarrow n\_h \times d\_{head}$$, we do $$d\_{model} \rightarrow l\_{dim} \quad \text{and} \quad l\_{dim} \rightarrow n\_h \times d\_{head}$$. Where $$l$$ will be the dimension of the latent vector when going from $$x \rightarrow k,v$$. **And instead of caching** $$k,v$$ **we cahce lower dimensional vector** $$l$$. $$x\in \mathcal{R}^{L \times d\_{model}}$$

How we do this mathematically

$$
l\_k = xW^l\\
k = l\_kW^k\_l
$$

Similiary for value

$$
l\_v = W^lx\\
v = W^v\_l l\_v
$$

And this basically means that big matrix such as $$W\_k \in \mathcal{R}^{(n\_h \times d\_{head}) \times d\_{model}}$$ and $$W\_v \in \mathcal{R}^{(n\_h \times d\_{head}) \times d\_{model}}$$ has been decomposed into lower rank matrices.&#x20;

**But just caching** $$l\_k,l\_v$$ **would mean that during inference, we would have to waste some inference compute to get** $$k,v$$ **from** $$l\_k,l\_v$$ by up-projecting.&#x20;

**Another Clever trick here**

&#x20;Instead of up-projecting from latent to actual and value. We can merge that up-projection for key with q matrix and for v, we can merge the up-projection with the output linear projection layer.&#x20;

\==============================

The reason low-rank compression is so effective is because there’s plenty of information overlap between what different attention heads need to know about. If we used low-rank compression on the key and value vectors of individual heads instead of all keys and values of all heads stacked together, the method would simply be equivalent to using a smaller head dimension to begin with and we would get no gain. Exploiting the fact that different heads need access to the same information is essential for the mechanism of multi-head latent attention.

Methods such as grouped-query attention exploit the possibility of the same overlap, but they do so ineffectively by forcing attention heads that are grouped together to all respond similarly to queries. In other words, information sharing becomes coupled to having identical behavior in some restricted sense, a clearly undesirable property. Low-rank compression, on the other hand, allows the same *information* to be used in *very different ways* by different heads. In theory, this could even have beneficial regularizing effects on training, and DeepSeek reports finding such effects in their technical reports.

I see this as one of those innovations that look obvious in retrospect but that require a good understanding of what attention heads are actually doing to come up with. Once you see the approach, it’s immediately obvious that it cannot be any worse than grouped-query attention and it’s also likely to be significantly better. However, coming up with the idea of trying this is another matter.
