4.1.12 · HinglishTransformer Architecture

The original - Attention is All You Need - architecture

2,969 words13 min readRead in English

4.1.12 · AI-ML › Transformer Architecture

What Problem Did It Solve?

Traditional seq2seq models mein teen pain points the:

  1. Sequential bottleneck: RNNs token-by-token process karte hain, sequence ke across parallelize nahi kar sakte
  2. Long-range dependencies: Information kaafi steps mein degrade ho jaati hai (vanishing gradients)
  3. Fixed context window: CNNs ke limited receptive fields hote hain

AIAYN architecture ne recurrence aur convolution ko bilkul eliminate kar diya, sirf attention mechanisms aur feed-forward networks use karke.

The Complete Architecture

Architecture Components (Detailed Breakdown)

1. Input Embedding + Positional Encoding

WHY: Raw tokens discrete symbols hote hain. Humein continuous vectors chahiye jo dono meaning (embedding) aur position (kyunki attention mein inherently koi order nahi hota) capture karein.

Formula Derivation:

Positional encoding sine/cosine functions use karta hai:

WHY ye specific functions?

  • Sinusoids model ko relative positions seekhne dete hain: ko ke linear function ke roop mein express kiya ja sakta hai
  • Alag-alag frequencies ( ke through) alag-alag scales par position encode karti hain
  • Training ke dauran unseen sequence lengths tak extrapolate karta hai

2. Encoder Layer (×6 Stack)

6 encoder layers mein se har ek mein ye hota hai:

Sub-layer 1: Multi-Head Self-Attention

Attention mechanism compute karta hai:

Scratch se Derivation:

  1. Input se shuru karo (n tokens, dims)
  2. Queries/keys/values mein project karo: , , jahan
  3. Similarity scores compute karo: (har token har token ko attend karta hai)
  4. se scale karo WHY? Dot products dimension ke saath badhte hain; bade values softmax ko saturation regions mein push karte hain jahan gradients tiny hote hain
  5. Row-wise softmax apply karo: attention weights har query ke liye 1 tak sum hote hain
  6. Values ka weighted sum: har token ka output sabhi tokens ke value vectors ka weighted combination hota hai

Multi-head attention attention operations parallel mein run karta hai:

Har head ka dimensions hote hain.

WHY multiple heads? Alag-alag heads alag-alag types ke relationships (syntactic, semantic, positional) seekhte hain. Ek head subject-verb agreement par focus kar sakta hai, doosra long-range dependencies par.

Sub-layer 2: Position-wise Feed-Forward Network

Jahan , , .

WHY? Attention linear hai (weighted sums). FFN non-linearity add karta hai aur model capacity badhata hai. Har position par independently apply hota hai (same network positions ke across, alag positions yahan interact nahi karte).

Har sub-layer ke around Residual Connection + Layer Norm:

WHY residuals? Deep networks (6 layers = encoder mein 12 sub-layers) mein gradient flow enable karte hain. Residuals ke bina, gradients vanish ho jaate hain.

WHY LayerNorm? Training stabilize karta hai har example ke liye features ke across normalize karke: jahan har token ke liye dimensions ke over compute hote hain.

3. Decoder Layer (×6 Stack)

Har decoder layer mein teen sub-layers hain:

Sub-layer 1: Masked Multi-Head Self-Attention

Encoder self-attention jaisa hi lekin causal masking ke saath:

Softmax se pehle apply hota hai:

WHY? Training ke dauran, decoder puri target sequence dekhta hai. Masking position ko future positions attend karne se rokta hai, autoregressive generation ensure karta hai (sirf past tokens use karke next token predict karo).

Sub-layer 2: Encoder-Decoder Attention (Cross-Attention)

WHY? Yahan decoder source sequence access karta hai! Decoder encoder ki input representation ko query karta hai. Translation "Le chat" → "The cat" ke liye, "cat" generate karte waqt decoder encoder output mein "chat" ko attend karta hai.

Sub-layer 3: Position-wise FFN (encoder jaisa hi)

Teeno sub-layers ke around Residual + LayerNorm.

4. Final Linear + Softmax

ko vocabulary size (e.g., 37000) tak project karta hai, next token par probability distribution produce karta hai.

Training Details from Paper

Optimizer: Adam with

Learning Rate Schedule: with warmup_steps = 4000.

WHY ye schedule?

  1. Linear warmup: early mein large gradients se instability prevent karta hai
  2. Inverse square root decay: model converge hone par LR gradually reduce karta hai
  3. se scaling: model size ke liye normalize karta hai

Why This Architecture Dominates

  1. Parallelization: Sabhi positions simultaneously process hote hain (sequential RNNs ke mukable)
  2. Constant path length: Koi bhi token pair directly attention ke through connected hai (RNN mein steps ke mukable)
  3. Learnable relationships: Attention weights data ke according adapt hote hain (fixed CNN kernels ke mukable)
  4. Scalability: Compute badhne par aur layers/heads/dimensions stack karo
Recall Ek 12-Saal Ke Bachche Ko Explain Karo

Socho tum ek lambi kahani padh rahe ho. Tumhara brain ek word padhta hai, use bhool jaata hai, phir agla padhta hai — aisa nahi hota. Tumhari aankhein jump kar sakti hain — tum ek word padh sakte ho, phir shuru ki koi cheez yaad kar sakte ho, phir end se connect kar sakte ho. Transformers yehi karte hain!

Purane AI models (RNNs) aise the jaise ek tiny sliding window se padhna: ek word dekho, agla move karo, lekin pehla bhool jao. Transformers POORA sentence ek saath dekhte hain. Har word har doosre word ko dekhta hai aur decide karta hai "Is word ko samajhne ke liye mujhe us word par kitna dhyan dena chahiye?"

Do parts hain: Encoder (input sentence padhta hai, jaise French mein "Le chat noir") aur Decoder (output likhta hai, jaise "The black cat"). Encoder figure out karta hai ki sab kuch kya matlab rakhta hai. Decoder jawab banata hai, lekin ek special trick ke saath: word3 likhte waqt, ye sirf words 1 aur 2 dekh sakta hai (future words nahi dekhna!) — bilkul jaise tum apni diary likhte waqt kal nahi dekh sakte.

Jadui ingredient hai attention — ek formula jo "Word A aur Word B kitne related hain?" compute karta hai har pair ke liye. Ye lakhon baar training ke dauran karo, aur AI language patterns seekh leta hai!

Connections


Flashcards

#flashcards/ai-ml

Transformer decoder ke har layer mein teen sub-layers kya hain?
(1) Masked multi-head self-attention, (2) Encoder-decoder cross-attention, (3) Position-wise feed-forward network. Har ek mein residual connection + LayerNorm hota hai.
Attention formula mein √d_k se scaling kyun use hoti hai?
Dot products dimension d_k ke saath magnitude mein badhte hain. Bade values softmax ko saturation regions mein push karte hain jahan gradients bahut chhote hote hain, learning hindered hoti hai. √d_k se divide karne par variance stable rehta hai.
Decoder mein causal masking ka purpose kya hai?
Position i ko training ke dauran future positions j > i attend karne se rokta hai. Ye autoregressive generation enforce karta hai: token i predict karne ke liye sirf tokens 1 se i-1 use ho sakte hain, future tokens nahi.
Transformer base model mein approximately kitne parameters hain?
~65 million parameters, input embedding, output embedding, aur pre-softmax projection ke beech weight tying use karke.
Original paper mein use ki gayi learning rate warmup schedule kya hai?
lr = d_model^(-0.5) × min(step^(-0.5), step × warmup_steps^(-1.5)) with warmup_steps=4000. 4000 steps tak linear increase, phir inverse square root decay.
Positional encoding add karne se pehle embeddings ko √d_model se multiply kyun karte hain?
Embeddings ki variance ~1 hoti hai, positional encodings bounded [-1,1] hoti hain. √d_model (~22.6 for d=512) se scale karna ensure karta hai ki embeddings initially dominate karein jabki PE positional information add kare bina semantic content ko overwhelm kiye.
Base model mein har attention head ki dimension kya hoti hai?
d_k = d_v = d_model / h = 512 / 8 = 64 dimensions per head.
Learned embeddings ki jagah sinusoidal positional encoding kyun use karte hain?
Sinusoids model ko training ke dauran dekhi gayi sequence lengths se lambi lengths tak extrapolate karne dete hain. PE(pos+k) ko PE(pos) ke linear function ke roop mein express kiya ja sakta hai, jo model ko relative positions seekhne mein help karta hai.
Base model mein FFN inner dimension d_ff kya hai?
2048 (model dimension 512 ka 4 guna). Upar project karta hai, ReLU apply karta hai, wapas neeche project karta hai.
Encoder-decoder attention, self-attention se kaise alag hai?
Cross-attention mein, Q decoder se aata hai, lekin K aur V encoder output se aate hain. Ye decoder ko target sequence generate karte waqt source sequence attend karne deta hai.
Label smoothing ε_ls=0.1 aur vocabulary size K ke saath, true class ko kya target probability milti hai?
1 − ε_ls + ε_ls/K. K=4 ke liye ye hai 0.9 + 0.025 = 0.925; baaki har class ko ε_ls/K = 0.025 milta hai.

Concept Map

motivates

replaced by

replaced by

uses only

uses only

enables

structure

encoder sub-layers

decoder sub-layers

every sub-layer

feeds

encodes position into

allows

Seq2Seq Pain Points

AIAYN Transformer

Recurrence RNN LSTM

Convolution

Attention Mechanisms

Feed-Forward Networks

Full Parallelism

Encoder-Decoder Stack N=6

Multi-Head Self-Attention

Masked + Cross Attention

Residual + LayerNorm

Embedding + Positional Encoding

Sinusoidal PE

Relative Position Learning