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.m_ , 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.amsgradbool= Nonefalse.Static methods
1Virtual methods
2Methods
5Allocate zero-initialised and buffers for slot_idx.
set_hyperparams
void set_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay, bool amsgrad)Replace the hyper-parameters between steps. See Adam::set_hyperparams.
Schema identifier for state_dict serialisation.
Returns
str"adamw_v1".
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.