Log inStart free
LESSON 28.03 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Tiled attention with SRAM reuse

Watch attention run tile by tile without writing scores to HBM.


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.03 · MEMORY BAYTILE 4 · SET 176 B / 256 B
01
Stage 1 of 6 · Split into tiles
FlashAttention splits Q, K and V into tiles of 4 tokens. One tile set needs 176 B of the 256 B SRAM.
Explain it simply

Attention needs every query row to meet every key row. The chip's fast memory holds only 256 bytes, so it can't hold all of that. The kernel cuts Q, K and V into tiles, small blocks of 4 rows. The gauge shows that one set of tiles fits on the chip.

HBM holds Q, K and V in 4 tiles each. SRAM shows empty slots for one tile set.

Q, K, VS, P, max m, sum ℓ (in the bars: traffic to remove); gradients dashedO, L (written to HBM) and byte countsSRAM, tile setempty, never written, or does not fit
SRAM · on chipHBMHBM bytes4 tiles of 4 tokenstile set 176 B ≤ 256 BQKVSmℓOQ16×4K16×4V16×4O16×4L16S, Pnever writtennaive, 3 kernels2,560 BFlashAttention-20 Bfloor512 Bforward passtile set = 2 B × (4·4·4 + 4² + 2·4) = 176 B ≤ 256 BSRAM · on chipHBMHBM bytes4 tiles of 4 tokenstile set 176 B ≤ 256 BQKVSmℓOQ16×4K16×4V16×4O16×4L16S, Pnever writtennaive, 3 kernels2,560 BFlashAttention-20 Bfloor512 Bforward passtile set = 2 B × (4·4·4 + 4² + 2·4) = 176 B ≤ 256 B
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
Mset=2 (4Bd+B2+2B)\sy{sram}{param}{M_{\text{set}}} = 2\,(4\sy{B}{param}{B}d + \sy{B}{param}{B}^2 + 2\sy{B}{param}{B})
Mset=2 (4⋅4⋅4+42+2⋅4)=176 B≤256 BM_{\text{set}} = 2\,(4\cdot 4\cdot 4 + 4^2 + 2\cdot 4) = 176\,\text{B} \le 256\,\text{B}

One tile set holds Q, K, V and O tiles, scores and row statistics.

Common mix-ups

MYTHTiled attention reads every input from HBM once.

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

Predict, then reveal
3 CHECKS · PRO
Q01With the FA-2 loop order and tiles of 2 tokens, do K and V come up more or fewer times?

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.