Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep an exported config's MTP declaration in agreement with the weights (#11042)

Daniel Hand7af91c49442
diff --git a/tests/test_save_mtp_policy.py b/tests/test_save_mtp_policy.py
new file mode 100644
index 00000000000..e1cf93675d5
--- /dev/null
+++ b/tests/test_save_mtp_policy.py
@@ -0,0 +1,322 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""An export must not declare an MTP head its weights do not contain: transformers drops
+`^mtp.*` on load for Qwen3.5, and llama.cpp's converter then asserts on the missing layer
+the config still promises. unsloth#7681."""
+
+import ast
+import json
+import types
+from pathlib import Path
+
+import pytest
+
+SAVE_PY = Path(__file__).resolve().parents[1] / "unsloth" / "save.py"
+
+
[email protected](scope = "module")
+def tree():
+ return ast.parse(SAVE_PY.read_text(encoding = "utf-8"))
+
+
+def _func(tree, name):
+ for node in ast.walk(tree):
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == name:
+ return node
+ raise AssertionError(f"{name} is not defined in save.py")
+
+
+def _calls(node):
+ out = set()
+ for sub in ast.walk(node):
+ if isinstance(sub, ast.Call):
+ f = sub.func
+ if isinstance(f, ast.Name):
+ out.add(f.id)
+ elif isinstance(f, ast.Attribute):
+ out.add(f.attr)
+ return out
+
+
+def test_generic_save_reconciles_the_exported_folder(tree):
+ assert "reconcile_mtp_config" in _calls(_func(tree, "unsloth_generic_save"))
+
+
+def test_generic_save_guards_the_push_branch_too(tree):
+ """A push has no local folder to repair afterwards."""
+ assert "_mtp_config_matching_tensors" in _calls(_func(tree, "unsloth_generic_save"))
+
+
+def test_unsloth_save_model_strips_the_declaration(tree):
+ assert "_strip_absent_mtp_declaration" in _calls(_func(tree, "unsloth_save_model"))
+
+
+def test_the_push_write_is_inside_the_guard(tree):
+ """Structural: outside the `with`, the guard exits before the write."""
+ func = _func(tree, "unsloth_generic_save")
+ guarded = False
+ for node in ast.walk(func):
+ if not isinstance(node, ast.With):
+ continue
+ names = {
+ item.context_expr.func.id
+ for item in node.items
+ if isinstance(item.context_expr, ast.Call)
+ and isinstance(item.context_expr.func, ast.Name)
+ }
+ if "_mtp_config_matching_tensors" in names and "push_to_hub" in _calls(node):
+ guarded = True
+ assert guarded, "model.push_to_hub is not inside _mtp_config_matching_tensors"
+
+
+def test_the_push_guard_reads_the_resident_tensors_when_no_state_dict_was_built(tree):
+ """lora and merged_4bit push with `state_dict is None`, which no-ops the guard."""
+ func = _func(tree, "unsloth_generic_save")
+ derived_from_model = False
+ for node in ast.walk(func):
+ if not isinstance(node, ast.Assign):
+ continue
+ if not any(isinstance(t, ast.Name) and t.id == "_mtp_tensor_names" for t in node.targets):
+ continue
+ for sub in ast.walk(node.value):
+ if (
+ isinstance(sub, ast.Call)
+ and isinstance(sub.func, ast.Attribute)
+ and sub.func.attr == "state_dict"
+ and isinstance(sub.func.value, ast.Name)
+ and sub.func.value.id == "model"
+ ):
+ derived_from_model = True
+ assert derived_from_model, (
+ "_mtp_tensor_names is never derived from model.state_dict(), so the push guard "
+ "does nothing for save methods that build no state_dict of their own"
+ )
+
+
[email protected](scope = "module")
+def save_module():
+ # Skip rather than fail on an older zoo; the AST tests above pin this repo's wiring.
+ pytest.importorskip(
+ "unsloth_zoo.saving_utils",
+ reason = "unsloth_zoo.saving_utils is unavailable",
+ )
+ from unsloth_zoo import saving_utils
+
+ for name in ("MTP_CONFIG_KEY", "mtp_head_is_present"):
+ if not hasattr(saving_utils, name):
+ pytest.skip(f"installed unsloth_zoo has no {name}")
+ # importorskip only skips ModuleNotFoundError, so catch the circular import by hand.
+ pytest.importorskip("unsloth", reason = "unsloth is not importable on this runner")
+ try:
+ import unsloth.save as module
+ except ImportError as error:
+ pytest.skip(f"unsloth.save is not importable on this runner: {error}")
+ return module
+
+
+BODY = ("model.language_model.embed_tokens.weight", "model.visual.blocks.0.attn.qkv.weight")
+WITH_MTP = BODY + ("mtp.fc.weight",)
+
+
+def test_stripper_removes_a_nested_declaration(save_module):
+ config = {
+ "model_type": "qwen3_5",
+ "text_config": {"num_hidden_layers": 24, "mtp_num_hidden_layers": 1},
+ }
+ assert save_module._strip_absent_mtp_declaration(config, BODY) is True
+ assert "mtp_num_hidden_layers" not in config["text_config"]
+ assert config["text_config"]["num_hidden_layers"] == 24
+
+
+def test_stripper_removes_a_top_level_declaration(save_module):
+ config = {"model_type": "qwen3_5_text", "mtp_num_hidden_layers": 1}
+ assert save_module._strip_absent_mtp_declaration(config, BODY) is True
+ assert "mtp_num_hidden_layers" not in config
+
+
+def test_stripper_keeps_a_declaration_the_weights_back(save_module):
+ config = {"text_config": {"mtp_num_hidden_layers": 1}}
+ assert save_module._strip_absent_mtp_declaration(config, WITH_MTP) is False
+ assert config["text_config"]["mtp_num_hidden_layers"] == 1
+
+
+def test_stripper_keeps_a_head_stored_as_extra_layers(save_module):
+ """DeepSeek-V3 / GLM keep the head in extra `layers.N` blocks, not under `mtp.`."""
+ config = {"num_hidden_layers": 24, "mtp_num_hidden_layers": 1}
+ names = BODY + ("model.layers.24.mlp.up_proj.weight",)
+ assert save_module._strip_absent_mtp_declaration(config, names) is False
+ assert config["mtp_num_hidden_layers"] == 1
+
+
+def test_stripper_strips_when_every_layer_is_within_the_count(save_module):
+ config = {"num_hidden_layers": 24, "mtp_num_hidden_layers": 1}
+ names = BODY + ("model.layers.23.mlp.up_proj.weight",)
+ assert save_module._strip_absent_mtp_declaration(config, names) is True
+ assert "mtp_num_hidden_layers" not in config
+
+
+def test_stripper_finds_the_layer_count_outside_the_declaring_container(save_module):
+ """`num_hidden_layers` can sit in `text_config` while the key is at the top level."""
+ config = {
+ "mtp_num_hidden_layers": 1,
+ "text_config": {"num_hidden_layers": 24},
+ }
+ names = BODY + ("model.language_model.layers.24.mlp.up_proj.weight",)
+ assert save_module._strip_absent_mtp_declaration(config, names) is False
+ assert config["mtp_num_hidden_layers"] == 1
+
+
+def test_stripper_is_a_noop_without_a_declaration(save_module):
+ config = {"model_type": "llama", "num_hidden_layers": 16}
+ assert save_module._strip_absent_mtp_declaration(config, BODY) is False
+ assert config == {"model_type": "llama", "num_hidden_layers": 16}
+
+
+def test_stripper_never_raises(save_module):
+ assert save_module._strip_absent_mtp_declaration(None, BODY) is False
+ assert save_module._strip_absent_mtp_declaration({"mtp_num_hidden_layers": 1}, None) is False
+
+
+def _fake_model(declared = 1, nested = True):
+ text_config = types.SimpleNamespace(num_hidden_layers = 24)
+ if nested and declared is not None:
+ text_config.mtp_num_hidden_layers = declared
+ config = types.SimpleNamespace(text_config = text_config)
+ if not nested and declared is not None:
+ config.mtp_num_hidden_layers = declared
+ return types.SimpleNamespace(config = config)
+
+
[email protected]("nested", [True, False])
+def test_guard_hides_then_restores_the_declaration(save_module, nested):
+ model = _fake_model(nested = nested)
+ holder = model.config.text_config if nested else model.config
+ with save_module._mtp_config_matching_tensors(model, BODY):
+ assert not hasattr(holder, "mtp_num_hidden_layers")
+ assert holder.mtp_num_hidden_layers == 1
+
+
+def test_guard_leaves_a_backed_declaration_in_place(save_module):
+ model = _fake_model()
+ with save_module._mtp_config_matching_tensors(model, WITH_MTP):
+ assert model.config.text_config.mtp_num_hidden_layers == 1
+ assert model.config.text_config.mtp_num_hidden_layers == 1
+
+
+def test_guard_restores_even_when_the_write_raises(save_module):
+ model = _fake_model()
+ with pytest.raises(RuntimeError):
+ with save_module._mtp_config_matching_tensors(model, BODY):
+ raise RuntimeError("write failed")
+ assert model.config.text_config.mtp_num_hidden_layers == 1
+
+
+def test_guard_does_nothing_when_the_tensor_names_are_unknown(save_module):
+ """`None` means unknown, which must never license editing the config."""
+ model = _fake_model()
+ with save_module._mtp_config_matching_tensors(model, None):
+ assert model.config.text_config.mtp_num_hidden_layers == 1
+ assert model.config.text_config.mtp_num_hidden_layers == 1
+
+
+def test_guard_never_raises_on_a_model_without_a_config(save_module):
+ with save_module._mtp_config_matching_tensors(types.SimpleNamespace(), BODY):
+ pass
+
+
+def zoo_without_the_helpers(save_module, monkeypatch):
+ import sys
+
+ real = sys.modules.get("unsloth_zoo.saving_utils")
+ stand_in = types.ModuleType("unsloth_zoo.saving_utils")
+ for name in dir(real or types.ModuleType("empty")):
+ if name in ("MTP_CONFIG_KEY", "mtp_head_is_present", "reconcile_mtp_config"):
+ continue
+ if name.startswith("__"):
+ continue
+ setattr(stand_in, name, getattr(real, name))
+ monkeypatch.setitem(sys.modules, "unsloth_zoo.saving_utils", stand_in)
+ return stand_in
+
+
+def _capture_warnings(save_module, monkeypatch):
+ said = []
+ logger = save_module.logger
+ monkeypatch.setattr(logger, "warning_once", lambda message, *a, **kw: said.append(message))
+ monkeypatch.setattr(logger, "warning", lambda message, *a, **kw: said.append(message))
+ return said
+
+
+def test_an_older_zoo_leaves_the_config_alone_and_says_nothing(
+ save_module, zoo_without_the_helpers, monkeypatch
+):
+ """The import failure shared a warning with real problems, so every save printed it."""
+ said = _capture_warnings(save_module, monkeypatch)
+ config = {
+ "model_type": "qwen3_5",
+ "text_config": {"num_hidden_layers": 24, "mtp_num_hidden_layers": 1},
+ }
+
+ assert save_module._strip_absent_mtp_declaration(config, BODY) is False
+ assert config["text_config"]["mtp_num_hidden_layers"] == 1
+ assert said == [], said
+
+
+def test_an_older_zoo_does_not_make_the_push_guard_complain(
+ save_module, zoo_without_the_helpers, monkeypatch
+):
+ said = _capture_warnings(save_module, monkeypatch)
+
+ class _Holder:
+ pass
+
+ text = _Holder()
+ setattr(text, "mtp_num_hidden_layers", 1)
+ text.num_hidden_layers = 24
+ config = _Holder()
+ config.text_config = text
+ model = _Holder()
+ model.config = config
+
+ with save_module._mtp_config_matching_tensors(model, BODY):
+ assert getattr(text, "mtp_num_hidden_layers") == 1
+ assert getattr(text, "mtp_num_hidden_layers") == 1
+ assert said == [], said
+
+
+def test_a_real_failure_is_still_reported(save_module, monkeypatch):
+ said = _capture_warnings(save_module, monkeypatch)
+
+ class _Exploding(dict):
+ def get(self, *args, **kwargs):
+ raise RuntimeError("config is not readable")
+
+ assert save_module._strip_absent_mtp_declaration(_Exploding(), BODY) is False
+ assert said and "config is not readable" in said[0], said
+
+
+def test_the_manual_merge_restores_the_config_when_the_write_fails(tree):
+ """With the restore on the success path only, a failed upload mutated the live model."""
+ func = _func(tree, "unsloth_save_model")
+ restoring = False
+ for node in ast.walk(func):
+ if not isinstance(node, ast.Try) or not node.finalbody:
+ continue
+ wrote = "save_pretrained" in _calls(node) or "upload_folder" in _calls(node)
+ restores = any(
+ isinstance(sub, ast.Attribute) and sub.attr == "config"
+ for stmt in node.finalbody
+ for sub in ast.walk(stmt)
+ ) and any(
+ isinstance(sub, ast.Name) and sub.id == "old_config"
+ for stmt in node.finalbody
+ for sub in ast.walk(stmt)
+ )
+ if wrote and restores:
+ restoring = True
+ assert restoring, (
+ "the scrubbed-config write is not in a try/finally that restores old_config, so a "
+ "failed save leaves the caller's model stripped"
+ )
diff --git a/tests/test_save_tokenizer_class.py b/tests/test_save_tokenizer_class.py
new file mode 100644
index 00000000000..3d20f0f8bf2
--- /dev/null
+++ b/tests/test_save_tokenizer_class.py
@@ -0,0 +1,128 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""An Unsloth save must record a `tokenizer_class` transformers can resolve: it is
+written from the live class name, and Unsloth once leaked `_Unsloth_Patched_` into
+`processor_class` that way (unsloth#4085). An invariant, not a fix."""
+
+import json
+import os
+from pathlib import Path
+
+import pytest
+
+MODEL_DIR = Path(os.environ.get("UNSLOTH_TEST_LOCAL_MODEL", "unsloth/gemma-3-270m-it"))
+
+# Must be MULTIMODAL: a text-only repo's AutoProcessor returns a plain tokenizer.
+PROCESSOR_DIR = os.environ.get("UNSLOTH_TEST_LOCAL_PROCESSOR")
+
+
+def _resolver():
+ tokenization_auto = pytest.importorskip(
+ "transformers.models.auto.tokenization_auto",
+ reason = "transformers is unavailable",
+ )
+ resolve = getattr(tokenization_auto, "tokenizer_class_from_name", None)
+ if resolve is None:
+ pytest.skip("this transformers has no tokenizer_class_from_name")
+ return resolve
+
+
+def _recorded_class(directory, filename_prefix = None):
+ name = (
+ f"{filename_prefix}-tokenizer_config.json" if filename_prefix else "tokenizer_config.json"
+ )
+ path = Path(directory) / name
+ if not path.is_file():
+ pytest.skip(f"the save wrote no {name}, so there is nothing to check")
+ with path.open("r", encoding = "utf-8") as handle:
+ config = json.load(handle)
+ if "tokenizer_class" not in config:
+ # Not a failure: a config may omit the key so AutoTokenizer resolves by model_type.
+ pytest.skip(f"{name} records no tokenizer_class")
+ return config["tokenizer_class"]
+
+
+def _assert_resolvable(recorded):
+ resolve = _resolver()
+ assert (
+ isinstance(recorded, str) and recorded
+ ), f"tokenizer_class must be a non-empty string, got {recorded!r}"
+ assert not recorded.startswith("_Unsloth_Patched_"), (
+ f"an Unsloth wrapper class name leaked into the export as {recorded!r}; "
+ f"this is the shape of unsloth#4085"
+ )
+ assert resolve(recorded) is not None, (
+ f"tokenizer_config.json records tokenizer_class={recorded!r}, which the "
+ f"installed transformers cannot resolve, so AutoTokenizer.from_pretrained "
+ f"on this export fails"
+ )
+
+
[email protected](scope = "module")
+def local_model():
+ """A directory or hub id `AutoTokenizer` can load: only an absolute path must exist."""
+ if MODEL_DIR.is_absolute() and not (MODEL_DIR / "config.json").is_file():
+ pytest.skip(f"no local model at {MODEL_DIR}")
+ return MODEL_DIR
+
+
+def test_a_plain_tokenizer_save_records_a_resolvable_class(local_model, tmp_path):
+ transformers = pytest.importorskip("transformers")
+ try:
+ tokenizer = transformers.AutoTokenizer.from_pretrained(str(local_model))
+ except Exception as error:
+ pytest.skip(f"could not load a tokenizer from {local_model}: {error}")
+ tokenizer.save_pretrained(str(tmp_path))
+ _assert_resolvable(_recorded_class(tmp_path))
+
+
+def test_an_unsloth_patched_tokenizer_save_records_a_resolvable_class(local_model, tmp_path):
+ transformers = pytest.importorskip("transformers")
+ save = pytest.importorskip("unsloth.save")
+ try:
+ tokenizer = transformers.AutoTokenizer.from_pretrained(str(local_model))
+ except Exception as error:
+ pytest.skip(f"could not load a tokenizer from {local_model}: {error}")
+
+ save.patch_saving_functions(tokenizer)
+ assert tokenizer.save_pretrained.__name__ == "unsloth_tokenizer_save_pretrained", (
+ "patch_saving_functions did not wrap the tokenizer's save_pretrained, so "
+ "this test is not exercising Unsloth's save path"
+ )
+
+ tokenizer.save_pretrained(str(tmp_path))
+ _assert_resolvable(_recorded_class(tmp_path))
+
+
+def test_a_processor_save_records_a_resolvable_tokenizer_class(tmp_path):
+ """Save gets a Processor, so `type(obj).__name__` is the wrong source for this key."""
+ if not PROCESSOR_DIR:
+ pytest.skip("set UNSLOTH_TEST_LOCAL_PROCESSOR to a multimodal checkpoint")
+ transformers = pytest.importorskip("transformers")
+ try:
+ processor = transformers.AutoProcessor.from_pretrained(PROCESSOR_DIR)
+ except Exception as error:
+ pytest.skip(f"no processor for {PROCESSOR_DIR}: {error}")
+ tokenizer = getattr(processor, "tokenizer", None)
+ if tokenizer is None:
+ pytest.skip(f"{type(processor).__name__} has no tokenizer to record")
+
+ processor.save_pretrained(str(tmp_path))
+ recorded = _recorded_class(tmp_path)
+ _assert_resolvable(recorded)
+ assert not recorded.endswith("Processor"), (
+ f"tokenizer_class was taken from the Processor ({recorded!r}) rather than "
+ f"from its tokenizer ({type(tokenizer).__name__!r})"
+ )
+
+
+def test_the_filename_prefix_variant_is_covered_too(local_model, tmp_path):
+ """`filename_prefix` writes `<prefix>-tokenizer_config.json`, which a repair must find."""
+ transformers = pytest.importorskip("transformers")
+ try:
+ tokenizer = transformers.AutoTokenizer.from_pretrained(str(local_model))
+ except Exception as error:
+ pytest.skip(f"could not load a tokenizer from {local_model}: {error}")
+ tokenizer.save_pretrained(str(tmp_path), filename_prefix = "unsloth")
+ _assert_resolvable(_recorded_class(tmp_path, filename_prefix = "unsloth"))
diff --git a/unsloth/save.py b/unsloth/save.py
index 694f854d37f..3cac6ec0f1b 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -654,6 +654,109 @@ def _preserve_tokenizer_eos_token(
)
+def _config_mtp_holders(config, key):
+ """Every config declaring the MTP layer count: `text_config` on Qwen3.5 multimodal, top level on text-only."""
+ holders = []
+ for candidate in (config, getattr(config, "text_config", None)):
+ if candidate is None:
+ continue
+ if key in getattr(candidate, "__dict__", {}):
+ holders.append(candidate)
+ return holders
+
+
+def _strip_absent_mtp_declaration(config_dict, tensor_names):
+ """Drop `mtp_num_hidden_layers` from a config dict when the tensors carry no MTP weights. Never raises. "Has a head" comes from the zoo's `mtp_head_is_present`, so this and `reconcile_mtp_config` cannot disagree."""
+ # Unknown is not empty: editing a declaration blind is never justified.
+ if tensor_names is None:
+ return False
+ try:
+ # Quiet: the zoo upgrades separately, and an older one is not a per-save error.
+ from unsloth_zoo.saving_utils import MTP_CONFIG_KEY, mtp_head_is_present
+ except ImportError:
+ return False
+ try:
+ if not isinstance(config_dict, dict):
+ return False
+ holders = [config_dict]
+ nested = config_dict.get("text_config")
+ if isinstance(nested, dict):
+ holders.append(nested)
+ holders = [holder for holder in holders if MTP_CONFIG_KEY in holder]
+ if not holders:
+ return False
+ tensor_names = list(tensor_names)
+ if any(mtp_head_is_present(tensor_names, config_dict, holder) for holder in holders):
+ return False
+ for holder in holders:
+ holder.pop(MTP_CONFIG_KEY, None)
+ logger.warning_once(
+ f"Unsloth: This checkpoint declares `{MTP_CONFIG_KEY}` but the "
+ f"merged weights carry no `mtp.*` tensors, so the declaration is "
+ f"omitted from the exported config. transformers does not load the "
+ f"multi-token prediction head, so a merge or re-save cannot "
+ f"preserve it. The export is otherwise complete and serves "
+ f"normally without speculative decoding."
+ )
+ return True
+ except Exception as error:
+ logger.warning_once(
+ f"Unsloth: Could not reconcile the multi-token prediction config: {error}"
+ )
+ return False
+
+
+@contextmanager
+def _mtp_config_matching_tensors(model, tensor_names):
+ """Drop `mtp_num_hidden_layers` for the duration of a save when the tensors carry no `mtp.*` weights, then put it back. Never raises: a metadata repair must not fail a save."""
+ # Recorded before anything is removed, so a partial failure still restores.
+ restore = []
+ try:
+ if tensor_names is not None:
+ try:
+ from unsloth_zoo.saving_utils import MTP_CONFIG_KEY, mtp_head_is_present
+ except ImportError:
+ MTP_CONFIG_KEY = None
+ if MTP_CONFIG_KEY is None:
+ tensor_names = None
+ if tensor_names is not None:
+ config = getattr(model, "config", None)
+ holders = _config_mtp_holders(config, MTP_CONFIG_KEY)
+ # Materialised once: the rule runs per holder, and this may be a generator.
+ tensor_names = list(tensor_names)
+ # Against the LIVE config, so the extra-`layers.N` spelling is seen.
+ if holders and not any(
+ mtp_head_is_present(tensor_names, config, holder) for holder in holders
+ ):
+ for holder in holders:
+ restore.append((holder, MTP_CONFIG_KEY, getattr(holder, MTP_CONFIG_KEY)))
+ delattr(holder, MTP_CONFIG_KEY)
+ if restore:
+ logger.warning_once(
+ f"Unsloth: This checkpoint declares `{MTP_CONFIG_KEY}` but "
+ f"the weights being exported carry no `mtp.*` tensors, so "
+ f"the declaration is omitted from the exported config. "
+ f"transformers does not load the multi-token prediction "
+ f"head, so a merge or re-save cannot preserve it. The "
+ f"export is otherwise complete and serves normally "
+ f"without speculative decoding."
+ )
+ except Exception as error:
+ logger.warning_once(
+ f"Unsloth: Could not reconcile the multi-token prediction config "
+ f"before saving: {error}"
+ )
+ try:
+ yield
+ finally:
+ # No import here: an exception from a finally block would mask the save's own.
+ for holder, key, value in restore:
+ try:
+ setattr(holder, key, value)
+ except Exception:
+ pass
+
+
def _is_qwen3_5_vlm(model):
config = getattr(model, "config", None)
if config is None or not hasattr(config, "vision_config"):
@@ -1264,6 +1367,8 @@ def unsloth_save_model(
new_config = model.config.to_dict()
if "quantization_config" in new_config:
del new_config["quantization_config"]
+ # The merged state dict is what gets written, so its keys decide MTP presence.
+ _strip_absent_mtp_declaration(new_config, state_dict.keys())
original_model = model
new_config = type(model.config).from_dict(new_config)
while hasattr(original_model, "model"):
@@ -1271,34 +1376,36 @@ def unsloth_save_model(
original_model.config = new_config
model.config = new_config
- if save_pretrained_settings["push_to_hub"] and (username != actual_username):
- print(f"Unsloth: Saving to organization with address {new_save_directory}")
- # Pushing to an organization: .save_pretrained does not work, so save locally first and upload manually.
- save_pretrained_settings["save_directory"] = new_save_directory
- save_pretrained_settings["push_to_hub"] = False
- internal_model.save_pretrained(**save_pretrained_settings)
-
- filenames = os.listdir(new_save_directory)
-
- hf_api = HfApi(token = save_pretrained_settings["token"])
-
- print("Unsloth: Uploading all files... Please wait...")
- hf_api.upload_folder(
- folder_path = new_save_directory,
- path_in_repo = ".",
- repo_id = new_save_directory,
- repo_type = "model",
- commit_message = "(Trained with Unsloth)",
- ignore_patterns = "*.md",
- )
- else:
- internal_model.save_pretrained(**save_pretrained_settings)
-
- original_model = model
- while hasattr(original_model, "model"):
- original_model = original_model.model
- original_model.config = old_config
- model.config = old_config
+ # try/finally: a failed write must not leave the caller holding the scrubbed config.
+ try:
+ if save_pretrained_settings["push_to_hub"] and (username != actual_username):
+ print(f"Unsloth: Saving to organization with address {new_save_directory}")
+ # Pushing to an organization: .save_pretrained does not work, so save locally first and upload manually.
+ save_pretrained_settings["save_directory"] = new_save_directory
+ save_pretrained_settings["push_to_hub"] = False
+ internal_model.save_pretrained(**save_pretrained_settings)
+
+ filenames = os.listdir(new_save_directory)
+
+ hf_api = HfApi(token = save_pretrained_settings["token"])
+
+ print("Unsloth: Uploading all files... Please wait...")
+ hf_api.upload_folder(
+ folder_path = new_save_directory,
+ path_in_repo = ".",
+ repo_id = new_save_directory,
+ repo_type = "model",
+ commit_message = "(Trained with Unsloth)",
+ ignore_patterns = "*.md",
+ )
+ else:
+ internal_model.save_pretrained(**save_pretrained_settings)
+ finally:
+ original_model = model
+ while hasattr(original_model, "model"):
+ original_model = original_model.model
+ original_model.config = old_config
+ model.config = old_config
print("Done.")
if push_to_hub and hasattr(model, "config"):
@@ -5552,19 +5659,30 @@ def unsloth_generic_save(
if state_dict is not None:
_save_kwargs["state_dict"] = state_dict
+ # A push has no local folder to repair afterwards, unlike a save.
+ if state_dict is not None:
+ _mtp_tensor_names = list(state_dict.keys())
+ else:
+ # push_to_hub serialises the resident state dict, so None disarms the guard.
+ try:
+ _mtp_tensor_names = list(model.state_dict().keys())
+ except Exception:
+ # Cannot report its tensors: leave the config exactly as the caller had it.
+ _mtp_tensor_names = None
if push_to_hub:
print(f"Unsloth: Pushing full fine-tuned model to '{save_directory}' ...")
- model.push_to_hub(
- repo_id = save_directory,
- token = token,
- private = private,
- commit_message = commit_message,
- create_pr = create_pr,
- revision = revision,
- commit_description = commit_description,
- tags = tags,
- **_save_kwargs,
- )
+ with _mtp_config_matching_tensors(model, _mtp_tensor_names):
+ model.push_to_hub(
+ repo_id = save_directory,
+ token = token,
+ private = private,
+ commit_message = commit_message,
+ create_pr = create_pr,
+ revision = revision,
+ commit_description = commit_description,
+ tags = tags,
+ **_save_kwargs,
+ )
if tokenizer is not None:
_tokenizer = tokenizer.tokenizer if hasattr(tokenizer, "tokenizer") else tokenizer
old_padding_side = _tokenizer.padding_side
@@ -5581,6 +5699,12 @@ def unsloth_generic_save(
else:
print(f"Unsloth: Saving full fine-tuned model to '{save_directory}' ...")
model.save_pretrained(save_directory, **_save_kwargs)
+ # Guarded: an older zoo must not raise once the weights are already on disk.
+ try:
+ from unsloth_zoo.saving_utils import reconcile_mtp_config
+ reconcile_mtp_config(save_directory)
+ except ImportError:
+ pass
if tokenizer is not None:
_tokenizer = tokenizer.tokenizer if hasattr(tokenizer, "tokenizer") else tokenizer
old_padding_side = _tokenizer.padding_side