Log inStart free
LESSON 6.07 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Mixed precision and loss scaling

Watch mixed precision round tiny FP16 gradients to zero, and loss scaling save them.


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

Go Pro · $8.25/mo
EXPLAINER · 6.07 · AMP BAY25 WEIGHTS IN FP16 · LOSS 4.0E−9 IN FP32
01
Stage 1 of 6 · Cast and forward
Autocast casts the FP32 weights to FP16 for the matmuls. The loss, 4.0e−9, stays in FP32 by rule; FP16 would round it to 0.
Explain it simply

Mixed precision means training with two sizes of number. Autocast copies the 32-bit weights, the gold dots, into 16-bit numbers for the big multiplications. The blue dots are the outputs. Autocast keeps the loss in 32-bit by rule. Here that matters: 16-bit would turn it into zero.

A log scale of number sizes with the FP16 range shaded. 25 weights and 16 outputs sit in the range. The loss, 4.0e−9, sits in FP32.

weights, updates, Soutputsloss, gradients, infrounded to 0number range
rounds to 0subnormalFP16 normal range0infsize of the value |v|, log scale (each tick is ×16)16 outputs ŷ (FP16)25 weights w (FP32 → FP16)loss L (FP32)2⁻³²2⁻²⁴2⁻¹⁶2⁻⁸2⁰2⁸2¹⁶castFP32 → FP16forwardmatmuls in FP16lossin FP32backwardFP16, times Sunscale÷ S in FP32Adam stepFP32 masterL = 4.0e−9max cast error 3.4e−4 of the weight · L = 4.01e−9 in FP32rounds to 0subnormalFP16 normal range0infsize of the value |v|, log scale (each tick is ×16)16 outputs ŷ (FP16)25 weights w (FP32 → FP16)loss L (FP32)2⁻³²2⁻²⁴2⁻¹⁶2⁻⁸2⁰2⁸2¹⁶castFP32 → FP16forwardmatmuls in FP16lossin FP32backwardFP16, times Sunscale÷ S in FP32Adam stepFP32 masterL = 4.0e−9max cast error 3.4e−4 of the weight · L = 4.01e−9 in FP32
Toy: a 4 → 4 → 1 net, 25 weights, outputs near 0.001, Adam
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
y^=cast16(W x),L=1N∑(y^−y)2 in FP32\sy{yhat}{out}{\hat y} = \mathrm{cast}_{16}(\sy{w}{param}{W}\,x), \quad \sy{L}{grad}{L} = \tfrac{1}{N}\textstyle\sum (\hat y - y)^2 \ \text{in FP32}
L=4.01×10−9L = 4.01\times 10^{-9}

Outputs come from 16-bit matmuls, and the loss averages in FP32.

Common mix-ups

MYTHA bigger loss scale is always safer.

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

Predict, then reveal
3 CHECKS · PRO
Q01With a loss scale of 2³², does the training step run?

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