The Full Encoder-Decoder Stack
Attention Is All You Need
- Prediction prompt: looking at Figure 1, how many attention sub-layers does one decoder layer have, and why one more than the encoder?
- Encoder: N = 6 identical layers, each with multi-head self-attention then the position-wise FFN, each wrapped with residual and LayerNorm
- Decoder: N = 6 identical layers with masked self-attention, encoder-decoder attention over the encoder output, and the FFN
- Embeddings (Section 3.4): learned embeddings to d_model; the same weight matrix shared between the two embedding layers and the pre-softmax linear transformation; embedding weights multiplied by sqrt(d_model)
- Output: linear transformation and softmax produce next-token probabilities
- Output embeddings are offset by one position so prediction i depends only on outputs before i
- Complete shape trace through one encoder layer and one decoder layer for a source of n tokens and a target of m tokens
- Walking Figure 1 bottom to top: inputs, positional encoding, N encoder layers, decoder layers, linear, softmax
نقشهٔ کلی encoder و decoder
```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="7" markerHeight="7" orient="auto-start-reverse"><path class="tm-arrow" d="M 0 0 L 10 5 L 0 10 z"/></marker></defs><text class="tm-text" x="220" y="45" text-anchor="middle" font-size="23">encoder</text><text class="tm-text" x="580" y="45" text-anchor="middle" font-size="23">decoder</text><rect class="tm-soft" x="80" y="115" width="280" height="190" rx="14"/><text class="tm-text" x="220" y="150" text-anchor="middle" font-size="18" direction="rtl">هر لایه</text><rect class="tm-accent" x="110" y="190" width="220" height="60" rx="10"/><text class="tm-text" x="220" y="226" text-anchor="middle" font-size="17">self-attention</text><rect class="tm-soft" x="440" y="115" width="280" height="190" rx="14"/><text class="tm-text" x="580" y="150" text-anchor="middle" font-size="18" direction="rtl">هر لایه</text><rect class="tm-accent" x="470" y="165" width="220" height="45" rx="10"/><text class="tm-text" x="580" y="193" text-anchor="middle" font-size="16">self-attention</text><rect class="tm-green" x="470" y="230" width="220" height="45" rx="10"/><text class="tm-text" x="580" y="258" text-anchor="middle" font-size="16">encoder-decoder attention</text><text class="tm-muted" x="580" y="335" text-anchor="middle" font-size="16" direction="rtl">خروجیهای قبلی + نمایش ورودی</text><text class="tm-text2" x="220" y="85" text-anchor="middle" font-size="16" direction="rtl">شش لایه با ساختار یکسان</text><text class="tm-text2" x="220" y="375" text-anchor="middle" font-size="16" direction="rtl">ورودی + positional encoding</text><line class="tm-line" x1="220" y1="350" x2="220" y2="310" marker-end="url(#a)"/><text class="tm-text2" x="220" y="425" text-anchor="middle" font-size="15" direction="rtl">خروجی: نمایشهای ورودی</text><text class="tm-text2" x="580" y="85" text-anchor="middle" font-size="16" direction="rtl">شش لایه با ساختار یکسان</text><text class="tm-text2" x="580" y="375" text-anchor="middle" font-size="16" direction="rtl">ورودی + positional encoding</text><line class="tm-line" x1="580" y1="350" x2="580" y2="310" marker-end="url(#a)"/><path class="tm-line-accent" d="M 360 105 L 400 105 L 400 252 L 465 252" marker-end="url(#a)"/><text class="tm-text-green" x="400" y="405" text-anchor="middle" font-size="15" direction="rtl">خروجی نهایی encoder به هر لایهٔ decoder</text><text class="tm-text-accent" x="580" y="425" text-anchor="middle" font-size="15" direction="rtl">linear → softmax → احتمال توکن بعدی</text></svg>
```
ورودیها و جابهجایی هدف
```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><text class="tm-text" x="400" y="32" font-size="19" text-anchor="middle" direction="rtl">دو مسیر ورودی</text><rect class="tm-soft" x="40" y="65" width="130" height="50" rx="10"/><text class="tm-text" x="105" y="96" font-size="16" text-anchor="middle" direction="rtl">توکن مبدأ</text><rect class="tm-soft" x="40" y="140" width="130" height="50" rx="10"/><text class="tm-text" x="105" y="171" font-size="16" text-anchor="middle" direction="rtl">توکن هدف</text><line class="tm-line" x1="170" y1="90" x2="215" y2="90" marker-end="url(#a)"/><line class="tm-line" x1="170" y1="165" x2="215" y2="165" marker-end="url(#a)"/><rect class="tm-accent" x="220" y="65" width="175" height="50" rx="10"/><rect class="tm-accent" x="220" y="140" width="175" height="50" rx="10"/><text class="tm-text" x="307" y="96" font-size="16" text-anchor="middle">embedding</text><text class="tm-text" x="307" y="171" font-size="16" text-anchor="middle">embedding</text><text class="tm-text2" x="422" y="96" font-size="16">512</text><text class="tm-text2" x="422" y="171" font-size="16">512</text><line class="tm-line" x1="460" y1="90" x2="485" y2="90" marker-end="url(#a)"/><line class="tm-line" x1="460" y1="165" x2="485" y2="165" marker-end="url(#a)"/><rect class="tm-gold" x="490" y="65" width="120" height="50" rx="10"/><rect class="tm-gold" x="490" y="140" width="120" height="50" rx="10"/><text class="tm-text" x="550" y="96" font-size="17" text-anchor="middle">× √512</text><text class="tm-text" x="550" y="171" font-size="17" text-anchor="middle">× √512</text><line class="tm-line" x1="610" y1="90" x2="630" y2="90" marker-end="url(#a)"/><line class="tm-line" x1="610" y1="165" x2="630" y2="165" marker-end="url(#a)"/><rect class="tm-green" x="635" y="65" width="145" height="50" rx="10"/><rect class="tm-green" x="635" y="140" width="145" height="50" rx="10"/><text class="tm-text" x="707" y="86" font-size="14" text-anchor="middle">+ positional</text><text class="tm-text" x="707" y="104" font-size="14" text-anchor="middle">encoding</text><text class="tm-text" x="707" y="161" font-size="14" text-anchor="middle">+ positional</text><text class="tm-text" x="707" y="179" font-size="14" text-anchor="middle">encoding</text><rect class="tm-accent" x="220" y="218" width="175" height="48" rx="10"/><text class="tm-text" x="307" y="247" font-size="15" text-anchor="middle" direction="rtl">linear پیش از softmax</text><path class="tm-line-accent" d="M 205 90 L 190 90 L 190 242 L 215 242" marker-end="url(#a)"/><text class="tm-text-accent" x="105" y="222" font-size="15" text-anchor="middle" direction="rtl">وزن مشترک</text><text class="tm-text2" x="415" y="247" font-size="15" direction="rtl">امتیاز واژگان</text><text class="tm-text" x="400" y="310" font-size="18" text-anchor="middle" direction="rtl">جابهجایی ورودی decoder</text><rect class="tm-soft" x="200" y="330" width="150" height="48" rx="10"/><text class="tm-text" x="275" y="359" font-size="15" text-anchor="middle" direction="rtl">توکن اول</text><line class="tm-line" x1="350" y1="354" x2="435" y2="354" marker-end="url(#a)"/><rect class="tm-accent" x="440" y="330" width="160" height="48" rx="10"/><text class="tm-text" x="520" y="359" font-size="15" text-anchor="middle" direction="rtl">پیشبینی توکن دوم</text><text class="tm-text2" x="400" y="414" font-size="15" text-anchor="middle" direction="rtl">masked self-attention: جایگاههای بعدی پنهاناند</text></svg>
```
ردگیری شکلها در لایهٔ encoder
مبدأ: \(7\) توکن؛ \(d_{\mathrm{model}}=512\)، \(h=8\)؛ بدون بُعد batch
**ورودی لایه**
\[ X\in\mathbb{R}^{7\times 512} \]
پس از embedding و اطلاعات موقعیت؛ سطرها توکن و ستونها ویژگی
**query، key و value در هر head**
\[ d_k=d_v=\frac{512}{8}=64,\qquad Q_i,K_i,V_i\in\mathbb{R}^{7\times 64} \]
**امتیازهای هر head**
\[ Q_iK_i^{\mathsf T}:\quad (7\times 64)(64\times 7)=7\times 7 \]
سطر: query؛ ستون: key
**ترکیب headها و تبدیل خروجی**
\[ \mathrm{head}_i: 7\times 64\quad\longrightarrow\quad \mathrm{Concat}: 7\times 512\quad\longrightarrow\quad 7\times 512 \]
**residual و LayerNorm اول**
\[ \mathrm{LayerNorm}(X+\mathrm{MultiHead}(X)): 7\times 512 \]
**position-wise FFN**
\[ 7\times 512\quad\longrightarrow\quad 7\times 2048\quad\longrightarrow\quad 7\times 512 \]
هر سطر مستقل از سطرهای دیگر
**residual و LayerNorm دوم**
\[ \mathrm{LayerNorm}(Y+\mathrm{FFN}(Y)): 7\times 512 \]
\(Y\): خروجی زیرلایهٔ اول
ردگیری شکلها در لایهٔ decoder
مبدأ: \(7\) توکن؛ هدف: \(5\) توکن؛ \(d_{\mathrm{model}}=512\)، \(h=8\)، \(d_{\mathrm{ff}}=2048\)، عرض هر head برابر \(64\)
**ورودیها**
\[ X_{\mathrm{enc}}\in\mathbb{R}^{7\times512},\quad X_{\mathrm{dec}}\in\mathbb{R}^{5\times512} \]
**masked self-attention؛ هر head**
\[ Q,K,V\in\mathbb{R}^{5\times64} \]
**امتیاز و mask**
\[ QK^{\mathsf T}\in\mathbb{R}^{5\times5} \]
حذف امتیاز جایگاههای آینده پیش از softmax
**ترکیب headها، residual و LayerNorm**
\[ \mathbb{R}^{5\times512}\longrightarrow\mathbb{R}^{5\times512} \]
**encoder-decoder attention؛ هر head**
\[ Q\in\mathbb{R}^{5\times64},\quad K,V\in\mathbb{R}^{7\times64} \]
query از decoder؛ key و value از encoder
**امتیاز هدف در برابر مبدأ**
\[ QK^{\mathsf T}\in\mathbb{R}^{5\times7} \]
**ترکیب headها، residual و LayerNorm**
\[ \mathbb{R}^{5\times512}\longrightarrow\mathbb{R}^{5\times512} \]
**FFN، residual و LayerNorm**
\[ \mathbb{R}^{5\times512}\longrightarrow\mathbb{R}^{5\times2048}\longrightarrow\mathbb{R}^{5\times512}\longrightarrow\mathbb{R}^{5\times512} \]
از شکلهای کلی تا احتمال
| بخش | شکل |
| --- | --- |
| ورودی و خروجی encoder | \(n \times 512\) |
| امتیاز self-attention، هر head | \(n \times n\) |
| ورودی و خروجی زیرلایهٔ decoder | \(m \times 512\) |
| امتیاز masked self-attention، هر head | \(m \times m\) |
| امتیاز encoder-decoder attention، هر head | \(m \times n\) |
| عرض میانی FFN | encoder: \(n \times 2048\) ؛ decoder: \(m \times 2048\) |
| خروجی linear و softmax | \(5 \times |V| \;\longrightarrow\; 5 \times |V|\)؛ \(|V|\): اندازهٔ واژگان |
Back to course