Log inStart free
LESSON 27.C3 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

One decoder layer as kernels

Watch one decode token cross one decoder layer, kernel by kernel.


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

Go Pro · $8.25/mo
EXPLAINER · 27.C3 · KERNEL BAY9 KERNELS · 2,032 B · 46.0 µs
01
Stage 1 of 6 · Norm and QKV
One RMSNorm kernel writes n₁. One GEMM on Tensor Cores makes q, k and v together.
Explain it simply

First the norm scales the token x by one number, r, so its size is near 1. Then Tensor Cores turn it into a query, a key and a value, with sums in FP32. One token fills only 1 of the 8 columns of each Tensor Core step. Here that takes 2 kernels.

The norm and the QKV projection run as 2 kernels. r is 1.5269. The MMA tile uses 1 of 8 columns.

kernel, activation, HBM tripweights, launchesextra HBM trip, overflowprobabilities, layer output, step timeunused MMA column, the 28.C1 kernel
HBMnormQKVRoPEKV writescoressoftmaxP·Voutaddnormgate-upSwiGLUdownaddinto GEMMs: 9 kernelsxHBM0.580.911.170.25−0.100.30−0.450.73x²reg0.340.831.370.060.010.090.200.53rreg1.53n₁HBM0.731.581.930.27−0.170.44−0.710.93qreg1.631.75−0.77−1.030.46−0.842.262.56kreg−1.740.52−1.04−2.310.631.242.580.73vreg0.31−0.800.300.981.421.070.100.10one MMA step on a Tensor Core×=W: FP16 weightsn₁: column 1q, k, v: FP32 sums,stored as FP167 unusedRMSNorm · 27.18QKV GEMM on Tensor Cores · 27.13HBMnormQKVRoPEKV writescoressoftmaxP·Voutaddnormgate-upSwiGLUdownaddinto GEMMs: 9 kernelsxHBM0.580.911.170.25−0.100.30−0.450.73x²reg0.340.831.370.060.010.090.200.53rreg1.53n₁HBM0.731.581.930.27−0.170.44−0.710.93qreg1.631.75−0.77−1.030.46−0.842.262.56kreg−1.740.52−1.04−2.310.631.242.580.73vreg0.31−0.800.300.981.421.070.100.10one MMA step on a Tensor Core×=W: FP16 weightsn₁: column 1q, k, v: FP32 sums,stored as FP167 unusedRMSNorm · 27.18QKV GEMM on Tensor Cores · 27.13
Toy: D 8, 2 heads, FFN 16, 8 positions, FP16 in HBM
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
n1=x r γ,q=Wq n1\sy{n}{data}{n_1} = \sy{x}{data}{x}\,\sy{r}{data}{r}\,\sy{g}{param}{\gamma},\quad \sy{q}{data}{q} = \sy{w}{param}{W_q}\,\sy{n}{data}{n_1}
r=1/x2‾+ϵ=1.5269,q0=1.63r = 1/\sqrt{\overline{x^2} + \epsilon} = 1.5269,\quad q_0 = 1.63

Scale x by r and the weights, then multiply by W.

Common mix-ups

MYTHFusing each piece on its own gives the fewest kernels.

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

Predict, then reveal
3 CHECKS · PRO
Q01How many kernels does the layer launch op by op?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 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.