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 quantization 8bit on backend unsloth instead of loading 16-bit (#1946)

ndablizov964e10f8e3f0
diff --git a/changelog.d/0.76.0/1946.fixed.md b/changelog.d/0.76.0/1946.fixed.md
new file mode 100644
index 00000000..5665c757
--- /dev/null
+++ b/changelog.d/0.76.0/1946.fixed.md
@@ -0,0 +1 @@
+- `quantization: 8bit` with `backend: unsloth` is refused at config load, naming `4bit`, `none` and `backend: transformers` for 8-bit: Soup's Unsloth loader passes `load_in_4bit` only, so `8bit` loaded the model in 16-bit with no word (#1811 by @ndablizov in #1946)
diff --git a/docs/backends-and-ops.md b/docs/backends-and-ops.md
index e372861f..6d66768d 100644
--- a/docs/backends-and-ops.md
+++ b/docs/backends-and-ops.md
@@ -201,6 +201,8 @@ training:
Works with the tasks that have an unsloth setup: SFT (text; SFT vision and audio still run the transformers setup), DPO, GRPO, PPO, KTO, ORPO, SimPO, IPO, BCO, preference, Pretrain, Embedding and TTS. The others (`reward_model`, `prm`, `classifier`, `reranker`, `cross_encoder`, `distill`, `unlearn`, `moe_lora_routing`, `online_dpo` and `asr`) have no unsloth setup, so `backend: unsloth` is refused at config load for them (#1357). If unsloth is installed but not enabled, Soup suggests it, but only for a config that would still load with `backend: unsloth` (#1736): a config one of these refusals would stop gets no suggestion.
+`quantization: 8bit` with `backend: unsloth` is refused at config load: Soup's Unsloth loader passes `load_in_4bit` only, so `8bit` used to load the model in 16-bit with no word (#1811). Use `4bit` or `none` on this backend, or `backend: transformers` for 8-bit.
+
> **Tip:** Soup auto-detects unsloth. When it is installed, `soup train` prints a hint if you haven't enabled it yet and your config would load with it.
diff --git a/src/soup_cli/config/schema.py b/src/soup_cli/config/schema.py
index a2eab6ad..181773d7 100644
--- a/src/soup_cli/config/schema.py
+++ b/src/soup_cli/config/schema.py
@@ -7520,6 +7520,24 @@ class SoupConfig(BaseModel):
)
return self
+ @model_validator(mode="after")
+ def _validate_unsloth_8bit(self) -> "SoupConfig":
+ """#1811 — ``quantization: 8bit`` on ``backend: unsloth`` loaded in 16-bit.
+
+ ``utils/unsloth.py`` passes ``load_in_4bit`` and nothing else, and the
+ declared floor (``unsloth>=2024.8``) has no ``load_in_8bit`` to pass, so
+ ``8bit`` loaded exactly like ``none`` with no word. Refuse it, naming the
+ two values the Unsloth path applies.
+ """
+ if self.backend == "unsloth" and self.training.quantization == "8bit":
+ raise ValueError(
+ "training.quantization: 8bit is not applied on backend: unsloth "
+ "(Soup's Unsloth loader passes load_in_4bit only, so 8bit loaded the model "
+ "in 16-bit). Use quantization: 4bit or none with backend: unsloth, or "
+ "backend: transformers for 8-bit."
+ )
+ return self
+
@model_validator(mode="after")
def _validate_preference_dispatcher(self) -> "SoupConfig":
"""v0.40.0 Part B — task='preference' requires preference_loss OR
diff --git a/src/soup_cli/utils/unsloth.py b/src/soup_cli/utils/unsloth.py
index 6d622ff5..4a0bb62a 100644
--- a/src/soup_cli/utils/unsloth.py
+++ b/src/soup_cli/utils/unsloth.py
@@ -43,6 +43,14 @@ def load_model_and_tokenizer(
``alpha / r``. It is forwarded on every call so the config's value, not
Unsloth's default, decides (#1853: the flag loaded and was never passed).
"""
+ if quantization == "8bit":
+ # #1811: the config refuses this pairing; name it here too rather than
+ # loading the model in 16-bit if the loader is ever reached another way.
+ raise ValueError(
+ "quantization 8bit is not supported by the Unsloth loader "
+ "(it passes load_in_4bit only); use 4bit or none"
+ )
+
from unsloth import FastLanguageModel
load_in_4bit = quantization == "4bit"
diff --git a/tests/test_unsloth_8bit_refused.py b/tests/test_unsloth_8bit_refused.py
new file mode 100644
index 00000000..500bc3d5
--- /dev/null
+++ b/tests/test_unsloth_8bit_refused.py
@@ -0,0 +1,67 @@
+"""#1811 — ``quantization: 8bit`` on ``backend: unsloth`` is refused, not loaded in 16-bit.
+
+``utils/unsloth.py`` computed ``load_in_4bit = quantization == "4bit"`` and passed
+nothing else, so ``8bit`` loaded exactly like ``none``: a full 16-bit model, with
+no word, on a config the schema accepted. The declared floor is
+``unsloth>=2024.8``, whose ``FastLanguageModel.from_pretrained`` has no
+``load_in_8bit``, so the value cannot be passed through; the config is refused at
+load instead, naming the two values the Unsloth path does apply.
+"""
+
+from __future__ import annotations
+
+import sys
+import types
+
+import pytest
+
+from soup_cli.config.loader import load_config_from_string
+
+
+def _yaml(task: str, quantization: str, backend: str = "unsloth") -> str:
+ return (
+ f"base: meta-llama/Llama-3.1-8B\ntask: {task}\nbackend: {backend}\n"
+ f"data:\n train: d.jsonl\ntraining:\n quantization: {quantization}\n"
+ )
+
+
[email protected]("task", ["sft", "dpo", "grpo"])
+def test_8bit_on_unsloth_is_refused_naming_what_applies(task):
+ with pytest.raises(ValueError) as excinfo:
+ load_config_from_string(_yaml(task, "8bit"))
+ message = " ".join(str(excinfo.value).split())
+ assert "quantization: 8bit" in message
+ assert "backend: unsloth" in message
+ assert "4bit" in message and "none" in message
+
+
[email protected]("quantization", ["4bit", "none"])
+def test_control_the_values_unsloth_applies_still_load(quantization):
+ cfg = load_config_from_string(_yaml("sft", quantization))
+ assert (cfg.backend, cfg.training.quantization) == ("unsloth", quantization)
+
+
+def test_control_8bit_on_transformers_still_loads():
+ cfg = load_config_from_string(_yaml("sft", "8bit", backend="transformers"))
+ assert cfg.training.quantization == "8bit"
+
+
+def test_the_loader_refuses_8bit_rather_than_loading_16bit(monkeypatch):
+ # The config refusal is the gate; the loader names the same thing if it is
+ # ever reached another way, instead of loading the model in 16-bit.
+ seen = []
+ fast = types.SimpleNamespace(
+ from_pretrained=lambda **kw: seen.append(kw) or (object(), object()),
+ get_peft_model=lambda model, **kw: model,
+ )
+ monkeypatch.setitem(sys.modules, "unsloth", types.SimpleNamespace(FastLanguageModel=fast))
+ from soup_cli.utils.unsloth import load_model_and_tokenizer
+
+ with pytest.raises(ValueError, match="8bit"):
+ load_model_and_tokenizer("m", 512, quantization="8bit")
+ assert seen == []
+
+ load_model_and_tokenizer("m", 512, quantization="4bit")
+ assert seen[-1]["load_in_4bit"] is True
+ load_model_and_tokenizer("m", 512, quantization="none")
+ assert seen[-1]["load_in_4bit"] is False