Log inStart free
LESSON 28.04 · 5 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Causal and variable-length FlashAttention

Watch FlashAttention skip masked tiles and pack sequences of different lengths.


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

Go Pro · $8.25/mo
EXPLAINER · 28.04 · KERNEL BAY16 SLOTS · TILES OF 4 · 16 TILES
01
Stage 1 of 5 · Tiles of scores
FlashAttention splits the 16 × 16 score grid into tiles of 4. Each step loads one Q tile and one K, V tile.
Explain it simply

Each row of the grid is a query token, and each column is a key token. One cell is one score. FlashAttention never stores the whole grid. It loads one small square of it, a tile, into the fast memory on the chip.

The score grid: 16 query rows and 16 key columns, in 16 tiles of 4 by 4.

tokens, one tint per sequencetiles, SRAMscores (filled: used, outline: masked, wasted)masked or skippedO writes
keysqueries1 sequence, 16 tokens1122334455667788991010111112121313141415151616QKVOSRAM 256 Ba step uses 176 BS tile 1,10.190.47−0.260.240.13−0.11−0.22−0.33−0.74−1.05−0.32−0.20−0.110.01−0.340.18score grid in tileskeysqueries1 sequence, 16 tokens1122334455667788991010111112121313141415151616QKVOSRAM 256 Ba step uses 176 BS tile 1,10.190.47−0.260.240.13−0.11−0.22−0.33−0.74−1.05−0.32−0.20−0.110.01−0.340.18score grid in tiles
Toy: 1 head, 16 tokens × 4 values in FP16, seeded random, not trained; batches of 8, 5 and 3 tokens
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
ntiles=(N/T)2\sy{nt}{param}{n_{\text{tiles}}} = \left(\sy{N}{data}{N} / \sy{T}{param}{T}\right)^2
ntiles=(16/4)2=16n_{\text{tiles}} = (16/4)^2 = 16

The grid of N by N scores holds this many tiles.

Common mix-ups

MYTHA causal kernel computes every score, then masks half of them.

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

Predict, then reveal
3 CHECKS · PRO
Q01With one sequence, tiles of 4 and no mask, how many of the 16 tiles does the kernel skip?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 3 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.