cudatraining · lab notes

LESSON 06 · 2026.04.18 · T4 · CAPSTONE

Flash Attention — five lessons converging into 80 lines

Tiled matmul + online softmax + HBM-traffic reduction. Everything lessons 1–5 built, compressed into a single kernel. 4.79× faster and 65× less memory traffic at N=4096.

GPU · T4 d · 64, FP32 sweep · 8 runs

Two implementations

Results

Nnaive msflash msspeedupnaive HBMflash HBMHBM ratio
5120.4020.5930.68×4.5 MB0.5 MB
10241.0880.8811.24×17.01.017×
20483.0761.2352.49×66.02.033×
409611.8572.4774.79×260.04.065×

GFLOP/s (by compute count): naive 169 → 366, flash 115 → 1754 (22% of T4 peak). Accuracy: both max_abs_err < 5e-7.

Lesson 1 · HBM reduction scales N² → N·d

At N=4096, d=64:

Ratio 65×, widening quadratically in N. This is the whole of FA. The rest is engineering that converts this reduction into time.

Lesson 2 · Crossover — naive is faster at N=512

At N=512, flash is actually slower (0.68×). Reasons:

"FA is always faster" is false. It wins once sequence length is long. That's why FA is decisive for real LLM prefill (N=4096–32k).

Lesson 3 · HBM reduction vs wall-clock speed

At N=4096, HBM drops 65× but wall-clock only 4.79×. Why?

FA's big wins appear on high-bandwidth + high-peak silicon like H100. On T4 the savings are real but not dramatic.

Lesson 4 · Five lessons converge in one kernel

Lesson 01 vector_add  → coalesced load pattern (FA's Q/K/V loads)
Lesson 02 memory      → HBM↔L2 traffic awareness (the reason FA exists)
Lesson 03 reduction   → warp reduce (row max/sum)
Lesson 04 matmul      → tiled matmul (S = Q@K^T, O += P@V)
Lesson 05 softmax     → online (max, sum) update (heart)
Lesson 06 flash       → fusion of the five above

All of that sits inside one 80-line kernel. That's why the FA paper is often described as "a sharp combination of not-particularly-novel techniques."

LLM-serving translation

Our single-kernel FA explains the dynamics on the prefill side. This closes "CUDA Level 1." Next lesson onward we attach this to PyTorch.