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
Record the repo id for ModelScope loads so merged_16bit and GGUF export work (#13150)
Daniel Han554eb6bde15c
diff --git a/studio/backend/core/inference/safetensors_agentic.py b/studio/backend/core/inference/safetensors_agentic.pyindex d1710b4eb46..b4a351c0f53 100644--- a/studio/backend/core/inference/safetensors_agentic.py+++ b/studio/backend/core/inference/safetensors_agentic.py@@ -1492,10 +1492,7 @@ def run_safetensors_tool_loop(image_share = Noneif decision.should_execute and mcp_image is not None:from core.inference.tools import mcp_image_share-- image_share = mcp_image_share(- decision.tool_name, decision.arguments, mcp_image- )+ image_share = mcp_image_share(decision.tool_name, decision.arguments, mcp_image)if image_share is not None:decision = tool_controller.reprepare_call(decision, provisional = provisional_matchdiff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.pyindex 9177d4849fe..7eb620751b0 100644--- a/studio/backend/core/inference/studio_tool_loop.py+++ b/studio/backend/core/inference/studio_tool_loop.py@@ -1871,7 +1871,11 @@ async def stream_with_studio_tools(# OpenAI, Anthropic and Gemini all reject that history instead of answering.exhausted_call = signed_provider_call or decision.as_assistant_tool_call()exhausted_extra = call.get("extra_content")- if signed_provider_call is None and isinstance(exhausted_extra, dict) and exhausted_extra:+ if (+ signed_provider_call is None+ and isinstance(exhausted_extra, dict)+ and exhausted_extra+ ):exhausted_call["extra_content"] = exhausted_extraassistant_tool_calls.append(exhausted_call)tool_messages.append(diff --git a/studio/backend/tests/test_external_import.py b/studio/backend/tests/test_external_import.pyindex 8955d35ab17..020a27a8825 100644--- a/studio/backend/tests/test_external_import.py+++ b/studio/backend/tests/test_external_import.py@@ -338,7 +338,15 @@ def test_claude_rewind_compaction_uses_its_logical_parent(claude_home):)messages = claude.read_transcript(path, "t", "s1").messagesids = [m["id"] for m in messages]- assert [m["parentId"] for m in messages] == [None, ids[0], ids[1], ids[2], ids[1], ids[4], ids[5]]+ assert [m["parentId"] for m in messages] == [+ None,+ ids[0],+ ids[1],+ ids[2],+ ids[1],+ ids[4],+ ids[5],+ ]def test_claude_releases_filtered_records_while_streaming(tmp_path, monkeypatch):diff --git a/studio/backend/tests/test_studio_tool_loop.py b/studio/backend/tests/test_studio_tool_loop.pyindex da73b29d453..1b3d17c4a9f 100644--- a/studio/backend/tests/test_studio_tool_loop.py+++ b/studio/backend/tests/test_studio_tool_loop.py@@ -725,9 +725,7 @@ def test_sharing_the_attached_image_asks_even_with_bypass(executed, monkeypatch)assert "mcp_image" not in executed[0]-def test_settled_image_call_is_replayed_and_deduped_with_sent_arguments(- executed, monkeypatch-):+def test_settled_image_call_is_replayed_and_deduped_with_sent_arguments(executed, monkeypatch):from core.inference.mcp_image import ATTACHED_IMAGE, McpImageimage = McpImage(mime = "image/png", data = b"IMG")@@ -742,9 +740,7 @@ def test_settled_image_call_is_replayed_and_deduped_with_sent_arguments(monkeypatch.setattr(loop_mod, "mcp_image_share", settle)monkeypatch.setattr(loop_mod, "begin_tool_decision", lambda *_args: object())- monkeypatch.setattr(- loop_mod, "wait_tool_decision", lambda *_args, **_kwargs: "allow"- )+ monkeypatch.setattr(loop_mod, "wait_tool_decision", lambda *_args, **_kwargs: "allow")monkeypatch.setattr(loop_mod, "abort_tool_decision", lambda *_args: None)call = {"index": 0, "function": {"name": "python", "arguments": "{}"}}transport = FakeTransport(@@ -782,9 +778,7 @@ def test_settled_image_call_keeps_signed_provider_replay_exact(executed, monkeypmonkeypatch.setattr(loop_mod, "mcp_image_share", settle)monkeypatch.setattr(loop_mod, "begin_tool_decision", lambda *_args: object())- monkeypatch.setattr(- loop_mod, "wait_tool_decision", lambda *_args, **_kwargs: "allow"- )+ monkeypatch.setattr(loop_mod, "wait_tool_decision", lambda *_args, **_kwargs: "allow")monkeypatch.setattr(loop_mod, "abort_tool_decision", lambda *_args: None)call = {"index": 0,@@ -808,9 +802,7 @@ def test_settled_image_call_keeps_signed_provider_replay_exact(executed, monkeypif message.get("role") == "assistant" and message.get("tool_calls"))["tool_calls"][0]assert replayed["function"]["arguments"] == "{ }"- assert replayed["extra_content"] == {- "google": {"thought_signature": "SIG-A"}- }+ assert replayed["extra_content"] == {"google": {"thought_signature": "SIG-A"}}def test_the_model_is_told_about_the_attached_image(executed, monkeypatch):diff --git a/studio/backend/tests/test_tool_loop_controller.py b/studio/backend/tests/test_tool_loop_controller.pyindex 953db28b731..8b4ec86abf1 100644--- a/studio/backend/tests/test_tool_loop_controller.py+++ b/studio/backend/tests/test_tool_loop_controller.py@@ -200,9 +200,7 @@ def test_reprepare_uses_normalized_arguments_for_replay_and_dedup():first = controller.reprepare_call(first)assert first.card_id == "painted_a"- assert first.as_assistant_tool_call()["function"]["arguments"] == (- '{"image":"attached_image"}'- )+ assert first.as_assistant_tool_call()["function"]["arguments"] == ('{"image":"attached_image"}')assert first.key == canonical_tool_call_key(tool_name, {"image": "attached_image"})controller.record_result(first, "ok")diff --git a/tests/saving/test_modelscope_repo_id.py b/tests/saving/test_modelscope_repo_id.pynew file mode 100644index 00000000000..413f8355b5e--- /dev/null+++ b/tests/saving/test_modelscope_repo_id.py@@ -0,0 +1,146 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""#3726: a ModelScope load must record the repo id, not the local snapshot, and the+16bit merge must fetch its base from ModelScope.++unsloth cannot be imported on GPU-less hosts, so the helpers are extracted via ast and+exec'd against fakes, like the other GPU-free saving tests."""++from __future__ import annotations++import ast+import os+import sys+import types+from pathlib import Path++import pytest++_ROOT = Path(__file__).resolve().parent.parent.parent+++def _load(relpath, name, namespace):+ source = (_ROOT / relpath).read_text(encoding = "utf-8")+ for node in ast.walk(ast.parse(source)):+ if isinstance(node, ast.FunctionDef) and node.name == name:+ exec(compile(ast.get_source_segment(source, node), relpath, "exec"), namespace)+ return namespace[name]+ raise AssertionError(f"{name} not found in {relpath}")+++def _tiny_model(name_or_path):+ from transformers import LlamaConfig, LlamaForCausalLM++ config = LlamaConfig(+ vocab_size = 32,+ hidden_size = 16,+ intermediate_size = 32,+ num_hidden_layers = 1,+ num_attention_heads = 2,+ num_key_value_heads = 2,+ )+ config._name_or_path = name_or_path+ model = LlamaForCausalLM(config)+ model.name_or_path = name_or_path+ return model+++def record():+ return _load("unsloth/models/loader.py", "_record_modelscope_repo_id", {"os": os})+++def test_snapshot_path_replaced_and_peft_records_repo_id(record, tmp_path):+ from peft import LoraConfig, get_peft_model++ snapshot = tmp_path / "models" / "unsloth--Qwen3-0.6B-unsloth-bnb-4bit" / "snapshots" / "master"+ snapshot.mkdir(parents = True)+ model = _tiny_model(str(snapshot))+ record(model, "unsloth/Qwen3-0.6B-unsloth-bnb-4bit", str(snapshot))++ assert model.config._name_or_path == "unsloth/Qwen3-0.6B-unsloth-bnb-4bit"+ assert model.name_or_path == "unsloth/Qwen3-0.6B-unsloth-bnb-4bit"+ assert model._unsloth_modelscope_snapshot == str(snapshot)+ peft_model = get_peft_model(model, LoraConfig(r = 2, target_modules = ["q_proj"]))+ assert (+ peft_model.peft_config["default"].base_model_name_or_path+ == "unsloth/Qwen3-0.6B-unsloth-bnb-4bit"+ )++[email protected]("repo_id, local_dir", [(None, None), ("unsloth/x", None), (None, "/a")])+def test_noop_without_modelscope_load(record, repo_id, local_dir):+ model = _tiny_model("unsloth/x-bnb-4bit")+ record(model, repo_id, local_dir)+ assert model.config._name_or_path == "unsloth/x-bnb-4bit"+++def test_other_names_untouched(record, tmp_path):+ model = _tiny_model(str(tmp_path / "elsewhere"))+ record(model, "unsloth/x", str(tmp_path / "snapshot"))+ assert model.config._name_or_path == str(tmp_path / "elsewhere")+++class _Logger:+ def __init__(self):+ self.messages = []++ def warning_once(self, message):+ self.messages.append(message)+++def merge_name(monkeypatch, tmp_path):+ calls = []++ def snapshot_download(name):+ calls.append(name)+ if name == "unsloth/missing":+ raise ValueError("not on ModelScope")+ return str(tmp_path / name.replace("/", "--"))++ monkeypatch.setitem(+ sys.modules, "modelscope", types.SimpleNamespace(snapshot_download = snapshot_download)+ )+ logger = _Logger()+ fn = _load(+ "unsloth/save.py",+ "_modelscope_base_model_name",+ {+ "os": os,+ "logger": logger,+ "get_model_name": lambda name, load_in_4bit: name.removesuffix("-unsloth-bnb-4bit"),+ },+ )+ return fn, calls, logger+++def test_modelscope_merge_fetches_16bit_base(merge_name, tmp_path):+ fn, calls, _ = merge_name+ got = fn("unsloth/Qwen3-14B-unsloth-bnb-4bit", load_in_4bit = False)+ assert got == str(tmp_path / "unsloth--Qwen3-14B")+ assert calls == ["unsloth/Qwen3-14B"]+++def test_modelscope_merge_keeps_local_dir_and_falls_back(merge_name, tmp_path):+ fn, calls, logger = merge_name+ assert fn(str(tmp_path), load_in_4bit = False) == str(tmp_path)+ assert fn("unsloth/missing", load_in_4bit = False) == "unsloth/missing"+ assert calls == ["unsloth/missing"]+ assert "trying Hugging Face" in logger.messages[0]+++def test_gguf_still_converts_the_modelscope_snapshot(tmp_path):+ class _Peft:+ pass++ reuses = _load(+ "unsloth/save.py",+ "_gguf_reuses_loaded_checkpoint",+ {"os": os, "PeftModel": _Peft, "PeftModelForCausalLM": _Peft},+ )+ model = types.SimpleNamespace(config = types.SimpleNamespace(_name_or_path = "unsloth/x"))+ assert not reuses(model)+ model._unsloth_modelscope_snapshot = str(tmp_path)+ assert reuses(model)diff --git a/tests/saving/test_prewarm_base_model_hub_cache.py b/tests/saving/test_prewarm_base_model_hub_cache.pyindex 98ee8a54a53..d915d082b28 100644--- a/tests/saving/test_prewarm_base_model_hub_cache.py+++ b/tests/saving/test_prewarm_base_model_hub_cache.py@@ -242,6 +242,15 @@ def test_kaggle_and_colab_skip(monkeypatch, tmp_path):assert stubs.snapshot_download.calls == [], f"{flag} must skip pre-warm"+def test_modelscope_skips(monkeypatch, tmp_path):+ # The merge fetches the base from ModelScope (#3726), so a Hub copy would go unused.+ monkeypatch.setenv("UNSLOTH_USE_MODELSCOPE", "1")+ fn, stubs = _build_env(monkeypatch, tmp_path)+ fn(_FakePeftModel(), save_method = "merged_16bit")+ assert stubs.snapshot_download.calls == []+ assert stubs.determine_base_model_source.calls == []++@pytest.mark.parametrize("save_method", ["merged_4bit", "forced_merged_4bit", "lora"])def test_non_downloading_save_methods_skip(monkeypatch, tmp_path, save_method):fn, stubs = _build_env(monkeypatch, tmp_path)diff --git a/tests/saving/test_save_method_lora_routing.py b/tests/saving/test_save_method_lora_routing.pyindex da1aadfb79d..db6af3da346 100644--- a/tests/saving/test_save_method_lora_routing.py+++ b/tests/saving/test_save_method_lora_routing.py@@ -28,6 +28,7 @@ from __future__ import annotationsimport astimport contextlib+import osimport sysimport typesfrom pathlib import Path@@ -254,6 +255,7 @@ def _routing_environment(monkeypatch, model):namespace = _load(*_GENERIC_SAVE_REAL_HELPERS,"unsloth_generic_save",+ os = os,PeftModel = _PeftModel,PreTrainedTokenizerBase = type("Tokenizer", (), {}),ProcessorMixin = type("Processor", (), {}),@@ -360,6 +362,27 @@ def test_every_other_method_still_merges(monkeypatch, tmp_path, save_method):assert len(calls["convert"]) == (save_method != "merged_4bit_forced")+ "save_method, from_modelscope", [("merged_16bit", True), ("merged_4bit_forced", False)]+)+def test_modelscope_base_is_fetched_only_when_the_merge_reads_one(+ monkeypatch, tmp_path, save_method, from_modelscope+):+ """#3726: an in-place 4bit merge folds into the loaded weights, so it downloads no 16bit base."""+ monkeypatch.setenv("UNSLOTH_USE_MODELSCOPE", "1")+ model = _PeftModel()+ generic_save, calls = _routing_environment(monkeypatch, model)+ resolvers = []+ sys.modules["unsloth_zoo.saving_utils"].merge_and_overwrite_lora = (+ lambda resolve, **kwargs: resolvers.append(resolve)+ )+ namespace = generic_save.__globals__+ namespace["_modelscope_base_model_name"] = lambda *args, **kwargs: None+ generic_save(model, None, save_directory = str(tmp_path), save_method = save_method)+ expected = "_modelscope_base_model_name" if from_modelscope else "get_model_name"+ assert resolvers == [namespace[expected]]++def test_a_model_with_no_adapter_is_unchanged(monkeypatch, tmp_path):"""A full fine-tune asked for "lora" has no adapter, so it writes itself, as before."""model = _FullModel()diff --git a/tests/test_optimized_precision_conflicts.py b/tests/test_optimized_precision_conflicts.pyindex 77518e69ae2..d8a3f9c946e 100644--- a/tests/test_optimized_precision_conflicts.py+++ b/tests/test_optimized_precision_conflicts.py@@ -37,11 +37,12 @@ def loader():n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "from_pretrained")method.decorator_list = []- helper = next(+ helpers = [nfor n in tree.body- if isinstance(n, ast.FunctionDef) and n.name == "_precision_flags_conflict"- )+ if isinstance(n, ast.FunctionDef)+ and n.name in ("_precision_flags_conflict", "_modelscope_snapshot_or_none")+ ]captured = {"config_calls": 0}def dispatch(**kwargs):@@ -70,6 +71,7 @@ def loader():ALLOW_BITSANDBYTES = True,ALLOW_PREQUANTIZED_MODELS = True,USE_MODELSCOPE = False,+ logger = SimpleNamespace(warning_once = lambda *args, **kwargs: None),SUPPORTS_LLAMA32 = True,get_model_name = lambda name, **kwargs: name,_revision_for_resolved_repo = lambda revision, *args: revision,@@ -89,7 +91,7 @@ def loader():# is an exception nothing raises. Read off loader.py so a new helper is covered too.for name in _mistral_format_names(tree):env.setdefault(name, RuntimeError if name[0].isupper() else (lambda *args, **kwargs: False))- exec(compile(ast.Module(body = [helper, method], type_ignores = []), str(path), "exec"), env)+ exec(compile(ast.Module(body = [*helpers, method], type_ignores = []), str(path), "exec"), env)return env, captured@@ -162,6 +164,10 @@ def modelscope_snapshot(monkeypatch, tmp_path):def snapshot_download(name, allow_file_pattern = None):calls.append((name, allow_file_pattern))+ if name.startswith("hf-only/"):+ raise ValueError(f"{name} is not on ModelScope")+ snapshot = cache / name.replace("/", "--")+ snapshot.mkdir(exist_ok = True)for filename in ("config.json","adapter_config.json",@@ -171,9 +177,9 @@ def modelscope_snapshot(monkeypatch, tmp_path):if allow_file_pattern is None or any(fnmatch.fnmatch(filename, pattern) for pattern in allow_file_pattern):- (cache / filename).write_text("test fixture")+ (snapshot / filename).write_text("test fixture")downloaded.append(filename)- return str(cache)+ return str(snapshot)monkeypatch.setitem(sys.modules, "modelscope", SimpleNamespace(snapshot_download = snapshot_download)@@ -183,7 +189,7 @@ def modelscope_snapshot(monkeypatch, tmp_path):def use_modelscope_adapter(env, cache, base_name):def config(name, **kwargs):- if name == str(cache):+ if name == str(cache / "owner--model"):raise ValueError("Adapter has no model config")return SimpleNamespace(model_type = "llama", rope_scaling = None)@@ -243,6 +249,23 @@ def test_modelscope_valid_loads_download_weights(env["from_pretrained"](model_name = "owner/model", **kwargs)assert "model.safetensors" in downloaded- assert captured["dispatch"]["model_name"] == (adapter_base or str(cache))+ # An adapter's base is fetched from ModelScope as well (#3726).+ loaded = adapter_base or "owner/model"+ assert captured["dispatch"]["model_name"] == str(cache / loaded.replace("/", "--"))assert captured["dispatch"]["load_in_4bit"] is expected_4bitassert calls[-1] == ("owner/model", None)+ assert (loaded, None) in calls+++def test_modelscope_adapter_base_missing_there_loads_from_the_hub(loader, modelscope_snapshot):+ """#3726: an adapter on ModelScope whose base is only on the Hub still loads that base by repo id."""+ env, captured = loader+ cache, downloaded, calls = modelscope_snapshot+ env["USE_MODELSCOPE"] = True+ use_modelscope_adapter(env, cache, "hf-only/base")++ with pytest.raises(DispatchReached):+ env["from_pretrained"](model_name = "owner/model", load_in_4bit = False, load_in_16bit = True)++ assert captured["dispatch"]["model_name"] == "hf-only/base"+ assert ("hf-only/base", None) in callsdiff --git a/tests/version_compat/test_trl_loss_normalization_contract.py b/tests/version_compat/test_trl_loss_normalization_contract.pyindex 86253fbcbb5..bc21b95f0c6 100644--- a/tests/version_compat/test_trl_loss_normalization_contract.py+++ b/tests/version_compat/test_trl_loss_normalization_contract.py@@ -175,7 +175,9 @@ def _trl_resolves(wanted):warnings.simplefilter("always")got = pristine(output_dir = "unused", loss_type = wanted).loss_typealiased = any(- issubclass(w.category, FutureWarning) and repr(wanted) in str(w.message) and "deprecated" in str(w.message)+ issubclass(w.category, FutureWarning)+ and repr(wanted) in str(w.message)+ and "deprecated" in str(w.message)for w in caught)return got if aliased else wanted@@ -250,7 +252,9 @@ def test_pristine_trl_sft_config_keeps_an_explicit_loss_type():for wanted in ("chunked_nll", "dft"):got = pristine(output_dir = "unused", loss_type = wanted).loss_type- assert got == _trl_resolves(wanted), f"explicit loss_type {wanted!r} was clobbered to {got!r}"+ assert got == _trl_resolves(+ wanted+ ), f"explicit loss_type {wanted!r} was clobbered to {got!r}"# "dft" is not an alias on any TRL, so it must always come back unchanged.assert _trl_resolves("dft") == "dft"diff --git a/unsloth/models/loader.py b/unsloth/models/loader.pyindex 58a695c7ace..aed68d94888 100644--- a/unsloth/models/loader.py+++ b/unsloth/models/loader.py@@ -196,6 +196,36 @@ def _revision_for_resolved_repo(return None+def _record_modelscope_repo_id(model, repo_id, local_dir):+ """A ModelScope load hands transformers the local snapshot, which becomes the model's name: PEFT copies it into adapter_config.json and the merge reads it as the base, so a 4bit snapshot refuses merged_16bit / GGUF and the adapter names a path on this machine only (#3726). Record the repo id, as a Hub load does."""+ if repo_id is None or local_dir is None:+ return+ from transformers import PreTrainedModel++ resolved = os.path.realpath(str(local_dir))+ for module in model.modules():+ if not isinstance(module, PreTrainedModel):+ continue+ for owner, attr in ((module, "name_or_path"), (module.config, "_name_or_path")):+ value = getattr(owner, attr, None)+ if isinstance(value, str) and value and os.path.realpath(value) == resolved:+ setattr(owner, attr, repo_id)+ # A non-PEFT GGUF export still converts the snapshot in place.+ module._unsloth_modelscope_snapshot = str(local_dir)+++def _modelscope_snapshot_or_none(repo_id):+ """ModelScope snapshot of an adapter's base, or None to load it by repo id from the Hugging Face Hub as before: the base need not be mirrored on ModelScope."""+ from modelscope import snapshot_download+ try:+ return snapshot_download(repo_id)+ except Exception as e:+ logger.warning_once(+ f"Unsloth: Could not download `{repo_id}` from ModelScope ({e}), loading it from Hugging Face."+ )+ return None++def _revision_for_tokenizer_repo(tokenizer_name,model_name,@@ -1176,8 +1206,11 @@ class FastLanguageModel(FastLlamaModel):# Only check the flags when no non-bitsandbytes quantization_config sets the precision.check_precision_flags = quantization_config is None or q_load_in_4bit or q_load_in_8bitmodelscope_pending_download = None+ modelscope_repo_id = modelscope_dir = Noneif USE_MODELSCOPE and not os.path.exists(model_name):from modelscope import snapshot_download++ modelscope_repo_id = model_nameif check_precision_flags and _precision_flags_conflict(load_in_4bit, load_in_8bit, load_in_16bit, load_in_fp8):@@ -1186,6 +1219,7 @@ class FastLanguageModel(FastLlamaModel):model_name = snapshot_download(model_name, allow_file_pattern = ["*.json", "*.py"])else:model_name = snapshot_download(model_name)+ modelscope_dir = model_name# Gate before the probe below, or a pinned 4bit load fails against the mirror.base_revision = _revision_for_resolved_repo(@@ -1384,6 +1418,21 @@ class FastLanguageModel(FastLlamaModel):load_in_8bit = Falseload_in_fp8 = Falseload_in_16bit = True+ # The adapter names its base by repo id, so fetch that from ModelScope too; a precision conflict raises below before any weights are needed.+ if (+ USE_MODELSCOPE+ and not os.path.exists(model_name)+ and not (+ check_precision_flags+ and _precision_flags_conflict(+ load_in_4bit, load_in_8bit, load_in_16bit, load_in_fp8+ )+ )+ ):+ _snapshot = _modelscope_snapshot_or_none(model_name)+ if _snapshot is not None:+ modelscope_repo_id, model_name = model_name, _snapshot+ modelscope_dir = _snapshot# After the -bf16 rule: the view path no longer carries the source's suffix.# No revision: the caller's ref names the adapter repo, and the base loads unpinned below._cache_dir = kwargs.get("cache_dir", None)@@ -1590,6 +1639,7 @@ class FastLanguageModel(FastLlamaModel):*args,**kwargs,)+ _record_modelscope_repo_id(model, modelscope_repo_id, modelscope_dir)if resize_model_vocab is not None:_resize_vocab(model, resize_model_vocab)@@ -2028,9 +2078,11 @@ class FastModel(FastBaseModel):load_in_fp8 = Falseload_in_16bit = True+ modelscope_repo_id = modelscope_dir = Noneif USE_MODELSCOPE and not os.path.exists(model_name):from modelscope import snapshot_download- model_name = snapshot_download(model_name)+ modelscope_repo_id = model_name+ model_name = modelscope_dir = snapshot_download(model_name)# Gate before the probe below, or a pinned 4bit load fails against the mirror.base_revision = _revision_for_resolved_repo(@@ -2406,6 +2458,12 @@ class FastModel(FastBaseModel):load_in_8bit = Falseload_in_fp8 = Falseload_in_16bit = True+ # The adapter names its base by repo id, so fetch that from ModelScope too.+ if USE_MODELSCOPE and not os.path.exists(model_name):+ _snapshot = _modelscope_snapshot_or_none(model_name)+ if _snapshot is not None:+ modelscope_repo_id, model_name = model_name, _snapshot+ modelscope_dir = _snapshot# After the -bf16 rule: the view path no longer carries the source's suffix.# No revision: the caller's ref names the adapter repo, and the base loads unpinned below._cache_dir = kwargs.get("cache_dir", None)@@ -2808,6 +2866,7 @@ class FastModel(FastBaseModel):**kwargs,)_drop_text_only_key_mapping(model, _text_key_mapping)+ _record_modelscope_repo_id(model, modelscope_repo_id, modelscope_dir)if resize_model_vocab is not None:_resize_vocab(model, resize_model_vocab)diff --git a/unsloth/save.py b/unsloth/save.pyindex 67a885705df..f83990eff5a 100644--- a/unsloth/save.py+++ b/unsloth/save.py@@ -3348,7 +3348,10 @@ def _gguf_reuses_loaded_checkpoint(model, state_dict = None):return Falseif state_dict is not None or getattr(model, "_unsloth_full_finetuning", False):return False- name_or_path = getattr(getattr(model, "config", None), "_name_or_path", None)+ # A ModelScope load keeps its snapshot here, since `_name_or_path` holds the repo id (#3726).+ name_or_path = getattr(model, "_unsloth_modelscope_snapshot", None) or getattr(+ getattr(model, "config", None), "_name_or_path", None+ )try:return bool(name_or_path and os.path.isdir(str(name_or_path)))except Exception:@@ -3438,7 +3441,9 @@ def _gguf_model_input_directory():"""The folder the converter reads, which is not always `save_directory`: a reused loaded checkpoint, which `unsloth_save_pretrained_gguf` assigns to `save_directory` before calling `save_to_gguf`. It matters only in the unwritable-CWD fallback, where the intermediate GGUF lands beside the reused checkpoint rather than the requested output, and the two can be on different filesystems."""if _gguf_reuses_loaded_checkpoint(model, state_dict):- return str(model.config._name_or_path)+ return str(+ getattr(model, "_unsloth_modelscope_snapshot", None) or model.config._name_or_path+ )return save_directory@@ -4094,7 +4099,9 @@ def unsloth_save_pretrained_gguf() from eelse:# Non-PEFT model: convert the loaded checkpoint in place when it still holds the weights to export.- original_path = getattr(self.config, "_name_or_path", None)+ original_path = getattr(self, "_unsloth_modelscope_snapshot", None) or getattr(+ self.config, "_name_or_path", None+ )if _gguf_reuses_loaded_checkpoint(self, state_dict):print(f"Unsloth: Model is not a PEFT model. Using existing checkpoint at {original_path}"@@ -5161,6 +5168,21 @@ from unsloth_zoo.llama_cpp import ()+def _modelscope_base_model_name(model_name, load_in_4bit = True):+ """`get_model_name` for a merge under UNSLOTH_USE_MODELSCOPE=1: the 16bit base comes from ModelScope, like the model it was trained on, not the Hugging Face Hub (#3726)."""+ name = get_model_name(model_name, load_in_4bit = load_in_4bit)+ if not name or os.path.exists(name):+ return name+ try:+ from modelscope import snapshot_download+ return snapshot_download(name)+ except Exception as e:+ logger.warning_once(+ f"Unsloth: Could not download `{name}` from ModelScope ({e}), trying Hugging Face."+ )+ return name++def _prewarm_base_model_hub_cache(model,save_method = "merged_16bit",@@ -5172,6 +5194,9 @@ def _prewarm_base_model_hub_cache(returnif IS_KAGGLE_ENVIRONMENT or IS_COLAB_ENVIRONMENT:return+ # The merge fetches the base from ModelScope, so a Hub cache copy would go unused.+ if os.environ.get("UNSLOTH_USE_MODELSCOPE", "0") == "1":+ return_true = ("1", "true", "yes", "on")if (os.environ.get("HF_HUB_OFFLINE", "").strip().lower() in _true@@ -5707,7 +5732,9 @@ def unsloth_generic_save(in_place = save_method in ("merged_4bit", "forced_merged_4bit")with nullcontext() if in_place else lora_relative_to_original_base(model):merge_and_overwrite_lora(- get_model_name,+ _modelscope_base_model_name+ if os.environ.get("UNSLOTH_USE_MODELSCOPE", "0") == "1" and not in_place+ else get_model_name,model = model,tokenizer = tokenizer,save_directory = save_directory,