Log inStart free
LESSON 28.C1 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

FlashAttention end to end

Follow 16 tokens through naive attention and FlashAttention, counting every HBM byte.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 28.C1 · KERNEL BAYNAIVE 2,560 B · S, P 2,048 B
01
Stage 1 of 6 · Naive kernels
Naive attention runs 3 kernels. S and P go to HBM and back: 2,048 of the 2,560 B.
Explain it simply

Each kernel is a separate program on the chip. When one ends, its fast memory is cleared. So the score grid S goes out to the big, slow memory, and the next kernel reads it back. The probabilities P make the same round trip, and that's most of the traffic.

Three kernel boxes sit in SRAM. S and P sit in HBM. The naive bar fills to 2,560 B; S and P are 2,048 B of it.

Q, K, VS, P, max m, sum ℓ (traffic to remove)O, written to HBMSRAM, current tileskipped, masked or never written; a dashed bar: does not fit, or no skip
SRAM 256 BHBMS and P in HBM: 512 B eachkernel 1S = Q·Kᵀ / √dwrites S to HBMkernel 2P = softmax(S)reads S, writes Pkernel 3O = P·Vreads P and VQ · 16×4K · 16×4V · 16×4O · 16×4K1K2K3K4Q1Q2Q3Q4naive, 3 kernels2,560 BFlashAttentionfloor: each matrix once512 Bfrom lesson 28.01naive attention: 3 kernelsSRAM 256 BHBMS and P in HBM: 512 B eachkernel 1S = Q·Kᵀ / √dwrites S to HBMkernel 2P = softmax(S)reads S, writes Pkernel 3O = P·Vreads P and VQ · 16×4K · 16×4V · 16×4O · 16×4K1K2K3K4Q1Q2Q3Q4naive, 3 kernels2,560 BFlashAttentionfloor: each matrix once512 Bfrom lesson 28.01naive attention: 3 kernels
Toy: 1 head, 16 tokens × 4 values in FP16, seeded random, not trained; SRAM 256 B
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Bnaive=2 (4Nd+4N2)\sy{b}{out}{B_{\text{naive}}} = 2\,(4Nd + 4N^2)
2 (4⋅16⋅4+4⋅162)=2,560 B2\,(4\cdot16\cdot4 + 4\cdot16^2) = 2{,}560\,\text{B}

Q, K, V and O give 4Nd values; S and P give 4N² values.

Common mix-ups

MYTHA causal mask saves the same bytes in every kernel.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With tiles of 2 and no mask, does FlashAttention move more or fewer bytes than tiles of 4?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.