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(train): the VRAM pre-flight checks mxfp4 runs (#1631) (#1655)
Yang Bai0295f68873cc
diff --git a/changelog.d/0.75.1/1558.fixed.md b/changelog.d/0.75.1/1558.fixed.mdindex b931b9b0..8a4047d7 100644--- a/changelog.d/0.75.1/1558.fixed.md+++ b/changelog.d/0.75.1/1558.fixed.md@@ -13,5 +13,4 @@`config.json` does not declare `quant_method: mxfp4`.The hardware-fit calculator (`soup_cli.utils.hardware_fit`) now prices an`mxfp4` base at 2 bytes per parameter, as the bf16 model it loads (it was- 0.55). The `soup train` pre-flight does not check `mxfp4` runs, so budget- the memory yourself: about 42 GB for a 20B base.+ 0.55).diff --git a/changelog.d/0.75.1/1655.fixed.md b/changelog.d/0.75.1/1655.fixed.mdnew file mode 100644index 00000000..30898e8c--- /dev/null+++ b/changelog.d/0.75.1/1655.fixed.md@@ -0,0 +1,10 @@+- **The `soup train` VRAM pre-flight skipped `training.quantization: mxfp4`+ (#1631 by @ybai08 in #1655).** An MXFP4 base is dequantized on load and+ trains in bf16, but the pre-flight built its estimate only for `none`,+ `4bit` and `8bit`, so an `mxfp4` run was never checked. It is now checked+ against the same estimate as a `quantization: none` run, about 2 bytes per+ parameter, and a run that does not fit is refused with one extra sentence+ saying the estimate prices the bf16 model. This refuses `mxfp4` configs that+ used to start on a card too small for the model; `--allow-oom-attempt`+ launches them anyway. The other pre-quantized formats (`gptq`, `awq`,+ `hqq:*`, `aqlm`, `eetq`, `fp8`) are still not pre-flighted.diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.mdindex f316bb39..c81b347a 100644--- a/docs/performance-and-quantization.md+++ b/docs/performance-and-quantization.md@@ -367,7 +367,7 @@ training:| `hqq:Nbit` | 1, 2, 3, 4, 5, 6, 8 | Wide bit range; compose with LoRA. | hqq || `aqlm` | 2 | Extreme compression. | aqlm || `eetq` | 8 | Fast 8-bit kernel for SM75+. | eetq |-| `mxfp4` | 4 (stored) | Train LoRA on top of an MXFP4 pre-quantized checkpoint (for example GPT-OSS). Loaded with `Mxfp4Config(dequantize=True)`, so it trains in bf16: transformers does not train MXFP4 weights as loaded. Budget memory for the bf16 model, about 2 bytes per parameter (roughly 42 GB for a 20B base); the `soup train` pre-flight does not check `mxfp4` runs. | — |+| `mxfp4` | 4 (stored) | Train LoRA on top of an MXFP4 pre-quantized checkpoint (for example GPT-OSS). Loaded with `Mxfp4Config(dequantize=True)`, so it trains in bf16: transformers does not train MXFP4 weights as loaded. Budget memory for the bf16 model, an estimated 2 bytes per parameter (roughly 42 GB for a 20B base). The `soup train` VRAM pre-flight checks an `mxfp4` run against that estimate, as it does a `quantization: none` run, and `--allow-oom-attempt` launches one it refuses. | — || `fp8` | — | Train fp16/bf16 on top of FP8-released checkpoints. | transformers ≥ 4.45 |**Compatibility matrix.** `soup train` runs `check_quant_distributed_compat()` atdiff --git a/src/soup_cli/commands/train.py b/src/soup_cli/commands/train.pyindex 69f70604..10ac9874 100644--- a/src/soup_cli/commands/train.py+++ b/src/soup_cli/commands/train.py@@ -274,7 +274,9 @@ def _build_hardware_fit_input(cfg):seq_len = getattr(cfg.data, "max_length", None)if not isinstance(seq_len, int) or isinstance(seq_len, bool):return None- quant = {"none": "none", "4bit": "4bit", "8bit": "8bit"}.get(+ # #1631: mxfp4 is dequantized on load and trains in bf16, so it is priced+ # (and gated) like "none". The formats that stay packed are still skipped.+ quant = {"none": "none", "4bit": "4bit", "8bit": "8bit", "mxfp4": "mxfp4"}.get(str(getattr(tcfg, "quantization", "none") or "none"))if quant is None:@@ -370,6 +372,13 @@ def _hardware_fit_preflight(cfg, gpu_info, *, allow_oom_attempt: bool) -> None:if report.ok:returnb = report.breakdown+ # #1631: a "quantized" run priced like an unquantized one needs a word why.+ note = (+ "An MXFP4 base is dequantized on load, so this estimate prices the "+ "bf16 model.\n"+ if inp.quant == "mxfp4"+ else ""+ )tail = ("[yellow]--allow-oom-attempt set: launching anyway.[/]"if allow_oom_attempt@@ -384,7 +393,7 @@ def _hardware_fit_preflight(cfg, gpu_info, *, allow_oom_attempt: bool) -> None:f"{report.available_vram_gb:.1f} GB available.\n"f"weights {b.weights_gb:.1f} | optim {b.optimizer_gb:.1f} | "f"grads {b.gradients_gb:.1f} | activations {b.activations_gb:.1f} "- f"| overhead {b.overhead_gb:.1f} GB\n\n" + tail,+ f"| overhead {b.overhead_gb:.1f} GB\n" + note + "\n" + tail,title=("[yellow]Hardware-fit warning[/]"if allow_oom_attemptdiff --git a/tests/test_issue1631_mxfp4_preflight.py b/tests/test_issue1631_mxfp4_preflight.pynew file mode 100644index 00000000..0033b1db--- /dev/null+++ b/tests/test_issue1631_mxfp4_preflight.py@@ -0,0 +1,141 @@+"""#1631 — the ``soup train`` VRAM pre-flight checks ``quantization: mxfp4`` runs.++``mxfp4`` is loaded with ``Mxfp4Config(dequantize=True)`` and trains in bf16, so it+needs the memory of the ``none`` run (#1466). ``_build_hardware_fit_input`` mapped+only ``none`` / ``4bit`` / ``8bit``, returned ``None`` for ``mxfp4`` and the gate was+skipped: a GPT-OSS-20B run was let through on a 24 GB card that refuses the same+config with ``quantization: none``.++No GPU here: the gate is called with a card size.+"""++from __future__ import annotations++import pytest+import typer++from soup_cli.commands import train as train_mod+from soup_cli.commands.train import _build_hardware_fit_input, _hardware_fit_preflight+from soup_cli.config.loader import load_config_from_string++_YAML = """+base: openai/gpt-oss-20b+task: sft+data:+ train: train.jsonl+ max_length: 2048+training:+ batch_size: 1+ quantization: {quant}+"""++SMALL_CARD = {"memory_total_bytes": int(24e9)}+LARGE_CARD = {"memory_total_bytes": int(96e9)}+++def _cfg(quant: str):+ return load_config_from_string(_YAML.format(quant=quant))+++class _Console:+ def __init__(self):+ self.printed = []++ def print(self, renderable):+ self.printed.append(renderable)+++def console(monkeypatch):+ fake = _Console()+ monkeypatch.setattr(train_mod, "console", fake)+ return fake+++class TestTheInput:+ def test_mxfp4_gets_an_input(self):+ inp = _build_hardware_fit_input(_cfg("mxfp4"))+ assert inp is not None+ assert inp.quant == "mxfp4"++ def test_mxfp4_is_budgeted_as_lora_on_a_frozen_base(self):+ # Not qlora: nothing stays quantized once the base is dequantized.+ assert _build_hardware_fit_input(_cfg("mxfp4")).peft == "lora"++ def test_mxfp4_differs_from_none_only_in_the_quant_name(self):+ mxfp4 = _build_hardware_fit_input(_cfg("mxfp4"))+ none = _build_hardware_fit_input(_cfg("none"))+ assert (mxfp4.params_b, mxfp4.seq_len, mxfp4.batch_size, mxfp4.peft) == (+ none.params_b,+ none.seq_len,+ none.batch_size,+ none.peft,+ )++ @pytest.mark.parametrize("quant", ["gptq", "awq", "aqlm", "eetq", "fp8", "hqq:4bit"])+ def test_the_formats_that_stay_packed_are_still_skipped(self, quant):+ assert _build_hardware_fit_input(_cfg(quant)) is None++[email protected]("quant", ["mxfp4", "none"])+class TestTheGate:+ """``none`` runs beside ``mxfp4`` as the control: the two must agree."""++ def test_a_card_too_small_for_the_bf16_model_is_refused(self, quant, console):+ with pytest.raises(typer.Exit) as exit_info:+ _hardware_fit_preflight(_cfg(quant), SMALL_CARD, allow_oom_attempt=False)+ assert exit_info.value.exit_code == 1+ assert len(console.printed) == 1++ def test_allow_oom_attempt_warns_and_launches(self, quant, console):+ _hardware_fit_preflight(_cfg(quant), SMALL_CARD, allow_oom_attempt=True)+ assert len(console.printed) == 1++ def test_a_card_that_fits_is_let_through_without_a_word(self, quant, console):+ _hardware_fit_preflight(_cfg(quant), LARGE_CARD, allow_oom_attempt=False)+ assert console.printed == []+++MXFP4_NOTE = "An MXFP4 base is dequantized on load, so this estimate prices the bf16 model.\n"+++def _refusal(console, quant: str, *, allow_oom_attempt: bool = False) -> str:+ try:+ _hardware_fit_preflight(_cfg(quant), SMALL_CARD, allow_oom_attempt=allow_oom_attempt)+ except typer.Exit:+ pass+ return console.printed[-1].renderable+++def test_mxfp4_and_none_are_refused_at_the_same_prediction(console):+ mxfp4, none = _refusal(console, "mxfp4"), _refusal(console, "none")+ assert "Predicted peak VRAM" in none+ # The same numbers and the same advice; mxfp4 adds its one sentence.+ assert mxfp4.replace(MXFP4_NOTE, "") == none++[email protected]("allow_oom_attempt", [False, True])+def test_the_mxfp4_panel_says_the_base_is_dequantized(console, allow_oom_attempt):+ panel = _refusal(console, "mxfp4", allow_oom_attempt=allow_oom_attempt)+ assert panel.count(MXFP4_NOTE) == 1++[email protected]("quant", ["none", "8bit"])+def test_no_other_quantization_gets_the_sentence(console, quant):+ # 8bit on a 20B base is about 20 GB of weights: refused on the 24 GB card too.+ panel = _refusal(console, quant)+ assert "Predicted peak VRAM" in panel+ assert "MXFP4" not in panel and "dequantized" not in panel+++def test_the_other_panels_keep_their_blank_line(console):+ # Only mxfp4 gains a line; every other panel keeps the gap before the advice.+ assert " GB\n\nReduce batch_size" in _refusal(console, "none")+ assert "bf16 model.\n\nReduce batch_size" in _refusal(console, "mxfp4")+++def test_the_warning_panels_keep_their_blank_line_too(console):+ # The same gap when --allow-oom-attempt turns the refusal into a warning.+ advice = "\n\n[yellow]--allow-oom-attempt set"+ assert " GB" + advice in _refusal(console, "none", allow_oom_attempt=True)+ assert "bf16 model." + advice in _refusal(console, "mxfp4", allow_oom_attempt=True)