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.

Fix push_to_hub_merged for full fine-tunes on transformers 5 (#11482)

Nilay76b06affe3d0
diff --git a/tests/test_merged_hub_destination.py b/tests/test_merged_hub_destination.py
index ba8f645a680..355bd316769 100644
--- a/tests/test_merged_hub_destination.py
+++ b/tests/test_merged_hub_destination.py
@@ -140,7 +140,9 @@ def saving(monkeypatch, tmp_path):
records["uploads"].append({**kwargs, "files": files, "token": self.token})
if records.get("fail_upload"):
raise OSError("upload failed")
- return "commit-info"
+ # Overridable so a test can hand back a real-shaped CommitInfo, whose `pr_url` is
+ # the only place the pull request's own address exists.
+ return records.get("commit_info", "commit-info")
env = dict(
os = os,
@@ -179,6 +181,7 @@ def saving(monkeypatch, tmp_path):
# safe_serialization=None mean, which is what several tests below assert on.
"_normalize_safe_serialization",
"_is_adapter_save_method",
+ "_honours_safe_serialization",
}
nodes = []
for node in tree.body:
@@ -453,3 +456,221 @@ def test_card_download_failure_does_not_overwrite_remote_card(saving, error):
env["unsloth_generic_push_to_hub_merged"](FullModel(), "owner/model", create_pr = True)
assert records["uploads"] == []
assert not any(directory.exists() for directory in records["directories"])
+
+
+class Transformers5Model(FullModel):
+ def __init__(self):
+ self.saved = []
+
+ def save_pretrained(self, directory, **kwargs):
+ self.saved.append(kwargs)
+ super().save_pretrained(directory)
+
+ def push_to_hub(
+ self,
+ repo_id,
+ *,
+ commit_message = None,
+ commit_description = None,
+ private = None,
+ token = None,
+ revision = None,
+ create_pr = False,
+ max_shard_size = "50GB",
+ tags = None,
+ ):
+ raise AssertionError("Model files must join the single staged commit")
+
+
+class Tokenizer:
+ padding_side = "right"
+
+ def save_pretrained(self, directory):
+ assert self.padding_side == "left"
+ (Path(directory) / "tokenizer.json").write_text("{}")
+
+ def push_to_hub(self, *args, **kwargs):
+ raise AssertionError("Tokenizer files must join the single staged commit")
+
+
+def test_default_full_finetune_push_stages_one_commit(saving):
+ env, records, _ = saving
+ model, tokenizer = Transformers5Model(), Tokenizer()
+ env["unsloth_generic_push_to_hub_merged"](
+ model, "owner/model", tokenizer, token = "fixture", datasets = ["owner/data"]
+ )
+ assert len(records["uploads"]) == 1
+ upload = records["uploads"][0]
+ assert upload["repo_id"] == "owner/model"
+ assert upload["revision"] is None and upload["create_pr"] is False
+ assert set(upload["files"]) == {
+ "config.json",
+ "model.safetensors",
+ "tokenizer.json",
+ "README.md",
+ }
+ assert ModelCard(upload["files"]["README.md"]).data.datasets == ["owner/data"]
+ assert "state_dict" in model.saved[0] and model.saved[0]["safe_serialization"] is True
+ assert tokenizer.padding_side == "right"
+ assert records["merges"] == []
+
+
+def test_default_full_finetune_push_uploads_16bit_safetensors(monkeypatch):
+ pytest.importorskip("unsloth", reason = "unsloth is not importable on this runner")
+ try:
+ import unsloth.save as save
+ except ImportError as error:
+ pytest.skip(f"unsloth.save is not importable on this runner: {error}")
+ import torch
+ import transformers
+ from safetensors.torch import load_file
+
+ commits = []
+
+ class Api:
+ def __init__(self, token):
+ self.token = token
+
+ def create_repo(self, **kwargs):
+ pass
+
+ def create_commit(self, **kwargs):
+ commits.append(
+ {
+ operation.path_in_repo: (
+ load_file(operation.path_or_fileobj)
+ if operation.path_in_repo.endswith(".safetensors")
+ else None
+ )
+ for operation in kwargs["operations"]
+ }
+ )
+
+ def download(*args, **kwargs):
+ raise EntryNotFoundError("No README at destination")
+
+ monkeypatch.setattr(save, "HfApi", Api)
+ monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
+ config = transformers.LlamaConfig(
+ vocab_size = 32,
+ hidden_size = 16,
+ intermediate_size = 32,
+ num_hidden_layers = 1,
+ num_attention_heads = 2,
+ num_key_value_heads = 2,
+ )
+ model = transformers.LlamaForCausalLM(config).float()
+ save.unsloth_generic_push_to_hub_merged(
+ model, "owner/model", token = "fixture", tags = ["fine-tuned"]
+ )
+ assert len(commits) == 1
+ assert {"config.json", "model.safetensors", "README.md"} <= set(commits[0])
+ target = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
+ assert {tensor.dtype for tensor in commits[0]["model.safetensors"].values()} == {target}
+ assert next(model.parameters()).dtype == torch.float32
+
+
+def test_the_push_names_the_destination_and_not_the_staging_folder(saving, capsys):
+ """The staged save reports the temp folder it wrote, which is deleted moments later.
+
+ A caller asked for a repository, so the repository is what the output has to name; otherwise
+ the last thing printed is a success line pointing at a path that no longer exists.
+ """
+ env, records, _ = saving
+ env["unsloth_generic_push_to_hub_merged"](FullModel(), "owner/model", token = "fixture")
+ printed = capsys.readouterr().out
+ assert "owner/model" in printed
+ assert "https://huggingface.co/owner/model" in printed
+
+
+ "kwargs,expected",
+ [
+ ({}, "https://huggingface.co/owner/model"),
+ # A branch upload is not on the repository's default branch.
+ ({"revision": "my-branch"}, "https://huggingface.co/owner/model/tree/my-branch"),
+ # An existing pull request, addressed the way the Hub spells it.
+ ({"revision": "refs/pr/3"}, "https://huggingface.co/owner/model/discussions/3"),
+ # A fresh pull request: the repository page can hold no model files at all.
+ ({"create_pr": True}, "https://huggingface.co/owner/model/discussions"),
+ # `create_pr` wins over the branch it was opened against.
+ (
+ {"create_pr": True, "revision": "my-branch"},
+ "https://huggingface.co/owner/model/discussions",
+ ),
+ ],
+)
+def test_the_printed_destination_is_where_the_files_landed(saving, capsys, kwargs, expected):
+ """A branch or pull-request upload does not appear on the repository page."""
+ env, records, _ = saving
+ env["unsloth_generic_push_to_hub_merged"](FullModel(), "owner/model", token = "fixture", **kwargs)
+ printed = capsys.readouterr().out
+ assert f"Saved model to {expected}\n" in printed, printed
+
+
+def test_the_pull_requests_own_url_is_preferred_when_the_hub_returns_one(saving, capsys):
+ """`CommitInfo.pr_url` names the exact pull request; nothing local can reconstruct it."""
+ env, records, _ = saving
+ records["commit_info"] = SimpleNamespace(
+ pr_url = "https://huggingface.co/owner/model/discussions/7"
+ )
+ env["unsloth_generic_push_to_hub_merged"](
+ FullModel(), "owner/model", token = "fixture", create_pr = True
+ )
+ printed = capsys.readouterr().out
+ assert "Saved model to https://huggingface.co/owner/model/discussions/7\n" in printed, printed
+
+
+def test_a_pickle_request_this_transformers_cannot_honour_is_reported(saving):
+ """transformers 5 removed `safe_serialization`, so `False` silently yields safetensors."""
+ env, records, _ = saving
+ said = []
+ env["logger"] = SimpleNamespace(warning_once = lambda message, *a, **kw: said.append(message))
+
+ class NoSafeSerialization(FullModel):
+ """transformers 5's shape: named parameters, but nothing that honours the request."""
+
+ def save_pretrained(
+ self,
+ directory,
+ max_shard_size = "50GB",
+ variant = None,
+ **kwargs,
+ ):
+ super().save_pretrained(directory)
+
+ class HonoursIt(FullModel):
+ def save_pretrained(
+ self,
+ directory,
+ safe_serialization = True,
+ **kwargs,
+ ):
+ super().save_pretrained(directory)
+
+ class Patched(FullModel):
+ """`patch_saving_functions` wraps the real method in a passthrough that tells us nothing,
+ and keeps the original, which is what has to be probed."""
+
+ def save_pretrained(self, *args, **kwargs):
+ FullModel.save_pretrained(self, args[0])
+
+ original_model_save_pretrained = HonoursIt.save_pretrained
+
+ honours = env["_honours_safe_serialization"]
+ assert honours(NoSafeSerialization().save_pretrained) is False
+ assert honours(HonoursIt().save_pretrained) is True
+ # Cannot tell, so it must not warn: a wrong warning is worse than none.
+ assert honours(Patched().save_pretrained) is True
+
+ def push(model, **kwargs):
+ said.clear()
+ env["unsloth_generic_push_to_hub_merged"](model, "owner/model", token = "fixture", **kwargs)
+ return [message for message in said if "not a pickle" in message]
+
+ assert push(NoSafeSerialization(), safe_serialization = False)
+ # A transformers that still takes it, a patched model whose original does, and the default
+ # `True`, are all silent.
+ assert push(HonoursIt(), safe_serialization = False) == []
+ assert push(Patched(), safe_serialization = False) == []
+ assert push(NoSafeSerialization()) == []
diff --git a/tests/test_save_mtp_policy.py b/tests/test_save_mtp_policy.py
index 39a197a42bb..ef914ccbc92 100644
--- a/tests/test_save_mtp_policy.py
+++ b/tests/test_save_mtp_policy.py
@@ -44,57 +44,10 @@ 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"
- )
-
-
@pytest.fixture(scope = "module")
def save_module():
# Skip rather than fail on an older zoo; the AST tests above pin this repo's wiring.
@@ -179,53 +132,6 @@ def test_stripper_never_raises(save_module):
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
-
-
@pytest.fixture
def zoo_without_the_helpers(save_module, monkeypatch):
import sys
@@ -265,28 +171,6 @@ def test_an_older_zoo_leaves_the_config_alone_and_says_nothing(
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)
@@ -325,7 +209,7 @@ def test_the_manual_merge_restores_the_config_when_the_write_fails(tree):
@pytest.fixture(scope = "module")
def save_module_any():
- """`unsloth.save` itself, without the MTP-aware zoo the guard tests need.
+ """`unsloth.save` itself, without the MTP-aware zoo the stripper tests need.
What this file's other behavioural fixture skips on is the installed zoo exporting the MTP
helpers. The cost question below is about this repo's own control flow, so it is answerable
@@ -342,9 +226,7 @@ def save_module_any():
def test_a_local_save_does_not_collect_the_resident_state_dict(
save_module_any, monkeypatch, tmp_path
):
- """The names are the push guard's, and only the push guard's.
-
- A local save reconciles the folder it just wrote, so reading the resident tensors for it
+ """A local save reconciles the folder it just wrote, so reading the resident tensors for it
bought nothing and cost a second full collection on top of save_pretrained's own. On an
offloaded or sharded model that materialises every weight, and on a distributed one it is
a collective the other ranks are not making, so it can stall rather than merely be slow.
@@ -390,26 +272,60 @@ def test_a_local_save_does_not_collect_the_resident_state_dict(
assert collected == ["state_dict"], collected
-def test_the_resident_tensor_names_are_read_only_on_the_push_branch(tree):
- """Structural, so the collection cannot drift back out of the branch that needs it."""
- func = _func(tree, "unsloth_generic_save")
- pushes = [
- node
- for node in ast.walk(func)
- if isinstance(node, ast.If)
- and isinstance(node.test, ast.Name)
- and node.test.id == "push_to_hub"
- ]
- assert pushes, "unsloth_generic_save no longer branches on push_to_hub"
- inside = {
- id(node) for push in pushes for statement in push.body for node in ast.walk(statement)
- }
- for node in ast.walk(func):
- if isinstance(node, ast.Assign) and any(
- isinstance(target, ast.Name) and target.id == "_mtp_tensor_names"
- for target in node.targets
- ):
- assert id(node) in inside, (
- "_mtp_tensor_names is assigned outside the push branch, so a local save pays "
- "for a collection only the push guard consumes"
- )
+def test_the_written_tensor_names_reach_the_reconciler(save_module_any, monkeypatch, tmp_path):
+ """Reading the names back off disk is not always possible, so hand over the ones already held.
+
+ `_checkpoint_tensor_names` declines to unpickle an unindexed `pytorch_model.bin` just to list
+ names, so a `safe_serialization = False` export reconciles against "unknown" and keeps an
+ `mtp_num_hidden_layers` the weights do not carry. Whenever a state dict was built it IS the
+ thing being written, so passing its keys costs nothing and removes the blind spot.
+
+ The names must be read BEFORE the write: transformers 5 pops each tensor out of the supplied
+ dict as it writes the shard holding it, so reading afterwards reports an export carrying no
+ tensors at all and strips a declaration the weights really do back.
+ """
+ import torch
+
+ seen = []
+
+ class _Model:
+ config = None
+
+ def state_dict(self):
+ return {"model.embed_tokens.weight": torch.zeros(1)}
+
+ def save_pretrained(self, directory, **kwargs):
+ os.makedirs(directory, exist_ok = True)
+ # What transformers 5 does: "remove it from state_dict to avoid keeping the ref",
+ # one pop per tensor as its shard is written, leaving the caller's dict empty.
+ for name in list(kwargs.get("state_dict") or ()):
+ kwargs["state_dict"].pop(name)
+
+ from unsloth_zoo import saving_utils
+
+ monkeypatch.setattr(
+ saving_utils,
+ "reconcile_mtp_config",
+ lambda directory, tensor_names = None: seen.append(tensor_names),
+ raising = False,
+ )
+ save_module_any.unsloth_generic_save(
+ _Model(),
+ None,
+ save_directory = str(tmp_path / "16bit"),
+ save_method = "merged_16bit",
+ push_to_hub = False,
+ )
+ assert seen == [["model.embed_tokens.weight"]], seen
+
+ # No state dict of its own means the names really are unknown here, and unknown must stay
+ # unknown rather than licence a guess.
+ seen.clear()
+ save_module_any.unsloth_generic_save(
+ _Model(),
+ None,
+ save_directory = str(tmp_path / "lora"),
+ save_method = "lora",
+ push_to_hub = False,
+ )
+ assert seen == [None], seen
diff --git a/unsloth/save.py b/unsloth/save.py
index 4da135f8ddc..48e2b2213bd 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -278,6 +278,31 @@ def _filter_push_to_hub_kwargs(push_fn, kwargs):
return kept
+def _honours_safe_serialization(save_fn):
+ """Does this `save_pretrained` still take `safe_serialization`?
+
+ transformers 5 removed the parameter and always writes safetensors, so an explicit `False`
+ is absorbed by `**kwargs` and the export is silently not the pickle that was asked for.
+ Probed from the signature, not a version, like `_filter_push_to_hub_kwargs`.
+
+ Answers True whenever it cannot tell, because a wrong warning is worse than none: an
+ unreadable signature, and a `(*args, **kwargs)` passthrough, which is the shape of
+ `patch_saving_functions`' own wrapper and says nothing about what it forwards to.
+ """
+ import inspect
+
+ try:
+ parameters = inspect.signature(save_fn).parameters
+ except (TypeError, ValueError):
+ return True
+ if "safe_serialization" in parameters:
+ return True
+ return not any(
+ p.kind not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
+ for p in parameters.values()
+ )
+
+
def _is_adapter_save_method(save_method):
"""Is `save_method` the adapter-only save, i.e. "do not merge anything"?
@@ -711,17 +736,6 @@ 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.
@@ -763,57 +777,6 @@ def _strip_absent_mtp_declaration(config_dict, tensor_names):
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"):
@@ -5411,7 +5374,10 @@ def _push_merged_to_hub_revision(save_kwargs):
dict.fromkeys([*(card.data.tags or []), *(save_kwargs["tags"] or []), "unsloth"])
)
card.save(card_path)
- return api.create_commit(
+ # The staged save prints the temp folder it wrote, which is not where the user asked the
+ # model to go and is deleted a moment later. Name the destination instead.
+ print(f"Unsloth: Uploading the merged model to '{repo_id}' ...")
+ commit = api.create_commit(
repo_id = repo_id,
repo_type = "model",
operations = [
@@ -5431,6 +5397,23 @@ def _push_merged_to_hub_revision(save_kwargs):
),
commit_description = save_kwargs["commit_description"],
)
+ # Where the files ACTUALLY landed: a branch or a pull request is not the repository
+ # page, which for a fresh PR upload holds no model files at all.
+ destination = getattr(commit, "pr_url", None)
+ if destination is None:
+ destination = f"https://huggingface.co/{repo_id}"
+ # A pull request is checked first: with `create_pr` the files are in the PR, not on
+ # `revision`, which is only the branch it was opened against.
+ if save_kwargs["create_pr"]:
+ destination += "/discussions"
+ elif revision is not None:
+ destination += (
+ f"/discussions/{revision.rsplit('/', 1)[-1]}"
+ if revision.startswith("refs/pr/")
+ else f"/tree/{revision}"
+ )
+ print(f"Saved model to {destination}")
+ return commit
@_normalize_tied_weights_keys_for_save
@@ -5472,7 +5455,7 @@ def unsloth_generic_save(
# this function's own keywords.
safe_serialization = _normalize_safe_serialization(safe_serialization)
- if push_to_hub and (create_pr or revision is not None):
+ if push_to_hub and (create_pr or revision is not None or not isinstance(model, PeftModel)):
return _push_merged_to_hub_revision(dict(locals()))
if isinstance(tokenizer, (PreTrainedTokenizerBase, ProcessorMixin)):
@@ -5495,6 +5478,17 @@ def unsloth_generic_save(
max_shard_size = max_shard_size,
variant = variant,
)
+ # Asked for a pickle and this transformers cannot give one: say so, rather than upload a
+ # format the caller explicitly declined. The same report `_filter_push_to_hub_kwargs`
+ # makes for the adapter path, which never reaches this branch. Against the ORIGINAL
+ # method, since `patch_saving_functions` wraps it in a `(*args, **kwargs)` passthrough
+ # that would hide a transformers which does honour the request.
+ _real_save = getattr(model, "original_model_save_pretrained", model.save_pretrained)
+ if safe_serialization is False and not _honours_safe_serialization(_real_save):
+ logger.warning_once(
+ "Unsloth: this transformers always writes safetensors, so "
+ "`safe_serialization = False` was not applied and the export is not a pickle."
+ )
is_qwen3_5_vlm = _is_qwen3_5_vlm(model)
if ("16bit" in save_method or is_qwen3_5_vlm) and state_dict is None:
state_dict = model.state_dict()
@@ -5509,64 +5503,31 @@ def unsloth_generic_save(
if state_dict is not None:
_save_kwargs["state_dict"] = state_dict
- if push_to_hub:
- # A push has no local folder to repair afterwards, unlike a save, so the config
- # has to match the tensors BEFORE they leave. Inside this branch, because that is
- # the only consumer: a local save reconciles the written folder below, and reading
- # the resident state dict for it was a second full collection on top of
- # save_pretrained's own -- which on an offloaded or sharded model materialises
- # every weight, and on a distributed one is a collective the other ranks are not
- # making.
- 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
- print(f"Unsloth: Pushing full fine-tuned model to '{save_directory}' ...")
- 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
- _tokenizer.padding_side = "left"
- tokenizer.push_to_hub(
- save_directory,
- token = token,
- private = private,
- commit_message = commit_message,
- create_pr = create_pr,
- revision = revision,
- )
- _tokenizer.padding_side = old_padding_side
- 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
- _tokenizer.padding_side = "left"
- tokenizer.save_pretrained(save_directory)
- _tokenizer.padding_side = old_padding_side
+ # BEFORE the write: transformers 5 pops each tensor out of a supplied state dict as it
+ # writes the shard holding it ("remove it from state_dict to avoid keeping the ref",
+ # modeling_utils.py), so reading the keys afterwards reports an export with no tensors
+ # at all and would strip an `mtp_num_hidden_layers` the weights really do carry.
+ _written_tensor_names = list(state_dict.keys()) if state_dict is not None else None
+
+ 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
+
+ # The names we just wrote, when we know them: `_checkpoint_tensor_names` declines to
+ # unpickle an unindexed `pytorch_model.bin`, so a `safe_serialization = False` export
+ # would otherwise read back "unknown" and keep an `mtp_num_hidden_layers` the weights
+ # do not carry. Free here -- this state dict is already materialised.
+ reconcile_mtp_config(save_directory, tensor_names = _written_tensor_names)
+ except ImportError:
+ pass
+ if tokenizer is not None:
+ _tokenizer = tokenizer.tokenizer if hasattr(tokenizer, "tokenizer") else tokenizer
+ old_padding_side = _tokenizer.padding_side
+ _tokenizer.padding_side = "left"
+ tokenizer.save_pretrained(save_directory)
+ _tokenizer.padding_side = old_padding_side
print(f"Unsloth: Model saved successfully to '{save_directory}'")
elif _is_adapter_save_method(save_method):