Skip to content
class

AdamW

extendsOptimizer
AdamW

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 gtg_t. This restores the rotation-invariance lost by Adam's adaptive learning rate when L2 is added to gg, and is strongly preferred over Adam + L2 for transformer training.

Math

mt=β1 mt−1+(1−β1) gt,vt=β2 vt−1+(1−β2) gt2 m_t = \beta_1\, m_{t-1} + (1 - \beta_1)\, g_t, \qquad v_t = \beta_2\, v_{t-1} + (1 - \beta_2)\, g_t^2 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} θt+1=(1−ηλ) θt−η⋅m^tv^t+ϵ \theta_{t+1} = (1 - \eta \lambda)\, \theta_t - \eta \cdot \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}

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_float
Step size η\eta. Default 1e-3.
beta1_, beta2_float
EMA decay rates. Defaults 0.9 / 0.999.
eps_float
Denominator stabiliser. Default 1e-8.
weight_decay_float
Decoupled penalty coefficient λ\lambda. Default 1e-2 — larger than Adam's because it is no longer scaled by the adaptive denominator.
m_, v_Storage[]
Per-parameter first- and second-moment buffers.
scalar_cache_AdamScalarCache
Per-step broadcast cache, including wd_factor.

Notes

The implementation realises the decay as a multiplicative pre-scale of θt\theta_t by (1−ηλ)(1 - \eta \lambda) (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

1
ctor

AdamW

void AdamW(int)

Construct an AdamW optimiser bound to params.

Parameters

paramsvector of TensorImpl shared pointers
Parameters to optimise.
lrfloat= None
Step size η\eta. Default 1e-3.
beta1, beta2float= None
First- and second-moment EMA decay rates. Defaults 0.9 and 0.999.
epsfloat= None
Denominator stabiliser ϵ\epsilon. Default 1e-8.
weight_decayfloat= None
Decoupled penalty coefficient λ\lambda. Default 1e-2.
amsgradbool= None
Use the running maximum of the second moment in the denominator (Reddi et al., 2018). Default false.

Static methods

1
static

check_hyperparams

void check_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay)

Check AdamW's hyper-parameters. See Adam::check_hyperparams.

Virtual methods

2
virtconst

lr

double lr()

Current learning rate η\eta.

virt

set_lr

void set_lr(double lr)

Set the learning rate and invalidate the scalar cache.

Notes

Same rationale as Adam::set_lr — additionally invalidates the cached AdamW wd_factor = (1−ηλ)(1 - \eta \lambda) scalar.

Methods

5
fn

init_state_slot

void init_state_slot(int slot_idx, const int & param)

Allocate zero-initialised mm and vv buffers for slot_idx.

fn

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.

fnconst

state_dict_id

→str
int state_dict_id()

Schema identifier for state_dict serialisation.

Returns

str

"adamw_v1".

fn

update_one

void update_one(int slot_idx, int & param, const int & grad)

Apply the decoupled-weight-decay Adam update.

Notes

Dispatches to adam_step_{cpu,gpu} with decoupled_wd = true so the kernel applies θ←(1−ηλ)θ\theta \leftarrow (1 - \eta \lambda) \theta before the Adam step.