Linear algebra for LLMsPart 9: Numbers on real hardware
Tiled matmul
6 min
Matrices multiply in blocks
Cut each matrix into tiles and multiply as if the tiles were numbers: row of tiles times column of tiles. The only rule is the one from Part 2, applied to tiles: inner sizes must match, and order matters.
Write
Two 4 × 4 matrices
Cut into tiles. To get the top-left tile of , pair the first tile-row of with the first tile-column of .
, one tile product at a time
Each tile product is an ordinary matmul; the results add up. Compare with the top-left of the full product: starts with rows and .
Write
Why tiles: reuse in fast memory
Naively, each output entry reads a whole row and a whole column. A GPU has 80 GB of slow HBM and only tens of megabytes of fast on-chip memory. Load a tile once into the fast memory and every number in it gets used times before it’s evicted. Reads fall by a factor of , so arithmetic intensity rises by about .
Write
✏️ Your turn
Count the reuse
By what factor does tiling cut the reads from slow memory? How many tile products make up one tile of ?
Work it out on paper, then go to the next card to check.
Answer
Each output tile accumulates 32 products of tiles, summed in FP32 (the number formats deck).
✏️ Your turn
Do one by hand
Compute .
Work it out on paper, then go to the next card to check.
Answer
and both swap, and two swaps make the identity.
📌 Recap
Tiled matmul in one card
- Block multiplication: on tiles.
- A tile loaded once into fast memory is reused times: intensity ≈ .
- Kernels accumulate tile products in FP32.
- FlashAttention is the same idea plus the online softmax.
Write