Macalinao StudioLearnAll enriched lessons ↗
College path · 7 sectionsSwitch to high school ↗

College / LONGER LESSON 15

Training numerics: updates that mean what you intend

Trace optimizer state, precision and accumulation before scaling a training run.

Preparation: Derivatives, averages and powers of ten.

Plan 20–35 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Begin with one slope

College path. Trace optimizer state, precision and accumulation before scaling a training run.

Preparation: Derivatives, averages and powers of ten. Work through one section at a time. Predict the experiment result before changing its controls, then explain what the example leaves out.

Gradient descent subtracts a learning-rate-scaled gradient. On a simple quadratic, too large a step can overshoot or diverge. A loss curve should be interpreted with the actual update rule. For L=x squared divided by two, the gradient is x and the update multiplies x by one minus the learning rate. Convergence requires its magnitude below one.

Worked step 1. Start x=2 and learning rate 0.5.

Worked step 2. Gradient is 2; new x is 1.

Worked step 3. Loss falls from 2 to 0.5.

Try it yourself. What does learning rate 2 do here?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. It changes x=2 to -2 and preserves the loss. Larger values diverge in this exact quadratic fixture.

Section 02 / 07

Correct the early moving average

Adam tracks moving averages of gradients and squared gradients. Zero initialization biases the early estimates toward zero; bias correction adjusts for the missing history. At step t, corrected first moment is m divided by one minus beta1 to power t; the second moment has its own beta2 correction. Epsilon stabilizes the denominator.

m̂ₜ = mₜ/(1−β₁ᵗ)

Worked step 1. First gradient is 2, beta1 is 0.9.

Worked step 2. First moment m is 0.2.

Worked step 3. Corrected m is 0.2 divided by 0.1, which is 2.

Try it yourself. Is the uncorrected first moment already the gradient average?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. It includes initialization bias. The correction recovers 2 for this first constant-gradient observation.

Section 03 / 07

Separate decay from adaptive scaling

AdamW applies decoupled weight decay rather than adding a penalty gradient and letting the adaptive optimizer rescale it. These procedures are generally different under adaptive preconditioning. A schematic AdamW update combines an adaptive gradient step with a learning-rate-times-decay shrinkage term. Implementation order and parameter exclusions should follow the actual optimizer configuration.

Worked step 1. With zero gradient, weight 2, rate 0.1 and decay 0.01 gives shrinkage 0.002.

Worked step 2. The illustrative updated weight is 1.998.

Try it yourself. Does weight decay always act on every bias and normalization parameter?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. Parameter groups often exclude some parameters. Inspect the configured groups instead of assuming.

Section 04 / 07

Accumulate the intended average

Gradient accumulation combines microbatches before an optimizer step. Scaling losses incorrectly changes the effective update, particularly when microbatches have different numbers of valid tokens. To match a global token mean, accumulate summed losses with the global valid-token denominator. Dropout and batch-dependent operations can prevent exact equivalence to a single large batch.

Worked step 1. Microbatch sums are 6 over 3 tokens and 2 over 1 token.

Worked step 2. Combined mean is 8 divided by 4, or 2.

Worked step 3. Use one optimizer step after accumulation.

Try it yourself. Should the optimizer step after every microbatch?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Not when intending one accumulated update. That would update parameters and optimizer state more frequently.

Section 05 / 07

Precision trades range for resolution

FP16 and BF16 are both commonly stored in sixteen bits, but distribute bits differently. BF16 has a wider exponent range; FP16 has more fraction bits. Neither makes all arithmetic safe. Mixed precision often retains selected operations or master state at higher precision. Loss scaling is commonly needed for FP16; BF16 usually needs less scaling, but overflow and numerical issues still require monitoring.

Worked step 1. Compare a very small gradient with the format’s representable range.

Worked step 2. Check finite losses and gradients.

Worked step 3. Inspect accumulation precision separately.

Try it yourself. Does sixteen-bit storage imply identical numerical behavior?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. Range, precision, kernels and accumulation rules differ between formats.

Section 06 / 07

Measure memory by component

Weights, gradients, optimizer state, activations and temporary buffers all use memory. Sharding one component does not eliminate every other component or communication cost. ZeRO stages partition different training states; activation checkpointing trades recomputation for activation memory. Measure peak memory and throughput under the actual batch and sequence length.

Worked step 1. List memory by category.

Worked step 2. Change one optimization.

Worked step 3. Record peak memory and examples per second after warm-up.

Try it yourself. Can parameter bytes alone predict peak training memory?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. The other states and temporary allocations can dominate or create the peak.

Section 07 / 07

Mastery and transfer

Bring the pieces together. First answer the checkpoint without the worked solutions. Then explain the mechanism in your own words, using one numerical example from this lesson.

Adam bias correction accounts for… Option 1: Zero-initialized moment estimates. Option 2: Missing labels in the dataset. Option 3: All floating-point errors.

Pause here and choose an answer before continuing. The checkpoint explanation follows.

Reveal worked solution

The correct choice is option 1: Zero-initialized moment estimates. Moment initialization is the specific issue; precision and data problems need separate handling.

Transfer task. Change one assumption in the worked example. State your prediction, calculate or test it in Workbench, then explain whether the result supports your prediction.

Reveal worked solution

Self-check rubric. Name the changed assumption, keep the other inputs fixed, show at least one intermediate calculation, and distinguish what the toy example guarantees from what would require a trained model or a real measurement.

LONGER ADAPTATION LESSONS

Learn the update and the teaching signal

Worked examples, local narration and connected experiments at your level.

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this college path?

This removes only this path’s notes, progress and experiments from this browser.