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 LoRA+ with model-only optimizers and GaLore at load (#1210) (#1255)

Mohd Uwaish12807838df16
diff --git a/changelog.d/0.75.1/1255.fixed.md b/changelog.d/0.75.1/1255.fixed.md
new file mode 100644
index 00000000..98133f53
--- /dev/null
+++ b/changelog.d/0.75.1/1255.fixed.md
@@ -0,0 +1 @@
+- **`training.loraplus_lr_ratio` with `optimizer: apollo_adamw`, `lomo` or `adalomo`, or with `use_galore`, is now refused at config load (#1210 by @mohdUwaish59 in #1255).** LoRA+ asks transformers for its optimizer without the model, and transformers can only build those three optimizers from one, so such a config passed `soup train`'s config load and failed only after the base model had been downloaded and loaded. LoRA+ with GaLore was likewise refused only at setup. Both now fail at load with a message that names the two fields.
diff --git a/docs/peft-and-efficiency.md b/docs/peft-and-efficiency.md
index bcb5a7ba..f5aad55e 100644
--- a/docs/peft-and-efficiency.md
+++ b/docs/peft-and-efficiency.md
@@ -408,6 +408,7 @@ training:
```
- **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`. `backend: unsloth` gets the same LoRA+ optimizer as `transformers`. Mutually exclusive with `use_lorafa`.
+- **Optimizers:** LoRA+ builds its optimizer without the model, so it is refused at config load with `optimizer: apollo_adamw`, `lomo` or `adalomo` (transformers can only build those from the model) and with `use_galore`.
## LoRA-FA (Frozen-A LoRA)
diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.py
index 2d90db15..0cb8f45a 100644
--- a/src/soup_cli/config/schema.py
+++ b/src/soup_cli/config/schema.py
@@ -4204,6 +4204,34 @@ class TrainingConfig(BaseModel):
)
return self
+ @model_validator(mode="after")
+ def _validate_loraplus_compat(self) -> "TrainingConfig":
+ """#1210 — refuse LoRA+ combinations that could only fail after the model loads.
+
+ build_loraplus_optimizer (called by attach_loraplus_optimizer and by PPO's
+ constructor injection) asks Trainer.get_optimizer_cls_and_kwargs(args) for the
+ optimizer without a model, and transformers builds these three only from one.
+ """
+ if self.loraplus_lr_ratio is None:
+ return self
+ if self.optimizer in ("apollo_adamw", "lomo", "adalomo"):
+ raise ValueError(
+ f"training.loraplus_lr_ratio cannot be combined with "
+ f"training.optimizer={self.optimizer!r}: LoRA+ builds its optimizer "
+ f"without the model, and transformers can only build {self.optimizer} "
+ f"from the model. Use an optimizer transformers can build without the "
+ f"model (for example adamw_torch) or remove loraplus_lr_ratio."
+ )
+ # build_loraplus_optimizer also refuses this at runtime; here it fails
+ # before the model is downloaded.
+ if self.use_galore:
+ raise ValueError(
+ "training.loraplus_lr_ratio and training.use_galore are mutually exclusive: "
+ "LoRA+ tunes LoRA A/B matrices while GaLore projects full-parameter "
+ "gradients. Enable one, not both."
+ )
+ return self
+
# ---- v0.61.0 Part A — Unlearning ---------------------------------------
# Schema-only release: validators here are reused by the SoupConfig
# cross-validator + UnlearnTrainerWrapper. Live trainer in v0.61.1.
diff --git a/tests/test_issue1210_loraplus_model_only_optimizers.py b/tests/test_issue1210_loraplus_model_only_optimizers.py
new file mode 100644
index 00000000..f3a2ac57
--- /dev/null
+++ b/tests/test_issue1210_loraplus_model_only_optimizers.py
@@ -0,0 +1,87 @@
+"""#1210 — LoRA+ with an optimizer transformers can only build from the model.
+
+`build_loraplus_optimizer` (called by `attach_loraplus_optimizer` and by PPO's constructor
+injection) asks `Trainer.get_optimizer_cls_and_kwargs(args)` for the optimizer without a
+model. For apollo_adamw, lomo and adalomo transformers needs one,
+so such a config passed load and failed only after the base model had loaded. It is
+now refused at config load, as is LoRA+ with GaLore.
+
+Every case runs under several tasks: the refusal is a TrainingConfig check and must not
+depend on the task, which a task-scoped refactor could otherwise break unnoticed.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from soup_cli.config.loader import load_config_from_string
+
+_BASE = """
+base: some-org/some-model
+task: {task}
+data:
+ train: ./train.jsonl
+training:
+ lora:
+ r: 8
+ alpha: 16
+"""
+
+_TASKS = ["sft", "dpo", "ppo"]
+
+
+def _config(task: str, **training: object) -> str:
+ lines = "".join(f" {key}: {value}\n" for key, value in training.items())
+ return _BASE.format(task=task) + lines
+
+
[email protected]("task", _TASKS)
[email protected]("optimizer", ["apollo_adamw", "lomo", "adalomo"])
+def test_loraplus_with_a_model_only_optimizer_is_refused_at_load(task, optimizer):
+ with pytest.raises(ValueError) as caught:
+ load_config_from_string(_config(task, loraplus_lr_ratio=16.0, optimizer=optimizer))
+ message = str(caught.value)
+ assert "training.loraplus_lr_ratio" in message
+ assert f"training.optimizer={optimizer!r}" in message
+
+
[email protected]("task", _TASKS)
+def test_optimizer_name_is_matched_case_insensitively(task):
+ with pytest.raises(ValueError, match="training.loraplus_lr_ratio"):
+ load_config_from_string(_config(task, loraplus_lr_ratio=16.0, optimizer="LOMO"))
+
+
[email protected]("task", _TASKS)
+def test_loraplus_with_galore_is_refused_at_load(task):
+ with pytest.raises(ValueError) as caught:
+ load_config_from_string(_config(task, loraplus_lr_ratio=16.0, use_galore="true"))
+ message = str(caught.value)
+ assert "training.loraplus_lr_ratio" in message
+ assert "training.use_galore" in message
+
+
[email protected]("task", _TASKS)
+ "training",
+ [
+ {"loraplus_lr_ratio": 16.0},
+ {"loraplus_lr_ratio": 16.0, "optimizer": "adamw_torch"},
+ {"loraplus_lr_ratio": 16.0, "optimizer": "adamw_8bit"},
+ {"optimizer": "apollo_adamw"},
+ {"optimizer": "lomo"},
+ {"optimizer": "adalomo"},
+ {"use_galore": "true"},
+ ],
+ ids=[
+ "loraplus-default-optimizer",
+ "loraplus-adamw_torch",
+ "loraplus-adamw_8bit",
+ "apollo_adamw-alone",
+ "lomo-alone",
+ "adalomo-alone",
+ "galore-alone",
+ ],
+)
+def test_each_setting_alone_still_loads(task, training):
+ # Controls: only the combination is refused.
+ load_config_from_string(_config(task, **training))