Log inStart free
LESSON 27.10 · 5 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Tiled matrix multiplication

Watch tiles of A and B load once and serve many outputs.


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

Go Pro · $8.25/mo
EXPLAINER · 27.10 · GPU BAYNAIVE · 1,056,768 B FROM HBM
01
Stage 1 of 5 · The naive kernel
The naive GPU kernel gives each output its own thread. It reads 64 values of A and 64 of B from HBM.
Explain it simply

Each output in C is one row of A times one column of B. In the naive kernel, one thread makes one output alone. It fetches all 128 of its numbers from HBM, the big and slow memory. So each number in A is fetched 64 times.

Matrices A, B and C, 64 by 64. Output C 20, 41 uses row 20 of A and column 41 of B. The naive kernel moves 1,056,768 bytes from HBM.

weights A, tile edgesinputs B, bytes movedoutputs C, math time
naive kernelA · weightsB · inputsC · outputsC = A · Beach 64 × 64, FP16one thread computes C[20][41]reads row 20 of A: 64 values from HBMreads column 41 of B: 64 values from HBMC[20][41] = 0.31254,096 threads, one output eacheach A value leaves HBM 64 timesbytes movedHBM1,056,768 Bnaive kernelA · weightsB · inputsC · outputsC = A · Beach 64 × 64, FP16one thread computes C[20][41]reads row 20 of A: 64 values from HBMreads column 41 of B: 64 values from HBMC[20][41] = 0.31254,096 threads, one output eacheach A value leaves HBM 64 timesbytes movedHBM1,056,768 B
Toy: the 64 × 64 matmul of 27.C2 on a toy GPU (HBM 64 GB/s, shared memory 512 GB/s)
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Cij=∑kAik Bkj\sy{c}{out}{C_{ij}} = \sum_{k} \sy{a}{param}{A_{ik}}\,\sy{b}{data}{B_{kj}}
C20,41=(−0.5)(0.75)+(−1)(−0.25)+⋯=0.3125C_{20,41} = (-0.5)(0.75) + (-1)(-0.25) + \dots = 0.3125

Each output adds 64 products of a row of A and a column of B.

Common mix-ups

MYTHTiling cuts the math the kernel does.

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

Predict, then reveal
3 CHECKS · PRO
Q01From 16 × 16 to 32 × 32 tiles: how many times does each A value leave HBM?

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 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.