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
fix(config): refuse training.expand_layers outside sft/pretrain on transformers text (#1390)
Kratos05b521d2b342
diff --git a/changelog.d/0.75.1/1390.fixed.md b/changelog.d/0.75.1/1390.fixed.mdnew file mode 100644index 00000000..d69d1a98--- /dev/null+++ b/changelog.d/0.75.1/1390.fixed.md@@ -0,0 +1 @@+- **`training.expand_layers` (LLaMA Pro) is refused at config load outside `task: sft` / `task: pretrain` on `backend: transformers` with `modality: text` (#1325 by @wanjinhao1 in #1390).** Block expansion is applied only by those two trainers' text setup, so every other task, `backend: unsloth` or `mlx`, and SFT with `modality: vision` or `audio` accepted the setting and trained the model without the new blocks, with no message. `task: pretrain` with a `modality` other than `text`, and `task: sft` with `modality: audio_out`, are refused too: those trainers ignore that `modality` and did expand the model, so set `modality: text` there. The error names the task, backend and modality it got; `task: sft` and `task: pretrain` on `backend: transformers` with `modality: text` load as before.diff --git a/docs/peft-and-efficiency.md b/docs/peft-and-efficiency.mdindex 307586cf..240a6430 100644--- a/docs/peft-and-efficiency.md+++ b/docs/peft-and-efficiency.md@@ -183,7 +183,7 @@ training:**What happens at trainer start.** Soup deep-copies the last `expand_layers` decoder blocks, zero-inits each clone's residual projections (`mlp.down_proj` + `self_attn.o_proj`) so the appended block initially acts as identity, appends them to `model.model.layers`, and updates `config.num_hidden_layers`. When `freeze_trainable_layers > 0` is set, every parameter except the appended blocks is frozen — this is the canonical LLaMA Pro "train only new blocks" recipe.-**Scope.** Works on both `task: sft` and `task: pretrain` with `backend: transformers`. Bounds: `expand_layers ∈ [1, 64]`. Over-expansion (more new blocks than the base has layers) silently clamps to the base layer count. Non-Llama-shaped architectures (e.g. Falcon's `dense_4h_to_h`) emit a `warnings.warn` because the residual zero-init heuristic only matches the standard `down_proj` / `o_proj` names — the appended blocks are still appended + trainable, but lose the identity-init guarantee.+**Scope.** Works on `task: sft` and `task: pretrain` with `backend: transformers` and `modality: text`; any other task, backend or modality is refused at config load, because no other trainer applies the expansion. Bounds: `expand_layers ∈ [1, 64]`. Over-expansion (more new blocks than the base has layers) silently clamps to the base layer count. Non-Llama-shaped architectures (e.g. Falcon's `dense_4h_to_h`) emit a `warnings.warn` because the residual zero-init heuristic only matches the standard `down_proj` / `o_proj` names — the appended blocks are still appended + trainable, but lose the identity-init guarantee.## Optimizer & PEFT Zoodiff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.pyindex 1c7d5f40..ea206d30 100644--- a/src/soup_cli/config/schema.py+++ b/src/soup_cli/config/schema.py@@ -4859,6 +4859,30 @@ class SoupConfig(BaseModel):"never happens. Remove it or set quantization: none.")+ @model_validator(mode="after")+ def _validate_expand_layers_scope(self) -> "SoupConfig":+ """#1325 — ``training.expand_layers`` (LLaMA Pro) is wired only in the+ transformers text SFT/pretrain ``_setup_transformers`` paths. Every+ other task/backend/modality accepts the fields and then silently+ trains the unexpanded model, so refuse at load with the actual+ task/backend/modality in the message."""+ tcfg = self.training+ if tcfg.expand_layers is None:+ return self+ if not (+ self.task in {"sft", "pretrain"}+ and self.backend == "transformers"+ and self.modality == "text"+ ):+ raise ValueError(+ "training.expand_layers (LLaMA Pro) is only wired for task "+ "sft/pretrain on backend transformers with modality text; got "+ f"task={self.task!r}, backend={self.backend!r}, "+ f"modality={self.modality!r}. Remove expand_layers and "+ "freeze_trainable_layers."+ )+ return self+@model_validator(mode="after")def _validate_quest_first_slice(self) -> "SoupConfig":"""Keep #674 on the one route supported by the measured prototype."""diff --git a/tests/test_issue1325_expand_layers_scope.py b/tests/test_issue1325_expand_layers_scope.pynew file mode 100644index 00000000..afa05e7f--- /dev/null+++ b/tests/test_issue1325_expand_layers_scope.py@@ -0,0 +1,98 @@+"""Issue #1325: ``training.expand_layers`` must refuse unsupported routes.++LLaMA Pro block expansion is applied only by the transformers text+SFT/pretrain ``_setup_transformers`` paths. Every other task, backend or+modality accepted the fields and trained without them; these tests pin the+load-time refusal and the two accepted routes.++Every refused config is first loaded WITHOUT ``expand_layers``, so a refusal+can only come from ``expand_layers`` and not from something else wrong with+the config. ``quantization: none`` keeps the scope refusal separate from the+quantized-base refusal (#1237).+"""++from __future__ import annotations++import typing++import pytest++from soup_cli.config.loader import load_config_from_string+from soup_cli.config.schema import SoupConfig++_SUPPORTED_TASKS = ("sft", "pretrain")+_ALL_TASKS = typing.get_args(SoupConfig.model_fields["task"].annotation)+_OTHER_TASKS = tuple(task for task in _ALL_TASKS if task not in _SUPPORTED_TASKS)++# The least each task needs to load on backend: transformers at all. A new task+# that needs more fails the baseline load below, which names it.+_ROOT = {"tts": "modality: audio_out\n"}+_DATA = {+ "asr": " format: asr\n",+ "unlearn": " forget_set: forget.jsonl\n retain_set: retain.jsonl\n",+}+_TRAINING = {+ "preference": " preference_loss: dpo\n",+ "online_dpo": " online_dpo_judge: https://judge.example.com/v1\n",+ "ppo": " reward_model: org/reward\n",+ "distill": " teacher_model: meta-llama/Llama-3.1-70B\n",+ "tts": " tts_family: orpheus\n",+ "classifier": " num_labels: 2\n",+ "reranker": " num_labels: 2\n",+ "cross_encoder": " num_labels: 2\n",+ "unlearn": " unlearn_method: npo\n",+ "moe_lora_routing": " mole_task_adapters:\n - ./a1\n - ./a2\n",+}+_EXPAND = " expand_layers: 2\n freeze_trainable_layers: 2\n"+++def _yaml(task, backend="transformers", modality=None, expand=True):+ root = f"modality: {modality}\n" if modality else _ROOT.get(task, "")+ return (+ "base: meta-llama/Llama-3.1-8B\n"+ f"task: {task}\n"+ f"backend: {backend}\n"+ f"{root}"+ "data:\n train: data.jsonl\n"+ + _DATA.get(task, "")+ + "training:\n quantization: none\n"+ + _TRAINING.get(task, "")+ + (_EXPAND if expand else "")+ )+++def test_the_task_list_comes_from_the_schema():+ assert len(_OTHER_TASKS) == len(_ALL_TASKS) - 2+ assert "dpo" in _OTHER_TASKS and "sft" not in _OTHER_TASKS++[email protected]("task", _OTHER_TASKS)+def test_refused_on_every_other_task(task):+ load_config_from_string(_yaml(task, expand=False))+ with pytest.raises(ValueError, match=rf"expand_layers.*task='{task}'"):+ load_config_from_string(_yaml(task))+++ "task,backend",+ [("sft", "unsloth"), ("pretrain", "unsloth"), ("sft", "mlx")],+)+def test_refused_off_transformers(task, backend):+ load_config_from_string(_yaml(task, backend=backend, expand=False))+ with pytest.raises(ValueError, match=rf"expand_layers.*backend='{backend}'"):+ load_config_from_string(_yaml(task, backend=backend))++[email protected]("modality", ["vision", "audio", "audio_out"])[email protected]("task", _SUPPORTED_TASKS)+def test_refused_off_text(task, modality):+ load_config_from_string(_yaml(task, modality=modality, expand=False))+ with pytest.raises(ValueError, match=rf"expand_layers.*modality='{modality}'"):+ load_config_from_string(_yaml(task, modality=modality))++[email protected]("task", _SUPPORTED_TASKS)+def test_supported_routes_still_load(task):+ cfg = load_config_from_string(_yaml(task, modality="text"))+ assert cfg.training.expand_layers == 2+ assert cfg.training.freeze_trainable_layers == 2