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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Create the branch before pushing a LoRA GGUF to a revision (#13915)
Nilay6f645a123a59
diff --git a/.github/workflows/consolidated-tests-ci.yml b/.github/workflows/consolidated-tests-ci.ymlindex 22fe57ea6de..1d5ac4dc0d6 100644--- a/.github/workflows/consolidated-tests-ci.yml+++ b/.github/workflows/consolidated-tests-ci.yml@@ -336,6 +336,7 @@ jobs:tests/saving/test_offline_gguf_vlm_tokenizer_7481.py \tests/saving/test_is_vlm_detection.py \tests/saving/test_save_method_lora_routing.py \+ tests/saving/test_push_to_hub_lora_gguf_revision.py \tests/utils/test_attention_masks.py \tests/utils/test_packing.py \tests/utils/test_trunc_normal_patch.py \@@ -490,6 +491,7 @@ jobs:tests/saving/test_offline_gguf_vlm_tokenizer_7481.py \tests/saving/test_is_vlm_detection.py \tests/saving/test_save_method_lora_routing.py \+ tests/saving/test_push_to_hub_lora_gguf_revision.py \tests/utils/test_attention_masks.py \tests/utils/test_packing.py \tests/utils/test_trunc_normal_patch.py \diff --git a/tests/saving/test_push_to_hub_lora_gguf_revision.py b/tests/saving/test_push_to_hub_lora_gguf_revision.pynew file mode 100644index 00000000000..8b7de8d0b0a--- /dev/null+++ b/tests/saving/test_push_to_hub_lora_gguf_revision.py@@ -0,0 +1,103 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++import contextlib+from types import SimpleNamespace++import huggingface_hub+import pytest+from huggingface_hub.errors import HfHubHTTPError++import unsloth.save as save_mod+++class _LoraModel:+ pass+++class _RecordingApi:+ calls = []+ branch_status = None++ def __init__(self, *args, **kwargs):+ pass++ def __getattr__(self, name):+ def record(*args, **kwargs):+ _RecordingApi.calls.append((name, kwargs))+ if name == "create_branch" and _RecordingApi.branch_status:+ raise HfHubHTTPError(+ "forbidden",+ response = SimpleNamespace(+ status_code = _RecordingApi.branch_status, headers = {}, request = None+ ),+ )++ return record+++def _fake_converter(cmd, **kwargs):+ open(cmd[cmd.index("--outfile") + 1], "wb").write(b"GGUF")+ yield SimpleNamespace(stdout = [], returncode = 0, wait = lambda: 0, args = cmd)+++def push(monkeypatch, tmp_path):+ _RecordingApi.calls = []+ _RecordingApi.branch_status = None+ (tmp_path / "convert_lora_to_gguf.py").write_text("")+ monkeypatch.setattr(huggingface_hub, "HfApi", _RecordingApi)+ monkeypatch.setattr(save_mod, "PeftModel", _LoraModel)+ monkeypatch.setattr(save_mod, "LLAMA_CPP_DEFAULT_DIR", str(tmp_path))+ monkeypatch.setattr(save_mod, "install_llama_cpp", lambda **kwargs: None)+ monkeypatch.setattr(save_mod, "save_lora_to_custom_dir", lambda *args: None)+ monkeypatch.setattr(save_mod, "_lora_base_model_id", lambda model: "u/base")+ monkeypatch.setattr(save_mod, "_loaded_via_remote_code", lambda model: False)+ monkeypatch.setattr(save_mod.subprocess, "Popen", _fake_converter)+ monkeypatch.setattr(save_mod, "raise_if_merging_mistral_format_view", lambda *args: None)++ def run(**kwargs):+ save_mod.unsloth_push_to_hub_gguf(+ _LoraModel(), "u/my-lora", tokenizer = object(), save_method = "lora", token = "t", **kwargs+ )+ return [name for name, _ in _RecordingApi.calls]++ return run+++def test_new_revision_is_created_before_the_upload(push):+ names = push(revision = "v2")+ assert names.index("create_branch") < names.index("upload_folder")+ branch = next(kw for name, kw in _RecordingApi.calls if name == "create_branch")+ assert branch["branch"] == "v2" and branch["exist_ok"] is True+ upload = next(kw for name, kw in _RecordingApi.calls if name == "upload_folder")+ assert upload["revision"] == "v2"+++ "kwargs",+ [{}, {"create_pr": True}, {"revision": "refs/pr/3"}, {"revision": "v2", "create_pr": True}],+)+def test_no_branch_is_created_without_a_new_revision(push, kwargs):+ names = push(**kwargs)+ assert "create_branch" not in names and "upload_folder" in names+++def test_branch_creation_errors_are_not_swallowed(push):+ _RecordingApi.branch_status = 403+ with pytest.raises(HfHubHTTPError):+ push(revision = "v2")+++ "kwargs, url",+ [+ ({}, "https://huggingface.co/u/my-lora\n"),+ ({"revision": "v2"}, "https://huggingface.co/u/my-lora/tree/v2\n"),+ ({"revision": "refs/pr/3"}, "https://huggingface.co/u/my-lora/discussions/3\n"),+ ],+)+def test_done_message_points_at_the_revision(push, capsys, kwargs, url):+ push(**kwargs)+ assert capsys.readouterr().out.endswith(f"Unsloth: Done. Uploaded to {url}")diff --git a/unsloth/save.py b/unsloth/save.pyindex a1acad4caea..7d55ac73412 100644--- a/unsloth/save.py+++ b/unsloth/save.py@@ -5376,7 +5376,12 @@ def _unsloth_save_lora_gguf(private = private,exist_ok = True,)- api.upload_folder(+ # huggingface_hub>=1.20 refuses create_pr with a revision in upload_folder, so a branch made first would be left behind.+ if revision is not None and not create_pr and not revision.startswith("refs/pr/"):+ api.create_branch(+ repo_id = save_directory, repo_type = "model", branch = revision, exist_ok = True+ )+ commit = api.upload_folder(folder_path = lora_dir,repo_id = save_directory,repo_type = "model",@@ -5386,7 +5391,17 @@ def _unsloth_save_lora_gguf(create_pr = create_pr,revision = revision,)- print(f"Unsloth: Done. Uploaded to https://huggingface.co/{save_directory.lstrip('/')}")+ destination = getattr(commit, "pr_url", None)+ if destination is None:+ from urllib.parse import quote+ destination = f"https://huggingface.co/{save_directory.lstrip('/')}"+ if revision is not None:+ destination += (+ f"/discussions/{revision.rsplit('/', 1)[-1]}"+ if revision.startswith("refs/pr/")+ else f"/tree/{quote(revision, safe = '')}"+ )+ print(f"Unsloth: Done. Uploaded to {destination}")return save_directoryfinally:if push_to_hub: