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 freeze_layers / freeze_ratio on tasks that do not apply them (#1497) (#1532)

Abdulwaarith Zakariyyac40c65870356
diff --git a/changelog.d/0.75.1/1532.fixed.md b/changelog.d/0.75.1/1532.fixed.md
new file mode 100644
index 00000000..86717a57
--- /dev/null
+++ b/changelog.d/0.75.1/1532.fixed.md
@@ -0,0 +1 @@
+- **`training.freeze_layers` / `training.freeze_ratio` are refused at load on the tasks that never applied them (#1497 by @abdulwaarith0 in #1532).** Both fields loaded on every task, but only `task: sft` (and `tts`, which trains through the SFT trainer) freezes layers. On every other task (`dpo`, `grpo`, `ppo`, `kto`, `orpo`, `simpo`, `ipo`, `bco`, `preference`, `reward_model`, `pretrain`, `embedding`, `prm`, `classifier`, `reranker`, `cross_encoder`, `distill`, `unlearn`, `moe_lora_routing`, `online_dpo`, `asr`) the run trained every layer and said nothing. **This is a breaking change: a config that sets either field on one of those tasks loaded before and now stops at load**, with a message naming the field, the task and the fix (use `task: sft`, or remove the key), and `soup doctor --config` exits 2. Removing the key changes nothing about what trains, since those runs were already training every layer. `sft` and `tts` configs, and dumped configs (which write both fields as `null`), are unchanged.
diff --git a/docs/peft-and-efficiency.md b/docs/peft-and-efficiency.md
index f5aad55e..d683ea45 100644
--- a/docs/peft-and-efficiency.md
+++ b/docs/peft-and-efficiency.md
@@ -511,7 +511,11 @@ training:
## Freeze Training
-Freeze bottom layers of the model — train only the top layers (like LLaMA-Factory's `finetuning_type: freeze`):
+Freeze bottom layers of the model — train only the top layers (like LLaMA-Factory's `finetuning_type: freeze`).
+Only `task: sft` (and `tts`, which trains through the SFT trainer) applies these two fields; on any
+other task the config is refused at load, because that trainer would train every layer (#1497).
+Within `sft` they are applied on the transformers text path only: `backend: mlx`, `backend: unsloth`,
+`modality: vision`, `modality: audio` and `stream_layers: true` still load them and train every layer.
```yaml
training:
diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.py
index f9594144..fef91c82 100644
--- a/src/soup_cli/config/schema.py
+++ b/src/soup_cli/config/schema.py
@@ -3351,13 +3351,21 @@ class TrainingConfig(BaseModel):
default=None,
ge=1,
le=1000,
- description="Freeze first N layers (from bottom). Train only remaining layers.",
+ description=(
+ "Freeze first N layers (from bottom). Train only remaining layers. "
+ "Applied by tasks sft and tts (on the transformers text path); "
+ "refused on the other tasks."
+ ),
)
freeze_ratio: Optional[float] = Field(
default=None,
gt=0.0,
lt=1.0,
- description="Freeze this fraction of layers (0.75 = freeze 75% from bottom).",
+ description=(
+ "Freeze this fraction of layers (0.75 = freeze 75% from bottom). "
+ "Applied by tasks sft and tts (on the transformers text path); "
+ "refused on the other tasks."
+ ),
)
# v0.71.23 #266 — Spectrum targeted training: full FT of selected params
unfrozen_parameters: Optional[List[str]] = Field(
@@ -4769,6 +4777,11 @@ UNSLOTH_SETUP_TASKS: frozenset[str] = frozenset({
"pretrain", "embedding", "tts",
})
+#: #1497 — the tasks whose trainer reads ``training.freeze_layers`` /
+#: ``training.freeze_ratio``: ``SFTTrainerWrapper._setup_transformers``, which
+#: ``tts`` inherits. Every other trainer ignores both, so they are refused there.
+FREEZE_APPLYING_TASKS: frozenset[str] = frozenset({"sft", "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"})
@@ -7943,6 +7956,30 @@ class SoupConfig(BaseModel):
)
return self
+ @model_validator(mode="after")
+ def _validate_freeze_is_applied(self) -> "SoupConfig":
+ """#1497 — ``freeze_layers`` / ``freeze_ratio`` loaded on every task, but
+ only the trainers in :data:`FREEZE_APPLYING_TASKS` freeze anything: on the
+ others the run trained every layer without a word. Placed after the #795
+ resolver and before the #1357 backend refusal, which stays last."""
+ if self.task in FREEZE_APPLYING_TASKS:
+ return self
+ tcfg = self.training
+ fields = [
+ f"training.{name}"
+ for name in ("freeze_layers", "freeze_ratio")
+ if getattr(tcfg, name) is not None
+ ]
+ if not fields:
+ return self
+ named = " and ".join(fields)
+ verb, keys = ("are", "both keys") if len(fields) > 1 else ("is", "the key")
+ raise ValueError(
+ f"{named} {verb} not applied by task={self.task!r}: only task='sft' (and "
+ "'tts', which trains through the SFT trainer) freezes layers, so this run "
+ f"would train every layer. Use task: sft, or remove {keys}."
+ )
+
@model_validator(mode="after")
def _validate_unsloth_has_a_setup(self) -> "SoupConfig":
"""#1357 — ``backend: unsloth`` on a task outside
diff --git a/tests/test_issue1497_freeze_refused_outside_sft.py b/tests/test_issue1497_freeze_refused_outside_sft.py
new file mode 100644
index 00000000..7de8e947
--- /dev/null
+++ b/tests/test_issue1497_freeze_refused_outside_sft.py
@@ -0,0 +1,251 @@
+"""#1497 — ``freeze_layers`` / ``freeze_ratio`` are refused at load where no trainer applies them.
+
+Both fields loaded on every task, but only ``SFTTrainerWrapper._setup_transformers``
+reads them (``tts`` inherits it). A ``freeze_layers: 2`` on a DPO or GRPO run
+trained every layer and said nothing. The config now stops at load, naming the
+field, the task and the way out. The split is ``FREEZE_APPLYING_TASKS``, and
+:class:`TestTheSplitFollowsTheTrainers` ratchets it against the wrappers that
+``trainer/dispatch.py`` builds, so a trainer that starts (or stops) reading the
+fields has to move its task.
+"""
+
+from __future__ import annotations
+
+import ast
+import importlib
+import inspect
+import typing
+from pathlib import Path
+
+import pytest
+import yaml
+
+from soup_cli.config.loader import load_config_from_string
+from soup_cli.config.schema import FREEZE_APPLYING_TASKS, SoupConfig
+
+_TASKS = tuple(typing.get_args(SoupConfig.model_fields["task"].annotation))
+_REFUSED = tuple(t for t in _TASKS if t not in FREEZE_APPLYING_TASKS)
+
+# The minimum each task needs to load at all (as in the #1357 tests).
+_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}, "reranker": {"num_labels": 2},
+ "cross_encoder": {"num_labels": 2},
+ "unlearn": {"unlearn_method": "npo"},
+ "moe_lora_routing": {"mole_task_adapters": ["./a", "./b"]},
+ "distill": {"teacher_model": "org/t"},
+ "online_dpo": {"online_dpo_judge": "pairrm"},
+}
+# preference builds one of these per preference_loss (trainer/preference.py).
+_PREFERENCE_DELEGATES = ("dpo", "simpo", "orpo", "ipo", "bco")
+
+
+def _yaml(task, **training):
+ raw = {
+ "base": "org/m", "task": task, **_TOP.get(task, {}),
+ "data": {"train": "./x.jsonl", **_DATA.get(task, {})},
+ "training": {**_TRAINING.get(task, {}), **training},
+ }
+ return yaml.safe_dump(raw)
+
+
+def _refusal(text):
+ with pytest.raises(ValueError) as info:
+ load_config_from_string(text)
+ return " ".join(str(info.value).split())
+
+
+_ONE_FIELD = [("freeze_layers", 2), ("freeze_ratio", 0.5)]
+
+
+class TestRefusedWhereNothingAppliesIt:
+ @pytest.mark.parametrize("task", _REFUSED)
+ @pytest.mark.parametrize(("field", "value"), _ONE_FIELD)
+ def test_the_message_names_the_field_the_task_and_the_fix(self, task, field, value):
+ message = _refusal(_yaml(task, **{field: value}))
+
+ assert f"training.{field} is not applied by task={task!r}" in message, message
+ assert "would train every layer" in message
+ assert "Use task: sft, or remove the key." in message
+
+ def test_both_fields_are_named_in_one_refusal(self):
+ message = _refusal(_yaml("dpo", freeze_layers=2, freeze_ratio=0.5))
+
+ assert (
+ "training.freeze_layers and training.freeze_ratio are not applied by "
+ "task='dpo'"
+ ) in message, message
+ assert "remove both keys" in message
+
+ @pytest.mark.parametrize("task", _REFUSED)
+ def test_without_the_fields_the_task_still_loads(self, task):
+ cfg = load_config_from_string(_yaml(task))
+
+ assert cfg.training.freeze_layers is None
+ assert cfg.training.freeze_ratio is None
+
+ @pytest.mark.parametrize("backend", ["transformers", "unsloth"])
+ @pytest.mark.parametrize("task", ["dpo", "reward_model"])
+ def test_the_refusal_does_not_depend_on_the_backend(self, task, backend):
+ raw = yaml.safe_load(_yaml(task, freeze_layers=2))
+ raw["backend"] = backend
+
+ message = _refusal(yaml.safe_dump(raw))
+
+ assert f"training.freeze_layers is not applied by task={task!r}" in message, message
+
+ @pytest.mark.parametrize("task", ["dpo", "pretrain", "kto", "orpo", "grpo"])
+ def test_the_issue_reproduction_is_refused(self, task):
+ """The issue's table: ``freeze_layers: 2``, ``quantization: none`` and a LoRA
+ block loaded on each of these and was ignored."""
+ message = _refusal(
+ _yaml(task, freeze_layers=2, quantization="none", lora={"r": 8})
+ )
+
+ assert f"task={task!r}" in message, message
+
+
+class TestTheApplyingTasksAreUnchanged:
+ # Written out, not read from FREEZE_APPLYING_TASKS, so dropping a task from
+ # the constant cannot also drop its control.
+ @pytest.mark.parametrize("task", ["sft", "tts"])
+ @pytest.mark.parametrize(("field", "value"), _ONE_FIELD)
+ def test_still_loads(self, task, field, value):
+ cfg = load_config_from_string(_yaml(task, **{field: value}))
+
+ assert getattr(cfg.training, field) == value
+
+ def test_sft_with_lora_r_zero_still_loads(self):
+ """#341: ``freeze_layers`` stays legal with full fine-tuning on sft."""
+ cfg = load_config_from_string(
+ _yaml("sft", freeze_layers=1, quantization="none", lora={"r": 0})
+ )
+
+ assert cfg.training.freeze_layers == 1
+
+
+class TestSoupDoctorReportsIt:
+ def test_doctor_exits_2_naming_the_task(self, tmp_path):
+ from typer.testing import CliRunner
+
+ from soup_cli.cli import app
+ from tests.conftest import strip_ansi
+
+ path = tmp_path / "soup.yaml"
+ path.write_text(_yaml("dpo", freeze_layers=2), encoding="utf-8")
+
+ result = CliRunner().invoke(app, ["doctor", "--config", str(path)])
+
+ assert result.exit_code == 2, result.output
+ out = " ".join(strip_ansi(result.output).split())
+ assert "training.freeze_layers is not applied by task='dpo'" in out
+
+
+class TestValidatorOrder:
+ def test_it_sits_before_the_backend_refusal_and_after_the_resolver(self):
+ """#795's resolver stays first and #1357's backend refusal stays last; this
+ one refuses on every backend, so it belongs in front of that."""
+ validators = SoupConfig.__pydantic_decorators__.model_validators
+ after = [name for name, dec in validators.items() if dec.info.mode == "after"]
+
+ assert after[0] == "_resolve_quantization_for_unhonouring_tasks"
+ assert (
+ 0
+ < after.index("_validate_freeze_is_applied")
+ < after.index("_validate_unsloth_has_a_setup")
+ ), after[-3:]
+
+
+def _dispatched_wrappers():
+ """task -> wrapper class, read from the if-chain in ``trainer/dispatch.py``.
+
+ Read from the source rather than listed here, so a task added to the
+ dispatcher is scanned without anyone remembering this file.
+ """
+ from soup_cli.trainer import dispatch
+
+ tree = ast.parse(Path(inspect.getsourcefile(dispatch)).read_text(encoding="utf-8"))
+ found = {}
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.If) or not isinstance(node.test, ast.Compare):
+ continue
+ test = node.test
+ if not (isinstance(test.left, ast.Name) and test.left.id == "task"):
+ continue
+ right = test.comparators[0]
+ if isinstance(test.ops[0], ast.Eq) and isinstance(right, ast.Constant):
+ tasks = [right.value]
+ elif isinstance(test.ops[0], ast.In) and isinstance(right, ast.Tuple):
+ tasks = [elt.value for elt in right.elts]
+ else:
+ continue
+ imports = [n for n in node.body if isinstance(n, ast.ImportFrom)]
+ assert len(imports) == 1, f"expected one import under task={tasks}"
+ module = importlib.import_module(imports[0].module)
+ cls = getattr(module, imports[0].names[0].name)
+ for task in tasks:
+ found[task] = cls
+ return found
+
+
+def _reads_the_freeze_fields(cls):
+ """True when the wrapper, or a soup class it inherits from, reads either field
+ as an attribute. Comments and docstrings do not count.
+
+ The limit of this scan: a trainer that gets the freeze plan through a shared
+ helper outside its own classes, or through ``getattr(tcfg, "freeze_layers")``,
+ is not seen by it."""
+ for klass in cls.__mro__:
+ if not klass.__module__.startswith("soup_cli."):
+ continue
+ tree = ast.parse(inspect.getsource(inspect.getmodule(klass)))
+ for node in ast.walk(tree):
+ if isinstance(node, ast.ClassDef) and node.name == klass.__name__:
+ for sub in ast.walk(node):
+ if isinstance(sub, ast.Attribute) and sub.attr in (
+ "freeze_layers", "freeze_ratio",
+ ):
+ return True
+ return False
+
+
+class TestTheSplitFollowsTheTrainers:
+ """``FREEZE_APPLYING_TASKS`` must follow the trainers: a wrapper that reads the
+ fields must not have its task refused, and a wrapper that does not must."""
+
+ def test_every_task_has_a_wrapper(self):
+ assert set(_dispatched_wrappers()) == set(_TASKS)
+
+ @pytest.mark.parametrize("task", _TASKS)
+ def test_the_schema_list_matches_the_wrapper(self, task):
+ wrappers = _dispatched_wrappers()
+ if task == "preference":
+ applies = all(_reads_the_freeze_fields(wrappers[t]) for t in _PREFERENCE_DELEGATES)
+ else:
+ applies = _reads_the_freeze_fields(wrappers[task])
+
+ assert (task in FREEZE_APPLYING_TASKS) == applies, (
+ f"task={task!r}: its trainer {'reads' if applies else 'does not read'} "
+ "freeze_layers / freeze_ratio; move it in or out of FREEZE_APPLYING_TASKS"
+ )
+
+ def test_the_scan_sees_the_sft_call_site(self):
+ """Guard on the scan itself: if it stopped seeing SFT's read, every task
+ would look refusable and the test above would still be consistent."""
+ assert _reads_the_freeze_fields(_dispatched_wrappers()["sft"])
+ assert not _reads_the_freeze_fields(_dispatched_wrappers()["dpo"])
+
+
+def test_a_dumped_config_still_reloads():
+ """A dumped config writes both fields out as null; that is not a request."""
+ cfg = load_config_from_string(_yaml("dpo"))
+ dumped = yaml.safe_dump(cfg.model_dump(mode="json"))
+
+ assert "freeze_layers: null" in dumped
+ assert load_config_from_string(dumped).task == "dpo"