Log inStart free
LESSON 32.01 · 5 STAGES · 2 CONTROLS · 2 CHECKSPRO · PREVIEW

Graph optimizations

Watch two compiler passes remove 5 of 13 kernels from a traced model block.


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

Go Pro · $8.25/mo
EXPLAINER · 32.01 · COMPILER BAY13 KERNELS · 293.8 µs PER CALL
01
Stage 1 of 5 · Traced graph
A tracer records the MLP block as 13 nodes. Each node runs as one GPU kernel on every call: 293.8 µs.
Explain it simply

A tracer runs the model once and writes down every operation as a box, joined by arrows. Each box becomes one GPU kernel, a small program the GPU starts on every call. The bar at the bottom is one call, and each piece is one kernel. All 13 together take 293.8 microseconds.

A graph of 13 nodes from input x to out. A bar below shows one call: 13 kernels, 293.8 µs in total.

input x, activationsweights, constantskernels, timesremoved work
traced graphW1 [H, D]W2 [D, H]2, LAYERSγ, βb1b2x[16, 4096]lnlayernormmm1matmuladd1addactgelumm2matmuladd2addresmuloutaddw1ttransposew2ttransposetwo_lmulrootsqrtscalereciprocalone call on the toy GPU293.8 µsw1tmm1w2tmm2traced graphW1 [H, D]W2 [D, H]2, LAYERSγ, βb1b2x[16, 4096]lnlayernormmm1matmuladd1addactgelumm2matmuladd2addresmuloutaddw1ttransposew2ttransposetwo_lmulrootsqrtscalereciprocalone call on the toy GPU293.8 µsw1tmm1w2tmm2
Toy: one MLP block, 13 nodes, a rounded H100-like GPU
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
T=∑k(tlaunch+max⁡(FkP, BkBW))\sy{t}{out}{T} = \sum_k \Big(t_{\text{launch}} + \max\big(\tfrac{F_k}{P},\, \tfrac{B_k}{BW}\big)\Big)
T=13 kernels=293.8 μsT = 13\text{ kernels} = 293.8\,\mu s

A call takes the sum of its kernel times.

Common mix-ups

MYTHConstant folding makes the matmuls faster.

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

Predict, then reveal
2 CHECKS · PRO
Q01At 1024 rows, does the time saved per call grow, shrink or stay the same?

Make your guess, then test it in the diagram. All 2 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 2 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.