Test MLX AdamW Weight Decay and Parameter Groups on a Cloud Mac
Training loss keeps falling, but biases keep shrinking. You set the gradients to zero, yet the parameters still change. That does not necessarily mean MLX is updating them incorrectly: AdamW weight decay may simply be doing its job. Before starting a long training run on a cloud Mac, checking optimizer behavior with a few small tensors makes troubleshooting easier than waiting for a full run and then guessing which settings were responsible.
This article checks only the decay formula, parameter groups, and update counts. It does not load a model or require a dataset. Everything runs over SSH; no graphical session is needed.
Separate the Sources of Parameter Updates
AdamW handles weight decay separately from gradient-driven updates. With a fresh optimizer that has no prior state, all-zero gradients should produce:
new parameter = old parameter × (1 − learning rate × decay coefficient)
The decay coefficient is not a fixed amount subtracted at each step. When the learning rate changes, the amount of shrinkage per step changes too. To compare two experiments, record both the learning rate and its schedule.
Zero gradients do not mean frozen parameters. An existing optimizer may retain nonzero momentum. Even without momentum, nonzero weight decay will change the parameters.
To isolate decay, you therefore need a fresh optimizer. Taking an optimizer that has just finished training and zeroing the gradients for the next batch is not a pure decay test.
Build a Minimal Test Fixture
Run these checks in a Python virtual environment already validated for your project. First, save the environment details so subsequent regression tests can use the same dependency versions:
python -m pip show mlx
python -m pip freeze > requirements-observed.txt
uname -m
In a native environment, the architecture output should be arm64. These records describe the current environment; they do not mean you have established a dependency lock that works across machines.
Save the following as verify_decay.py. The values are deliberately easy to check by hand. They are test inputs, not recommended training hyperparameters.
import mlx.core as mx
import mlx.optimizers as optim
lr = 0.01
decay = 0.1
initial = mx.array([2.0, -3.0, 0.0], dtype=mx.float32)
def check(actual, expected):
mx.eval(actual, expected)
assert bool(mx.allclose(
actual, expected, rtol=1e-6, atol=1e-7
).item())
optimizer = optim.AdamW(
learning_rate=lr,
weight_decay=decay,
)
params = {"weight": initial}
grads = {"weight": mx.zeros_like(initial)}
params = optimizer.apply_gradients(grads, params)
mx.eval(params, optimizer.state)
check(params["weight"], initial * (1 - lr * decay))
Run python verify_decay.py. It should finish with a zero exit code. Positive values shrink toward zero, negative values decrease in absolute magnitude, and zero stays zero. Testing only a positive value, rather than checking both signs, can let a sign error slip through.
Verify Parameter Groups
Whether biases and normalization parameters should be excluded from decay depends on the model's training recipe. Do not exclude a tensor simply because it is one-dimensional: one-dimensional tensors can serve other trainable roles.
First, flatten the trainable parameter tree and produce an inventory of each parameter's full path, shape, and group. Then assign groups using module types and explicit path rules. At a minimum, assert that the groups do not overlap and that their union equals the expected set of trainable parameters. Frozen parameters must not be included in that set.
To avoid relying on a version-specific parameter-group API, you can create separate optimizers for two disjoint parameter trees. Append this to the earlier script:
bias_initial = mx.array([2.0, -3.0], dtype=mx.float32)
no_decay_optimizer = optim.AdamW(
learning_rate=lr,
weight_decay=0.0,
)
bias_params = {"bias": bias_initial}
bias_params = no_decay_optimizer.apply_gradients(
{"bias": mx.zeros_like(bias_initial)},
bias_params,
)
mx.eval(bias_params, no_decay_optimizer.state)
check(bias_params["bias"], bias_initial)
During actual training, both optimizers must receive their corresponding gradients from the same backward pass, and each must perform exactly one update. Their learning-rate schedules must also advance in sync; neither group should take an extra step. When combining the results, write them back to the model using the original parameter paths so that renaming does not obscure group membership.
Check Consecutive Updates
Passing a single-step test is not enough. With a fixed learning rate, two consecutive zero-gradient updates should produce:
initial parameter × (1 − learning rate × decay coefficient)²
Append the following check. It uses the same optimizer that has already performed one update:
params = optimizer.apply_gradients(grads, params)
mx.eval(params, optimizer.state)
check(params["weight"], initial * (1 - lr * decay) ** 2)
If the result is close to three applications of decay, check whether the training loop calls both the model update API and apply_gradients, causing two parameter updates within one logical training step.
Also confirm that the second call receives the parameters returned by the previous step, not the initial parameter tree. MLX array operations do not automatically update every old reference to the new values. Ignoring the return value can cause the step count to advance while the weights are repeatedly updated from their starting values.
Explicitly evaluate the computation before each assertion. Here, mx.eval forces computation and exposes errors; it is not a timing tool.
Keep Evidence for Regression Testing
Add this fixture to your pre-training checks, and save the dependency list, learning rate, decay coefficient, and a summary of parameter groups. Do not print full weights to the logs. Paths and shapes are usually enough to diagnose group-membership problems.
When a check fails, narrow down the cause in this order:
- Single-step formula mismatch: first check the learning rate, decay coefficient, and whether the optimizer is fresh.
- Excluded parameters change: check for overlapping groups, overwrites when writing parameters back, and residual momentum.
- Consecutive-update mismatch: check for duplicate calls, stale parameter references, and schedule step counts.
- The small fixture passes but training behaves unexpectedly: check the model's parameter mapping, then add integration tests with nonzero gradients.
Finally, turn these conditions into assertions: parameters that should decay do decay, parameters excluded from decay retain their values, and each step performs exactly one update. Only once these boundaries are explicit do you have a reliable basis for interpreting changes in the training curve.
Frequently asked questions
Why does AdamW change weights when gradients are zero?
Decoupled weight decay operates separately from the gradient update. With a fresh optimizer, p_new = p_old × (1 − learning_rate × weight_decay). Existing moments can introduce additional changes.
Should bias and normalization parameters always be excluded?
Follow the model's training recipe rather than a universal rule. When exclusions are required, use disjoint groups covering all intended parameters and set weight_decay=0 for the excluded group.
Choose a cloud Mac
Choose the unified memory and rental term that fit your experiment. Get an entire machine to yourself and connect via SSH. Configuration and availability are shown live in the console.
Rent a cloud Mac