4.1.1 · HinglishTransformer Architecture

Limitations of RNNs motivating transformers

3,222 words15 min readRead in English

4.1.1 · AI-ML › Transformer Architecture

Overview

Recurrent Neural Networks (RNNs) 1990s se lekar 2016 tak sequence modeling ke liye dominant architecture thi, lekin inme kuch fundamental architectural constraints hain jinhe overcome karne ke liye transformers ko explicitly design kiya gaya tha. RNNs kyun fail hote hain ye samajhna is baat ki appreciation ke liye critical hai ki transformer architecture kyun kaam karta hai.

Figure — Limitations of RNNs motivating transformers

Core Problem: Sequential Processing Bottleneck

Limitation 1: Time Ke Across Parallelize Nahi Ho Sakta

Math: Parallelization Kyun Impossible Hai

RNN recurrence relation se shuru karte hain:

Aao directly compute karne ki koshish karte hain. Humein chahiye:

Lekin ke liye chahiye:

Aur ke liye chahiye:

Conclusion: compute karne ke liye, humein pehle sequentially ZAROOR compute karne padte hain. "Skip ahead" karne ka koi tarika nahi hai kyunki har hidden state apne previous state ki ek non-linear function hai.

Limitation 2: Vanishing Gradient Problem

Gradient Flow Derive Karna

Backpropagation through time (BPTT) ke dauran, gradients ko recurrence ke through backward flow karna padta hai:

wale vanilla RNN ke liye:

Kyunki , aur typically zyaadatar values ke liye :

jahan , ki average derivative hai.

timesteps ke upar:

Limitation 3: Fixed Compression Bottleneck

Information Theory Perspective

Ek RNN encoder ko poori input sequence ko ek single fixed-size vector (final hidden state) mein compress karna padta hai.

Attention Mechanism: Ek Partial Solution

Attention mechanism (Bahdanau et al., 2014) specifically fixed bottleneck address karne ke liye introduce kiya gaya tha:

Decoder ab sirf final ki bajaye SAARE encoder hidden states ko "dekhne ke liye" wapas ja sakta hai.

Limitation 4: Long-Range Dependencies ke Saath Difficulty

Long-Range Problem ko Quantify Karna

Positions aur ke beech dependency sikhne ke baare mein sochon jahan ho.

Ek RNN mein:

  • Gradient path ki length hai (must propagate through intermediate steps)
  • Gradient magnitude ke roop mein scale hoti hai jahan
  • Effective learning distance ke saath exponentially diminish hoti hai

Ek Transformer mein:

  • Self-attention ke zariye direct connection: position , position ko directly attend karta hai
  • Gradient path ki length 1 hai (plus network ki depth, lekin sequence length nahi)
  • Saari distances ke liye learning equally effective hai

Limitation 5: Memory aur Computation Inefficiency

Training Complexity

RNN Training:

  • Backpropagation (BPTT) ke liye saare hidden states store karne padte hain
  • Memory: jahan hidden size hai
  • Efficiently checkpoint nahi kar sakte kyunki har , par depend karta hai

Transformer Training:

  • Koi sequential dependency nahi; activation checkpointing use kar sakte hain
  • Store karne ki bajaye backward pass ke dauran activations recompute karo
  • Memory: checkpointing ke saath

Transformers Revolutionary Kyun The

Recall Ek 12-saal ke bache ko explain karo

Socho tum ek lambi kahani ek ek word sun ke samajhne ki koshish kar rahe ho, lekin tumhari memory bahut kharab hai. Jab tak tum word 100 sunte ho, tab tak word 1 kya tha ye bhul chuke hote ho. Isi tarah RNNs kaam karte hain - unhe words ek order mein process karne padte hain aur wo bahut pehle ki cheezein bhool jaate hain.

Ab socho tum POORI kahani likhi hui dekh sako, aur jab bhi chaho uska koi bhi hissa dekh sako. Tum ek saath word 1 aur word 100 dekh sako! Tum kai logon ko alag-alag hisse simultaneously padhne bhi de sakte ho. Isi tarah transformers kaam karte hain - wo ek saath saare words "dekh" sakte hain aur kisi bhi word par dhyan de sakte hain, chahe words kitni bhi door hon.

Result? Transformers hain:

  1. Way faster - jaise 100 log alag alag hisse padh rahe hon vs. 1 insaan ek ek word padh raha ho
  2. Better memory - wo words ke beech connections yaad rakh sakte hain chahe wo bahut door hon
  3. Smarter understanding - wo dekh sakte hain ki SAARE words ek doosre se kaise relate karte hain, sirf nearby words se nahi

Transformer Solution: Teen Key Innovations

  1. Self-Attention: Har position directly har doosre position se connect hoti hai

  2. Positional Encoding: Kyunki koi sequential processing nahi hai, positions explicitly encode ki jaati hain

  3. Parallel Processing: Saare positions simultaneously compute hote hain

    • Position 1 apni representation compute karta hai jabki position 1000 apni representation compute karta hai
    • Previous timesteps ka koi wait nahi

Connections


Flashcards

#flashcards/ai-ml

RNNs mein sequential processing bottleneck kya hai? :: RNNs hidden states iteratively compute karte hain ke roop mein, ek causal chain banate hain jahan tab tak compute nahi ho sakta jab tak available na ho. Isse computation sequential steps ban jaata hai jo parallelize nahi ho sakta.

RNNs time ke across computation parallelize kyun nahi kar sakte?
Kyunki har hidden state , previous state ki ek non-linear function hai, compute karne ka koi tarika nahi hai bina pehle sab sequence mein compute kiye.

RNNs ke liye T timesteps mein vanishing gradient formula derive karo :: se shuru karte hue, hamare paas hai jahan . steps mein, gradients ke roop mein decay karte hain. Agar ye product 1 se kam hai, to gradients exponentially vanish hote hain.

Encoder-decoder RNNs mein compression bottleneck kya hai?
Encoder ko poori input sequence ko ek single fixed-size hidden state mein compress karna padta hai. Isse ek information bottleneck banta hai: ek -dimensional vector ki limited capacity hoti hai jabki sequence mein bits information ho sakti hai.
Transformers RNNs se long sequences par kitna faster ho sakte hain?
Transformers ki parallel time complexity hai RNNs ki sequential time ke comparison mein. length ki sequences par sufficient parallel hardware ke saath, transformers wall-clock time mein ~1000× faster ho sakte hain.

LSTMs vanishing gradient problem fully kyun solve NAHI karte? :: LSTMs problem ko mitigate karte hain, eliminate nahi karte. Forget gate gradient ko abhi bhi multiply karta hai: . Agar zyaadatar steps par hai, gradients abhi bhi exponentially decay karte hain, bas thoda slow. LSTMs effective range ko ~10 se ~100 steps tak extend karte hain, lekin 1000+ token sequences ke liye problems wapas aati hain.

RNNs ke liye effective context window formula kya hai?
Effective context wo distance ke roop mein define karo jahan gradient 1% tak decay ho jaaye: , to . wale vanilla RNN ke liye: steps. wale LSTM ke liye: steps.
Self-attention long-range dependency problem kaise solve karta hai?
Self-attention saare positions ke beech direct connections create karta hai: position , position ko single matrix operation se attend karta hai, distance se independent. Gradient path ki length 1 hai (length nahi), distance ke saath exponential decay eliminate karta hai.

Concept Map

uses

creates

causes

causes

causes

results in

leads to

motivates

motivates

relies on

enables

solves

RNNs for sequences

Recurrence ht=f h t-1, xt

Causal dependency chain

No parallelization

Long-range info loss

Training inefficiency

Sequential time O of T

GPUs sit idle

Transformers

Self-attention

Parallel time O of 1