4.1.5 · HinglishTransformer Architecture

Multi-head attention

2,909 words13 min readRead in English

4.1.5 · AI-ML › Transformer Architecture

What Problem Does It Solve?

Single-head attention ki representational capacity limited hoti hai. projections ka ek single set sirf ek type ka relationship pattern seekh sakta hai. Lekin language (aur zyaattar sequential data) mein ek saath kaafi saare structure types hote hain:

  • Syntactic dependencies (subject verb se agree karta hai)
  • Semantic relationships (synonym, antonyms)
  • Positional patterns (nearby words vs. distant context)
  • Co-reference (pronouns jo pehle ke nouns se link hote hain)

Multi-head attention (MHA) multiple representation subspaces parallel mein seekhta hai, jisme har head alag patterns mein specialize karta hai.


Definition and Architecture

Figure — Multi-head attention

Derivation From First Principles

Step 1: Why Reduce Dimension Per Head?

Goal: attention heads ko parallel mein run karo bina computational cost badhaaye.

Agar hamare paas hai aur hum heads chahte hain:

  • Naive approach: Har head full 512-dim use kare → total cost
  • Smart approach: Har head use kare → total cost

Yeh kaam kyun karta hai: Attention cost hai jahan = sequence length, = dimension.

  • 8 heads of 64-dim each:
  • Ek 512-dim head jitna hi total FLOPs, lekin hume milte hain 8 alag views!

Step 2: Projecting Into Head Subspaces

Head ke liye, hum shared (shape ) ko mein project karte hain:

jahan learned hain, har head ke liye alag.

Alag projections kyun?

  • Agar saare heads same use karein, toh woh identical attention patterns compute karenge—koi faayda nahi!
  • Alag projections har head ko same input se alag features extract karna seekhne dete hain.

Intuition: Har ko ek alag sawaal poochne ki tarah socho:

  • Head 1 capture kar sakta hai "Kaun se words grammatically related hain?"
  • Head 2 capture kar sakta hai "Kaun se words semantically similar hain?"
  • Head 3 capture kar sakta hai "Kaun se words position mein nearby hain?"

Step 3: Compute Scaled Dot-Product Attention Per Head

Har head independently compute karta hai:

Yeh har head ke liye shape ka output deta hai.

Same scaled dot-product formula kyun?

  • Kaam karta hai! Jaadu alag-alag learned projections mein hai, attention mechanism mein khud nahi.
  • Same mechanism use karne se har head same tarike se interpretable hota hai.

Step 4: Concatenate Heads

head outputs ko side-by-side stack karo:

Yeh shape produce karta hai (kyunki ).

Sum/average ki jagah concatenate kyun?

  • Hum har head ki saari information preserve karna chahte hain.
  • Sum karne se woh merge ho jaate, aur har head ne jo distinct patterns seekhe hain woh kho jaate.
  • Concatenation unhe alag rakhta hai, lekin unhe blend karne ke liye ek aur step chahiye...

Step 5: Final Linear Projection

jahan learned hai.

Yeh final projection kyun?

  • Concatenated heads abhi bhi alag "channels" mein hain— model ko heads ke across information mix karne deta hai.
  • Yeh seekhta hai ki alag types ki attention (syntactic + semantic + positional) ko kaise combine karein.
  • Output ko full dimension par waapas laata hai, next layer ke liye ready.

Complete Forward Pass Example



Common Mistakes


Parameter Count

Chalo multi-head attention ke parameters count karte hain:

Per head:

  • :
  • :
  • :
  • Total per head:

All heads: (kyunki )

Output projection: ke paas parameters hain

Total: 4 d_{\text{model}}^2==$ parameters


Computational Complexity

Per head:

  • compute karna: har ek , total
  • Attention scores :
  • Softmax:
  • ke saath weighted sum:

All heads: Per-head cost ka guna

Dominant term: Attention scores (kyunki )

Output projection:

Lambi sequences ke liye jahan , attention term dominate karta hai. Isliye Transformers bahut lambi sequences (length 10,000+) ke saath struggle karte hain—sequence length mein quadratic!


Recall Ek 12-Saal-Ke Bachche Ko Explain Karo

Socho tum apne dost ki lambi kahani samajhne ki koshish kar rahe ho. Tum ek saath sab par focus nahi kar sakte, toh tumhara brain kuch smart karta hai: woh ek saath alag-alag tareekon se alag-alag parts par dhyan deta hai.

  • Tumhare brain ka ek hissa sunata hai ki kisne kya kiya (jaise "Alice ne ball throw ki").
  • Doosra hissa emotions sunata hai (jaise "woh excited thi").
  • Teesra hissa track karta hai ki cheezein kab hui (jaise "pehle... phir... aakhir mein..."). Multi-head attention waise hi kaam karta hai! Computer ke paas multiple "attention heads" hote hain (jaise tumhare brain ke alag-alag hisse), aur har ek text mein alag patterns par focus karta hai:
  • Head 1 un words ko connect kar sakta hai jo grammatically saath jaate hain ("the cat" → "sat").
  • Head 2 un words ko connect kar sakta hai jo similar cheezein mean karte hain ("happy" ← "joyful").
  • Head 3 nearby words ko connect kar sakta hai.

Har head ek special talent waale dost ki tarah hai—ek grammar mein great hai, ek feelings samajhne mein, ek order yaad rakhne mein. In saare doston ko saath kaam karwa ke (wahi "multi" part hai), computer kahani ko kahin behtar samajhta hai jaise ki sirf ek dost help kar raha ho!

Ant mein "concatenate and project" step waise hai jaise saare doston ko baith ke share karwana ki unhone kya notice kiya, taaki tumhe poori picture mile.



Connections Scaled Dot-Product Attention: Har head ke andar use hone wala attention mechanism

  • Self-Attention: MHA typically self-attention mein use hota hai (Q, K, V same source se)
  • Cross-Attention: MHA encoder-decoder attention ke liye bhi use ho sakta hai (Q decoder se, K,V encoder se)
  • Transformer Encoder Layer: MHA ke baad feed-forward network use karta hai
  • Positional Encoding: Position information provide karta hai jise attention heads leverage kar sakte hain
  • Attention Visualization: Techniques jo interpret karti hain ki har head ne kya seekha
  • Query-Key-Value Intuition: Projections samajhne ki foundation
  • Linear Projections in Neural Networks: matrices kya karte hain

#flashcards/ai-ml

Single-head attention ke mukable multi-head attention ka primary advantage kya hai? :: Multi-head attention multiple representation subspaces parallel mein seekhta hai, jisse model ek saath alag types ke relationships (syntactic, semantic, positional) capture kar sakta hai. Ek single head ki representational capacity limited hoti hai.

heads aur model dimension wale multi-head attention mein, har head ki dimension kya hoti hai?
Har head dimension mein operate karta hai. Yeh total computation constant rakhta hai jabki parallel specialization enable karta hai.
Har head ke liye alag projection matrices kyun use karte hain?
Alag projections har head ko same input se alag features seekhne dete hain. Agar saare heads identical projections use karein, toh woh identical attention patterns compute karenge, koi faayda nahi.
Multi-head attention ka formula kya hai?
jahan
Head outputs ko sum karne ki jagah concatenate kyun karte hain?
Concatenation har head ki distinct information preserve karta hai. Summing patterns ko mix kar deta, aur har head ne jo specialized information seekhi hai woh kho jaati. Final projection phir seekh sakta hai ki heads ko optimally kaise combine karein.
Model dimension wale multi-head attention ka total parameter count kya hai?
parameters: saare heads mein projections ke liye , plus output projection ke liye .
Multi-head attention ki computational complexity kya hai?
Dominant term hai jahan sequence length hai, jo saari positions mein attention scores compute karne se aata hai. Yeh sequence length mein quadratic hai.
Multi-head attention mein final projection kyun exist karta hai?
model ko heads ke across information mix karne deta hai. Concatenation ke baad, heads abhi bhi alag "channels" mein hain— seekhta hai ki alag types ki attention (syntactic + semantic + positional) ko task ke liye optimally kaise combine karein.

Concept Map

limited capacity

motivates

runs

each learns

specializes in

reduces dim per head

keeps constant

project via

feeds

combined by

produces

Single-head attention

One relationship type only

Multi-head attention

h parallel heads

Own representation subspace

Syntax semantics position coreference

d_k equals d_model over h

Total FLOPs and params

Learned W_Q W_K W_V

Attention per head

Concat then W_O

Richer output representation