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): refuse unapplied moe_aux_loss_coeff on unsloth and stream_layers (#1412) (#1749)

Nurkhan Esenbekb8d86e50e90c
diff --git a/changelog.d/0.76.0/1749.fixed.md b/changelog.d/0.76.0/1749.fixed.md
new file mode 100644
index 00000000..f0460bf5
--- /dev/null
+++ b/changelog.d/0.76.0/1749.fixed.md
@@ -0,0 +1 @@
+- `training.moe_aux_loss_coeff` set to a non-default value on `backend: unsloth` (sft, pretrain) or with `training.stream_layers: true` is now refused at config load instead of loading and being ignored: neither setup applies the MoE auxiliary loss (#1412 by @kok-o in #1749).
diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.md
index cc359852..35d683e5 100644
--- a/docs/performance-and-quantization.md
+++ b/docs/performance-and-quantization.md
@@ -1291,7 +1291,7 @@ Both reject silently-no-op combinations: setting either flag without `moe_lora=t
|---|---|---|
| `moe_lora` | `sft`, `pretrain`, `tts`, (since #798) `dpo`, `kto`, `orpo`, `simpo`, `grpo`, (since #1099) `ipo`, `bco`, `reward_model`, `ppo`, `embedding`, `online_dpo`, and (since #1151) `distill`, `unlearn`, and `classifier`, `reranker`, `cross_encoder` with `classifier_lora: true` | refused at config load, naming the reason, on `asr`, `moe_lora_routing`, `prm`, the classifier family without `classifier_lora: true` and `lora.r > 0`, `backend: unsloth` (any task), and `task: sft` with `modality: vision` or `audio` |
| `moe_expert_quant`, `train_router_only` | `sft`, `tts` | refused at config load, naming the task |
-| `moe_aux_loss_coeff` | `sft` (text), `tts`, `pretrain` | a **non-default** value is refused, including on `task: sft` with `modality: vision` or `audio` (#1394), whose setup never applies the auxiliary loss; the default `0.01` still loads, because every stored config and the shipped MoE recipes on those tasks write it |
+| `moe_aux_loss_coeff` | `sft` (text), `tts`, `pretrain` | a **non-default** value is refused, including on `task: sft` with `modality: vision` or `audio` (#1394), on `backend: unsloth`, and with `training.stream_layers: true` (#1412), whose setups never apply the auxiliary loss; the default `0.01` still loads, because every stored config and the shipped MoE recipes on those tasks 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: `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.
diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.py
index ead5ac37..0a73be01 100644
--- a/src/soup_cli/config/schema.py
+++ b/src/soup_cli/config/schema.py
@@ -6058,9 +6058,9 @@ class SoupConfig(BaseModel):
f"(trainer/sft.py), so it would be stored and never take "
f"effect. Remove it, or use one of those tasks."
)
+ default_aux = type(tcfg).model_fields["moe_aux_loss_coeff"].default
if self.task not in _MOE_AUX_LOSS_TASKS:
- default = type(tcfg).model_fields["moe_aux_loss_coeff"].default
- if tcfg.moe_aux_loss_coeff != default:
+ if tcfg.moe_aux_loss_coeff != default_aux:
raise ValueError(
f"training.moe_aux_loss_coeff={tcfg.moe_aux_loss_coeff!r} is "
f"not applied by task={self.task!r}: only "
@@ -6072,14 +6072,29 @@ class SoupConfig(BaseModel):
# #1394: only SFT's text setup reads it. The vision and audio setups
# build their model without the MoE step, the same gap #1179 closed
# for moe_lora on these paths.
- default = type(tcfg).model_fields["moe_aux_loss_coeff"].default
- if tcfg.moe_aux_loss_coeff != default:
+ if tcfg.moe_aux_loss_coeff != default_aux:
raise ValueError(
f"training.moe_aux_loss_coeff={tcfg.moe_aux_loss_coeff!r} is "
f"not applied by task='sft' with modality={self.modality!r}: "
"that setup never applies the MoE auxiliary loss. Remove it, "
"or train with modality: text."
)
+ if self.backend == "unsloth":
+ # #1412: unsloth loader never applies the MoE auxiliary loss coefficient.
+ if tcfg.moe_aux_loss_coeff != default_aux:
+ raise ValueError(
+ f"training.moe_aux_loss_coeff={tcfg.moe_aux_loss_coeff!r} is "
+ "not applied on backend='unsloth': that setup never applies "
+ "the MoE auxiliary loss. Remove it, or train with backend: transformers."
+ )
+ if tcfg.stream_layers:
+ # #1412: layer streaming setup never sets the router coefficient.
+ if tcfg.moe_aux_loss_coeff != default_aux:
+ raise ValueError(
+ f"training.moe_aux_loss_coeff={tcfg.moe_aux_loss_coeff!r} is "
+ "not applied with training.stream_layers: that setup never "
+ "applies the MoE auxiliary loss. Remove it, or train without stream_layers."
+ )
return self
@model_validator(mode="after")
diff --git a/tests/test_issue1412_moe_aux_loss_unsloth_stream.py b/tests/test_issue1412_moe_aux_loss_unsloth_stream.py
new file mode 100644
index 00000000..7f9c89a2
--- /dev/null
+++ b/tests/test_issue1412_moe_aux_loss_unsloth_stream.py
@@ -0,0 +1,67 @@
+"""#1412 - training.moe_aux_loss_coeff refused on backend: unsloth and stream_layers.
+
+_setup_unsloth (sft, pretrain) and _setup_streaming_transformers never apply
+the MoE router auxiliary loss coefficient, so non-default values are refused
+at config load.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from soup_cli.config.loader import load_config_from_string
+
+
+def _yaml(
+ task: str = "sft",
+ backend: str = "transformers",
+ stream_layers: bool = False,
+ coeff: str | None = None,
+) -> str:
+ lines = ["base: some/model", f"task: {task}"]
+ if backend != "transformers":
+ lines.append(f"backend: {backend}")
+ lines += ["data:", " train: ./data.jsonl", " format: auto"]
+ training_lines = []
+ if stream_layers:
+ training_lines += [" stream_layers: true", " batch_size: 1"]
+ if coeff is not None:
+ training_lines.append(f" moe_aux_loss_coeff: {coeff}")
+ if training_lines:
+ lines.append("training:")
+ lines.extend(training_lines)
+ return "\n".join(lines) + "\n"
+
+
[email protected]("task", ["sft", "pretrain"])
+def test_non_default_coeff_refused_on_backend_unsloth(task: str) -> None:
+ with pytest.raises(ValueError, match=r"not applied on backend='unsloth'") as exc:
+ load_config_from_string(_yaml(task=task, backend="unsloth", coeff="0.05"))
+ assert "moe_aux_loss_coeff" in str(exc.value)
+ assert "backend: transformers" in str(exc.value)
+
+
[email protected]("task", ["sft", "pretrain"])
[email protected]("coeff", [None, "0.01"])
+def test_default_coeff_still_loads_on_backend_unsloth(task: str, coeff: str | None) -> None:
+ cfg = load_config_from_string(_yaml(task=task, backend="unsloth", coeff=coeff))
+ assert cfg.training.moe_aux_loss_coeff == 0.01
+
+
+def test_non_default_coeff_refused_with_stream_layers() -> None:
+ with pytest.raises(ValueError, match=r"not applied with training\.stream_layers") as exc:
+ load_config_from_string(_yaml(task="sft", stream_layers=True, coeff="0.05"))
+ assert "moe_aux_loss_coeff" in str(exc.value)
+ assert "stream_layers" in str(exc.value)
+
+
[email protected]("coeff", [None, "0.01"])
+def test_default_coeff_still_loads_with_stream_layers(coeff: str | None) -> None:
+ cfg = load_config_from_string(_yaml(task="sft", stream_layers=True, coeff=coeff))
+ assert cfg.training.moe_aux_loss_coeff == 0.01
+
+
[email protected]("task", ["sft", "pretrain"])
+def test_transformers_backend_keeps_non_default_coeff(task: str) -> None:
+ cfg = load_config_from_string(_yaml(task=task, backend="transformers", coeff="0.05"))
+ assert cfg.training.moe_aux_loss_coeff == 0.05