Skip to content
class

Adam

extendsOptimizer
Adam

Adam optimiser — adaptive moment estimation (Kingma & Ba, 2014).

Maintains exponential moving averages of the gradient and squared gradient for each parameter, then applies a bias-corrected step scaled element-wise by the inverse second-moment v^\sqrt{\hat v}.

Weight decay (when non-zero) is added to the gradient before the moment update — i.e. the L2-regularisation form, NOT the decoupled AdamW form. Use AdamW for decoupled decay.

Math

Per parameter, with tt the parameter's own update count (Optimizer::steps_):

gt←gt+λ θt(if λ>0) g_t \leftarrow g_t + \lambda\, \theta_t \quad \text{(if } \lambda > 0\text{)} 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=θt−η⋅m^tv^t+ϵ \theta_{t+1} = \theta_t - \eta \cdot \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}

References

Kingma & Ba, "Adam: A Method for Stochastic Optimization" (ICLR 2015). Reddi, Kale & Kumar, "On the Convergence of Adam and Beyond" (ICLR 2018) — AMSGrad variant.

See Also

AdamW : decoupled-weight-decay variant. NAdam : Nesterov-momentum variant. RAdam : variance-rectified variant.

Attributes

lr_float
Step size η\eta. Default 1e-3.
beta1_, beta2_float
EMA decay rates for the first and second moments. Defaults 0.9 and 0.999 from the original paper.
eps_float
Denominator stabiliser ϵ\epsilon. Default 1e-8.
weight_decay_float
L2 penalty coefficient λ\lambda added to the gradient. Set to 0 to disable.
amsgrad_bool
Use the AMSGrad running maximum — see Notes.
m_, v_Storage[]
Per-parameter first-moment and second-moment buffers, matching each parameter's shape and dtype.
scalar_cache_AdamScalarCache
Per-step broadcast cache for the GPU kernel.

Notes

tt counts the updates each parameter has received, as the reference framework's per-parameter state["step"] does: a parameter that first receives a gradient late starts its bias correction at t=1t = 1, whatever the other parameters have reached.

AMSGrad (Reddi et al., 2018) is enabled by the amsgrad ctor flag: the running maximum of vtv_t replaces vtv_t in the denominator.

Constructors

1
ctor

Adam

void Adam(int)

Construct an Adam optimiser bound to params.

Parameters

paramsvector of TensorImpl shared pointers
Parameters to optimise. One m and one v buffer is allocated per parameter on first step() call.
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
L2 penalty coefficient λ\lambda. Default 0.
amsgradbool= None
Use the AMSGrad variant — see class Notes. Default false.

Static methods

1
static

check_hyperparams

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

Check Adam's hyper-parameters — the reference framework's constructor checks, run by the constructor and set_hyperparams.

Raises

InvalidArgument
If lr, eps or weight_decay is negative or NaN, or a beta lies outside [0, 1).

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.

Parameters

lrfloat
New step size η\eta.

Notes

The 3.4 perf pass folds η\eta (and 1−ηλ1 - \eta \lambda for AdamW) into AdamScalarCache; mutating η\eta here flips scalar_cache_.valid = false so the cached MLX scalars are rebuilt on the next step.

Methods

8
fnconst

beta1

double beta1()

First-moment EMA decay rate β1\beta_1.

fnconst

beta2

double beta2()

Second-moment EMA decay rate β2\beta_2.

fnconst

eps

double eps()

Denominator stabiliser ϵ\epsilon.

fn

init_state_slot

void init_state_slot(int slot_idx, const int & param)

Allocate zero-initialised mm and vv buffers for slot_idx.

Parameters

slot_idxsize_t
Index into m_ / v_.
paramTensorImpl shared pointer
Reference parameter — buffers inherit its shape / dtype.
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.

Nothing is applied when check_hyperparams rejects them. The scalar cache is rebuilt on the next step. Switching amsgrad on gives each slot that holds state a zero running maximum, as a fresh optimizer built with it on would start from; switching it off keeps the maximum, so switching it on again resumes it.

fnconst

state_dict_id

→str
int state_dict_id()

Schema identifier for state_dict serialisation.

Returns

str

"adam_v1".

fn

update_one

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

Apply the standard Adam update to a single parameter.

Parameters

slot_idxsize_t
Index into m_ / v_.
paramTensorImpl shared pointer
In-place parameter to update.
gradStorage
Gradient gtg_t matching param shape / dtype / device.

Notes

Dispatches to adam_step_gpu (GPU stream) or adam_step_cpu (CPU stream) with decoupled_wd = false.