4.1.13 · HinglishTransformer Architecture

Computational complexity of attention

2,465 words11 min readRead in English

4.1.13 · AI-ML › Transformer Architecture

The fundamental cost breakdown

Memory and operations in standard self-attention

Self-attention mechanism har token ke liye teen matrices compute karta hai: queries (Q), keys (K), aur values (V). Complexity sabhi pairs of tokens ke beech attention scores compute karne se aati hai.

Chaliye scratch se complexity derive karte hain.

Deriving the time complexity

Step 1: Linear projections

Input aur weight matrices ke liye:

Yeh ek matrix multiplication hai: output.

Cost: Har output element ke liye multiplications chahiye, total: operations.

Teeno projections (Q, K, V) ke liye: operations.

Yeh step kyun? Hum har token ki embedding ko query/key/value space mein transform kar rahe hain. Yeh sequence length mein linear hai—problem yeh nahi hai.

Step 2: Attention scores compute karna

Matrix dimensions:

Cost: output elements mein se har ek ke liye multiplications chahiye.

Total: operations.

Yeh step kyun? Yahi woh jagah hai jahan har token khud ko har doosre token se compare karta hai. output inherently quadratic hai—yahi bottleneck hai.

Step 3: Softmax

ki har row ke liye (ek query), compute karo:

Cost: per row, rows total: operations.

Yeh step kyun? Attention weights ko normalize karna. Scores ki sankhya mein abhi bhi quadratic hai.

Step 4: Weighted sum

Cost: operations.

Yeh step kyun? Har output token sabhi value vectors ka weighted combination hota hai.

Step 5: Output projection

Heads ko -dimensional vector mein concatenate karne ke baad, hum output projection apply karte hain:

Cost: operations.

Yeh step kyun? Output projection heads ke beech information mix karta hai aur model dimension par wapas map karta hai. Yeh ek aur term contribute karta hai—linear-projection cost count karte waqt ise bhoolna mat.

Memory complexity

Figure — Computational complexity of attention

Why each component matters

Linear projections:

Yeh sequence length ke saath linearly scale karta hai. double karo toh kaam double ho jaata hai. Lambi sequences ke liye bottleneck nahi hai lekin bahut choti sequences ke liye dominate karta hai jahan . Yeh term Q, K, V projections aur output projection ko cover karta hai—milake roughly operations, lekin asymptotically .

Attention matrix:

Yahi core problem hai. term ka matlab hai:

  • 2× sequence length → 4× compute
  • 10× sequence length → 100× compute

Standard attention mein yeh kyun unavoidable hai: Har token ko decide karne ke liye ki "kise dhyan dena hai", har doosre token ke saath score chahiye.

Multi-head attention scaling

attention heads ke saath, har dimension ka:

  • Attention cost: heads mein se har ek ka cost hai, toh total (kyunki ).
  • Projection cost: Q, K, V projections aur output projection sabhi full -dimensional space par ek baar operate karte hain, har head ke liye ek baar nahi. Toh projection cost rehta hai — yeh se multiply nahi hota.

Key insight: Zyada heads use karne se asymptotic complexity nahi badalti (abhi bhi ), aur projection term mein ka factor nahi lagta. Heads mein split karna sirf same -dimensional projections ko reshape karta hai.

Comparison with other architectures

| Architecture | Time complexity | Memory | Receptive field | |------------|-----------------|-----------------| | Self-attention | | | Global (sabhi tokens) | | RNN (LSTM/GRU) | | | Sequential (limited) | | CNN (kernel ) | | | Local ( tokens) | | Sparse attention | | | Structured patterns | | Linear attention | | | Global (approximation) |

Trade-off: Self-attention global context turant deta hai (kisi bhi do tokens ke beech depth-1 path) lekin quadratic cost chukani padti hai. RNNs linear hain lekin door ke tokens ko connect karne ke liye sequential steps chahiye.

Efficient attention variants

bottleneck ne ek research area ko janam diya hai:

  1. Sparse attention (Sparse Transformer, Longformer): Sirf token pairs ke subset ke liye attention compute karo (jaise local windows + global tokens). ya tak reduce ho jaata hai.

  2. Linear attention (Performers, Linear Transformers): Kernel methods se attention approximate karo, achieve karte hue matrix ko kabhi materialize kiye bina.

  3. Flash Attention: complexity rakhta hai lekin tiling use karke memory access patterns optimize karta hai, memory ko se tak reduce karta hai full attention matrix store kiye bina.

Practical implications

Training vs. inference

Training: ko backpropagation ke liye store karna padta hai. Memory aksar limiting factor hoti hai, compute nahi.

Inference: Full matrices store kiye bina on-the-fly attention kar sakta hai. KV-caching help karta hai: past keys/values store karo taaki har naye token ke liye unhe recompute na karna pade.

Why context length is expensive

Quadratic scaling ka matlab hai:

  • GPT-3 (2K context): manageable
  • GPT-4 (32K context): attention term mein 8K ke comparison mein 16× memory aur compute
  • 100K context: 4K ke comparison mein 625× cost

Isi liye long-context models ya toh sparse attention use karte hain ya run karne ke liye bahut expensive hain.

Recall Ek 12-saal ke bacche ko explain karo

Socho tum students ki ek class mein ho, aur har kisi ko information share karne ke liye har doosre ko ek chit deni padti hai (yahi attention hai). Agar 10 students hain, toh tumhe chits chahiye. Class double karke 20 students kar do? Ab tumhe chits chahiye—chaar guna zyada!

Isi liye transformers lambe texts ke saath slow ho jaate hain: har word ko har doosre word se "baat" karni padti hai, toh length double karne par kaam chaar guna ho jaata hai. Samajhdar researchers aisa tarike dhundh rahe hain jisse words sirf apne neighbors se baat kar sakein ya sab chits dene se bachne ke liye tricks use kar sakein, jisse yeh bahut faster ban jaaye.

Connections

  • 4.1.1-Self-attention-mechanism: Woh mechanism jo yeh complexity cause karta hai
  • 4.1.7-Multi-head-attention: Heads split karne se computational cost kaise affect hoti hai
  • 4.3.2-Sparse-attention-patterns: Quadratic complexity reduce karne ke solutions
  • 4.5.1-Memory-optimization-techniques: Flash Attention aur gradient checkpointing
  • 5.2.3-Positional-encoding-scaling: Kyun lambi sequences ko efficient attention chahiye

#flashcards/ai-ml

Standard self-attention ki time complexity kya hai? :: , lambi sequences ke liye dominant hota hai jahan computation ko operations lagte hain.

Self-attention sequence length mein quadratic kyun hai?
Kyunki attention scores compute karne par ek matrix banti hai jahan har token ko har doosre token ke saath score compute karna padta hai, jiske liye operations chahiye.
Self-attention ki memory complexity kya hai?
. term backpropagation ke liye attention score matrix store karne se aata hai, jo lambi sequences mein dominate kar sakta hai.
Sequence length double karne par attention compute par kya asar padta hai?
Compute chaar guna ho jaata hai kyunki attention hai. se jaane par cost se ho jaati hai.

Self-attention aur RNN ki time complexity compare karo :: Self-attention hai lekin parallel hai; RNN hai lekin sequential hai. Lambi sequences ke liye (), attention slower hai lekin ek hi step mein global receptive field deta hai.

Kya multi-head attention projection cost ko h se multiply karta hai?
Nahi. Q, K, V aur output projections full -dimensional space par ek baar operate karte hain, toh unka cost hai— nahi. Sirf attention term heads ke across sum hota hai, jo deta hai.

Self-attention mein computational bottleneck kya hai? :: matrix multiplication, jo hai. Tokens ke beech yeh all-to-all comparison inherently quadratic hai aur lambi sequences mein dominate karta hai.

term mein kaunse projections contribute karte hain?
Query, key, value projections aur output projection . Milake yeh roughly operations hain, lekin asymptotically .
n=2048 ke saath ek layer mein attention scores ke liye kitni memory chahiye?
million floats. FP32 (4 bytes each) par, roughly 16 MB per attention head. Multiple heads aur layers ke saath, yeh jaldi badh jaata hai.
Aap ek poori book ko naively GPT mein feed kyun nahi kar sakte?
Quadratic complexity ki wajah se: ek book mein 100K tokens ho sakte hain. Yeh billion attention computations per layer hai, jiske liye massive memory ( floats ≈ 40 GB sirf attention matrices ke liye) aur compute chahiye.

Concept Map

step 1

step 2

feeds

feeds

then

costs

every pair compared

adds

costs

costs nd^2

causes

doubling n quadruples compute

Self-attention mechanism

Linear projections Q K V

Scores S = QK^T

Softmax normalization

Value aggregation AV

Output projection W_O

Linear cost 3nd*dk

Quadratic cost n^2*dk

Quadratic bottleneck

Efficient attention variants