Key insight: HBM se SRAM mein data move karna on-chip SRAM mein move karne se ~12× slower (bandwidth mein) hai, isliye memory movement — arithmetic nahi — runtime dominate karta hai!
Example calculation:
N = 2048, d = 64 (typical)
HBM reads/writes: 20482=4M floats = 16 MB (sirf S ke liye)
Memory transfer time: 16 MB/1.5 TB/s≈10μs
Softmax elementwise work ka compute time: ~4 µs
Transfer akela compute ke same order ka time leta hai, aur kyunki standard attention repeatedly N2 matrices read/write karta hai, memory movement (FLOPs nahi) true bottleneck ban jaata hai.
Standard softmax ko poori row chahiye:
softmax(xi)=∑jexjexi
Problem: Koi bhi output compute karne se pehle humein ∑jexj chahiye!
Solution: Chunks mein process karo aur statistics incrementally update karo.
Step 1: Safe softmax (numerical stability)
Raw ex overflow karta hai. Standard trick:
softmax(xi)=∑jexj−mexi−m
jahan m=max(x).
Step 2: Sum ko blocks mein split karo
Maano hum attention scores do blocks mein process karte hain: x(1) aur x(2).
Block 1:
m(1)=max(x(1)),ℓ(1)=∑j∈block 1exj−m(1)
Block 2:
m(2)=max(x(2)),ℓ(2)=∑j∈block 2exj−m(2)
Step 3: Blocks merge karo
Global max: mnew=max(m(1),m(2))
Rescale kyun? Humare sums alag "reference points" use karte the (m(1) vs m(2)). Humein ek common baseline chahiye.
Corrected sum:
ℓnew=em(1)−mnewℓ(1)+em(2)−mnewℓ(2)
Yeh kyun kaam karta hai:ℓnew=∑jexj−mnew=∑j∈block 1exj−mnew+∑j∈block 2exj−mnew=∑j∈block 1e(xj−m(1))+(m(1)−mnew)+∑j∈block 2e(xj−m(2))+(m(2)−mnew)=em(1)−mnewℓ(1)+em(2)−mnewℓ(2)
Isi tarah, weighted output accumulate hota hai. Agar O~(b)=∑j∈block bexj−m(b)vjunnormalized block output hai, to:
Onew=ℓnewem(1)−mnewO~(1)+em(2)−mnewO~(2)
Yeh kisi bhi number of blocks tak generalize hota hai!
Socho tum ek 1000×1000 Sudoku puzzle solve kar rahe ho, aur tumhe check karna hai ki har cell kaise har doosri cell se relate karti hai — yeh 1 million comparisons hain!
Bura tarika: Saare 1 million results kagaz par likh lo, phir use karo. Lekin tumhara kagaz khatam ho jaata hai (memory)!
Flash Attention tarika: Puzzle ko chhote 100×100 chunks mein divide karo. Ek time mein ek chunk check karo, "maine ab tak kya dekha" ka ek running note rakho, phir chunk phenk do aur agla load karo. Tumhe kabhi ek million sheets of paper ki zaroorat nahi — sirf ek chunk ke liye kaafi!
Trick "running note" hai — tum apna summary update karte ho "maine ab tak sabse bada number kya dekha" aur "ab tak sum kya hai" bina har ek number keep kiye. Yahi incremental softmax math hai!
Kyun faster hai: Apne backpack mein paper andar bahar karna (memory) slow hai. Agar tum sab kuch apne haath mein rakh sako (fast memory), tum bahut faster kaam karte ho. Flash Attention chhote chunks par kaam karke sab kuch haath mein rakhta hai.
4.13-Scaling-laws-for-transformers — Efficient attention larger N enable karta hai, scaling laws affect karta hai
5.27-Gradient-checkpointing — Similar recomputation trade-off
4.1.8-Positional-encoding — Flash Attention positional information preserve karta hai
4.2.1-BERT-architecture — BERT long documents ke liye Flash se benefit karta hai
4.2.3-GPT-architecture — GPT-3/4 long context ke liye Flash-like optimizations use karte hain
#flashcards/ai-ml
Standard attention vs Flash Attention ki memory complexity kya hai?
Standard: O(N2) attention matrix store karne ke liye. Flash: O(N) tiling se aur full matrix materialize kiye bina.
Flash Attention zyada FLOPs karne ke bawajood faster kyun hai?
Memory bandwidth bottleneck hai, compute nahi. Flash HBM accesses O(N2) se O(N) tak reduce karta hai, jo recomputation se thoda FLOPs badhne par dominate karta hai.
Flash Attention mein incremental softmax kya hai?
Running max (m) aur sum (ℓ) maintain karke chunks mein softmax compute karna, phir blocks merge karna rescaling se: ℓnew=em(1)−mnewℓ(1)+em(2)−mnewℓ(2).
Kya Flash Attention standard attention ke jaisa hi output produce karta hai?
Haan, forward pass mein numerically equivalent. Backward pass mein recomputation/rounding order ki wajah se tiny differences, lekin negligible hain.
Window size w ke saath local attention ki complexity kya hai?
O(Nw). Constant w ke liye, yeh sequence length N mein linear hai, full attention ke O(N2) ke comparison mein.
Linear attention O(N) complexity kaise achieve karta hai?
Kernel trick use karke: (QKT)V ki jagah ϕ(Q)(ϕ(K)TV). Yeh operations ka order O(N2d) se O(Nd2) mein change karta hai — N mein linear agar d constant ho.
Flash Attention ke backward pass mein main trade-off kya hai?
Attention matrices (S, P) store karne ki jagah recompute karo. ~20% compute badhta hai lekin O(N2) memory bachti hai, jo ek huge win hai kyunki memory bandwidth bottleneck hai.
Flash Attention speedup sequence length ke saath kyun badhta hai?
Standard attention ki memory cost O(N2) badhti hai, Flash ki O(N). Absolute gap longer sequences ke liye aur bada hota hai.