Scale the backward path before tiny gradients disappear
Explain when loss scaling preserves gradient range, why unscaling order matters, and how overflow checks change whether an optimizer step occurs.
On this page
The question for today
What you’ll understand
- Derive the scaled-gradient identity for a finite constant loss scale.
- Distinguish preventing backward underflow from rescaling an already rounded zero.
- Order unscaling, clipping, finite checks, and optimizer stepping under the framework's actual contract.
A small gradient can matter to learning and still fall below an intermediate format's representable range. Loss scaling multiplies the loss before backpropagation so intermediate gradients can occupy a more useful magnitude range, then removes the factor before the mathematical update. The timing is essential: multiplying a gradient that already rounded to zero cannot resurrect it. Scaling also increases large magnitudes, so overflow detection and update policy are parts of the method rather than optional cleanup.
Use the chain rule with a stated constant scale
For a constant positive finite scale S independent of the parameters, differentiating S times the loss multiplies the exact gradient by S. Dividing the resulting gradient by the same factor recovers the intended gradient in exact arithmetic. In mixed precision, that transformation can prevent some intermediate backward values from becoming zero. It does not widen the exponent range of every forward operation, fix an already invalid loss, or recover information discarded before the scaled path reaches an operation. Check where underflow actually occurs before attributing a training problem to one small final gradient.
For IEEE binary16 with gradual underflow, the smallest positive normal is 2⁻¹⁴ and the smallest positive subnormal is 2⁻²⁴. Under nearest rounding, some still smaller positive values round to zero. Flush-to-zero behavior can remove a larger region. These details depend on the actual instruction and backend, not just the tensor's storage dtype. Wider gradient accumulation and optimizer state can complement scaling, but output casting, reduction precision, and individual operation support still need inspection. Another low-precision format can have different range and precision tradeoffs.
Unscale before operations defined on the true gradient
Gradient clipping thresholds are usually defined for the unscaled gradient norm. Clipping scaled gradients at the original threshold changes the effective rule, so unscale at the documented point before inspecting or clipping them. During gradient accumulation, keep the scale and normalization consistent across the effective batch; combining gradients with incompatible scales and dividing once produces a different quantity. The framework may perform unscaling and nonfinite checks within a helper, so understand that path instead of adding a second unscale operation blindly.
A large scale can overflow one or more gradient components. Dynamic scaling detects nonfinite gradients, avoids applying the invalid optimizer step according to its contract, and adjusts scale for later work. This changes the actual step history, which can interact with scheduler or accumulated-state logic. Do not force a step merely to keep an iteration counter advancing. Record skipped updates, scale trajectory, and finite-value checks when diagnosing convergence. A finite scale value alone is not evidence that every forward intermediate, loss, or optimizer state remains valid.
Probe range and updates separately
Use controlled batches with known tiny and large components, compare gradients before and after unscale, and verify whether parameters actually changed. Separate forward instability from backward range loss and from optimizer-state quantization. Check the documented AMP operation policy for the deployed framework and hardware; autocasting and gradient scaling perform different jobs. Compare quality and throughput under the same effective batch and update policy. A faster loop that silently skips most meaningful updates is not an equivalent training run, even if its loss values remain finite for a while.
The quadratic lab illustrates the exact scaling identity only. Reset, set both coefficient a=1 and position x=0.5, predict tangent slope one, then Play or Step. Reset and re-enter both a=2 and x=0.5, predict slope two. The fixed function is y=ax² with derivative 2ax, so changing a scales that mathematical gradient. It does not emulate tiny gradients, binary16, dynamic scales, clipping, or skipped optimizer steps. Controls retain elapsed time on edits, so each comparison uses Reset and re-enters both values.
Work it through
Preserve a tiny component before converting back
Assume a scaled backward path can evaluate exact gradient components g_small=2⁻²⁶ and g_large=2 before binary16 rounding, with gradual underflow and nearest rounding. Use scale S=2¹⁴, then convert scaled gradients to binary32 before unscaling.
Without this preventive scaling, 2⁻²⁶ is below half the minimum positive binary16 subnormal and rounds to zero. The large component two is representable.
The scaled components are 2⁻¹² and 2¹⁵=32,768, both representable and finite in binary16. This assumes the scaled computation preserves those values before its rounding points.
After conversion to binary32, divide by 2¹⁴ to recover 2⁻²⁶ and two. Dividing back in binary16 would lose the tiny component again, and scaling an already stored zero would never recover it.
Scaling can prevent backward range loss under the stated path assumptions, while a suitable unscale destination and overflow policy remain necessary.
The living notebook
Explore the model
Interactive lab · function
Scaling a loss scales its mathematical derivative
Reset, set both a=1 and x=0.5, predict slope one, then Play or Step. Reset and re-enter a=2 and x=0.5 for slope two. The fixed y=ax² model shows derivative 2ax; it has no dtype, underflow, GradScaler, clipping, or optimizer-step mechanism. Each comparison resets because edits retain elapsed time.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forThe chain-rule identity explains the intended scale, while actual range preservation depends on when and where rounding occurs.
Pause & reason
Check your understanding
Your turn
Try it yourself
With binary16 maximum finite value 65,504, a mathematical gradient component four is scaled by S=2¹⁴. Check the range and explain the correct response under a dynamic-scaling workflow when the resulting gradients are nonfinite.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compute the scaled value and distinguish a numerically valid optimizer update from merely advancing an iteration counter.
Compare with the solution
The scaled component is 4 × 16,384 = 65,536, above the maximum finite value. Under the relevant rounding path it can overflow. The dynamic-scaling contract should detect nonfinite gradients, avoid applying the invalid step, and adjust scale rather than force an update. Other components and earlier intermediates still need checks; this one component already violates the illustrative range condition.
Keep the idea
Take it with you
- Loss scaling applies a constant factor before backward rounding can erase tiny values.
- Unscale at the proper precision and before operations defined on the true gradient.
- Overflow checks, skipped steps, and consistent accumulation belong to the training semantics.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.