Test MLX Training and Inference Modes on a Cloud Mac
If repeated evaluations of the same weights on a cloud Mac give inconsistent results, do not rerun training just yet. The model may still be in training mode, with Dropout changing the forward-pass output. Another common mistake is forgetting to restore training mode after evaluation: subsequent updates still run, but the regularization behavior has changed. The small experiment below establishes acceptance checks for MLX mode switching without downloading any weights.
Fix the test inputs and environment
Set up a Python environment that runs natively, then execute the following commands in a dedicated directory. Keep the dependency list after the first installation. For subsequent checks, use the same verified versions rather than upgrading dependencies while comparing results.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install mlx
python -m pip freeze > requirements.lock
python -c 'import platform; print(platform.machine())'
The architecture output should be arm64. This test does not load a language model or use sampling-based decoding: the input is a fixed vector of ones, and the module contains only nested Dropout. If the output is wrong, you can inspect the mode directly instead of investigating the tokenizer, weights, and generation settings at the same time.
Save the code in the next section as mode_check.py. The test uses only very small tensors; there is no need to deploy a full inference service just to verify mode switching.
Verify that inference disables Dropout recursively
With the Dropout probability set to 0.5, the training output should contain zeroed elements, while retained elements should be scaled to twice their original value. In inference mode, the input should pass through unchanged. The nested containers verify that the mode change reaches beyond the outermost module.
import mlx.core as mx
import mlx.nn as nn
mx.random.seed(17)
dropout = nn.Dropout(p=0.5)
model = nn.Sequential(nn.Sequential(dropout))
x = mx.ones((4096,), dtype=mx.float32)
def forward():
y = model(x)
mx.eval(y)
return y
def equal(a, b):
return bool(mx.all(a == b).item())
model.train()
train_y = forward()
assert bool(mx.all((train_y == 0) | (train_y == 2)).item())
assert bool(mx.any(train_y == 0).item())
assert bool(mx.any(train_y == 2).item())
model.eval()
assert not model.training
assert not dropout.training
eval_a = forward()
eval_b = forward()
assert equal(eval_a, x)
assert equal(eval_b, x)
model.train()
assert model.training
assert dropout.training
restored_y = forward()
assert bool(mx.any(restored_y == 0).item())
print("mode checks passed")
Run python mode_check.py. A failed assertion produces a nonzero exit code. The code checks both the output and the internal module flags, so it will not miss a case where a flag changes but the forward-pass logic ignores it.
The training-mask check is a smoke test with a fixed seed and size, not a mathematical guarantee for every random sequence. Do not assert that exactly half the elements are dropped on every pass: a probability of one half does not mean that every finite sample splits evenly.
Separate mode from randomness
A fixed seed is no substitute for eval(). The seed controls the random sequence; inference mode controls module behavior. Resetting the seed before every pass can reproduce the same incorrect training output, making it look “stable” without making it correct inference.
Reproducible output does not mean the mode is correct. For this fixture, the inference output must equal the input—not merely match the output of another pass.
The example calls mx.eval(y) on every pass to ensure that each stage's output has been evaluated before moving to the next stage. MLX uses lazy evaluation. Do not accumulate unevaluated results across multiple passes and check them all at the end; that makes it harder to identify the stage at which something went wrong.
A full language model also involves generation sampling. Even with Dropout disabled, sampling-based decoding can still produce different text. Mode regression tests should first compare forward-pass results for fixed inputs. If you also need to compare generated text, fix the decoding strategy separately.
Restore state and isolate concurrent requests
When inserting evaluation into training, use try/finally to restore the mode that was active on entry. The following applies when the entire model was originally in a uniform training or inference mode:
previous = model.training
try:
model.eval()
result = forward()
assert equal(result, x)
finally:
model.train(previous)
If a project intentionally keeps some submodules in different modes, a single Boolean cannot preserve that arrangement. Record and restore the relevant module states individually, or use a separate evaluation instance. Do not assume that one recursive mode change can restore a mixed-mode configuration.
Restoring state after an exception does not make the code concurrency-safe, either. If one request calls eval() while another task calls train(), the shared object's behavior can change. Prefer a separate instance for inference, loading a snapshot of weights from completed updates at request boundaries.
If sharing is unavoidable, the lock must cover the entire sequence: mode switching, the forward pass, explicit evaluation, and state restoration. Locking only the line that changes the mode does not protect the computation that follows.
Add regression checks and diagnose failures
Start by using the small fixture as a quick check, then add a set of fixed inputs for the actual model. When a real model involves different operators and precision levels, use explicit numerical tolerances rather than copying the exact elementwise equality checks used here.
For every run, retain the Python and MLX versions, the code commit identifier, an input summary, and the assertion results. If a regression appears, check the following in order:
- Inference output differs from the input: check whether Dropout is still in training mode.
- Dropout is disabled in the outer module but not in an inner module: check whether the module system is tracking the submodules correctly.
- Training behavior changes after evaluation: inspect exception paths and mixed-mode restoration.
- Standalone execution works, but concurrent execution fails: check whether tasks share an instance and switch its mode.
Finally, deliberately remove one model.eval() call, confirm that the test fails, and then restore the code. This proves that the check catches the intended failure rather than merely allowing the correct path to pass. Keeping this small experiment is usually more direct than trying to infer the cause from unstable output in a full model.
Frequently asked questions
Can a fixed random seed replace model.eval()?
No. A seed controls the random sequence, but training-mode Dropout still drops and rescales elements. Call model.eval() before inference and check repeated outputs.
Can training and inference share one model instance concurrently?
Do not let concurrent tasks toggle a shared instance's mode. Prefer a separate inference instance with an explicit weight snapshot. If sharing is unavoidable, serialize mode switching, forward execution, explicit evaluation and restoration together.
Choose a cloud Mac by model and memory
Dedicated Apple Silicon M4 / M4 Pro machines with 16 GB, 24 GB, or 64 GB of unified memory. Pay by the day, week, month, or quarter; service stops at the end of your term unless you renew.