Skip to content

16.3 Adam and AdamW: bias correction and decoupled weight decay

The training loop you build in Training loop and the autograd bigram from AI Engineering needs an optimizer that trains every model in that book without retuning: a bigram table, a 64-unit MLP on digits, an LSTM, a Transformer, and the 10M-parameter Llama-style model of Capstone: TinyStories, owned end to end. Plain SGD with momentum, from The Optimizer protocol: SGD, momentum, Nesterov, weight decay, needs a learning rate tuned per model. Worse, inside one model it is too slow for layers with small gradients and unstable for layers with large ones: an embedding row that sees a rare token gets a gradient a thousand times smaller than a layer norm gain.

Concepts from other sections (3)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Adam fixes this by rescaling each coordinate by its own gradient history, and AdamW is the version every modern language model trains with. The capstone’s training spec defaults to adamw with betas (0.9, 0.95), and the checkpoint format already reserves exp_avg and exp_avg_sq for its state, so that a platform workflow such as Platform workflows: TrainRun and EvalSuite in Systems Engineering can kill a training worker and resume it. This section builds that optimizer and checks that it equals torch.optim.AdamW step for step.

The section builds on the Optimizer protocol of the previous section and on the exponential moving average and its bias correction from Series convergence, the EMA as a geometric series, and bias correction. The Adam derivation problems in Optimization problem set, part a check the algebra by hand. The reference solution is course/solutions/python/tinyllm/optim/adamw.py, which provides Adam and AdamW with step, zero_grad, state_dict, and load_state_dict; the contract is course/contracts/py/tinyllm/optim/adamw.pyi, and the checkpoint keys are fixed by formats/checkpoint.md.

Learning objectives

  • Write Adam’s two moving averages and its normalized update, and explain why the update is invariant to the gradient’s scale.
  • Derive the bias correction from the geometric sum and show that the first step is exactly η\eta per coordinate.
  • Explain why weight decay as an L2 gradient fails under Adam and how AdamW’s decoupled decay fixes it.
  • Implement Adam and AdamW that match torch’s arithmetic, including where ϵ\epsilon goes, and resume bit for bit.

The symbols below follow torch’s names where they appear in code. All operations on vectors are elementwise: g2g^2 squares each coordinate, v\sqrt{v} takes each square root, and m/vm / \sqrt{v} divides coordinate by coordinate.

SymbolMeaningType / shape
wwone parameter tensor (the optimizer treats each coordinate the same way)float32[...], p.data
gtg_tthe gradient of the loss with respect to ww at step ttsame shape as ww, p.grad
ttthe step counter: 1 for the first updateint
η\etathe learning rate, read on every stepfloat, opt.lr
β1,β2\beta_1, \beta_2decay rates of the two moving averages, in [0,1)[0, 1)float, opt.betas
mtm_tfirst moment: moving average of ggsame shape as ww, exp_avg
vtv_tsecond moment: moving average of g2g^2 (elementwise)same shape as ww, exp_avg_sq
m^t,v^t\hat m_t, \hat v_tbias-corrected moments, mt/(1−β1t)m_t / (1 - \beta_1^t) and vt/(1−β2t)v_t / (1 - \beta_2^t)same shape as ww
ϵ\epsilona small constant that keeps the denominator away from 0float, default 10−810^{-8}
λ\lambdathe weight decay coefficientfloat, opt.weight_decay

Gradient descent moves ww against the gradient: w←w−η gtw \leftarrow w - \eta\, g_t. A minibatch gradient is noisy, and its direction changes from step to step. Momentum smooths it with an exponential moving average (EMA):

mt=β1 mt−1+(1−β1) gt,m0=0.m_t = \beta_1\, m_{t-1} + (1 - \beta_1)\, g_t, \qquad m_0 = 0 .

Unrolling the recursion shows what mtm_t is: a weighted sum of all past gradients with geometrically decaying weights, mt=(1−β1)∑s=1tβ1t−sgsm_t = (1 - \beta_1) \sum_{s=1}^{t} \beta_1^{t-s} g_s. With β1=0.9\beta_1 = 0.9 the last 10 or so steps carry most of the weight. Adam keeps a second EMA, of the squared gradient:

vt=β2 vt−1+(1−β2) gt2,v0=0.v_t = \beta_2\, v_{t-1} + (1 - \beta_2)\, g_t^2, \qquad v_0 = 0 .

With β2=0.999\beta_2 = 0.999 it remembers about 1000 steps. vtv_t estimates E[g2]\mathbb{E}[g^2], the mean squared size of this coordinate’s gradient.

The second average exists so that Adam can divide by it. The update divides the first moment by the square root of the second:

wt=wt−1−η m^tv^t+ϵ.w_t = w_{t-1} - \eta\, \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon} .

v^t\sqrt{\hat v_t} has the units of the gradient, so the ratio m^t/v^t\hat m_t / \sqrt{\hat v_t} has no units. Multiplying every gradient by a constant c>0c > 0 multiplies both m^t\hat m_t and v^t\sqrt{\hat v_t} by cc and leaves the update unchanged (when ϵ\epsilon is negligible). Each coordinate’s step is therefore about η\eta in size when its gradient keeps a consistent sign, and smaller when the sign flips, because positive and negative gradients cancel in mm but not in vv, so ∣m^∣≪v^|\hat m| \ll \sqrt{\hat v}. The learning rate becomes a step length in parameter space, which is why one value such as 3×10−43 \times 10^{-4} works across many models. ϵ\epsilon only matters when v^\sqrt{\hat v} is near or below it, for coordinates whose gradients are almost always zero.

The hats in that formula need explaining. Both averages start at 0, so early on they are too small. Take every gsg_s equal to the same value gg. Then mt=(1−β1) g∑s=0t−1β1s=(1−β1t) gm_t = (1 - \beta_1)\, g \sum_{s=0}^{t-1} \beta_1^{s} = (1 - \beta_1^t)\, g by the geometric sum from Sequences, geometric series, and frequency ladders. The average is short of gg by exactly the factor 1−β1t1 - \beta_1^t, and dividing by it removes the bias:

m^t=mt1−β1t,v^t=vt1−β2t.\hat m_t = \frac{m_t}{1 - \beta_1^t}, \qquad \hat v_t = \frac{v_t}{1 - \beta_2^t} .

For random gradients with constant mean the same argument holds in expectation. The factor matters most for vv: with β2=0.999\beta_2 = 0.999, 1−β21=0.0011 - \beta_2^{1} = 0.001, so the uncorrected v1v_1 is 1000 times too small and the first step would be 1000≈32\sqrt{1000} \approx 32 times too large in the ratio m/vm / \sqrt{v}. That is partly offset by the uncorrected mm, which is 10 times too small, for a net of 3.163.16 times too large. After a few thousand steps both factors are 1 and the correction disappears.

At t=1t = 1 the corrections give m^1=g1\hat m_1 = g_1 and v^1=g12\hat v_1 = g_1^2, so the first update is η g1/(∣g1∣+ϵ)\eta\, g_1 / (|g_1| + \epsilon): exactly η\eta times the sign of g1g_1 when ϵ=0\epsilon = 0. torch folds the corrections into the step for speed, and the solution uses the same arrangement, because it decides where ϵ\epsilon goes:

wt=wt−1−η1−β1t⋅mtvt/1−β2t+ϵ.w_t = w_{t-1} - \frac{\eta}{1 - \beta_1^t}\cdot \frac{m_t}{\sqrt{v_t} / \sqrt{1 - \beta_2^t} + \epsilon} .

Weight decay shrinks the weights toward 0 a little on every step, which regularizes the model. For SGD it is the same as adding the penalty λ2∥w∥2\frac{\lambda}{2}\lVert w \rVert^2 to the loss, whose gradient is λw\lambda w: the step w−η(g+λw)=(1−ηλ) w−ηgw - \eta(g + \lambda w) = (1 - \eta\lambda)\, w - \eta g decays ww by the factor 1−ηλ1 - \eta\lambda.

For Adam the two are not the same, and the normalization you just saw is the reason. Adam with L2 (torch.optim.Adam(weight_decay=...)) adds λw\lambda w to gg before both moments see it, and then the normalization divides it by v^\sqrt{\hat v}. A coordinate with large gradients gets almost no decay, and with g=0g = 0 the penalty itself becomes a normalized step of size η\eta, whatever the weight’s size. AdamW applies the decay directly to the weights, before the Adam update, and leaves the gradient alone:

w←(1−ηλ) w,then the Adam step on gt.w \leftarrow (1 - \eta\lambda)\, w, \quad \text{then the Adam step on } g_t .

Every coordinate shrinks by the same factor, as intended. The decay is scaled by η\eta, so a schedule that lowers η\eta (the subject of the next section) lowers the decay with it. Parameters whose gradient is None (the loss did not reach them this step) are skipped entirely: no decay, no moment update.

The optimizer state is tt, mm, and vv for every parameter, plus the hyperparameters. Training that stops and resumes must continue as if it never stopped, so state_dict returns copies of everything, and load_state_dict restores all of it, including tt: with tt reset to 0 the bias correction would restart and the next step would be badly mis-scaled. formats/checkpoint.md writes the moments as <name>.exp_avg and <name>.exp_avg_sq, in the parameter’s dtype.

Take one scalar parameter x0=1x_0 = 1, gradients g1=2g_1 = 2 then g2=−1g_2 = -1 (given, not computed from a loss), η=0.1\eta = 0.1, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=0\epsilon = 0, and no weight decay. The two steps work out as follows:

Stepmtm_tvtv_t1−β1t1 - \beta_1^t1−β2t1 - \beta_2^tm^t\hat m_tv^t\hat v_tupdate ηm^/v^\eta \hat m / \sqrt{\hat v}xtx_t
10.1⋅2=0.20.1 \cdot 2 = 0.20.001⋅4=0.0040.001 \cdot 4 = 0.0040.10.10.0010.00122440.1⋅2/2=0.10.1 \cdot 2 / 2 = 0.10.90.9
20.9⋅0.2+0.1⋅(−1)=0.080.9 \cdot 0.2 + 0.1 \cdot (-1) = 0.080.999⋅0.004+0.001⋅1=0.0049960.999 \cdot 0.004 + 0.001 \cdot 1 = 0.0049960.190.190.0019990.0019998/19≈0.421058/19 \approx 0.421054996/1999≈2.499254996/1999 \approx 2.499250.1⋅0.42105/1.58090≈0.0266340.1 \cdot 0.42105 / 1.58090 \approx 0.026634≈0.873366\approx 0.873366

Two things are worth seeing. Step 1 moves by exactly η=0.1\eta = 0.1: that is bias correction at work (without it, 0.1⋅0.2/0.004≈0.3160.1 \cdot 0.2 / \sqrt{0.004} \approx 0.316). Step 2 has a negative gradient, yet xx keeps decreasing: mm still points the old way, and the step is smaller because v^\hat v remembers the large first gradient.

Now add weight decay λ=0.1\lambda = 0.1. AdamW first multiplies xx by 1−ηλ=0.991 - \eta\lambda = 0.99: step 1 gives 0.99−0.1=0.890.99 - 0.1 = 0.89; step 2 gives 0.89⋅0.99−0.026634≈0.8544660.89 \cdot 0.99 - 0.026634 \approx 0.854466. Adam with L2 instead feeds g1+λx0=2.1g_1 + \lambda x_0 = 2.1 and g2+λx1=−1+0.09=−0.91g_2 + \lambda x_1 = -1 + 0.09 = -0.91 to the moments. Step 1 still moves by exactly 0.10.1 (the first step is η\eta times a sign, whatever the gradient), to 0.90.9; step 2 lands at ≈0.868123\approx 0.868123. The penalty changed x2x_2 by 0.00520.0052 under L2 and by 0.01890.0189 under AdamW.

The two classes share one interface:

class Adam:
lr: float; betas: tuple[float, float]; eps: float; weight_decay: float
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0) -> None
def step(self) -> None # one update of every p whose grad is not None, in place
def zero_grad(self) -> None # every p.grad = None
def state_dict(self) -> dict # {"step", "lr", "betas", "eps", "weight_decay", "exp_avg": [...], "exp_avg_sq": [...]}
def load_state_dict(self, sd: dict) -> None
class AdamW(Adam): # decoupled decay; weight_decay defaults to 0.01
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.01) -> None

A parameter is any object with data (a float ndarray) and grad (an ndarray or None). A two-attribute class is enough, and the training loop passes the autograd tensors of Tensor, broadcasting backward, and no_grad. The step counter tt is one global count of step() calls, as formats/checkpoint.md stores one step. torch keeps a counter per parameter instead; the two agree whenever every parameter has a gradient on every step, which is how the models in this series train. A schedule changes the learning rate by assigning opt.lr before step().

The tests hold the implementation to a long list of behaviors, each tied to a downstream need. It must reproduce both hand-worked steps to float64 precision, and the λ=0.1\lambda = 0.1 variants for both kinds of decay, since that is the one difference between the classes. It must match 20 steps of torch AdamW on two tensors, including the final moments, and 20 steps of torch Adam with weight_decay = 0.1, where L2 coupling feeds both moments. A learning rate changed on every step (with betas (0.9, 0.95)) must be used, because schedules write opt.lr. Gradients of size 10−610^{-6} with ϵ=10−6\epsilon = 10^{-6} must match torch, which is where the placement of ϵ\epsilon becomes visible. float32 parameters must follow torch’s float32 trajectory, with float32 moments, because model parameters are float32 (see Module system and basic layers).

Two laws are checked as properties: with ϵ=0\epsilon = 0 the first step is η⋅sign(g)\eta \cdot \mathrm{sign}(g) at every scale, and gradients multiplied by 10−310^{-3} or 250250 give the same trajectory. With zero gradient, starting from the test’s weights, AdamW gives 3.83.8 and Adam with L2 gives 3.93.9: decoupling, in one line. A parameter with grad = None gets no decay and no moment change, while tt still advances. The update is in place: p.data keeps its identity and a view writes through to its buffer, because the model and the flat buffers of large-scale training hold the arrays. zero_grad sets every grad to None, since the autograd accumulates into grad. Five steps, save, load into a fresh optimizer, and five more equal ten steps bit for bit; a saved dict does not change after later steps; the layout’s keys and shapes are checked, hyperparameters restored, and mismatches rejected. Finally, bad hyperparameters (negative lr, eps, or decay, a beta outside [0,1)[0, 1), no parameters, integer data) are errors at construction, not NaN at step 9000.

The design follows from the principles in eight steps.

  1. Two EMAs per coordinate. mm tracks the gradient’s direction, vv its squared size. They have the parameter’s shape and dtype and start at 0.
  2. Normalize to make the step a length. Dividing m^\hat m by v^\sqrt{\hat v} removes the gradient’s scale, so η\eta is a step size in parameter space.
  3. Correct the zero start. Dividing by 1−β1t1 - \beta_1^t and 1−β2t1 - \beta_2^t makes the first step exactly η\eta, which requires tt to count from 1 at the first step().
  4. Put ϵ\epsilon where torch does. Folding both corrections into one scalar step_size = lr / (1 - beta1^t) and dividing v\sqrt{v} by 1−β2t\sqrt{1 - \beta_2^t} before adding ϵ\epsilon reproduces torch to the last bit; adding ϵ\epsilon elsewhere changes small-gradient coordinates.
  5. Two kinds of decay, one flag. Adam folds λw\lambda w into gg (so both moments see it); AdamW multiplies ww by 1−ηλ1 - \eta\lambda before the update and leaves gg alone. Everything else is shared, so AdamW is Adam with a class flag and a different default.
  6. Read hyperparameters on every step. Schedules assign opt.lr between steps, so step reads self.lr each call.
  7. Update in place, skip None gradients. The arrays are shared with the model; a parameter that got no gradient must not decay or change its moments, while the global tt still advances.
  8. State is tt, mm, vv, and the hyperparameters, copied. Restoring tt keeps the bias correction where it was.

The constructor validates and allocates:

class Adam:
_decoupled = False # AdamW decays the weights directly instead
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0) -> None:
_check_hyper(lr, betas, eps, weight_decay) # lr, eps, decay >= 0; betas in [0, 1)
self.params = list(params)
if not self.params:
raise ValueError("params is empty: nothing to optimize")
for i, p in enumerate(self.params):
d = getattr(p, "data", None)
if not isinstance(d, np.ndarray) or not np.issubdtype(d.dtype, np.floating):
raise ValueError(f"parameter {i}: data must be a floating-point ndarray")
self.lr = float(lr)
self.betas = (float(betas[0]), float(betas[1]))
self.eps = float(eps)
self.weight_decay = float(weight_decay)
self.t = 0 # becomes 1 on the first step
# Moments in the parameter's own dtype, allocated up front so every
# parameter has an entry in the checkpoint even before its first grad.
self.exp_avg = [np.zeros_like(p.data) for p in self.params]
self.exp_avg_sq = [np.zeros_like(p.data) for p in self.params]

Integer data is rejected because p.data -= ... on an integer array would truncate every update (or raise), a failure that shows up thousands of steps later as a model that never trains. The _check_hyper conditions are written not lr >= 0.0, so NaN is rejected too. Moments are allocated eagerly, unlike SGD’s lazy buffers, because the checkpoint format stores one exp_avg and exp_avg_sq per parameter.

The step is a direct transcription of torch’s single-tensor path:

def step(self) -> None:
self.t += 1 # t = 1 on the first call
lr, (beta1, beta2), eps, wd = self.lr, self.betas, self.eps, self.weight_decay # read now
bias_correction1 = 1.0 - beta1**self.t
bias_correction2_sqrt = math.sqrt(1.0 - beta2**self.t)
step_size = lr / bias_correction1 # torch's arrangement: one scalar
for p, m, v in zip(self.params, self.exp_avg, self.exp_avg_sq):
g = p.grad
if g is None:
continue # no decay, no moment update
g = np.asarray(g, dtype=p.data.dtype)
if g.shape != p.data.shape:
raise ValueError(f"grad shape {g.shape} != parameter shape {p.data.shape}")
if self._decoupled:
p.data *= 1.0 - lr * wd # AdamW: shrink w, leave g alone
elif wd != 0.0:
g = g + wd * p.data # Adam: L2 gradient, seen by m and v
m *= beta1
m += (1.0 - beta1) * g # m_t = b1 m + (1 - b1) g
v *= beta2
v += (1.0 - beta2) * (g * g) # v_t = b2 v + (1 - b2) g^2
denom = np.sqrt(v) / bias_correction2_sqrt + eps # eps after the correction
p.data -= step_size * (m / denom) # in place, in p's dtype

Trace the hand example’s step 1 through it. With t=1t = 1, bias_correction1 =0.1= 0.1, bias_correction2_sqrt =0.001= \sqrt{0.001}, and step_size =1= 1. With g=2g = 2: m=0.2m = 0.2, v=0.004v = 0.004, denom =0.004/0.001=2= \sqrt{0.004}/\sqrt{0.001} = 2, and xx moves by 1⋅0.2/2=0.11 \cdot 0.2/2 = 0.1 to 0.90.9. Step 2 with g=−1g = -1 gives m=0.08m = 0.08, v=0.004996v = 0.004996, step_size =0.1/0.19= 0.1/0.19, denom =0.004996/0.001999= \sqrt{0.004996/0.001999}, and x2≈0.873366x_2 \approx 0.873366. With AdamW and λ=0.1\lambda = 0.1, the decay line first multiplies xx by 0.990.99, and the same arithmetic lands at 0.8544660.854466; with Adam and L2 the gradients become 2.12.1 and −0.91-0.91, landing at 0.8681230.868123. The moments are updated with *= and +=, so they stay the same arrays and keep the parameter’s dtype; a float32 model never silently gets float64 moments.

Saving, loading, and the AdamW subclass complete the code:

def state_dict(self) -> dict:
return {
"step": self.t,
"lr": self.lr,
"betas": [self.betas[0], self.betas[1]],
"eps": self.eps,
"weight_decay": self.weight_decay,
"exp_avg": [m.copy() for m in self.exp_avg], # snapshots, not live arrays
"exp_avg_sq": [v.copy() for v in self.exp_avg_sq],
}
def load_state_dict(self, sd: dict) -> None:
ms, vs = list(sd["exp_avg"]), list(sd["exp_avg_sq"])
# ... same number of moments as parameters, each with the parameter's shape ...
betas = (float(sd["betas"][0]), float(sd["betas"][1]))
_check_hyper(float(sd["lr"]), betas, float(sd["eps"]), float(sd["weight_decay"]))
self.t = int(sd["step"]) # restore t: the correction continues
# ... lr, betas, eps, weight_decay ...
self.exp_avg = [np.array(m, dtype=p.data.dtype, copy=True) for p, m in zip(self.params, ms)]
self.exp_avg_sq = [np.array(v, dtype=p.data.dtype, copy=True) for p, v in zip(self.params, vs)]
class AdamW(Adam):
_decoupled = True
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.01) -> None:
super().__init__(params, lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)

As with SGD, validation happens before assignment so a bad checkpoint leaves the optimizer unchanged, and loaded moments are copied and cast to the parameter’s dtype. AdamW changes only the flag and the default weight_decay (0.01, torch’s default).

The full solution is in adamw.py.

Per step and parameter, the cost is about ten elementwise array operations and one square root. Memory is two moment arrays per parameter, twice the model size, which is the main reason production systems shard or quantize optimizer state (see Further reading).

The code matches torch’s arithmetic exactly. Writing the textbook form m^/(v^+ϵ)\hat m / (\sqrt{\hat v} + \epsilon) is mathematically the same but places ϵ\epsilon differently relative to the correction and rounds differently; matching _single_tensor_adam statement for statement is what makes the golden trajectories agree.

There is one global step counter. torch keeps one per parameter; with one counter the checkpoint stores one step, and the two agree whenever every parameter gets a gradient every step. The tradeoff is that a parameter that starts receiving gradients late gets a slightly wrong correction.

Inheritance beats duplication here. The two optimizers differ in one branch, and a class flag keeps the shared update in one place, so a fix to one is a fix to both.

Most Adam bugs are scaling bugs that train, just badly. These are the ones to look for:

PitfallSymptomHow the solution avoids it
no bias correction, or correcting only one momentthe first steps are about 3 times too large (or too small); early loss spikesstep_size = lr / (1 - beta1**t) and sqrt(v) / sqrt(1 - beta2**t)
counting tt from 0 inside the correction, or using t+1t + 11−β0=01 - \beta^0 = 0 divides by zero, or every step is mis-scaledself.t += 1 is the first line of step, so t=1t = 1 on the first update
AdamW adding λw\lambda w to the gradientAdamW behaves like Adam with L2: decay is normalized away for large-gradient weightsthe _decoupled branch multiplies p.data; only Adam adds to g
decaying by 1−λ1 - \lambda instead of 1−ηλ1 - \eta\lambdawith λ=0.1\lambda = 0.1 the weights lose 10% per step and collapse toward 0p.data *= 1.0 - lr * wd
ϵ\epsilon inside the square root, or before the correctionrare-feature coordinates take steps of the wrong size; only visible when gradients are near ϵ\epsilonnp.sqrt(v) / bias_correction2_sqrt + eps
caching the learning rate at constructionthe schedule is silently ignoredstep reads self.lr on every call
p.data = p.data - ... instead of p.data -= ...the optimizer updates a copy; the model keeps its old weightsp.data -= ... and p.data *= ...
returning live arrays from state_dict, or not restoring tta resumed run differs from the uninterrupted one; bias correction restarts after every resumecopies out and in, and self.t = int(sd["step"])
decaying parameters whose grad is Noneembeddings of tokens absent from the batch shrink anywaythe None check comes before the decay line

From here on, nearly every model in AI Engineering trains with this optimizer. In Training loop and the autograd bigram, train_step calls opt.zero_grad(), the backward pass, then opt.step() on every model of its part. Safetensors for every dtype, atomic checkpoints, and the token stream stores state_dict() as <name>.exp_avg, <name>.exp_avg_sq, and the step, and resume calls load_state_dict. The next section, Learning-rate schedules (cosine, WSD, Noam) and gradient clipping, sets opt.lr between steps and clips before step().

Later, AdamW trains the encoder-decoder of Encoder-decoder with teacher forcing, the 2017 Transformer with the Noam schedule in Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing, the pretraining objectives starting with GPT: the decoder-only language model, the fine-tuning heads of Fine-tuning heads and the linear-head export, and the recurrent models of RNN language model with stateful TBPTT. The modern decoder of The Llama-Family Model: Config, Weight Loading, and Download and the TinyStories capstone use betas (0.9,0.95)(0.9, 0.95) and λ=0.1\lambda = 0.1, with checkpoints holding exp_avg and exp_avg_sq. Mixed precision, loss scaling, gradient accumulation, and activation checkpointing and Supervised fine-tuning: chat rendering, assistant-only loss, packing train with it too.

  • Kingma and Ba, “Adam: A Method for Stochastic Optimization” (2015), sections 2 and 3; Loshchilov and Hutter, “Decoupled Weight Decay Regularization” (2019), sections 2 and 3; the torch source torch/optim/adam.py, _single_tensor_adam.
  • torch.optim.AdamW’s foreach and fused paths (torch/optim/adam.py, _multi_tensor_adam, _fused_adam) update all parameters in one kernel launch; the fused CUDA kernel also handles AMP grad scaling.
  • torch’s per-parameter state["step"] gives correct bias correction for parameters that start receiving gradients late.
  • Dettmers et al., “8-bit Optimizers via Block-wise Quantization” (2022): bitsandbytes quantizes mm and vv block-wise, cutting optimizer memory by 4 times.
  • Rajbhandari et al., “ZeRO” (2020): with ZeRO stage 1 (DeepSpeed, FSDP) each data-parallel rank owns a shard of mm and vv; see Data parallelism and ZeRO stages 1 to 3.
  • Jordan et al., “Muon” (2024): orthogonalized momentum for 2-D weights, AdamW for the rest.
  • Exponential moving average (EMA): a recursive average mt=βmt−1+(1−β)xtm_t = \beta m_{t-1} + (1 - \beta) x_t whose weights decay geometrically with age.
  • First moment (mm, exp_avg): the EMA of the gradient.
  • Second moment (vv, exp_avg_sq): the EMA of the elementwise squared gradient.
  • Bias correction: dividing mtm_t and vtv_t by 1−β1t1 - \beta_1^t and 1−β2t1 - \beta_2^t to undo the zero initialization.
  • Adam with L2: weight decay added to the gradient, so it passes through the normalization.
  • Decoupled weight decay (AdamW): multiplying the weights by 1−ηλ1 - \eta\lambda outside the gradient path.
  • Adam keeps two exponential moving averages per coordinate, mm of the gradient and vv of its square, and moves each coordinate by η m^/(v^+ϵ)\eta\, \hat m / (\sqrt{\hat v} + \epsilon).
  • Bias correction divides mtm_t by 1−β1t1 - \beta_1^t and vtv_t by 1−β2t1 - \beta_2^t, which undoes the pull toward the zero they start from; the first step is then exactly η\eta per coordinate.
  • Dividing by v^\sqrt{\hat v} makes the update invariant to the scale of the gradient, so one learning rate serves layers whose gradients differ by orders of magnitude.
  • AdamW decouples weight decay: it multiplies the weights by 1−ηλ1 - \eta\lambda instead of adding λw\lambda w to the gradient, where Adam’s normalization would wash the penalty out.
  • The optimizer state is part of the model: a save and load between steps must reproduce the uninterrupted run bit for bit.
  1. Why is the first Adam step exactly η\eta per coordinate when ϵ=0\epsilon = 0?

    Answer

    At t=1t = 1, m1=(1−β1)g1m_1 = (1 - \beta_1)g_1 and v1=(1−β2)g12v_1 = (1 - \beta_2)g_1^2. Dividing by 1−β11 - \beta_1 and 1−β21 - \beta_2 gives m^1=g1\hat m_1 = g_1 and v^1=g12\hat v_1 = g_1^2, so the update is η g1/∣g1∣=η sign(g1)\eta\,g_1/\lvert g_1\rvert = \eta\,\mathrm{sign}(g_1), whatever the gradient’s scale.

  2. Why does AdamW multiply the weights by 1−ηλ1 - \eta\lambda instead of adding λw\lambda w to the gradient?

    Answer

    Anything added to the gradient is divided by v^\sqrt{\hat v}. For a coordinate with large gradients the penalty becomes negligible; for one with zero gradient it becomes a full normalized step of size η\eta regardless of the weight’s size. Shrinking the weights directly applies the same relative decay to every coordinate, which is what weight decay is meant to do. In the zero-gradient test case, AdamW gives 3.8 and Adam with L2 gives 3.9.

  3. Where exactly is ϵ\epsilon added, and why does it matter?

    Answer

    To vt/1−β2t\sqrt{v_t}/\sqrt{1 - \beta_2^t}, that is, after the second moment is bias-corrected and square-rooted. This is torch’s placement. Adding it inside the square root, or before the correction, changes the denominator by an amount comparable to v\sqrt{v} for coordinates whose gradients are near ϵ\epsilon (rare features), so their steps would differ from torch’s.

  4. What goes wrong after a resume if load_state_dict does not restore step?

    Answer

    tt restarts at 0, so the next step uses 1−β11 - \beta^1 in both corrections while the moments already hold thousands of steps of history. v^\hat v becomes about 1000 times too large and m^\hat m about 10 times too large; the result is a step about 10/1000≈0.3210/\sqrt{1000} \approx 0.32 times the right size at first, and the trajectory no longer matches the uninterrupted run.

  5. Why does the optimizer read self.lr at the start of every step instead of storing it once?

    Answer

    Schedules change the learning rate by assigning opt.lr before each step. A value cached at construction would silently ignore the schedule, and both the Adam step and AdamW’s 1−ηλ1 - \eta\lambda decay must use the current rate.