Audit MLX LoRA freezing: prove that only adapters update

Audit MLX LoRA freezing: prove that only adapters update

You start an MLX LoRA fine-tuning run on a cloud Mac. The loss drops as expected, and the saved adapter loads successfully. Neither proves that the base weights were left untouched. Before launching a long run, use a small experiment that requires no model download to turn three questions into assertions: what is trainable, what stays unchanged, and what actually updates. That is more direct than tracking down unintended parameter changes after training.

Define the update boundary

LoRA splits a linear layer’s output into a base branch and a low-rank update: base(x) + up(down(x)) * scale. The base branch provides the original mapping; only the two low-rank matrices go into the optimizer. This article tests the update boundary, not model quality, and does not treat a low loss as evidence that freezing is correct.

Prepare a Python environment that runs natively, then execute the following in the remote terminal:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install mlx
python -m pip freeze > requirements-resolved.txt
uname -m

The architecture should be arm64. Save the resolved dependency versions after the first installation, and use the same dependency file for subsequent checks. Do not upgrade dependencies while comparing weight changes: doing both at once makes failures harder to isolate.

Freezing constrains the set of trainable parameters; it does not disable forward computation. The base layer must still participate in the computation. Do not remove the entire branch to “freeze” it.

Build a minimal adapter

Save the following as freeze_check.py. The example uses fixed inputs, mean squared error, and a single update so that data loading, random sampling, and a full training framework do not obscure freezing errors.

import mlx.core as mx
import mlx.nn as nn
import mlx.optimizers as optim
from mlx.utils import tree_flatten

class Adapter(nn.Module):
    def __init__(self):
        super().__init__()
        self.base = nn.Linear(4, 3, bias=False)
        self.down = nn.Linear(4, 2, bias=False)
        self.up = nn.Linear(2, 3, bias=False)
        self.down.weight = mx.full((2, 4), 0.1)
        self.up.weight = mx.zeros((3, 2))
        self.base.freeze()

    def __call__(self, x):
        return self.base(x) + self.up(self.down(x)) * 0.5

def loss_fn(model, x, y):
    return mx.mean(mx.square(model(x) - y))

mx.random.seed(7)
model = Adapter()
x = mx.ones((2, 4))
y = model(x) + mx.ones((2, 3))
mx.eval(y)

expected = {"down.weight", "up.weight"}
actual = dict(tree_flatten(model.trainable_parameters()))
assert set(actual) == expected

before = {
    name: mx.array(value)
    for name, value in tree_flatten(model.parameters())
}
mx.eval(list(before.values()))

optimizer = optim.Adam(learning_rate=0.01)
loss, grads = nn.value_and_grad(model, loss_fn)(model, x, y)
assert set(dict(tree_flatten(grads))) == expected
optimizer.update(model, grads)
mx.eval(loss, model.parameters(), optimizer.state)

after = dict(tree_flatten(model.parameters()))
assert bool(mx.array_equal(before["base.weight"], after["base.weight"]))
assert float(mx.max(mx.abs(
    after["up.weight"] - before["up.weight"]
)).item()) > 0

The target is deliberately constructed to differ from the initial output, ensuring that the adapter has an error to correct. Real training uses independent labels; do not carry this target-construction method into a production loss function.

Verify the three assertions

Run python freeze_check.py. A zero exit code means this example passes. Check the name sets first, then check numerical changes—not the other way around. If the optimizer leaves a base array unchanged, that does not prove the array was correctly excluded: its gradient may simply have been zero on this step.

The base weights must match element for element, because the contract is that they do not update at all. For the adapter, check that the difference is greater than zero. Do not require the change to exceed an arbitrary fixed threshold; small learning rates and different numerical types affect its magnitude.

MLX uses lazy evaluation, so call mx.eval explicitly before comparing. Save the pre-update arrays, not another reference to the model object. Evaluating them before the update also makes the snapshot boundary explicit. These freezing assertions are not intended to determine whether training results on two machines are bit-for-bit identical.

Check the first-step pitfalls

When the up-projection is initialized to zero, the update branch initially leaves the base output unchanged, but the down-projection’s gradient may be zero on the first step. You therefore cannot assert that both adapter matrices change on the first update. You can perform a second update and then check the down-projection, while retaining the assertion that the base weights remain unchanged.

Also run two negative tests. Removing self.base.freeze() must cause the parameter-set assertion to fail. Setting the learning rate to zero must cause the up-projection change assertion to fail. Run these separately so that the first failure does not prevent the second check from executing.

If an incorrect configuration still passes, fix the test before scaling up training. Do not add a tolerance to the base-weight equality assertion to “eliminate false positives”: that would hide small unintended updates.

Apply the checks to real training

When integrating a real model, enumerate trainable_parameters() only after all adapter replacements and freezing are complete. Save the full paths of parameters allowed to update as an audit checklist, and reject unexpected embedding layers, output layers, or base linear layers. Explicitly list any exceptions that genuinely need to be trained.

Do not identify adapters solely by whether a path contains a particular short string; naming changes can cause modules to be missed. During the audit, record paths, shapes, numerical types, and the total parameter count together. This catches cases where a layer has the right name but the adapter has the wrong dimensions.

For a full model, you do not need to copy all base weights at every step. Read and compare them layer by layer before and after training, or use blockwise digests to reduce additional memory usage. Keep direct array comparisons in the minimal experiment so that specific changes are easy to locate.

For every run, save the code revision, dependency versions, list of parameters allowed to update, and failed assertions. The acceptance criterion is not proof that the model improved, but proof that training changed only the approved parts. Once that boundary is established, evaluate loss, generation quality, and the training budget.

Frequently asked questions

Do frozen base weights still participate in the forward pass?

Yes. Freezing excludes them from trainable parameters; it does not remove the base layer from the computation. The adapter still receives gradients through the graph.

Why does only one LoRA matrix change on the first step?

A zero-initialized up projection can give the down projection a zero gradient on the first step. Check that the up projection updates first, then test subsequent steps.

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

Launch one and test your idea

Choose the memory and rental term, then run your experiment on a dedicated physical Mac mini.