Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
Refuse training.loraplus_lr_ratio on backend mlx at config load (#1324) (#1383)
Kratoscd51422c832a
diff --git a/changelog.d/0.75.1/1383.fixed.md b/changelog.d/0.75.1/1383.fixed.mdnew file mode 100644index 00000000..96f6cdbd--- /dev/null+++ b/changelog.d/0.75.1/1383.fixed.md@@ -0,0 +1 @@+- **`training.loraplus_lr_ratio` is now refused at config load on `backend: mlx` (#1324 by @wanjinhao1 in #1383).** The MLX optimizer trains every LoRA tensor at one learning rate, so the ratio was accepted by validation, `soup train --dry-run` and `soup doctor --config`, and then never applied. An MLX config that sets it and loaded before now fails with an error naming the field, the backend and the way out: remove the key, which trains exactly what those runs trained, or use `backend: transformers`. `transformers` and `unsloth` are unaffected.diff --git a/docs/peft-and-efficiency.md b/docs/peft-and-efficiency.mdindex d752a0da..2d39be3a 100644--- a/docs/peft-and-efficiency.md+++ b/docs/peft-and-efficiency.md@@ -402,7 +402,7 @@ training:alpha: 16```-- **Compatibility:** Wired on every Trainer-based task that trains a LoRA adapter, including the preference and RL tasks (`dpo`, `kto`, `orpo`, `simpo`, `ipo`, `bco`, `grpo`, `online_dpo`, `ppo`, `reward_model`, `distill`). Most attach the LoRA+ optimizer once the trainer is built; `ppo` passes it to the trainer's constructor instead, because trl's PPO trainer builds its optimizer and LR scheduler eagerly, and adds the value model to it at the base learning rate, as trl's default optimizer does. `classifier`, `reranker`, `cross_encoder` and `asr` full fine-tune by default, so LoRA+ applies there only with `classifier_lora: true` / `asr_lora: true` and `lora.r > 0`. Refused at config parse on tasks with no trainable LoRA $B$ matrix: `prm` (full fine-tune), `moe_lora_routing` (only the routing gate trains), and `classifier` / `reranker` / `cross_encoder` / `asr` without their LoRA flag. Also refused on `unlearn`, which runs its own optimizer loop rather than a Trainer. Refused with `lora.use_vera` (VeRA trains scaling vectors, not $A$/$B$ matrices, so every trainable tensor would run at `lr * ratio`). Mutually exclusive with `use_lorafa`.+- **Compatibility:** Wired on every Trainer-based task that trains a LoRA adapter, including the preference and RL tasks (`dpo`, `kto`, `orpo`, `simpo`, `ipo`, `bco`, `grpo`, `online_dpo`, `ppo`, `reward_model`, `distill`). Most attach the LoRA+ optimizer once the trainer is built; `ppo` passes it to the trainer's constructor instead, because trl's PPO trainer builds its optimizer and LR scheduler eagerly, and adds the value model to it at the base learning rate, as trl's default optimizer does. `classifier`, `reranker`, `cross_encoder` and `asr` full fine-tune by default, so LoRA+ applies there only with `classifier_lora: true` / `asr_lora: true` and `lora.r > 0`. Refused at config parse on tasks with no trainable LoRA $B$ matrix: `prm` (full fine-tune), `moe_lora_routing` (only the routing gate trains), and `classifier` / `reranker` / `cross_encoder` / `asr` without their LoRA flag. Also refused on `unlearn`, which runs its own optimizer loop rather than a Trainer. Refused with `lora.use_vera` (VeRA trains scaling vectors, not $A$/$B$ matrices, so every trainable tensor would run at `lr * ratio`). Also refused on the `mlx` backend (#1324): the MLX optimizer Soup builds (`trainer/mlx_optim.py`) runs every LoRA tensor at one learning rate (no parameter groups), so the ratio would be silently ignored; remove it or use `backend: transformers`. Mutually exclusive with `use_lorafa`.## LoRA-FA (Frozen-A LoRA)diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.pyindex 374f4552..04460a27 100644--- a/src/soup_cli/config/schema.py+++ b/src/soup_cli/config/schema.py@@ -7169,6 +7169,9 @@ class SoupConfig(BaseModel):``unlearn`` is refused in :meth:`_validate_unlearn_compat` for adifferent reason (it has an adapter but no ``Trainer`` optimizer).++ The ``mlx`` backend is refused separately, in+ :meth:`_validate_loraplus_backend` (#1324), after the MLX task gate."""tcfg = self.trainingif tcfg.loraplus_lr_ratio is None:@@ -7401,6 +7404,32 @@ class SoupConfig(BaseModel):f"training helper). Use backend=transformers for task={self.task}.")+ @model_validator(mode="after")+ def _validate_loraplus_backend(self) -> "SoupConfig":+ """#1324: ``loraplus_lr_ratio`` is not implemented on ``backend: mlx``.++ MLX builds its LoRA adapter with ``mlx_lm``'s ``linear_to_lora_layers``+ and its optimizer from a single learning rate (``plan_optimizer`` in+ ``trainer/mlx_optim.py`` takes no parameter groups), so the ratio would+ validate, print nothing in ``soup doctor --config``, and train A and B+ at one learning rate. ``unsloth`` reaches the transformers wrappers'+ ``attach_loraplus_optimizer`` call, so it stays allowed (#1080).++ Defined after :meth:`_validate_mlx_task_support` and+ :meth:`_validate_loraplus_has_a_trainable_lora_b` on purpose: a task+ MLX cannot run, or one with no LoRA B matrix on any backend, is refused+ for that reason first, so "use backend: transformers" below always+ leads to a config that loads.+ """+ if self.backend == "mlx" and self.training.loraplus_lr_ratio is not None:+ raise ValueError(+ "Refused: training.loraplus_lr_ratio is not implemented on "+ "backend='mlx' (the MLX optimizer uses one "+ "learning rate for every LoRA tensor). Remove "+ "training.loraplus_lr_ratio, or use backend: transformers."+ )+ return self+@model_validator(mode="after")def _validate_uld_compat(self) -> "SoupConfig":"""v0.70.0 Part B — Universal Logit Distillation gate.diff --git a/tests/test_issue1324_mlx_loraplus_refusal.py b/tests/test_issue1324_mlx_loraplus_refusal.pynew file mode 100644index 00000000..779d125c--- /dev/null+++ b/tests/test_issue1324_mlx_loraplus_refusal.py@@ -0,0 +1,134 @@+"""Issue #1324 — ``training.loraplus_lr_ratio`` is refused on ``backend: mlx``.++On ``backend: mlx`` (``task: sft`` is the only reachable MLX LoRA case), the+config used to load, validate, pass ``soup train --dry-run`` and+``soup doctor --config``, and then train plain LoRA: the MLX optimizer is+built from a single scalar (``plan_optimizer`` in ``trainer/mlx_optim.py``+takes no parameter groups), so A and B ran at one learning rate with no+warning. Mirroring LoRA-FA's backend gate (#725), the field is now refused+at load on every backend except ``transformers`` and ``unsloth`` (unsloth+reaches the transformers wrappers' ``attach_loraplus_optimizer`` call, so it+stays allowed — regression guard for #1080's wiring).++The pre-existing ``training.use_lorafa`` row in the ``_MLX_SFT`` doctor+table stays: the mlx trainer itself warns about it at runtime, and the+#755 drift test requires the table to match that warning list.+"""++import re+from pathlib import Path++import pytest+from typer.testing import CliRunner++from soup_cli.cli import app+from soup_cli.config.backend_support import _MLX_SFT+from soup_cli.config.loader import load_config_from_string++_MLX_LORAPLUS = """+base: mlx-community/Qwen2.5-0.5B-Instruct-4bit+task: sft+backend: mlx+data:+ train: ./data/train.jsonl+ format: alpaca+training:+ epochs: 1+ lr: 1.0e-4+ loraplus_lr_ratio: 16+ lora:+ r: 8+ alpha: 16+output: ./output+"""+++def _transformers_variant(backend: str) -> str:+ return _MLX_LORAPLUS.replace("backend: mlx", f"backend: {backend}").replace(+ "mlx-community/Qwen2.5-0.5B-Instruct-4bit", "Qwen/Qwen2.5-0.5B-Instruct"+ )+++def _plain(text: str) -> str:+ """Strip ANSI escapes and collapse whitespace (Rich colours and wraps on CI)."""+ return " ".join(re.sub(r"\x1b\[[0-9;]*[A-Za-z]", "", text or "").split())+++def test_mlx_loraplus_is_refused_at_load_with_message():+ with pytest.raises(ValueError) as excinfo:+ load_config_from_string(_MLX_LORAPLUS)+ msg = str(excinfo.value)+ assert "training.loraplus_lr_ratio is not implemented on backend='mlx'" in msg+ assert "Remove training.loraplus_lr_ratio, or use backend: transformers." in msg+++def test_transformers_loraplus_still_loads():+ cfg = load_config_from_string(_transformers_variant("transformers"))+ assert cfg.training.loraplus_lr_ratio == pytest.approx(16.0)+ assert cfg.backend == "transformers"+++def test_unsloth_loraplus_still_loads():+ cfg = load_config_from_string(_transformers_variant("unsloth"))+ assert cfg.training.loraplus_lr_ratio == pytest.approx(16.0)+ assert cfg.backend == "unsloth"+++def test_mlx_loraplus_without_ratio_still_loads():+ # The gate is on the ratio field, not on the backend itself.+ cfg = load_config_from_string(+ _MLX_LORAPLUS.replace(" loraplus_lr_ratio: 16\n", "")+ )+ assert cfg.backend == "mlx"+++def test_train_dry_run_exits_nonzero_on_mlx_loraplus(tmp_path: Path):+ cfg_file = tmp_path / "c1_mlx_loraplus.yaml"+ cfg_file.write_text(_MLX_LORAPLUS, encoding="utf-8")+ result = CliRunner().invoke(app, ["train", "--config", str(cfg_file), "--dry-run"])+ assert result.exit_code == 1, (result.output, repr(result.exception))+ assert "training.loraplus_lr_ratio is not implemented on backend='mlx'" in _plain(+ result.output+ )+++ "task, data_format",+ [("dpo", "dpo"), ("kto", "kto"), ("pretrain", "plaintext"), ("embedding", "alpaca")],+)+def test_a_task_mlx_cannot_run_is_refused_for_that_first(task, data_format):+ """The MLX task gate runs first: switching backend is the whole fix there."""+ text = _MLX_LORAPLUS.replace("task: sft", f"task: {task}").replace(+ "format: alpaca", f"format: {data_format}"+ )+ with pytest.raises(ValueError) as excinfo:+ load_config_from_string(text)+ msg = str(excinfo.value)+ assert "MLX backend only ships SFT" in msg+ assert "is not implemented on backend=" not in msg+++def test_a_task_with_no_lora_b_keeps_its_own_refusal_on_mlx():+ """moe_lora_routing refuses LoRA+ on every backend; "use backend: transformers"+ would lead straight into that refusal, so it must come first."""+ text = """+base: org/m+task: moe_lora_routing+backend: mlx+data:+ train: ./x.jsonl+training:+ loraplus_lr_ratio: 16+ mole_task_adapters: [./a, ./b]+"""+ with pytest.raises(ValueError) as excinfo:+ load_config_from_string(text)+ msg = str(excinfo.value)+ assert "trains only the routing gate" in msg+ assert "is not implemented on backend=" not in msg+++def test_mlx_doctor_table_has_no_loraplus_row():+ # A refused-at-load field must not appear in the doctor table (#958).+ names = {entry.field for entry in _MLX_SFT}+ assert "training.loraplus_lr_ratio" not in names