Contents

Foundation Model Engineering

Manuscript updated (UTC) · Chapter source on GitHub · Cite this chapter (RIS)

On this page

6.6 Optimization Mechanics and Fair Experiments

A loss is a measurement; an optimizer turns its derivatives into a trajectory. Before tuning a trillion-parameter run, be able to explain one parameter update and verify that batching preserves the intended objective.

From Likelihood to an Update

For logits zkz_k, class probability is pk=exp⁡(zk)/∑jexp⁡(zj)p_k=\exp(z_k)/\sum_j\exp(z_j). The loss for correct class yy is −log⁡py-\log p_y, whose logit derivative is pk−1[k=y]p_k-\mathbf{1}[k=y]. The chain rule maps that derivative through the network to gt=∇θLtg_t=\nabla_\theta L_t, the gradient of the batch objective at update tt. Gradient descent uses θt=θt−1−ηtgt\theta_t=\theta_{t-1}-\eta_t g_t, with learning rate ηt\eta_t.

For the diagnostic quadratic L(x,y)=(x2+100y2)/2L(x,y)=(x^2+100y^2)/2, the gradient at (1,1)(1,1) is (1,100)(1,100). One SGD step with η=0.01\eta=0.01 gives (0.99,0)(0.99,0); with η=0.03\eta=0.03 it gives (0.97,−2)(0.97,-2). Along the high-curvature direction, convergence requires 0<η<2/1000<\eta<2/100. A small gradient in one coordinate does not make the same step size stable in another. This is a local teaching example, not a convergence guarantee for a neural network.

What AdamW Remembers

Adam keeps exponential averages of the gradient and its elementwise square. With m0=v0=0m_0=v_0=0, decay coefficients β1,β2\beta_1,\beta_2, and update index t≥1t\ge1 [1]:

mt=β1mt−1+(1−β1)gt,vt=β2vt−1+(1−β2)gt2m_t=\beta_1m_{t-1}+(1-\beta_1)g_t,\qquad v_t=\beta_2v_{t-1}+(1-\beta_2)g_t^2 m^t=mt1−β1t,v^t=vt1−β2t\widehat m_t=\frac{m_t}{1-\beta_1^t},\qquad \widehat v_t=\frac{v_t}{1-\beta_2^t}

AdamW applies decay coefficient λ\lambda separately from the adaptive update; ϵ>0\epsilon>0 stabilizes the denominator [2]:

θt=(1−ηtλ)θt−1−ηtm^tv^t+ϵ.\theta_t=(1-\eta_t\lambda)\theta_{t-1} -\eta_t\frac{\widehat m_t}{\sqrt{\widehat v_t}+\epsilon}.

At the first step, bias correction gives m^1=g1\widehat m_1=g_1 and v^1=g12\widehat v_1=g_1^2. Ignoring ϵ\epsilon and decay, each nonzero coordinate moves approximately −η1sign⁡(g1)-\eta_1\operatorname{sign}(g_1), not proportionally to its raw gradient magnitude. This is why “same learning rate” does not mean “same update” across optimizers.

Adding λ∥θ∥22/2\lambda\|\theta\|_2^2/2 to the loss instead puts λθ\lambda\theta into Adam’s moments and preconditioner. It is therefore different from AdamW. In plain SGD the two forms coincide under matching coefficients. Record which parameters receive decay; excluding biases and normalization parameters is a policy to test, not a mathematical necessity.

Token Weighting Must Survive Parallelism

Let rank rr have summed valid-token loss SrS_r, token count nrn_r, and let RR be the number of ranks. The desired objective is L=∑rSr/NL=\sum_r S_r/N with N=∑rnrN=\sum_r n_r. If the distributed reducer averages gradients, each rank must backpropagate RSr/NR S_r/N to recover that objective. A reducer that sums needs a different factor.

For two ranks, take S1=4,n1=2S_1=4,n_1=2 and S2=7,n2=7S_2=7,n_2=7. Averaging local means gives (2+1)/2=1.5(2+1)/2=1.5, while the token mean is 11/911/9. The gradient generally changes too. Across accumulated microbatches, use the valid count over the entire update window; do not average unequal microbatch means. Unscale mixed-precision gradients first, aggregate the complete update, then clip the global norm once. Clipping each microbatch is a different algorithm.

For fixed-length dense batches, effective input tokens per update are local batch × data-parallel ranks × accumulation steps × sequence length. Prompt masking and padding reduce the supervised-token count. Track both. Increasing batch size changes gradient noise and updates per token; linear learning-rate scaling is a hypothesis, not a universal law.

Design an Optimizer Experiment

Use the 7.5 lab as the executable contract: input manifest and integer tokenizer, masked next-token objective, FP32 CPU memory budget, AdamW settings, token-normalized metrics, abort checks, complete local checkpoint, held-out evaluation, and reproducibility gate. It uses no reference model. The schedule multiplies a ten-update warmup envelope by a cosine envelope indexed against the final planned budget. At fixed batch size, steps map to tokens; if batch size changes, preserve the intended token-indexed schedule explicitly.

Change one hypothesis at a time, but tune each competing optimizer on development data with an equal search budget. Match initialization, data stream, target-token budget, and evaluation points; also report wall time and optimizer overhead. Log gradient and parameter-update norms separately. Select the recipe on validation data, then evaluate the locked choice on untouched test clusters. Reusing the lab’s test set for repeated recipe selection turns it into development data.

Research snapshot, as of 2026-11-17. Li et al. report that the Muon–AdamW ordering after fine-tuning can reverse as pretraining data budget grows [3]. Arefin et al. report delayed effects of temporary Muon weight decay: an intervention can look worse when removed and better at the final budget [4]. These are bounded experimental findings, not a universal optimizer ranking. They motivate measuring the final downstream objective and full learning trajectory, rather than selecting from early pretraining loss alone.

Quizzes

Quiz 1: Why does restoring only weights fail to reproduce an AdamW run? The next update depends on first and second moments, step-dependent bias correction, learning-rate schedule, RNG state, and the next data batch. Equal weights do not imply equal next updates.

Quiz 2: With an averaging reducer, why is the local loss multiplied by world size? The reducer divides the sum of local gradients by R. Backpropagating R times the local loss sum divided by the global token count cancels that division and yields the global token-mean gradient.

Quiz 3: An optimizer reaches lower loss after 20% of training. What is still missing? The final-budget comparison, downstream adaptation and retention results, equal tuning budgets, run-to-run variation, and actual compute cost. Early ordering need not persist.

References

  1. Kingma & Ba. Adam: A Method for Stochastic Optimization. Paper.
  2. Loshchilov & Hutter. Decoupled Weight Decay Regularization. Paper.
  3. Li et al. (2026). Pretraining Data Scale Reverses the Muon-AdamW Fine-Tuning Gap. v2.
  4. Arefin et al. (2026). Delayed Effects of Temporary Weight Decay in Muon Pretraining. v1.