4.1.14 · HinglishTransformer Architecture

Flash attention and efficient attention

2,728 words12 min readRead in English

4.1.14 · AI-ML › Transformer Architecture

The Standard Attention Problem

Computational flow:

  1. compute karo — attention scores matrix
  2. apply karo — attention probability matrix
  3. Output compute karo

Memory Hierarchy: Why Speed Isn't About FLOPs

Modern GPUs ki ek memory hierarchy hoti hai:

SRAM (on-chip, fast): ~20 MB, ~19 TB/s bandwidth
HBM (GPU RAM, slow): ~40 GB, ~1.5 TB/s bandwidth

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: floats = 16 MB (sirf S ke liye)
  • Memory transfer time:
  • Softmax elementwise work ka compute time: ~4 µs
  • Transfer akela compute ke same order ka time leta hai, aur kyunki standard attention repeatedly matrices read/write karta hai, memory movement (FLOPs nahi) true bottleneck ban jaata hai.

Flash Attention: Tiled Computation

Derivation: Incremental Softmax

Standard softmax ko poori row chahiye:

Problem: Koi bhi output compute karne se pehle humein chahiye!

Solution: Chunks mein process karo aur statistics incrementally update karo.

Step 1: Safe softmax (numerical stability)

Raw overflow karta hai. Standard trick: jahan .

Step 2: Sum ko blocks mein split karo

Maano hum attention scores do blocks mein process karte hain: aur .

Block 1:

Block 2:

Step 3: Blocks merge karo

Global max:

Rescale kyun? Humare sums alag "reference points" use karte the ( vs ). Humein ek common baseline chahiye.

Corrected sum:

Yeh kyun kaam karta hai:

Isi tarah, weighted output accumulate hota hai. Agar unnormalized block output hai, to:

Yeh kisi bhi number of blocks tak generalize hota hai!

Figure — Flash attention and efficient attention

Backward Pass: Recomputation

Yeh kyun kaam karta hai:

  • Forward pass: Q,K,V ka 1 read + HBM mein writes
  • Standard backward: Stored S,P matrices read karo ( elements)
  • Flash backward: S,P SRAM mein recompute karo (koi HBM read nahi), sirf Q,K,V load karo

Memory bandwidth: Flash reads karta hai vs standard reads. Long sequences ke liye, Flash massively jeetta hai.

Other Efficient Attention Variants

Practical Impact

Recall 12-Saal-Ke-Bachche Ko Explain Karo

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.

Connections

  • 4.1.1-Self-attention-mechanism — Woh core operation jise Flash optimize karta hai
  • 4.1.3-Multi-head-attention — Flash Attention per-head kaam karta 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: attention matrix store karne ke liye. Flash: 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 se tak reduce karta hai, jo recomputation se thoda FLOPs badhne par dominate karta hai.
Flash Attention mein incremental softmax kya hai?
Running max () aur sum () maintain karke chunks mein softmax compute karna, phir blocks merge karna rescaling se: .
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 ke saath local attention ki complexity kya hai?
. Constant ke liye, yeh sequence length mein linear hai, full attention ke ke comparison mein.
Linear attention complexity kaise achieve karta hai?
Kernel trick use karke: ki jagah . Yeh operations ka order se mein change karta hai — N mein linear agar constant ho.
Flash Attention ke backward pass mein main trade-off kya hai?
Attention matrices (, ) store karne ki jagah recompute karo. ~20% compute badhta hai lekin 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 badhti hai, Flash ki . Absolute gap longer sequences ke liye aur bada hota hai.

Concept Map

computes

stored in

scales as

causes

slower than

dominates over

solves

processes in

avoids

keeps data in

enables

yields

Standard Attention

Score Matrix N x N

HBM slow GPU RAM

Quadratic N^2 Memory

Memory Bandwidth Bottleneck

SRAM on-chip fast

Compute FLOPs

Flash Attention

Tiles Streaming

Longer Sequences

2-4x Faster Less Memory