Skip to content
class

AdamScalarCache

AdamScalarCache

Per-step scalar broadcast cache for Adam-family GPU updates.

Holds the eight broadcast scalars (β1\beta_1, 1−β11-\beta_1, β2\beta_2, 1−β21-\beta_2, ϵ\epsilon, η\eta, 1/(1−β1t)1/(1-\beta_1^t), 1/(1−β2t)1/(1-\beta_2^t)) plus the optional weight-decay scalar and the AdamW decay factor as pre-allocated MLX scalar arrays. All entries are rebuilt once per optimiser step at the first parameter and reused across the remaining ~64 parameters of a typical ResNet-18 / transformer step.

Attributes

validbool
true when the cached arrays match the current step / dtype.
dtDtype
Element type the arrays were built for.
for_stepint64
Step number the arrays were built at.
b1, omb1, b2, omb2mlx array (scalar)
β1\beta_1, 1−β11 - \beta_1, β2\beta_2, 1−β21 - \beta_2 broadcasts.
eps_a, lr_a, wd_amlx array (scalar)
ϵ\epsilon, η\eta, λ\lambda broadcasts. wd_a is present only when weight_decay != 0.
inv_bc1, inv_bc2mlx array (scalar)
1/(1−β1t)1 / (1 - \beta_1^t) and 1/(1−β2t)1 / (1 - \beta_2^t).
wd_factormlx array (scalar)
(1−ηλ)(1 - \eta \lambda), the AdamW multiplicative decay factor.

Notes

Without this cache, adam_step_gpu rebuilt params.size() * 8 MLX scalar arrays per call (~520 allocations for ResNet-18). Each MLX array construction has constant overhead, and the bias-correction factors don't depend on which parameter is being updated, so the scalars are pure step-level invariants.

The cache invalidates when the slot's step count differs from the one it was built at, the param dtype changes (mixed-precision case), or when set_lr / set_hyperparams changes a hyper-parameter.

Constructors

1

Destructor

1
dtor

~AdamScalarCache

Out-of-line destructor — mlx::core::array is only forward- declared in this header, so unique_ptr cannot inline-destroy it. The definition lives in Adam.cpp.

Operators

1
op

operator=

AdamScalarCache & operator=(const AdamScalarCache &)