> For the complete documentation index, see [llms.txt](https://doraemonzzz.gitbook.io/transformer_evolution_paper/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doraemonzzz.gitbook.io/transformer_evolution_paper/head/001.md).

# Multi-Head Attention Collaborate Instead of Concatenate

### Multi-Head Attention: Collaborate Instead of Concatenate

论文地址：

* <https://arxiv.org/abs/2006.16362>

### 整体思路以及计算方式

对$$\mathbf Q,\mathbf K$$降维，然后通过对角阵增加模型表达力，最后达到相当的效果。

计算方式：

* 压缩率$$p$$，$$h$$为头数；
* 输入：$$\mathbf X\in \mathbb R^{n\times d},\mathbf Y\in \mathbb R^{m\times d},m\_i\in \mathbb R,i=1,\ldots, h$$，记$$d\_1=\lfloor pd\rfloor$$
* for $$i=1,\ldots, h$$
  * 计算$$\mathbf Q= \mathbf X\mathbf W\_Q^{(i)} \in \mathbb R^{n\times (d\_1 /h)},\mathbf K= \mathbf X\mathbf W\_K^{(i)} \in \mathbb R^{m\times (d\_1 /h)}, \mathbf V =\mathbf X\mathbf W\_V^{(i)}\in \mathbb R^{m\times (d /h)}$$
  * $$\mathbf H^{(i)}=\mathrm{MHA}(\mathbf Q\mathrm{diag}(m\_i), \mathbf K, \mathbf V)$$
* 输出：$$\mathrm{Concat}\[\mathbf H^{(i)}]$$

说明：

* 尽管原文中不同头算$$\mathbf Q,\mathbf K$$的$$\mathbf W\_Q,\mathbf W\_K$$是共享的，但实际实现的时候并不是；

### 时间复杂度

对于每个头，时间复杂度为：

$$
O(nmd\_1/h + mnd/h)=O(nmpd/h + mnd/h)
$$

所以$$h$$个头的时间复杂度为：

$$
O(mn(p+1)d )
$$

### 训练以及loss

略过。

### 代码

* <https://github.com/lucidrains/x-transformers/blob/main/x_transformers/x_transformers.py#L514>
* <https://github.com/epfml/collaborative-attention/blob/master/src/collaborative_attention/collaborative_attention.py>
* <https://github.com/epfml/collaborative-attention>

### 实验以及适用场景

因为只改了Head部分，所以适用于所有场景；作者进行了大量实验，效果均不错。

### 细节

降维比例为30%的时候也能达到相当效果。

### 简评

总结：

* 很简洁的思路，通过降维减少参数量，然后再通过少量参数恢复性能；
* 非常简洁，值得复现；


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://doraemonzzz.gitbook.io/transformer_evolution_paper/head/001.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
