Training & LoRA¶
Trainer¶
mlx_diffuser.training.DiffusionTrainer
¶
Source code in src/mlx_diffuser/training/trainer.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
fit(dataset, *, steps=None, epochs=1, log_every=50)
¶
Train over dataset (an iterable of x0 or (x0, y) batches).
Source code in src/mlx_diffuser/training/trainer.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
step(x0, y=None)
¶
One optimization step on a batch; returns the (scalar) loss.
Source code in src/mlx_diffuser/training/trainer.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
mlx_diffuser.training.EMA
¶
Tracks an EMA of a model's parameters for more stable sampling weights.
Source code in src/mlx_diffuser/training/ema.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
copy_to(model)
¶
Overwrite the model's parameters with the EMA weights (in place).
Source code in src/mlx_diffuser/training/ema.py
22 23 24 | |
mlx_diffuser.training.batch_iterator(data, batch_size, *, shuffle=True, seed=0, drop_last=True)
¶
Yield mini-batches from one or more aligned arrays.
A single array yields array batches; a tuple of arrays yields tuples of
correspondingly-indexed batches (e.g. (images, labels)).
Source code in src/mlx_diffuser/training/data.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
mlx_diffuser.training.mse_loss(pred, target, weights=None)
¶
Mean squared error, optionally weighted per-sample.
Source code in src/mlx_diffuser/training/losses.py
11 12 13 14 15 16 | |
mlx_diffuser.training.min_snr_weights(scheduler, t, gamma=5.0)
¶
Min-SNR-gamma loss weighting (Hang et al., 2023) for VP diffusion.
Returns per-sample weights min(SNR, gamma) / SNR for epsilon prediction
(/(SNR+1) adjustment is applied for v-prediction).
Source code in src/mlx_diffuser/training/losses.py
19 20 21 22 23 24 25 26 27 28 29 30 | |
LoRA¶
mlx_diffuser.lora.inject_lora(model, rank=8, alpha=16.0, dropout=0.0, targets=DEFAULT_LORA_TARGETS)
¶
Replace target nn.Linear layers with LoRA adapters and freeze the base.
Returns the number of layers adapted. After this, trainable_parameters
contains only the adapter weights.
Source code in src/mlx_diffuser/lora/lora.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
mlx_diffuser.lora.merge_lora(model)
¶
Fuse all adapters into dense layers in place; returns the model.
Source code in src/mlx_diffuser/lora/lora.py
109 110 111 112 113 114 115 116 117 | |
mlx_diffuser.lora.save_lora(model, save_directory, *, rank, alpha, targets=DEFAULT_LORA_TARGETS, dropout=0.0)
¶
Source code in src/mlx_diffuser/lora/lora.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
mlx_diffuser.lora.load_lora(model, path)
¶
Inject adapters per adapter_config.json and load their weights.
Source code in src/mlx_diffuser/lora/lora.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
mlx_diffuser.lora.LoRALinear
¶
Bases: Module
Wraps a (frozen) nn.Linear with a trainable low-rank update.
Source code in src/mlx_diffuser/lora/lora.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
fused()
¶
Return a dense nn.Linear with the adapter merged in.
Source code in src/mlx_diffuser/lora/lora.py
45 46 47 48 49 50 51 52 53 | |
Performance¶
mlx_diffuser.perf.compile_model(model, *, shapeless=False)
¶
Return a compiled callable for inference with this model.
Parameters are passed as implicit inputs so weight updates (e.g. after a LoRA
merge) are picked up without a stale graph. Use shapeless=True only when
input ranks are stable but sizes vary a lot (read MLX's shapeless-compile
caveats first) to avoid recompiling per resolution.
Source code in src/mlx_diffuser/perf.py
31 32 33 34 35 36 37 38 39 | |
mlx_diffuser.perf.memory_report()
¶
Snapshot of unified-memory usage in GB (active / peak / cache).
Source code in src/mlx_diffuser/perf.py
42 43 44 45 46 47 48 | |