4.1.9 · HinglishTransformer Architecture

Residual connections and layer norm placement

3,279 words15 min readRead in English

4.1.9 · AI-ML › Transformer Architecture

Overview

Residual connections (skip connections) aur layer normalization ka placement Transformers mein critical architectural choices hain jo training stability, gradient flow, aur final performance determine karte hain. In dono mechanisms ke beech interaction non-obvious hai: LayerNorm ko residual path ke relative kahan rakhte ho, yeh fundamentally badal deta hai ki gradients kaise propagate hote hain aur model kaise seekhta hai.

Figure — Residual connections and layer norm placement

Core Concepts


Do Architectures: Post-LN vs Pre-LN

Post-LN (Original Transformer, 2017)

Post-LN structure ek full layer ke liye (attention + FFN):

Pre-LN (Modern Standard)

Pre-LN structure ek full layer ke liye:


Mathematical Comparison: Gradient Flow


Practical Implications


Advanced: The Residual Stream Interpretation


Implementation Details


Connections

  • 4.1.01-Self-attention-mechanism: Residual connections self-attention blocks ke around wrap hote hain
  • 4.1.08-Multi-head-attention: Har head ka output project aur residual stream mein add hota hai
  • 4.2.02-Training-stability-and-convergence: Pre-LN deep models mein training stability dramatically improve karta hai
  • 4.3.01-Positional-encoding: Residual stream positional information layers ke across carry karta hai
  • 5.1.03-Gradient-flow-in-deep-networks: Core gradient flow concepts jo explain karte hain Pre-LN kyun kaam karta hai
  • 6.2.05-Layer-scaling-and-initialization: Residual connections ke liye proper initialization
  • 4.1.12-Encoder-decoder-attention: Encoder-decoder cross-attention ke liye same Pre-LN pattern use karta hai

Active Recall

Recall Ek 12-saal ke bachche ko explain karo: Skip connections kyun chahiye?

Socho tum 24 logon ke saath telephone ka game khel rahe ho. Normally, message end tak pahunchte pahunchte garbled ho jaata hai—har insaan thoda sunne mein galti kar sakta hai aur use thoda badal sakta hai.

Ab socho agar pehla insaan directly aakhri insaan ko whisper kar sake, aur beech ke sabhi log bhi aakhri insaan ko whisper kar sakein. Aakhri insaan original message clearly aur saare changes jo sabne kiye sunta hai. Woh ek skip connection hai!

Neural networks mein, skip connections original information ko seedha baad ki layers mein travel karne deti hain bina beech ke saare math se "garbled" hue. Yeh network ko better seekhne mein help karta hai kyunki woh original input ko processed version se compare kar sakta hai aur seekh sakta hai ki kaun se changes actually helpful the.


#flashcards/ai-ml

Transformers mein residual connection ka mathematical form kya hai?
, jahan input directly transformed output mein add hota hai, ek identity gradient path create karta hai.
Pre-LN Post-LN se better gradient flow kyun provide karta hai?
Pre-LN mein, gradients residual path se ek clean identity term () ke saath main pathway par flow karte hain, jabki Post-LN mein gradients ko har layer par LayerNorm ke Jacobian se pass karna padta hai, depth-dependent, scale-sensitive bottlenecks create karte hain.
Ek Transformer layer ke liye Pre-LN architecture formula likho
phir , jahan LayerNorm pehle residual branch ke andar har sublayer ke apply hota hai.
Ek Transformer layer ke liye Post-LN architecture formula likho
phir , jahan LayerNorm residual addition ke baad apply hota hai.
Residual gradient mein "+1" term kya guarantee karta hai?
Yeh guarantee karta hai ki kam se kam ek identity gradient backward flow kare, vanishing gradients prevent karta hai chahe sublayer kuch na seekhe ya initially near-zero gradients ho.
Kya Pre-LN learning-rate warmup ki zaroorat eliminate kar deta hai?
Nahi. Pre-LN warmup ke sensitivity ko reduce karta hai, lekin GPT-3 aur

Concept Map

solved by

formula

derivative gives

creates

normalizes over

chosen over

combined with

placement determines

placed after add

placed before add

contrasts with

better

Deep networks vanishing gradients

Residual connection

y = x + F of x

Plus one identity gradient

Residual stream highway

Layer normalization

Feature dimension per example

Batch norm avoids seq-length dependence

Training stability and gradient flow

Post-LN 2017 original

Pre-LN variant