4.1.14Transformer Architecture

Flash attention and efficient attention

2,943 words13 min readdifficulty · medium1 backlinks

The Standard Attention Problem

The computational flow:

  1. Compute S=QKTRN×NS = QK^T \in \mathbb{R}^{N \times N} — the attention scores matrix
  2. Apply P=softmax(S/dk)P = \text{softmax}(S / \sqrt{d_k}) — the attention probability matrix
  3. Compute output O=PVO = PV

Memory Hierarchy: Why Speed Isn't About FLOPs

Modern GPUs have a memory hierarchy:

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

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=4M2048^2 = 4M floats = 16 MB (just for S)
  • Memory transfer time: 16 MB/1.5 TB/s10 μs16\text{ MB} / 1.5\text{ TB/s} \approx 10\ \mu 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 N2N^2 matrices, memory movement (not FLOPs) becomes the true bottleneck.

Flash Attention: Tiled Computation

Derivation: Incremental Softmax

Standard softmax requires the full row: softmax(xi)=exijexj\text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}

Problem: We need jexj\sum_j e^{x_j} before computing any output!

Solution: Process in chunks and update statistics incrementally.

Step 1: Safe softmax (numerical stability)

Raw exe^{x} overflows. Standard trick: softmax(xi)=eximjexjm\text{softmax}(x_i) = \frac{e^{x_i - m}}{\sum_j e^{x_j - m}} where m=max(x)m = \max(x).

Step 2: Split sum across blocks

Suppose we process attention scores in two blocks: x(1)x^{(1)} and x(2)x^{(2)}.

Block 1: m(1)=max(x(1)),(1)=jblock 1exjm(1)m^{(1)} = \max(x^{(1)}), \quad \ell^{(1)} = \sum_{j \in \text{block 1}} e^{x_j - m^{(1)}}

Block 2: m(2)=max(x(2)),(2)=jblock 2exjm(2)m^{(2)} = \max(x^{(2)}), \quad \ell^{(2)} = \sum_{j \in \text{block 2}} e^{x_j - m^{(2)}}

Step 3: Merge blocks

Global max: mnew=max(m(1),m(2))m^{\text{new}} = \max(m^{(1)}, m^{(2)})

Why rescale? Our sums used different "reference points" (m(1)m^{(1)} vs m(2)m^{(2)}). We need a common baseline.

Corrected sum: new=em(1)mnew(1)+em(2)mnew(2)\ell^{\text{new}} = e^{m^{(1)} - m^{\text{new}}}\, \ell^{(1)} + e^{m^{(2)} - m^{\text{new}}}\, \ell^{(2)}

Why this works: new=jexjmnew\ell^{\text{new}} = \sum_{j} e^{x_j - m^{\text{new}}} =jblock 1exjmnew+jblock 2exjmnew= \sum_{j \in \text{block 1}} e^{x_j - m^{\text{new}}} + \sum_{j \in \text{block 2}} e^{x_j - m^{\text{new}}} =jblock 1e(xjm(1))+(m(1)mnew)+jblock 2e(xjm(2))+(m(2)mnew)= \sum_{j \in \text{block 1}} e^{(x_j - m^{(1)}) + (m^{(1)} - m^{\text{new}})} + \sum_{j \in \text{block 2}} e^{(x_j - m^{(2)}) + (m^{(2)} - m^{\text{new}})} =em(1)mnew(1)+em(2)mnew(2)= e^{m^{(1)} - m^{\text{new}}}\, \ell^{(1)} + e^{m^{(2)} - m^{\text{new}}}\, \ell^{(2)}

Similarly, the weighted output accumulates. If O~(b)=jblock bexjm(b)vj\tilde{O}^{(b)} = \sum_{j \in \text{block } b} e^{x_j - m^{(b)}}\, v_j is the unnormalized block output, then: Onew=em(1)mnewO~(1)+em(2)mnewO~(2)newO^{\text{new}} = \frac{e^{m^{(1)} - m^{\text{new}}}\, \tilde{O}^{(1)} + e^{m^{(2)} - m^{\text{new}}}\, \tilde{O}^{(2)}}{\ell^{\text{new}}}

This generalizes to any number of blocks!

Figure — Flash attention and efficient attention

Backward Pass: Recomputation

Why this works:

  • Forward pass: 1 read of Q,K,V + writes to HBM
  • Standard backward: Read stored S,P matrices (N2N^2 elements)
  • Flash backward: Recompute S,P in SRAM (no HBM read), only load Q,K,V

Memory bandwidth: Flash reads 3Nd3Nd vs standard reads Nd+2N2Nd + 2N^2. For long sequences, Flash wins massively.

Other Efficient Attention Variants

Practical Impact

Recall Explain to a 12-Year-Old

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.

Connections

  • 4.1.1-Self-attention-mechanism — The core operation Flash optimizes
  • 4.1.3-Multi-head-attention — Flash Attention works per-head
  • 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)O(N^2) for storing attention matrix. Flash: O(N)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)O(N^2) to O(N)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 (mm) and sum (\ell), then merging blocks by rescaling: new=em(1)mnew(1)+em(2)mnew(2)\ell^{\text{new}} = e^{m^{(1)} - m^{\text{new}}} \ell^{(1)} + e^{m^{(2)} - m^{\text{new}}} \ell^{(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 ww?
O(Nw)O(Nw). For constant ww, this is linear in sequence length NN, compared to O(N2)O(N^2) for full attention.
How does linear attention achieve O(N)O(N) complexity?
By using kernel trick: ϕ(Q)(ϕ(K)TV)\phi(Q)(\phi(K)^T V) instead of (QKT)V(QK^T)V. This changes order of operations from O(N2d)O(N^2 d) to O(Nd2)O(Nd^2)—linear in NN if dd constant.
What is the main trade-off in Flash Attention's backward pass?
Recompute attention matrices (SS, PP) instead of storing them. Adds ~20% compute but saves O(N2)O(N^2) 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)O(N^2), Flash's as O(N)O(N). The absolute gap widens for longer sequences.

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

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

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.

Go deeper — visual, from zero

Test yourself — Transformer Architecture

Connections