Multi-Head Attention
Attention Is All You Need
- Prediction prompt: a single attention distribution averages over positions; what information might get lost if one word needs to attend to its subject and its object at the same time?
- Section 3.2.2 and Figure 2 right: project Q, K, V h times with different learned linear projections to d_k, d_k, d_v dimensions, run attention in parallel, concatenate, project again
- Formulas: MultiHead(Q, K, V) = Concat(head_1, ..., head_h) W^O, where head_i = Attention(Q W_i^Q, K W_i^K, V W_i^V)
- Parameter shapes: W_i^Q and W_i^K in R^(d_model x d_k), W_i^V in R^(d_model x d_v), W^O in R^(h·d_v x d_model)
- Paper settings: h = 8 heads, d_k = d_v = d_model / h = 64
- Shape trace for n tokens: input (n x 512) -> each head's Q, K, V (n x 64) -> scores (n x n) -> head output (n x 64) -> concat (n x 512) -> output (n x 512)
- Why total cost is similar to single-head attention with full dimensionality: each head works in a reduced dimension
- The paper's rationale: jointly attending to information from different representation subspaces at different positions, which a single head inhibits by averaging
چند الگوی توجه همزمان
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><rect class="tm-surface" x="90" y="35" width="150" height="55" rx="12"/><text class="tm-text" x="165" y="69" text-anchor="middle" font-size="20" direction="rtl">فاعل</text><rect class="tm-surface" x="325" y="35" width="150" height="55" rx="12"/><text class="tm-text" x="400" y="69" text-anchor="middle" font-size="20" direction="rtl">واژه</text><rect class="tm-surface" x="560" y="35" width="150" height="55" rx="12"/><text class="tm-text" x="635" y="69" text-anchor="middle" font-size="20" direction="rtl">مفعول</text><text class="tm-text2" x="400" y="155" text-anchor="middle" font-size="19" direction="rtl">یک توزیع attention چه چیزی را ترکیب میکند؟</text><path class="tm-line" d="M165 90 L350 205 M400 90 L400 205 M635 90 L450 205"/><rect class="tm-soft" x="285" y="205" width="230" height="55" rx="12"/><text class="tm-text" x="400" y="239" text-anchor="middle" font-size="19" direction="rtl">یک ترکیب وزندار</text><text class="tm-muted" x="400" y="292" text-anchor="middle" font-size="17" direction="rtl">جدا نگه داشتن رابطهها میتواند دشوارتر شود</text><rect class="tm-accent" x="90" y="335" width="270" height="65" rx="12"/><text class="tm-text" x="225" y="363" text-anchor="middle" font-size="18" direction="rtl">head اول: وزن بیشتر به فاعل</text><text class="tm-text2" x="225" y="386" text-anchor="middle" font-size="15" direction="rtl">زیرفضای بازنمایی متفاوت</text><rect class="tm-green" x="440" y="335" width="270" height="65" rx="12"/><text class="tm-text" x="575" y="363" text-anchor="middle" font-size="18" direction="rtl">head دوم: وزن بیشتر به مفعول</text><text class="tm-text2" x="575" y="386" text-anchor="middle" font-size="15" direction="rtl">زیرفضای بازنمایی متفاوت</text><text class="tm-muted" x="400" y="430" text-anchor="middle" font-size="16" direction="rtl">مثال فرضی؛ هر دو به همه موقعیتهای مجاز دسترسی دارند</text></svg>
```
مسیر موازی Multi-Head Attention
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><defs><marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto"><path class="tm-arrow" d="M 0 0 L 10 5 L 0 10 z"/></marker></defs><rect class="tm-surface" x="20" y="185" width="90" height="70" rx="10"/><text class="tm-text" x="65" y="226" text-anchor="middle" font-size="20">Q, K, V</text><path class="tm-line" d="M110 205 H145 V100 H175" fill="none" marker-end="url(#a)"/><path class="tm-line" d="M110 235 H145 V310 H175" fill="none" marker-end="url(#a)"/><rect class="tm-soft" x="175" y="55" width="190" height="90" rx="10"/><text class="tm-text" x="270" y="80" text-anchor="middle" font-size="18">head 1</text><text class="tm-text" x="270" y="115" text-anchor="middle" font-size="19">QW₁Q KW₁K VW₁V</text><rect class="tm-soft" x="175" y="265" width="190" height="90" rx="10"/><text class="tm-text" x="270" y="290" text-anchor="middle" font-size="18">head 2</text><text class="tm-text" x="270" y="325" text-anchor="middle" font-size="19">QW₂Q KW₂K VW₂V</text><text class="tm-muted" x="270" y="220" text-anchor="middle" font-size="25">⋮</text><path class="tm-line" d="M365 100 H395" fill="none" marker-end="url(#a)"/><path class="tm-line" d="M365 310 H395" fill="none" marker-end="url(#a)"/><rect class="tm-accent" x="395" y="55" width="155" height="90" rx="10"/><text class="tm-text" x="472" y="90" text-anchor="middle" font-size="18">Attention</text><text class="tm-text" x="472" y="119" text-anchor="middle" font-size="15">softmax → value</text><rect class="tm-accent" x="395" y="265" width="155" height="90" rx="10"/><text class="tm-text" x="472" y="300" text-anchor="middle" font-size="18">Attention</text><text class="tm-text" x="472" y="329" text-anchor="middle" font-size="15">softmax → value</text><path class="tm-line" d="M550 100 H580 V200 H610" fill="none" marker-end="url(#a)"/><path class="tm-line" d="M550 310 H580 V240 H610" fill="none" marker-end="url(#a)"/><rect class="tm-green" x="610" y="185" width="105" height="70" rx="10"/><text class="tm-text" x="662" y="217" text-anchor="middle" font-size="18">Concat</text><text class="tm-text" x="662" y="239" text-anchor="middle" font-size="14" direction="rtl">راستای ویژگی</text><path class="tm-line" d="M715 220 H735" fill="none" marker-end="url(#a)"/><rect class="tm-gold" x="735" y="190" width="55" height="60" rx="10"/><text class="tm-text" x="762" y="227" text-anchor="middle" font-size="20">Wᴼ</text></svg>
```
شکل ماتریسهای projection هر head
\(d_{\mathrm{model}}\): عرض ورودی؛ \(h\): تعداد headها؛ \(d_k\): عرض query و key؛ \(d_v\): عرض value
**ماتریسهای query و key**
\[ W_i^Q,W_i^K\in\mathbb{R}^{d_{\mathrm{model}}\times d_k} \]
**ماتریس value**
\[ W_i^V\in\mathbb{R}^{d_{\mathrm{model}}\times d_v} \]
**ماتریس خروجی**
\[ W^O\in\mathbb{R}^{(h d_v)\times d_{\mathrm{model}}} \]
**تنظیم مقاله**
\[ d_{\mathrm{model}}=512,\qquad h=8 \]
**عرض هر head**
\[ d_k=d_v=\frac{512}{8}=64 \]
**projectionهای هر head**
\[ W_i^Q,W_i^K,W_i^V\in\mathbb{R}^{512\times 64} \]
برای هر head، سه ماتریس جداگانه و بدون وزن مشترک
**اتصال و projection خروجی**
\[ h d_v=8\times64=512\quad\Longrightarrow\quad W^O\in\mathbb{R}^{512\times512} \]
ردگیری شکلها در self-attention
ورودی بدون محور batch؛ \(h=8\)، \(d_{\mathrm{model}}=512\)
**ورودی**
\[ X\in\mathbb{R}^{10\times512} \]
**projection هر head**
\[ W_i^Q,W_i^K,W_i^V\in\mathbb{R}^{512\times64},\qquad Q_i=XW_i^Q,\ K_i=XW_i^K,\ V_i=XW_i^V\in\mathbb{R}^{10\times64} \]
**امتیازها**
\[ Q_iK_i^{\mathsf T}:\quad (10\times64)(64\times10)=10\times10 \]
**مقیاسبندی و softmax**
\[ A_i=\operatorname{softmax}\!\left(\frac{Q_iK_i^{\mathsf T}}{\sqrt{64}}\right)\in\mathbb{R}^{10\times10} \]
**خروجی هر head**
\[ H_i=A_iV_i:\quad (10\times10)(10\times64)=10\times64 \]
**اتصال خروجیها**
\[ \operatorname{Concat}(H_1,\ldots,H_8)\in\mathbb{R}^{10\times512} \]
**projection خروجی**
\[ W^O\in\mathbb{R}^{512\times512},\qquad (10\times512)(512\times512)=10\times512 \]
**حالت کلی**
\[ X\in\mathbb{R}^{n\times512},\quad Q_iK_i^{\mathsf T}\in\mathbb{R}^{n\times n},\quad Y\in\mathbb{R}^{n\times512} \]
چرا هزینه چند head مشابه است؟
مقایسه برای \(10\) توکن، \(d_{\mathrm{model}}=512\) و \(h=8\)
**عرض هر head**
\[ d_k=\frac{512}{8}=64 \]
**ضرب query و key در head تمامعرض**
\[ 10^2\times512=51200 \]
**ضرب query و key در چند head**
\[ 8\times10^2\times64=51200 \]
**ضرب وزنها در value**
\[ 8\times10^2\times64=10^2\times512 \]
**projectionهای ورودی و نهایی**
\[ 3\times10\times512^2+10\times512^2=4\times10\times512^2 \]
در هر دو حالت
**مرتبه هزینه کل**
\[ O\!\left(10\times512^2+10^2\times512\right) \]
مشابه، نه الزاماً زمان اجرای برابر
بازگشت به دوره