Residual Connections, Layer Normalization, and the Position-wise FFN
Attention Is All You Need
- Prediction prompt: attention mixes information across positions; what component do you think processes each position on its own?
- Sub-layer wrapper from Section 3.1: output = LayerNorm(x + Sublayer(x))
- Why every sub-layer and embedding outputs dimension d_model = 512: so residual additions are shape-compatible
- Position-wise feed-forward network, Equation (2): FFN(x) = max(0, x W_1 + b_1) W_2 + b_2
- Dimensions: input and output 512, inner layer d_ff = 2048; weight shapes W_1 (512 x 2048) and W_2 (2048 x 512)
- Same parameters across positions within a layer, different parameters from layer to layer; equivalent to two convolutions with kernel size 1
- Division of labor: attention moves information between positions, the FFN transforms each position independently
- Shape trace: (n x 512) -> (n x 2048) -> (n x 512)
تفاوت attention و FFN
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><defs><marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" 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="65" font-size="24" text-anchor="middle" direction="rtl">دنباله با دو token</text><rect class="tm-surface" x="100" y="130" width="160" height="60" rx="12"/><text class="tm-text" x="180" y="167" font-size="20" text-anchor="middle" direction="rtl">token اول</text><rect class="tm-surface" x="100" y="270" width="160" height="60" rx="12"/><text class="tm-text" x="180" y="307" font-size="20" text-anchor="middle" direction="rtl">token دوم</text><text class="tm-text-accent" x="400" y="105" font-size="20" text-anchor="middle">self-attention</text><line class="tm-line" x1="260" y1="160" x2="500" y2="160" marker-end="url(#arrow)"/><line class="tm-line" x1="260" y1="300" x2="500" y2="300" marker-end="url(#arrow)"/><line class="tm-line-accent" x1="260" y1="280" x2="500" y2="180" marker-end="url(#arrow)"/><rect class="tm-soft" x="510" y="130" width="190" height="60" rx="12"/><text class="tm-text" x="605" y="167" font-size="18" text-anchor="middle" direction="rtl">نمایش token اول</text><rect class="tm-soft" x="510" y="270" width="190" height="60" rx="12"/><text class="tm-text" x="605" y="307" font-size="18" text-anchor="middle" direction="rtl">نمایش token دوم</text><line class="tm-line" x1="605" y1="190" x2="605" y2="215" marker-end="url(#arrow)"/><rect class="tm-accent tm-enter" x="550" y="220" width="110" height="38" rx="9"/><text class="tm-text" x="605" y="245" font-size="18" text-anchor="middle">FFN</text><line class="tm-line" x1="605" y1="330" x2="605" y2="355" marker-end="url(#arrow)"/><rect class="tm-accent tm-enter" x="550" y="360" width="110" height="38" rx="9"/><text class="tm-text" x="605" y="385" font-size="18" text-anchor="middle">FFN</text><text class="tm-text2" x="335" y="405" font-size="18" text-anchor="middle" direction="rtl">پردازش جداگانهٔ هر موقعیت</text></svg>
```
جمع residual پیش از LayerNorm
ترتیب عملیات و مثال فرضی برای یک موقعیت
**فرمول**
\[ \operatorname{LayerNorm}\bigl(x+\operatorname{Sublayer}(x)\bigr) \]
**بردارهای فرضی**
\[ x=(1,2),\qquad \operatorname{Sublayer}(x)=(3,4) \]
**جمع عضو به عضو**
\[ (1,2)+(3,4)=(4,6) \]
**نرمالسازی پس از جمع**
\[ \operatorname{LayerNorm}\bigl((1,2)+(3,4)\bigr)=\operatorname{LayerNorm}(4,6) \]
مثال فرضی است و اندازهٔ واقعی مدل نیست.
شرط شکل در اتصال residual
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><rect class="tm-soft" x="50" y="80" width="190" height="105" rx="12"/><text class="tm-text" x="145" y="120" font-size="20" text-anchor="middle" direction="rtl">ورودی</text><text class="tm-text-accent" x="145" y="157" font-size="24" text-anchor="middle">10 × 512</text><text class="tm-muted" x="145" y="215" font-size="16" text-anchor="middle" direction="rtl">هر ردیف: یک موقعیت</text><rect class="tm-soft" x="305" y="80" width="190" height="105" rx="12"/><text class="tm-text" x="400" y="120" font-size="20" text-anchor="middle" direction="rtl">خروجی sub-layer</text><text class="tm-text-accent" x="400" y="157" font-size="24" text-anchor="middle">10 × 512</text><text class="tm-text" x="272" y="145" font-size="28" text-anchor="middle">+</text><text class="tm-text" x="525" y="145" font-size="28" text-anchor="middle">=</text><rect class="tm-green" x="555" y="80" width="190" height="105" rx="12"/><text class="tm-text" x="650" y="120" font-size="20" text-anchor="middle" direction="rtl">حاصل جمع</text><text class="tm-text-green" x="650" y="157" font-size="24" text-anchor="middle">10 × 512</text><text class="tm-text2" x="400" y="255" font-size="19" text-anchor="middle" direction="rtl">جمع عضو به عضو</text><rect class="tm-gold" x="155" y="295" width="490" height="70" rx="12"/><text class="tm-text" x="400" y="325" font-size="20" text-anchor="middle" direction="rtl">خروجی نامناسب برای جمع مستقیم</text><text class="tm-text-gold" x="400" y="352" font-size="20" text-anchor="middle">10 × 512 + 10 × 2048</text><text class="tm-text-accent tm-enter" x="400" y="410" font-size="19" text-anchor="middle" direction="rtl">داخل sub-layer: تغییر موقت ابعاد مجاز است</text></svg>
```
تبدیل عرض در FFN
معادلهٔ \(2\) و شکلها برای \(10\) token
**معادلهٔ FFN**
\[ \operatorname{FFN}(x)=\max(0,xW_1+b_1)W_2+b_2 \]
**نمونهٔ ReLU**
\[ \operatorname{ReLU}([-2,3])=[0,3] \]
**ورودی و تبدیل اول**
\[ X\in\mathbb{R}^{10\times512},\quad W_1\in\mathbb{R}^{512\times2048} \]
**فعالسازی میانی**
\[ \operatorname{ReLU}(XW_1+b_1)\in\mathbb{R}^{10\times2048} \]
جمع \(b_1\) با هر ردیف؛ ReLU شکل را حفظ میکند.
**تبدیل دوم و خروجی**
\[ W_2\in\mathbb{R}^{2048\times512},\quad \operatorname{FFN}(X)\in\mathbb{R}^{10\times512} \]
جمع \(b_2\) با هر ردیف؛ خروجی همشکل مسیر residual است.
اشتراک پارامترها در موقعیتها
```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"><path class="tm-arrow" d="M 0 0 L 10 5 L 0 10 z"/></marker></defs><text class="tm-text" x="400" y="35" text-anchor="middle" font-size="22" direction="rtl">یک لایه، پارامترهای مشترک</text><rect class="tm-soft" x="60" y="75" width="140" height="55" rx="10"/><text class="tm-text" x="130" y="110" text-anchor="middle" font-size="19">token 1</text><rect class="tm-soft" x="60" y="165" width="140" height="55" rx="10"/><text class="tm-text" x="130" y="200" text-anchor="middle" font-size="19">token 2</text><rect class="tm-accent" x="300" y="75" width="200" height="55" rx="10"/><text class="tm-text" x="400" y="110" text-anchor="middle" font-size="19">FFN</text><rect class="tm-accent" x="300" y="165" width="200" height="55" rx="10"/><text class="tm-text" x="400" y="200" text-anchor="middle" font-size="19">FFN</text><path class="tm-line" d="M 200 102 L 295 102" marker-end="url(#a)"/><path class="tm-line" d="M 200 192 L 295 192" marker-end="url(#a)"/><text class="tm-text-accent" x="625" y="150" text-anchor="middle" font-size="20" direction="rtl">W₁، b₁، W₂، b₂</text><path class="tm-line-accent" d="M 500 102 L 545 132"/><path class="tm-line-accent" d="M 500 192 L 545 160"/><text class="tm-text2" x="400" y="270" text-anchor="middle" font-size="19" direction="rtl">دو convolution با kernel اندازه 1؛ بدون اختلاط موقعیتها</text><rect class="tm-soft" x="125" y="305" width="220" height="55" rx="10"/><text class="tm-text" x="235" y="339" text-anchor="middle" font-size="18" direction="rtl">پارامترهای یک لایه</text><rect class="tm-gold" x="455" y="305" width="220" height="55" rx="10"/><text class="tm-text" x="565" y="339" text-anchor="middle" font-size="18" direction="rtl">پارامترهای لایه بعد</text><text class="tm-text" x="400" y="340" text-anchor="middle" font-size="25">≠</text><text class="tm-text" x="400" y="405" text-anchor="middle" font-size="19" direction="rtl">10 × 512 → 10 × 2048 → 10 × 512 → جمع با ورودی → LayerNorm</text></svg>
```
Back to course