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
Studio export: save MLX merged and base pushes once into a reused folder (#16278)
Daniel Hana3446e8431d0
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.pyindex a44061305e3..4782822c8b8 100644--- a/studio/backend/core/export/export.py+++ b/studio/backend/core/export/export.py@@ -734,27 +734,87 @@ def _hub_push_token(hf_token: HfTokenArg) -> HfTokenArg:return hf_token+def _mlx_kept_sidecars(model, folder, written) -> Optional[List[str]]:+ """unsloth_zoo's MLX save copies source sidecars and its fallback README only when missing:+ a kept sidecar counts as written if it equals a fresh copy; any other kept one -> None."""+ try:+ kept = {+ path.name+ for path in Path(folder).iterdir()+ if path.is_file() and path.name not in written+ }+ except OSError as exception:+ logger.info(f"Could not list the reused folder, staging a second save: {exception}")+ return None+ if "README.md" in kept:+ logger.info("Reused folder keeps README.md, staging a second save")+ return None+ source = getattr(model, "_src_path", None)+ if not kept or not isinstance(source, (str, os.PathLike)) or not os.path.isdir(source):+ return written+ try:+ from unsloth_zoo.mlx.utils import _copy_source_sidecars+ except Exception as exception:+ logger.info(f"MLX sidecar rules unavailable, staging a second save: {exception}")+ return None+ import filecmp++ written = list(written)+ try:+ with tempfile.TemporaryDirectory(prefix = _STAGING_PREFIX) as scratch:+ # Exactly the sidecars a save into an empty folder copies (small, non-weight files).+ _copy_source_sidecars(source, scratch)+ for name in sorted(kept):+ fresh = Path(scratch, name)+ if not fresh.is_file():+ continue+ if not filecmp.cmp(fresh, Path(folder, name), shallow = False):+ logger.info(f"Reused folder keeps a different {name}, staging a second save")+ return None+ written.append(name)+ except Exception as exception:+ logger.info(f"Could not check the kept MLX sidecars, staging a second save: {exception}")+ return None+ return sorted(written)++def _push_mlx_merged(- model, tokenizer, *, save_method, output_path, output_is_fresh, repo_id, hf_token, private+ model,+ tokenizer,+ *,+ save_method,+ output_path,+ output_is_fresh,+ repo_id,+ hf_token,+ private,+ reused_writes = None,):"""Upload an MLX merged save; returns the repo id the Hub resolved."""with contextlib.ExitStack() as stack:upload_dir = output_path+ written = Noneif not output_is_fresh:- # A reused folder can hold leftovers, so upload a clean second save; without a- # local save this is the only one.+ # A reused folder can hold leftovers: upload only what this save wrote+ written = reused_writes.written() if reused_writes else None+ if written:+ written = _mlx_kept_sidecars(model, output_path, written)+ if not output_is_fresh and not written:+ # Without a local save this is the only one.upload_dir = stack.enter_context(_staging_dir(Path(output_path).parent)if output_pathelse tempfile.TemporaryDirectory())model.save_pretrained_merged(upload_dir, tokenizer, save_method = save_method)+ written = Nonehf_api = HfApi(token = hf_token)repo_id = _open_hub_repo(hf_api, repo_id, private)hf_api.upload_folder(folder_path = upload_dir,repo_id = repo_id,repo_type = "model",+ allow_patterns = _escaped_patterns(written),ignore_patterns = _HUB_UPLOAD_IGNORE,delete_patterns = [_REAP_REPORT],)@@ -1406,7 +1466,13 @@ class ExportBackend:def _export_dir_overlaps_checkpoint(self, save_directory) -> bool:"""Export dir inside / around the checkpoint or a local base: some files pre-exist."""config = getattr(self.current_model, "config", None)- for source in (self.current_checkpoint, getattr(config, "_name_or_path", None)):+ # An MLX model also names the folder its sidecars are copied from.+ sources = (+ self.current_checkpoint,+ getattr(config, "_name_or_path", None),+ getattr(self.current_model, "_src_path", None),+ )+ for source in sources:if not isinstance(source, (str, os.PathLike)) or not os.path.isdir(source):continuetry:@@ -1604,7 +1670,6 @@ class ExportBackend:ensure_dir(Path(save_directory))if (push_to_hub- and not _IS_MLXand not (is_compressed or is_torchao or save_dir_was_empty)and not self._export_dir_overlaps_checkpoint(save_directory)):@@ -1683,6 +1748,7 @@ class ExportBackend:repo_id = repo_id,hf_token = hf_token,private = private,+ reused_writes = reused_writes,)else:uploaded = False@@ -1806,7 +1872,6 @@ class ExportBackend:ensure_dir(Path(save_directory))if (push_to_hub- and not _IS_MLXand not save_dir_was_emptyand not self._export_dir_overlaps_checkpoint(save_directory)):@@ -1852,6 +1917,7 @@ class ExportBackend:repo_id = repo_id,hf_token = hf_token,private = private,+ reused_writes = reused_writes,)else:base_model = base_model_id or _card_base_model(self.current_model)diff --git a/studio/backend/tests/test_export_hub_push.py b/studio/backend/tests/test_export_hub_push.pyindex 90e9b211668..dd046d18895 100644--- a/studio/backend/tests/test_export_hub_push.py+++ b/studio/backend/tests/test_export_hub_push.py@@ -1430,3 +1430,219 @@ def test_reexport_never_touches_the_leftovers_while_saving(tmp_path, monkeypatchassert {path: during[path] for path in before} == beforeassert {path: stat_all()[path] for path in before} == beforeassert seen["uploaded"] == ["model.safetensors"]+++# unsloth_zoo's MLX merged save (save_merged_model), as far as which files it writes and keeps.+_ZOO_CORE_NAMES = {"config.json", "model.safetensors.index.json", "README.md", ".gitattributes"}+++def _zoo_copy_source_sidecars(src_path, path):+ # unsloth_zoo.mlx.utils._copy_source_sidecars: copies a source sidecar only when it is missing.+ for source in Path(src_path).iterdir():+ name = source.name+ if (+ not source.is_file()+ or name in _ZOO_CORE_NAMES+ or name in {"quantize_config.json", "quant_config.json"}+ or name.startswith(("model-", "pytorch_model"))+ or source.suffix not in (".json", ".jinja", ".model", ".txt", ".py")+ ):+ continue+ target = Path(path, name)+ if not target.exists():+ shutil.copy2(source, target)+++class _MlxZooModel(_Model):+ shards = 2+ shard_bytes = b"merged"++ def __init__(+ self,+ hf_api,+ source,+ card_fails = False,+ ):+ super().__init__()+ self.hf_api = hf_api+ self._src_path = str(source)+ self.card_fails = card_fails++ def save_pretrained_merged(+ self,+ save_directory,+ tokenizer,+ save_method = None,+ token = None,+ ):+ self.merges.append(save_method)+ path = Path(save_directory)+ path.mkdir(parents = True, exist_ok = True)+ # mlx_lm.utils.save_model: every shard and the index are written.+ names = [+ f"model-{i:05d}-of-{self.shards:05d}.safetensors" for i in range(1, self.shards + 1)+ ]+ for name in names:+ (path / name).write_bytes(self.shard_bytes)+ (path / "model.safetensors.index.json").write_text(+ json.dumps({"weight_map": {str(i): name for i, name in enumerate(names)}})+ )+ (path / "config.json").write_text('{"model_type": "qwen2"}')+ (path / "tokenizer.json").write_text("{}")+ (path / "tokenizer_config.json").write_text("{}")+ _zoo_copy_source_sidecars(self._src_path, path)+ readme = path / "README.md"+ if not self.card_fails:+ readme.write_text("---\nlibrary_name: mlx\n---\n")+ elif not readme.exists():+ readme.write_text("---\nlibrary_name: mlx\ntags:\n- mlx\n---\n")+++def _mlx_zoo_backend(+ monkeypatch,+ tmp_path,+ calls,+ seen,+ export,+ card_fails = False,+ zoo = True,+):+ backend, push = _mlx_backend(+ monkeypatch, "test_export_hub_push_mlx_zoo_backend", calls, seen, export+ )+ source = tmp_path / "hf_snapshot"+ source.mkdir()+ (source / "config.json").write_text("{}")+ (source / "model.safetensors").write_bytes(b"base weights")+ (source / "tokenizer.json").write_text('{"source": 1}')+ (source / "generation_config.json").write_text('{"eos_token_id": 2}')+ (source / "modeling_qwen.py").write_text("# remote code")+ (source / "quantize_config.json").write_text("{}")+ if zoo:+ for name in ("unsloth_zoo", "unsloth_zoo.mlx"):+ package = types.ModuleType(name)+ package.__path__ = []+ monkeypatch.setitem(sys.modules, name, package)+ utils = types.ModuleType("unsloth_zoo.mlx.utils")+ utils._copy_source_sidecars = _zoo_copy_source_sidecars+ monkeypatch.setitem(sys.modules, "unsloth_zoo.mlx.utils", utils)+ else:+ monkeypatch.setitem(sys.modules, "unsloth_zoo.mlx.utils", None)+ backend.current_model = _MlxZooModel(+ sys.modules[type(backend).__module__].HfApi, source, card_fails = card_fails+ )+ return backend, push, source+++def _fresh_mlx_upload(backend, tmp_path):+ """The files (and bytes) a save into an empty folder holds, i.e. what the staged path uploads."""+ model = backend.current_model+ merges = list(model.merges)+ fresh = Path(tempfile.mkdtemp(dir = tmp_path))+ model.save_pretrained_merged(str(fresh), backend.current_tokenizer, save_method = "merged_16bit")+ model.merges[:] = merges+ return {path.name: path.read_bytes() for path in fresh.iterdir()}+++def _mlx_reexport(push, export_dir, monkeypatch, export_module, uploaded):+ real_upload = export_module.HfApi.upload_folder++ def record(+ self,+ *,+ folder_path,+ allow_patterns = None,+ **kwargs,+ ):+ from fnmatch import fnmatchcase+ uploaded.update(+ {+ path.name: path.read_bytes()+ for path in Path(folder_path).iterdir()+ if path.is_file()+ and path.name != "export_metadata.json"+ and (+ allow_patterns is None or any(fnmatchcase(path.name, p) for p in allow_patterns)+ )+ }+ )+ return real_upload(self, folder_path = folder_path, allow_patterns = allow_patterns, **kwargs)++ monkeypatch.setattr(export_module.HfApi, "upload_folder", record)+ return push(str(export_dir), push_to_hub = True, repo_id = "model", hf_token = "hf_fake")++[email protected]("export", _MLX_EXPORTS)+def test_mlx_reexport_into_the_same_folder_saves_once(tmp_path, monkeypatch, export):+ calls: list[str] = []+ seen: dict = {}+ backend, push, _source = _mlx_zoo_backend(monkeypatch, tmp_path, calls, seen, export)+ export_module = sys.modules[type(backend).__module__]+ export_dir = tmp_path / "export"+ # An earlier export into this folder with another shard layout, plus unrelated leftovers.+ backend.current_model.shards = 3+ backend.current_model.save_pretrained_merged(str(export_dir), None, save_method = "merged_16bit")+ (export_dir / "export_metadata.json").write_text("{}")+ (export_dir / "old.Q4_K_M.gguf").write_bytes(b"GGUF")+ (export_dir / "adapters.safetensors").write_bytes(b"lora")+ _age(export_dir)+ backend.current_model.shards = 2+ backend.current_model.merges.clear()+ expected = _fresh_mlx_upload(backend, tmp_path)+ uploaded: dict = {}++ success, message, output_path = _mlx_reexport(+ push, export_dir, monkeypatch, export_module, uploaded+ )++ assert success is True, message+ assert backend.current_model.merges == [_MLX_EXPORTS[export][3]]+ assert seen["folder"] == output_path == str(export_dir.resolve())+ # The kept generation_config.json and remote code are what a fresh save copies, so they ship.+ assert "generation_config.json" in uploaded and "modeling_qwen.py" in uploaded+ assert uploaded == expected+ assert seen["uploaded"] == sorted(expected)+ assert seen["delete_patterns"] == ["reap_report.json"]+ assert Path(output_path, "old.Q4_K_M.gguf").is_file()+++ "why", ["changed_sidecar", "kept_card", "no_zoo_rules", "source_folder", "fresh_leftover"]+)+def test_mlx_reexport_stages_a_clean_save_when_a_kept_file_may_belong_to_it(+ tmp_path, monkeypatch, why+):+ calls: list[str] = []+ seen: dict = {}+ backend, push, source = _mlx_zoo_backend(+ monkeypatch,+ tmp_path,+ calls,+ seen,+ "merged",+ card_fails = why == "kept_card",+ zoo = why != "no_zoo_rules",+ )+ export_module = sys.modules[type(backend).__module__]+ export_dir = source if why == "source_folder" else tmp_path / "export"+ export_dir.mkdir(exist_ok = True)+ (export_dir / "generation_config.json").write_text(+ '{"eos_token_id": 9}' if why == "changed_sidecar" else '{"eos_token_id": 2}'+ )+ (export_dir / "README.md").write_text("old card")+ (export_dir / "old.Q4_K_M.gguf").write_bytes(b"GGUF")+ if why != "fresh_leftover":+ _age(export_dir)+ expected = _fresh_mlx_upload(backend, tmp_path)+ uploaded: dict = {}++ success, message, output_path = _mlx_reexport(+ push, export_dir, monkeypatch, export_module, uploaded+ )++ assert success is True, message+ assert backend.current_model.merges == ["merged_16bit", "merged_16bit"]+ assert seen["folder"] != output_path+ assert not Path(seen["folder"]).exists()+ assert uploaded == expected+ assert "old.Q4_K_M.gguf" not in seen["uploaded"]