Linear algebra for LLMsPart 9: Numbers on real hardware

Tiled matmul

6 min

Matrices multiply in blocks

[A11A12A21A22][B11B12B21B22]\displaystyle \begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{bmatrix} \begin{bmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \end{bmatrix}
C11=A11B11+A12B21\displaystyle C_{11} = A_{11} B_{11} + A_{12} B_{21}

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

Cij=∑kAikBkjC_{ij} = \sum_k A_{ik} B_{kj}, with tiles in place of numbers.

Two 4 × 4 matrices

Cut into 2×22 \times 2 tiles. To get the top-left tile of CC, pair the first tile-row of AA with the first tile-column of BB.

C11C_{11}, one tile product at a time

A11B11=A_{11}B_{11} =A12B21=\quad A_{12}B_{21} =C11=C_{11} =

Each tile product is an ordinary 2×22 \times 2 matmul; the results add up. Compare with the top-left of the full product: ABAB starts with rows [2,3][2, 3] and [2,2][2, 2].

Write

C11=A11B11+A12B21C_{11} = A_{11}B_{11} + A_{12}B_{21}: accumulate tile products.

Why tiles: reuse in fast memory

naive: ≈2n3 reads\displaystyle \text{naive: } \approx 2n^3 \text{ reads}
b×b tiles: ≈2n3/b reads\displaystyle b \times b \text{ tiles: } \approx 2n^3 / b \text{ reads}

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 b×bb \times b tile once into the fast memory and every number in it gets used bb times before it’s evicted. Reads fall by a factor of bb, so arithmetic intensity rises by about bb.

Write

Tiling: each loaded number is used bb times ⇒ intensity ≈ bb.

✏️ Your turn

Count the reuse

n=4096,b=128\displaystyle n = 4096, \quad b = 128

By what factor does tiling cut the reads from slow memory? How many tile products make up one tile of CC?

Work it out on paper, then go to the next card to check.

Answer

reads fall by b=128\displaystyle \text{reads fall by } b = 128
n/b=4096/128=32 tile products per output tile\displaystyle n / b = 4096 / 128 = 32 \text{ tile products per output tile}

Each output tile accumulates 32 products of 128×128128 \times 128 tiles, summed in FP32 (the number formats deck).

✏️ Your turn

Do one by hand

Compute C11=A11B11+A12B21C_{11} = A_{11}B_{11} + A_{12}B_{21}.

Work it out on paper, then go to the next card to check.

Answer

[2111]+[1001]=[3112]\displaystyle \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 3 & 1 \\ 1 & 2 \end{bmatrix}

A12A_{12} and B21B_{21} both swap, and two swaps make the identity.

📌 Recap

Tiled matmul in one card

  • Block multiplication: Cij=∑kAikBkjC_{ij} = \sum_k A_{ik}B_{kj} on tiles.
  • A tile loaded once into fast memory is reused bb times: intensity ≈ bb.
  • Kernels accumulate tile products in FP32.
  • FlashAttention is the same idea plus the online softmax.

Write

The four lines above.