Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(config): the moe_lora + unsloth refusal gives a reason true for the task (#1264) (#1323)

Srinivasan Re6f47f9dddda
diff --git a/changelog.d/0.75.1/1179.fixed.md b/changelog.d/0.75.1/1179.fixed.md
index 6775536a..a3371072 100644
--- a/changelog.d/0.75.1/1179.fixed.md
+++ b/changelog.d/0.75.1/1179.fixed.md
@@ -6,9 +6,8 @@
`lora.r > 0` (that trainer then full-fine-tunes and builds no adapter), `asr` (it
trains only Whisper, which has no experts), `moe_lora_routing` (it builds no LoRA
adapter) and `prm` (it fine-tunes every base parameter). It is also refused on
- `backend: unsloth` for every task, since unsloth attaches a fixed attention list,
- and on `task: sft` with `modality: vision` or `audio`, whose setup skips the MoE
- step. Every path now either reads the flag or refuses it, except `backend: mlx`,
+ `backend: unsloth` for every task, and on `task: sft` with `modality: vision` or
+ `audio`, whose setup skips the MoE step. Every path now either reads the flag or refuses it, except `backend: mlx`,
where it loads and `soup doctor` reports it as ignored. Because
`training.train_router_only` and `training.moe_expert_quant` require
`moe_lora: true`, a config setting either on `backend: unsloth` now fails to load
diff --git a/changelog.d/0.75.1/1323.fixed.md b/changelog.d/0.75.1/1323.fixed.md
new file mode 100644
index 00000000..ec6e2e72
--- /dev/null
+++ b/changelog.d/0.75.1/1323.fixed.md
@@ -0,0 +1,2 @@
+- **`training.moe_lora` with `backend: unsloth` is refused with a reason that is true for the task (#1264 by @Srinivasan8888 in #1323).**
+ The refusal said "unsloth attaches its own fixed attention targets" on every task, but ten tasks have no unsloth setup at all (`reward_model`, `prm`, `classifier`, `reranker`, `cross_encoder`, `distill`, `unlearn`, `moe_lora_routing`, `online_dpo`, `asr`): there it is `backend: unsloth` that goes unapplied, and seven of them now say so. The other three (`asr`, `prm`, `moe_lora_routing`), and `task: sft` with `modality: vision` or `audio`, now get their own refusal first, which is the same on every backend, so switching to `backend: transformers` no longer surfaces a second, different refusal for the same config. Nothing that loaded before stops loading.
diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.md
index 7634a8b2..60939042 100644
--- a/docs/performance-and-quantization.md
+++ b/docs/performance-and-quantization.md
@@ -1152,7 +1152,7 @@ Both reject silently-no-op combinations: setting either flag without `moe_lora=t
| `moe_expert_quant`, `train_router_only` | `sft`, `tts` | refused at config load, naming the task |
| `moe_aux_loss_coeff` | `sft`, `tts`, `pretrain` | a **non-default** value is refused; the default `0.01` still loads, because every stored config and eleven shipped recipes write it |
-**`moe_lora` on the remaining LoRA tasks (#1099).** #798 left it loading but unread on `ipo`, `bco`, `reward_model`, `ppo` and `embedding`, and `online_dpo` had the same gap. All six build their adapter through the same `build_lora_config` path, so they were wired to the same helper rather than refused. On `embedding` it applies only with `lora.r >= 1`; at `r: 0` that trainer full-fine-tunes and builds no adapter for the flag to select. #1151 closed the remainder: `distill` and `unlearn` build their adapter the same way and are wired to the same helper, and so is `classifier` / `reranker` / `cross_encoder` (one trainer) on its opt-in adapter path. Without `classifier_lora: true` and `lora.r > 0` that trainer full-fine-tunes and builds no adapter, so there the flag is refused at config load; `asr`, which trains only Whisper (no experts), `moe_lora_routing`, which builds no LoRA adapter, and `prm`, which fine-tunes every base parameter, refuse the flag at config load. Two paths refuse it whatever the task: `backend: unsloth`, whose setup attaches a fixed attention list, and `task: sft` with `modality: vision` or `audio`, whose setup builds its adapter without the MoE step. Every path now either reads `moe_lora` or refuses it, except `backend: mlx`, where it loads and `soup doctor --config` reports it as ignored; a source ratchet keeps a new adapter-building trainer from missing it. `train_router_only` and `moe_expert_quant` require `moe_lora: true`, so on `backend: unsloth` they now fail to load with the same message. `preference` is covered through the trainers it dispatches to, and `tts` through the SFT trainer it subclasses.
+**`moe_lora` on the remaining LoRA tasks (#1099).** #798 left it loading but unread on `ipo`, `bco`, `reward_model`, `ppo` and `embedding`, and `online_dpo` had the same gap. All six build their adapter through the same `build_lora_config` path, so they were wired to the same helper rather than refused. On `embedding` it applies only with `lora.r >= 1`; at `r: 0` that trainer full-fine-tunes and builds no adapter for the flag to select. #1151 closed the remainder: `distill` and `unlearn` build their adapter the same way and are wired to the same helper, and so is `classifier` / `reranker` / `cross_encoder` (one trainer) on its opt-in adapter path. Without `classifier_lora: true` and `lora.r > 0` that trainer full-fine-tunes and builds no adapter, so there the flag is refused at config load; `asr`, which trains only Whisper (no experts), `moe_lora_routing`, which builds no LoRA adapter, and `prm`, which fine-tunes every base parameter, refuse the flag at config load. Two paths refuse it whatever the task: `task: sft` with `modality: vision` or `audio`, whose setup builds its adapter without the MoE step, and `backend: unsloth`. On unsloth the reason depends on the task (#1264): the thirteen tasks with an unsloth setup attach its fixed attention list and never read the flag, while the rest have no unsloth setup at all, so there it is the backend that goes unapplied. Both messages point to `backend: transformers`, where the flag is read. The other `moe_lora` refusals in the same check come first, so `asr`, `prm`, `moe_lora_routing` and SFT vision/audio get one refusal whatever the backend. Every path now either reads `moe_lora` or refuses it, except `backend: mlx`, where it loads and `soup doctor --config` reports it as ignored; a source ratchet keeps a new adapter-building trainer from missing it. `train_router_only` and `moe_expert_quant` require `moe_lora: true`, so on `backend: unsloth` they now fail to load with the same message. `preference` is covered through the trainers it dispatches to, and `tts` through the SFT trainer it subclasses.
**`moe_lora` requires `lora.dropout: 0.0` on a fused-expert MoE.** transformers 5.x keeps a Qwen3-MoE's experts as fused 3-D parameters (`mlp.experts.gate_up_proj`), which peft adapts through `lora.ParamWrapper`, and that wrapper raises `lora.ParamWrapper does not work with lora_dropout != 0.` With the schema default of `0.05` the LoRA attach failed outright, so `moe_lora` did not work on any task — including `sft`. Soup now stops at the attach with a message naming the flag, instead of letting peft's reach the user, and all 31 shipped MoE recipes pin `lora.dropout: 0.0`. The check is made against the loaded model, not at config load: whether the experts are fused depends on the checkpoint and the transformers version, and a model with one module per expert takes dropout normally. A dense base is untouched — there the flag is a no-op.
diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.py
index aac4a3bc..7c05d9dd 100644
--- a/src/soup_cli/config/schema.py
+++ b/src/soup_cli/config/schema.py
@@ -4611,6 +4611,15 @@ _QUANTIZATION_UNHONOURED_TASKS = frozenset({
_MOE_EXPERT_KNOB_TASKS = frozenset({"sft", "tts"})
_MOE_AUX_LOSS_TASKS = frozenset({"sft", "tts", "pretrain"})
+#: #1264 — the tasks whose trainer has an unsloth setup: a ``_setup_unsloth`` on
+#: the wrapper (``tts`` inherits SFT's), or on every wrapper ``preference``
+#: delegates to. The other tasks have none, so ``backend: unsloth`` is not
+#: applied there at all.
+UNSLOTH_SETUP_TASKS: frozenset[str] = frozenset({
+ "sft", "dpo", "grpo", "ppo", "kto", "orpo", "simpo", "ipo", "bco", "preference",
+ "pretrain", "embedding", "tts",
+})
+
#: The bitsandbytes values: ``4bit`` was the default, so every config Soup dumped
#: for these tasks carries one of them literally (#795 review).
_BNB_QUANTIZATION_VALUES = frozenset({"4bit", "8bit"})
@@ -4834,7 +4843,13 @@ class SoupConfig(BaseModel):
attaches ``utils/unsloth.py``'s fixed attention list, and SFT's vision and
audio setups build their adapter without the MoE step, so neither reads it
(found by a local CodeRabbit review of #1179). MLX stays declared-ignored in
- ``backend_support`` instead, which ``soup doctor`` reports."""
+ ``backend_support`` instead, which ``soup doctor`` reports.
+
+ #1264: the refusals in this check that hold on every backend run before
+ the unsloth one, so switching backend never meets a second refusal from
+ this check, and the unsloth reason is split by
+ :data:`UNSLOTH_SETUP_TASKS`: a task with no unsloth setup does read the
+ flag; there it is the backend that goes unapplied."""
if not self.training.moe_lora:
return self
tcfg = self.training
@@ -4847,12 +4862,6 @@ class SoupConfig(BaseModel):
"trainer full-fine-tunes and builds no adapter for the flag to select. Set "
"classifier_lora: true and lora.r >= 1, or remove moe_lora."
)
- if self.backend == "unsloth":
- raise ValueError(
- "training.moe_lora is not applied on backend='unsloth': unsloth attaches "
- "its own fixed attention targets and never reads the flag. Use backend: "
- "transformers, or remove moe_lora."
- )
if self.task == "sft" and self.modality in ("vision", "audio"):
raise ValueError(
f"training.moe_lora is not applied by task='sft' with "
@@ -4865,11 +4874,23 @@ class SoupConfig(BaseModel):
"and builds no LoRA adapter of its own",
"prm": "that trainer fine-tunes every base parameter and builds no LoRA adapter",
}.get(self.task)
- if why is None:
+ if why is not None:
+ raise ValueError(
+ f"training.moe_lora is not applied by task={self.task!r}: {why}. "
+ "Remove moe_lora (or set it to false)."
+ )
+ if self.backend != "unsloth":
return self
+ if self.task in UNSLOTH_SETUP_TASKS:
+ raise ValueError(
+ "training.moe_lora is not applied on backend='unsloth': unsloth attaches "
+ "its own fixed attention targets and never reads the flag. Use backend: "
+ "transformers, or remove moe_lora."
+ )
raise ValueError(
- f"training.moe_lora is not applied by task={self.task!r}: {why}. "
- "Remove moe_lora (or set it to false)."
+ f"training.moe_lora is refused with backend='unsloth': task={self.task!r} has "
+ "no unsloth setup, so backend='unsloth' is not applied to it at all. Use "
+ "backend: transformers, which applies moe_lora for this task."
)
@model_validator(mode="after")
diff --git a/tests/test_issue1264_unsloth_moe_lora_reason.py b/tests/test_issue1264_unsloth_moe_lora_reason.py
new file mode 100644
index 00000000..6f8f9734
--- /dev/null
+++ b/tests/test_issue1264_unsloth_moe_lora_reason.py
@@ -0,0 +1,180 @@
+"""#1264 -- ``training.moe_lora`` with ``backend: unsloth`` is refused with a reason
+that is true for the task, and the refusals that apply on every backend come first.
+
+Before the fix, one message ("unsloth attaches its own fixed attention targets")
+was given on every task. It is false on the ten tasks whose trainer has no unsloth
+setup at all: there ``backend: unsloth`` is what goes unapplied, and the flag is
+read. And because the unsloth check ran first, ``asr`` / ``prm`` /
+``moe_lora_routing`` and SFT vision / audio were told to switch to
+``backend: transformers``, which only surfaced a second refusal.
+"""
+
+from __future__ import annotations
+
+import importlib
+import typing
+
+import pytest
+import yaml
+
+from soup_cli.config.loader import load_config_from_string
+from soup_cli.config.schema import SoupConfig
+
+# What each task needs besides base / task / data.train to load at all.
+_TOP = {"tts": {"modality": "audio_out"}}
+_DATA = {
+ "dpo": {"format": "dpo"}, "kto": {"format": "kto"}, "pretrain": {"format": "plaintext"},
+ "unlearn": {"forget_set": "./f.jsonl"},
+}
+_TRAINING = {
+ "preference": {"preference_loss": "dpo"},
+ "tts": {"tts_family": "orpheus"},
+ "classifier": {"num_labels": 2, "classifier_lora": True},
+ "reranker": {"num_labels": 2, "classifier_lora": True},
+ "cross_encoder": {"num_labels": 2, "classifier_lora": True},
+ "unlearn": {"unlearn_method": "npo"},
+ "moe_lora_routing": {"mole_task_adapters": ["./a", "./b"]},
+ "distill": {"teacher_model": "org/t"},
+ "online_dpo": {"online_dpo_judge": "pairrm"},
+}
+
+# The trainer each task dispatches to in commands/train.py.
+_WRAPPERS = {
+ "sft": "sft:SFTTrainerWrapper", "dpo": "dpo:DPOTrainerWrapper",
+ "grpo": "grpo:GRPOTrainerWrapper", "ppo": "ppo:PPOTrainerWrapper",
+ "reward_model": "reward_model:RewardModelTrainerWrapper",
+ "kto": "kto:KTOTrainerWrapper", "orpo": "orpo:ORPOTrainerWrapper",
+ "simpo": "simpo:SimPOTrainerWrapper", "ipo": "ipo:IPOTrainerWrapper",
+ "bco": "bco:BCOTrainerWrapper", "preference": "preference:PreferenceTrainerWrapper",
+ "pretrain": "pretrain:PretrainTrainerWrapper",
+ "embedding": "embedding:EmbeddingTrainerWrapper", "prm": "prm:PRMTrainerWrapper",
+ "tts": "tts:TTSTrainerWrapper", "classifier": "classifier:ClassifierTrainerWrapper",
+ "reranker": "classifier:ClassifierTrainerWrapper",
+ "cross_encoder": "classifier:ClassifierTrainerWrapper",
+ "distill": "distill:DistillTrainerWrapper", "unlearn": "unlearn:UnlearnTrainerWrapper",
+ "moe_lora_routing": "mole_routing:MoleRoutingTrainerWrapper",
+ "online_dpo": "online_dpo:OnlineDPOTrainerWrapper", "asr": "asr:AsrTrainerWrapper",
+}
+# preference builds one of these per preference_loss (trainer/preference.py).
+_PREFERENCE_DELEGATES = ("dpo", "simpo", "orpo", "ipo", "bco")
+
+_WITH_UNSLOTH = (
+ "sft", "dpo", "grpo", "ppo", "kto", "orpo", "simpo", "ipo", "bco", "preference",
+ "pretrain", "embedding", "tts",
+)
+# No unsloth setup, and moe_lora is read on transformers: the unsloth refusal
+# reaches these. prm / moe_lora_routing / asr get their task refusal instead.
+_WITHOUT_UNSLOTH = (
+ "reward_model", "classifier", "reranker", "cross_encoder", "unlearn", "distill",
+ "online_dpo",
+)
+_TASK_REFUSED = ("prm", "moe_lora_routing", "asr")
+
+
+def _yaml(task, backend, moe_lora=True, modality=None):
+ top = dict(_TOP.get(task, {}))
+ if modality:
+ top["modality"] = modality
+ data = {"train": "./x.jsonl", **_DATA.get(task, {})}
+ if modality:
+ data["format"] = {"vision": "llava", "audio": "audio"}[modality]
+ training = {"moe_lora": moe_lora}
+ if task != "prm": # prm refuses any lora block: it fine-tunes every parameter
+ training["lora"] = {"r": 4, "dropout": 0.0}
+ training.update(_TRAINING.get(task, {}))
+ raw = {"base": "org/m", "task": task, "backend": backend, **top, "data": data,
+ "training": training}
+ return yaml.safe_dump(raw)
+
+
+def _refusal(task, backend, modality=None):
+ with pytest.raises(ValueError) as info:
+ load_config_from_string(_yaml(task, backend, modality=modality))
+ return " ".join(str(info.value).split())
+
+
+class TestTheReasonIsTrueForTheTask:
+ @pytest.mark.parametrize("task", _WITH_UNSLOTH)
+ def test_a_task_with_an_unsloth_setup_is_told_about_its_fixed_targets(self, task):
+ message = _refusal(task, "unsloth")
+
+ assert "fixed attention targets" in message, message
+ assert "no unsloth setup" not in message
+
+ @pytest.mark.parametrize("task", _WITHOUT_UNSLOTH)
+ def test_a_task_without_one_is_told_unsloth_does_not_run_it(self, task):
+ message = _refusal(task, "unsloth")
+
+ assert f"task={task!r} has no unsloth setup" in message, message
+ assert "fixed attention targets" not in message
+
+ @pytest.mark.parametrize("task", _WITH_UNSLOTH + _WITHOUT_UNSLOTH)
+ def test_the_advice_to_use_transformers_holds(self, task):
+ """Both messages say to use backend: transformers; there, the config loads."""
+ cfg = load_config_from_string(_yaml(task, "transformers"))
+
+ assert cfg.training.moe_lora is True and cfg.backend == "transformers"
+
+
+class TestTheTaskRefusalComesFirst:
+ @pytest.mark.parametrize("task", _TASK_REFUSED)
+ def test_one_refusal_whatever_the_backend(self, task):
+ on_unsloth = _refusal(task, "unsloth")
+
+ assert f"not applied by task={task!r}" in on_unsloth, on_unsloth
+ assert on_unsloth == _refusal(task, "transformers")
+
+ @pytest.mark.parametrize("modality", ["vision", "audio"])
+ def test_sft_vision_and_audio_too(self, modality):
+ on_unsloth = _refusal("sft", "unsloth", modality=modality)
+
+ assert f"modality={modality!r}" in on_unsloth, on_unsloth
+ assert on_unsloth == _refusal("sft", "transformers", modality=modality)
+
+ @pytest.mark.parametrize("task", ["classifier", "reranker", "cross_encoder"])
+ def test_the_classifier_family_without_its_adapter_too(self, task):
+ """Without classifier_lora the flag has nothing to select on any backend,
+ so that refusal must come before the unsloth one, whose advice would be false."""
+
+ def refusal(backend):
+ raw = yaml.safe_load(_yaml(task, backend))
+ del raw["training"]["classifier_lora"]
+ with pytest.raises(ValueError) as info:
+ load_config_from_string(yaml.safe_dump(raw))
+ return " ".join(str(info.value).split())
+
+ on_unsloth = refusal("unsloth")
+
+ assert f"not applied by task={task!r} unless" in on_unsloth, on_unsloth
+ assert on_unsloth == refusal("transformers")
+
+
+class TestTheSplitMatchesTheTrainers:
+ """The task lists in the schema must follow the trainers: a task whose wrapper
+ gains or loses ``_setup_unsloth`` has to move, or its message turns false."""
+
+ def test_every_task_is_classified(self):
+ tasks = set(typing.get_args(SoupConfig.model_fields["task"].annotation))
+
+ assert tasks == set(_WRAPPERS)
+ assert tasks == set(_WITH_UNSLOTH) | set(_WITHOUT_UNSLOTH) | set(_TASK_REFUSED)
+
+ @staticmethod
+ def _has_unsloth_setup(task):
+ if task == "preference":
+ return all(_TestHelpers.has_setup(t) for t in _PREFERENCE_DELEGATES)
+ return _TestHelpers.has_setup(task)
+
+ @pytest.mark.parametrize("task", sorted(_WRAPPERS))
+ def test_the_schema_list_matches_the_wrapper(self, task):
+ from soup_cli.config.schema import UNSLOTH_SETUP_TASKS
+
+ assert (task in UNSLOTH_SETUP_TASKS) == self._has_unsloth_setup(task)
+
+
+class _TestHelpers:
+ @staticmethod
+ def has_setup(task):
+ module, name = _WRAPPERS[task].split(":")
+ wrapper = getattr(importlib.import_module(f"soup_cli.trainer.{module}"), name)
+ return hasattr(wrapper, "_setup_unsloth")