← All 96 breakdowns
FLASH

FLASH: the breakdown

From Adam Rosler. The thing you commented FLASH for: how FlashAttention gets the same answer with far less memory.

The problem

Attention scores every token against every other token: an N x N grid per head (8,000 tokens: 64 million scores). The plain way writes that grid to the GPU's big memory (HBM), reads it back for the softmax, writes it again, reads it again to multiply by V. Memory traffic, not maths, is the bottleneck, and the grid grows with N^2.

The trick (Dao, Fu, Ermon, Rudra & Ré, NeurIPS 2022)

  1. Tile it: load a block of queries and a block of keys/values into the chip's small, fast on-chip memory (SRAM: on an A100, 19 TB/s and 20 MB, against HBM's 1.5 TB/s and 40 GB).
  2. Compute there: the scores for that block, and the softmax as you go, keeping a running maximum and sum.
  3. Accumulate: add the block's contribution to the output, rescaling with the running totals.
  4. Never store the grid: memory grows with N, not N^2; for training, the backward pass recomputes blocks instead of reading a stored grid.

Our run (one attention layer, 16 heads, fp16, NVIDIA GeForce RTX 5070 Ti 15.5 GB)

RTX 5070 Ti, 16 GB
tokens  plain          flash
 4,096  5.2 ms 1.0 GB  0.8 ms 16 MB
 8,192  19.4 ms 4.0 GB 3.0 ms 33 MB
16,384  out of memory  12.1 ms 65 MB
65,536  out of memory  185.9 ms 260 MB

What to do with it

  • If a long context runs out of memory, check that your stack uses a fused attention kernel (FlashAttention or PyTorch's scaled_dot_product_attention with a flash backend) before buying a bigger GPU.

Source: Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra & Christopher Ré (Stanford; University at Buffalo), "FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness", NeurIPS 2022, arXiv:2205.14135.

FREE ONE-TIME UNLOCK

Unlock the full FLASH breakdown

Drop your email once to read the complete step-by-step guide, grab the files, and unlock all 96 Reel breakdowns on this device forever.

✓ Instant reveal right here✓ No spam, unsubscribe any time