Key insight: Moving data from HBM to SRAM is ~12× slower (in bandwidth) than moving within on-chip SRAM, so memory movement—not arithmetic—dominates runtime!
Example calculation:
N = 2048, d = 64 (typical)
HBM reads/writes: 20482=4M floats = 16 MB (just for S)
Memory transfer time: 16 MB/1.5 TB/s≈10μs
Compute time for the softmax elementwise work: ~4 µs
The transfer alone takes about the same order as compute, and because standard attention repeatedly reads/writes these N2 matrices, memory movement (not FLOPs) becomes the true bottleneck.
Standard softmax requires the full row:
softmax(xi)=∑jexjexi
Problem: We need ∑jexj before computing any output!
Solution: Process in chunks and update statistics incrementally.
Step 1: Safe softmax (numerical stability)
Raw ex overflows. Standard trick:
softmax(xi)=∑jexj−mexi−m
where m=max(x).
Step 2: Split sum across blocks
Suppose we process attention scores in two blocks: x(1) and 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: Merge blocks
Global max: mnew=max(m(1),m(2))
Why rescale? Our sums used different "reference points" (m(1) vs m(2)). We need a common baseline.
Corrected sum:
ℓnew=em(1)−mnewℓ(1)+em(2)−mnewℓ(2)
Why this works:ℓ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)
Similarly, the weighted output accumulates. If O~(b)=∑j∈block bexj−m(b)vj is the unnormalized block output, then:
Onew=ℓnewem(1)−mnewO~(1)+em(2)−mnewO~(2)
Imagine you're solving a 1000×1000 Sudoku puzzle, and you need to check how every cell relates to every other cell—that's 1 million comparisons!
Bad way: Write down all 1 million results on paper, then use them. But you run out of paper (memory)!
Flash Attention way: Divide the puzzle into smaller 100×100 chunks. Check one chunk at a time, keep a running note of "what I've seen so far," then throw away the chunk and load the next one. You never need a million sheets of paper—just enough for one chunk!
The trick is the "running note"—you update your summary of "what's the biggest number I've seen" and "what's the sum so far" without keeping every single number. That's the incremental softmax math!
Why it's faster: Moving paper in and out of your backpack (memory) is slow. If you can keep everything in your hands (fast memory), you work much faster. Flash Attention keeps everything in your hands by working on smaller chunks.
4.13-Scaling-laws-for-transformers — Efficient attention enables larger N, affecting scaling laws
5.27-Gradient-checkpointing — Similar recomputation trade-off
4.1.8-Positional-encoding — Flash Attention preserves positional information
4.2.1-BERT-architecture — BERT benefits from Flash for long documents
4.2.3-GPT-architecture — GPT-3/4 use Flash-like optimizations for long context
#flashcards/ai-ml
What is the memory complexity of standard attention vs Flash Attention?
Standard: O(N2) for storing attention matrix. Flash: O(N) by tiling and never materializing full matrix.
Why is Flash Attention faster despite doing more FLOPs?
Memory bandwidth is the bottleneck, not compute. Flash reduces HBM accesses from O(N2) to O(N), which dominates the small increase in FLOPs from recomputation.
What is incremental softmax in Flash Attention?
Computing softmax in chunks by maintaining running max (m) and sum (ℓ), then merging blocks by rescaling: ℓnew=em(1)−mnewℓ(1)+em(2)−mnewℓ(2).
Does Flash Attention produce the same output as standard attention?
Yes, numerically equivalent in forward pass. Tiny differences in backward pass due to recomputation/rounding order, but negligible.
What is the complexity of local attention with window size w?
O(Nw). For constant w, this is linear in sequence length N, compared to O(N2) for full attention.
How does linear attention achieve O(N) complexity?
By using kernel trick: ϕ(Q)(ϕ(K)TV) instead of (QKT)V. This changes order of operations from O(N2d) to O(Nd2)—linear in N if d constant.
What is the main trade-off in Flash Attention's backward pass?
Recompute attention matrices (S, P) instead of storing them. Adds ~20% compute but saves O(N2) memory, which is a huge win since memory bandwidth is the bottleneck.
Why does Flash Attention speedup increase with sequence length?
Standard attention's memory cost grows as O(N2), Flash's as O(N). The absolute gap widens for longer sequences.
Chalo is topic ka core intuition samajhte hain. Transformer mein jab attention calculate hota hai, to woh ek bahut bada matrix banata hai jiska size sequence_length × sequence_length hota hai. Matlab agar tumhare paas 2048 tokens hai, to yeh matrix 2048×2048 ka ho jaata hai — bahut sara memory kha jaata hai! Normal attention pehle poora yeh matrix GPU ki memory mein store karta hai, phir use karta hai. Yeh waise hi hai jaise tum poori library ki photocopy nikalo bina yeh soche ki tumhe sirf kuch pages chahiye. Flash Attention ka core idea yeh hai ki poora matrix banao hi mat — chhote-chhote tiles (blocks) mein kaam karo, ek kitaab ki tarah page-by-page padhte jao.
Ab yeh "why it matters" wali baat samajhna zaroori hai. Log sochte hain ki GPU fast hone ka matlab hai zyada calculations (FLOPs) karna, lekin asli bottleneck compute nahi, balki memory movement hai. GPU mein do type ki memory hoti hai — SRAM (on-chip, super fast, chhoti) aur HBM (GPU RAM, badi lekin slow). Jab bar-bar yeh N×N matrix ko HBM mein likhna aur wapas padhna padta hai, to yeh data ka aana-jaana hi sabse zyada time leta hai, chahe calculation kitni bhi jaldi ho jaaye. Isliye Flash Attention in memory round-trips ko hataake attention ko 2-4× faster bana deta hai aur memory bhi O(N²) se O(N) tak kam kar deta hai.
Iska practical faayda yeh hai ki ab hum bahut lambi sequences ko process kar sakte hain jo pehle memory ki wajah se possible hi nahi thi. Flash Attention ek clever trick use karta hai jise "incremental softmax" kehte hain — softmax ke liye normally poori row chahiye hoti hai, lekin yeh running statistics maintain karke chunks mein kaam kar leta hai, aur backward pass mein values ko store karne ke bajaye on-the-fly recompute kar leta hai. Yeh yaad rakhna important hai kyunki modern LLMs jaise GPT aur LLaMA sab isi technique par chalte hain — yeh sirf theory nahi, real-world efficiency ka backbone hai jo AI ko scalable banata hai.