AdamW optimiser — Adam with decoupled weight decay (Loshchilov & Hutter, 2019).
Decouples the L2 penalty from the adaptive moment estimation: the weight-decay term is applied directly to the parameter, NOT folded into the gradient . This restores the rotation-invariance lost by Adam's adaptive learning rate when L2 is added to , and is strongly preferred over Adam + L2 for transformer training.
Math
References
Loshchilov & Hutter, "Decoupled Weight Decay Regularization" (ICLR 2019). Kingma & Ba, "Adam: A Method for Stochastic Optimization" (ICLR 2015).
See Also
Adam : L2-regularisation variant.
Attributes
lr_float1e-3.beta1_, beta2_float0.9 / 0.999.eps_float1e-8.weight_decay_float1e-2 — larger than Adam's because it is no longer scaled by the adaptive denominator.step_count_int64m_, v_Storage[]scalar_cache_AdamScalarCachewd_factor.Notes
The implementation realises the decay as a multiplicative pre-scale
of by (cached in
AdamScalarCache::wd_factor), then performs the standard Adam
step. Equivalent to the additive form above but avoids a separate
MLX op invocation per parameter.
Constructors
1Construct an AdamW optimiser bound to params.
Parameters
paramsvector of TensorImpl shared pointerslrfloat= None1e-3.beta1, beta2float= None0.9 and 0.999.epsfloat= None1e-8.weight_decayfloat= None1e-2.Virtual methods
2Methods
7Allocate zero-initialised and buffers for slot_idx.
Restore per-parameter state. See Adam::load_state_buffers.
Set the global step counter.
Export per-parameter state (exp_avg and exp_avg_sq).
Schema identifier for state_dict serialisation.
Returns
str"adamw_v1".
Global step counter .
Apply the decoupled-weight-decay Adam update.
Notes
Dispatches to adam_step_{cpu,gpu} with decoupled_wd = true
so the kernel applies before the Adam step.