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 .
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 global step counter step_count_:
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_float1e-3.beta1_, beta2_float0.9 and 0.999 from the original paper.eps_float1e-8.weight_decay_float0 to disable.amsgrad_boolfalse.step_count_int64state_dict under state_step.m_, v_Storage[]scalar_cache_AdamScalarCacheNotes
step_count_ is a global counter incremented once per step()
call regardless of how many parameters are updated, so all parameters
share the same bias-correction factor within a step. This matches
reference-framework semantics.
AMSGrad (Reddi et al., 2018) is declared via the amsgrad ctor
flag but not yet implemented — passing true raises
not_implemented at construction time.
Constructors
1Construct an Adam optimiser bound to params.
Parameters
paramsvector of TensorImpl shared pointersm and one v buffer is allocated per parameter on first step() call.lrfloat= None1e-3.beta1, beta2float= None0.9 and 0.999.epsfloat= None1e-8.weight_decayfloat= None0.amsgradbool= Nonefalse — see class Notes.Raises
not_implementedamsgrad == true.Virtual methods
2Current learning rate .
Set the learning rate and invalidate the scalar cache.
Parameters
lrfloatNotes
The 3.4 perf pass folds (and for
AdamW) into AdamScalarCache; mutating here flips
scalar_cache_.valid = false so the cached MLX scalars are
rebuilt on the next step.
Methods
10First-moment EMA decay rate .
Second-moment EMA decay rate .
Denominator stabiliser .
Allocate zero-initialised and buffers for slot_idx.
Parameters
slot_idxsize_tm_ / v_.paramTensorImpl shared pointerRestore per-parameter state from a previous state_buffers.
Parameters
bufsvector of NamedBuffersexp_avg / exp_avg_sq keys.Raises
runtime_errorSet the global step counter (used by checkpoint loading).
Export per-parameter state (exp_avg = , exp_avg_sq = ).
Returns
vector of NamedBuffersOne entry per parameter, each containing the named buffers exp_avg and exp_avg_sq.
Schema identifier for state_dict serialisation.
Returns
str"adam_v1".
Global step counter used for bias correction.
Apply the standard Adam update to a single parameter.
Parameters
slot_idxsize_tm_ / v_.paramTensorImpl shared pointergradStorageparam shape / dtype / device.Notes
Dispatches to adam_step_gpu (GPU stream) or adam_step_cpu
(CPU stream) with decoupled_wd = false.