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.

Studio export: upload a reused folder's fresh files instead of saving the model twice (#16266)

Daniel Han75245661923f
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py
index ac20044ba22..a44061305e3 100644
--- a/studio/backend/core/export/export.py
+++ b/studio/backend/core/export/export.py
@@ -7,6 +7,7 @@ import glob
import json
import structlog
import tempfile
+import time
from loggers import get_logger
import os
import re
@@ -605,12 +606,99 @@ def _dir_is_fresh(directory):
)
-def _holds_checkpoint_weights(directory):
+def _holds_checkpoint_weights(directory, files = None):
+ if files is not None:
+ return any(
+ "/" not in name and Path(name).suffix in (".safetensors", ".bin") for name in files
+ )
return Path(directory).is_dir() and any(
entry.suffix in (".safetensors", ".bin") for entry in Path(directory).iterdir()
)
+def _escaped_patterns(names):
+ # glob.escape keeps "model[v2].safetensors" a literal for upload_folder's fnmatch.
+ return None if names is None else [glob.escape(name) for name in names]
+
+
+# Single-file weights carry no index: a leftover one beside them is not part of the save.
+_UNSHARDED_WEIGHTS = ({"model.safetensors"}, {"pytorch_model.bin"})
+# A rewrite within one timestamp tick (FAT/exFAT: 2 s) of a file's last write can keep its mtime.
+_SAME_TICK_NS = 3_000_000_000
+
+
+class _ReusedFolderWrites:
+ """Files one save wrote into a reused folder, told apart from leftovers by a read-only
+ (size, mtime, inode, ctime) snapshot, so a killed export leaves the folder untouched.
+ ``written()`` is None when they cannot be told apart: the caller stages a second save."""
+
+ def __init__(self, directory):
+ self._root = Path(directory)
+ try:
+ self._before: Optional[dict] = self._scan(time.time_ns() - _SAME_TICK_NS)
+ except OSError as exception:
+ logger.info(f"Reused folder not tracked, staging a second save: {exception}")
+ self._before = None
+
+ def _scan(self, settled_before: Optional[int] = None) -> dict:
+ notes = {}
+ for path in self._root.rglob("*"):
+ if path.is_symlink():
+ raise OSError(f"{path} is a symlink")
+ if not path.is_file():
+ continue
+ stat = path.stat()
+ # A hard link changes with a file outside the folder (the save may write one).
+ if stat.st_nlink > 1:
+ raise OSError(f"{path} is hard linked")
+ # Also a clock skewed ahead (network filesystems): a rewrite could match its times.
+ if settled_before is not None and stat.st_mtime_ns > settled_before:
+ raise OSError(f"{path} was written too recently to tell a rewrite apart")
+ notes[path.relative_to(self._root).as_posix()] = (
+ stat.st_size,
+ stat.st_mtime_ns,
+ stat.st_ino,
+ stat.st_ctime_ns,
+ )
+ return notes
+
+ def _index_shards(self, name) -> Optional[set]:
+ try:
+ weight_map = json.loads((self._root / name).read_text(encoding = "utf-8"))["weight_map"]
+ return {str(shard) for shard in weight_map.values()}
+ except Exception:
+ return None
+
+ def written(self) -> Optional[List[str]]:
+ """Relative posix paths the save wrote, or None to stage a second save."""
+ if self._before is None:
+ return None
+ try:
+ after = self._scan()
+ except OSError as exception:
+ logger.info(f"Reused folder not tracked, staging a second save: {exception}")
+ return None
+ written = {name for name, note in after.items() if self._before.get(name) != note}
+ # A save can keep a file it would write into an empty folder: huggingface_hub's local_dir
+ # download skips one its bookkeeping or hash calls current (unsloth_zoo fetches the shard
+ # index that way), and unsloth_zoo fetches tokenizer.model only when it is missing.
+ kept = set(after) - written
+ if "tokenizer.model" in kept:
+ logger.info("Reused folder keeps tokenizer.model, staging a second save")
+ return None
+ for name in sorted(kept):
+ if "/" in name or not name.endswith(".index.json"):
+ continue
+ shards = self._index_shards(name)
+ if shards is None or shards in _UNSHARDED_WEIGHTS:
+ logger.info(f"Reused folder keeps {name}, staging a second save")
+ return None
+ # Listing only shards this save wrote = its own skipped index; else a previous export's.
+ if shards and shards <= written:
+ written.add(name)
+ return sorted(written)
+
+
def _ensure_hub_repo_private(hf_api, repo_id):
"""Tighten an existing repo to private: create_repo sets `private` only at creation."""
try:
@@ -1315,6 +1403,20 @@ class ExportBackend:
except Exception as exc: # noqa: BLE001 - the report never blocks an export
logger.warning(f"Could not update reap_report.json in {folder}: {exc}")
+ 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)):
+ if not isinstance(source, (str, os.PathLike)) or not os.path.isdir(source):
+ continue
+ try:
+ export_dir, source = Path(save_directory).resolve(), Path(source).resolve()
+ except OSError:
+ return True
+ if export_dir == source or source in export_dir.parents or export_dir in source.parents:
+ return True
+ return False
+
def _write_export_metadata(self, save_directory: str):
"""Write export_metadata.json with base model info for Chat page discovery."""
try:
@@ -1363,6 +1465,7 @@ class ExportBackend:
output_path: Optional[str] = None
save_dir_was_empty = False
+ reused_writes: Optional[_ReusedFolderWrites] = None
# compressed-tensors (NVIDIA-only) vs portable torchao FP8/INT8.
_LABEL_TO_ALIAS = {
"FP8 (compressed-tensors)": "fp8",
@@ -1499,6 +1602,13 @@ class ExportBackend:
# Reused folders may hold leftovers, so only a fresh one is pushed as is.
save_dir_was_empty = _dir_is_fresh(save_directory)
ensure_dir(Path(save_directory))
+ if (
+ push_to_hub
+ and not _IS_MLX
+ and not (is_compressed or is_torchao or save_dir_was_empty)
+ and not self._export_dir_overlaps_checkpoint(save_directory)
+ ):
+ reused_writes = _ReusedFolderWrites(save_directory)
# save.py turns None into get_token(), so the merge needs the token spelled out.
merged_token_kw = (
@@ -1580,8 +1690,11 @@ class ExportBackend:
# Upload the built artifact; push_to_hub_merged would redo the merge and quantization.
with contextlib.ExitStack() as stack:
upload_dir = output_path
- if not (is_compressed or is_torchao or save_dir_was_empty):
- # A reused folder can hold leftovers, so upload from a clean staged second save
+ # A reused folder can hold leftovers: upload only what this save wrote
+ written = reused_writes.written() if reused_writes else None
+ if written is None and not (
+ is_compressed or is_torchao or save_dir_was_empty
+ ):
upload_dir = stack.enter_context(
_staging_dir(Path(output_path).parent)
)
@@ -1595,7 +1708,7 @@ class ExportBackend:
self._sync_reap_report(upload_dir)
# Whatever was built, only weights are worth a repo; without them the
# merging push below runs instead, as it did before this was uploaded.
- if _holds_checkpoint_weights(upload_dir):
+ if _holds_checkpoint_weights(upload_dir, written):
hf_api = HfApi(token = hf_token)
repo_url = hf_api.create_repo(
repo_id, private = private, exist_ok = True
@@ -1607,6 +1720,7 @@ class ExportBackend:
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],
)
@@ -1683,12 +1797,20 @@ class ExportBackend:
output_path: Optional[str] = None
save_dir_was_empty = False
+ reused_writes: Optional[_ReusedFolderWrites] = None
try:
if save_directory:
save_directory = str(resolve_export_write_dir(save_directory))
logger.info(f"Saving base model locally to: {save_directory}")
save_dir_was_empty = _dir_is_fresh(save_directory)
ensure_dir(Path(save_directory))
+ if (
+ push_to_hub
+ and not _IS_MLX
+ and not save_dir_was_empty
+ and not self._export_dir_overlaps_checkpoint(save_directory)
+ ):
+ reused_writes = _ReusedFolderWrites(save_directory)
if _IS_MLX:
# fuse() is a no-op without LoRA layers, so this handles non-LoRA models too.
@@ -1736,8 +1858,9 @@ class ExportBackend:
with contextlib.ExitStack() as stack:
upload_dir = save_directory
- if save_directory and not save_dir_was_empty:
- # A reused folder can hold leftovers, so upload from a clean staged second save
+ # A reused folder can hold leftovers: upload only what this save wrote
+ written = reused_writes.written() if reused_writes else None
+ if save_directory and not save_dir_was_empty and written is None:
upload_dir = stack.enter_context(
_staging_dir(Path(save_directory).parent)
)
@@ -1769,6 +1892,7 @@ class ExportBackend:
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],
)
diff --git a/studio/backend/tests/test_export_hub_push.py b/studio/backend/tests/test_export_hub_push.py
index f69af76348e..90e9b211668 100644
--- a/studio/backend/tests/test_export_hub_push.py
+++ b/studio/backend/tests/test_export_hub_push.py
@@ -6,6 +6,9 @@ from __future__ import annotations
import weakref
import functools
+import json
+import os
+import shutil
import importlib.util
import sys
import tempfile
@@ -109,6 +112,16 @@ def _expected_merged_calls(private):
return ["create_repo", *visibility, "upload_folder", "model_card"]
+_OLD_NS = 1_600_000_000_000_000_000
+
+
+def _age(directory):
+ """Leftovers of an earlier export: written long before this one (a fresh write is staged)."""
+ for path in Path(directory).rglob("*"):
+ if path.is_file():
+ os.utime(path, ns = (_OLD_NS, _OLD_NS))
+
+
@pytest.mark.parametrize("private", [True, False])
def test_base_export_push_creates_the_repo_without_push_to_hub_mixin(
tmp_path, monkeypatch, private
@@ -200,7 +213,11 @@ def test_merged_export_push_to_a_reused_folder_does_not_upload_its_leftovers(tmp
backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
export_dir = tmp_path / "export"
export_dir.mkdir()
- (export_dir / "adapter_config.json").write_text("{}")
+ leftover = export_dir / "adapter_config.json"
+ leftover.write_text("{}")
+ os.utime(leftover, ns = (1_600_000_000_000_000_000, 1_600_000_000_123_456_789))
+ # The value the filesystem kept (NTFS stores 100 ns ticks).
+ kept_ns = leftover.stat().st_mtime_ns
success, message, output_path = backend.export_merged_model(
str(export_dir),
@@ -211,9 +228,131 @@ def test_merged_export_push_to_a_reused_folder_does_not_upload_its_leftovers(tmp
assert success is True, message
assert output_path == str(export_dir.resolve())
+ # One merge: the reused folder is uploaded as is, limited to the files this save wrote.
+ assert backend.current_model.merges == ["merged_16bit"]
+ assert seen["folder"] == output_path
+ assert seen["uploaded"] == ["model.safetensors"]
+ assert leftover.read_text() == "{}"
+ assert leftover.stat().st_mtime_ns == kept_ns
+
+
+def test_merged_export_push_to_a_reused_folder_uploads_overwritten_files(tmp_path, monkeypatch):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ (export_dir / "eagle3").mkdir(parents = True)
+ (export_dir / "model.safetensors").write_bytes(b"stale")
+ (export_dir / "eagle3" / "old.json").write_text("{}")
+ (export_dir / "weird[v2].txt").write_text("leftover")
+ _age(export_dir)
+ save = backend.current_model.save_pretrained_merged
+
+ def save_with_extras(
+ save_directory,
+ tokenizer,
+ save_method = None,
+ token = None,
+ ):
+ save(save_directory, tokenizer, save_method = save_method, token = token)
+ shutil.copy2(export_dir / "eagle3" / "old.json", Path(save_directory, "copied.json"))
+ Path(save_directory, "eagle3", "config.json").write_text("{}")
+ Path(save_directory, "model[v2].bin").write_bytes(b"w")
+
+ backend.current_model.save_pretrained_merged = save_with_extras
+
+ success, message, output_path = backend.export_merged_model(
+ str(export_dir), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"]
+ assert seen["folder"] == output_path
+ assert seen["uploaded"] == [
+ "copied.json",
+ "eagle3/config.json",
+ "model.safetensors",
+ "model[v2].bin",
+ ]
+ assert Path(output_path, "model.safetensors").read_bytes() == b"weights"
+
+
[email protected]("why", ["hard_link", "base_model_dir"])
+def test_merged_export_push_stages_a_clean_save_when_the_folder_cannot_be_tracked(
+ tmp_path, monkeypatch, why
+):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ leftover = export_dir / "adapter_config.json"
+ leftover.write_text("{}")
+ if why == "hard_link":
+ os.link(leftover, tmp_path / "elsewhere.json")
+ else:
+ config = types.SimpleNamespace(_name_or_path = str(export_dir), model_type = "qwen2")
+ monkeypatch.setattr(backend.current_model, "config", config, raising = False)
+ before = leftover.stat().st_mtime_ns
+
+ success, message, output_path = backend.export_merged_model(
+ str(export_dir), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+
+ 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 seen["uploaded"] == ["model.safetensors"]
+ assert leftover.stat().st_mtime_ns == before
+
+
+def test_reused_folder_tracking_leaves_leftover_times_alone_when_the_save_fails(
+ tmp_path, monkeypatch
+):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ leftover = export_dir / "adapter_config.json"
+ leftover.write_text("{}")
+ os.utime(leftover, ns = (1_600_000_000_000_000_000, 1_600_000_000_000_000_007))
+ kept_ns = leftover.stat().st_mtime_ns # NTFS rounds to 100 ns ticks
+
+ def fail(*args, **kwargs):
+ raise OSError(28, "No space left on device")
+
+ backend.current_model.save_pretrained_merged = fail
+ success, message, _ = backend.export_merged_model(
+ str(export_dir), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+ assert success is False and "No space left on device" in message
+ assert leftover.stat().st_mtime_ns == kept_ns
+ assert calls == []
+
+
+def test_merged_export_push_into_the_checkpoint_folder_stages_a_clean_save(tmp_path, monkeypatch):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "checkpoint"
+ export_dir.mkdir()
+ (export_dir / "adapter_config.json").write_text("{}")
+ backend.current_checkpoint = str(export_dir)
+
+ success, message, output_path = backend.export_merged_model(
+ str(export_dir / "merged"), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"] # fresh subfolder, no staging
+
+ (export_dir / "merged" / "leftover.json").write_text("{}")
+ success, message, output_path = backend.export_merged_model(
+ str(export_dir / "merged"), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"] * 3
+ assert seen["folder"] != output_path
assert seen["uploaded"] == ["model.safetensors"]
@@ -225,6 +364,7 @@ def test_whisper_merged_export_is_a_16bit_merge(tmp_path, monkeypatch):
export_dir = tmp_path / "export"
export_dir.mkdir()
(export_dir / "adapter_config.json").write_text("{}")
+ _age(export_dir)
success, message, _ = backend.export_merged_model(
str(export_dir),
@@ -234,7 +374,7 @@ def test_whisper_merged_export_is_a_16bit_merge(tmp_path, monkeypatch):
)
assert success is True, message
- assert backend.current_model.merges == ["merged_16bit", "merged_16bit"]
+ assert backend.current_model.merges == ["merged_16bit"]
assert seen["uploaded"] == ["model.safetensors"]
@@ -251,6 +391,8 @@ def test_merged_export_push_stages_the_clean_save_where_there_is_room(
export_dir.mkdir()
(export_dir / "adapter_config.json").write_text("{}")
export_parent = export_dir.resolve().parent
+ # Only an untracked reused folder is staged.
+ backend._export_dir_overlaps_checkpoint = lambda directory: True
def fake_disk_usage(path):
under_export = Path(path).resolve() == export_parent
@@ -590,6 +732,7 @@ def test_base_export_push_to_a_reused_folder_does_not_upload_its_leftovers(tmp_p
export_dir.mkdir()
(export_dir / "old.Q4_K_M.gguf").write_bytes(b"GGUF")
(export_dir / "export_metadata.json").write_text("{}")
+ _age(export_dir)
success, message, output_path = backend.export_base_model(
str(export_dir),
@@ -601,8 +744,8 @@ def test_base_export_push_to_a_reused_folder_does_not_upload_its_leftovers(tmp_p
assert success is True, message
assert output_path == str(export_dir.resolve())
assert Path(output_path, "model.safetensors").is_file()
- assert seen["folder"] != output_path
- assert not Path(seen["folder"]).exists()
+ assert Path(output_path, "old.Q4_K_M.gguf").is_file()
+ assert seen["folder"] == output_path
assert seen["uploaded"] == ["model.safetensors", "tokenizer.json"]
@@ -613,6 +756,8 @@ def test_base_export_staging_failure_leaves_the_hub_untouched(tmp_path, monkeypa
export_dir = tmp_path / "export"
export_dir.mkdir()
(export_dir / "old.gguf").write_bytes(b"GGUF")
+ # Only an untracked reused folder is staged.
+ backend._export_dir_overlaps_checkpoint = lambda directory: True
save = backend.current_model.save_pretrained
saved_to = []
@@ -1117,3 +1262,171 @@ def test_hub_push_with_no_token_and_no_cli_login_is_refused(tmp_path, monkeypatc
assert success is False
assert "Hugging Face token required" in message
assert calls == []
+
+
+def _reexport(backend, export_dir, extra_save):
+ save = backend.current_model.save_pretrained_merged
+
+ def save_like_the_merge(
+ save_directory,
+ tokenizer,
+ save_method = None,
+ token = None,
+ ):
+ save(save_directory, tokenizer, save_method = save_method, token = token)
+ extra_save(Path(save_directory))
+
+ backend.current_model.save_pretrained_merged = save_like_the_merge
+ return backend.export_merged_model(
+ str(export_dir), push_to_hub = True, repo_id = "model", hf_token = "hf_fake"
+ )
+
+
+def test_reexport_uploads_the_shard_index_huggingface_hub_kept(tmp_path, monkeypatch):
+ # unsloth_zoo fetches a Hub base's index with hf_hub_download(local_dir = export folder):
+ # one an earlier export fetched at this commit is kept as is, yet belongs to this save.
+ from huggingface_hub import hf_hub_download
+ from huggingface_hub._local_folder import write_download_metadata
+
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ shards = [f"model-0000{i}-of-00002.safetensors" for i in (1, 2)]
+ index = {"weight_map": {"a": shards[0], "b": shards[1]}}
+ (export_dir / "model.safetensors.index.json").write_text(json.dumps(index))
+ commit = "c" * 40
+ write_download_metadata(
+ local_dir = export_dir, filename = "model.safetensors.index.json", commit_hash = commit, etag = "e"
+ )
+ for shard in shards:
+ (export_dir / shard).write_bytes(b"old")
+ _age(export_dir)
+ before = (export_dir / "model.safetensors.index.json").stat()
+
+ def merge(folder):
+ (folder / "model.safetensors").unlink()
+ for shard in shards:
+ (folder / shard).write_bytes(b"merged")
+ hf_hub_download(
+ "org/base", "model.safetensors.index.json", revision = commit, local_dir = folder
+ )
+
+ success, message, output_path = _reexport(backend, export_dir, merge)
+
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"]
+ assert seen["folder"] == output_path
+ kept = Path(output_path, "model.safetensors.index.json").stat()
+ assert (kept.st_mtime_ns, kept.st_ino) == (before.st_mtime_ns, before.st_ino)
+ assert seen["uploaded"] == sorted(["model.safetensors.index.json", *shards])
+
+
+def test_reexport_with_a_new_shard_layout_does_not_upload_the_old_shards(tmp_path, monkeypatch):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ old = [f"model-0000{i}-of-00003.safetensors" for i in (1, 2, 3)]
+ for shard in old:
+ (export_dir / shard).write_bytes(b"old")
+ (export_dir / "model.safetensors.index.json").write_text(
+ json.dumps({"weight_map": {str(i): shard for i, shard in enumerate(old)}})
+ )
+ (export_dir / "README.md").write_text("old card")
+ (export_dir / ".huggingface").mkdir()
+ (export_dir / ".huggingface" / "upload.lock").write_text("")
+ _age(export_dir)
+
+ success, message, output_path = _reexport(backend, export_dir, lambda folder: None)
+
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"]
+ assert seen["folder"] == output_path
+ assert seen["uploaded"] == ["model.safetensors"]
+
+
[email protected]("leftover", ["tokenizer.model", "single_file_index", "fresh"])
+def test_reexport_stages_a_clean_save_when_a_kept_file_may_belong_to_it(
+ tmp_path, monkeypatch, leftover
+):
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ if leftover == "tokenizer.model":
+ # unsloth_zoo downloads tokenizer.model only when it is missing.
+ (export_dir / "tokenizer.model").write_bytes(b"spm")
+ elif leftover == "single_file_index":
+ (export_dir / "model.safetensors.index.json").write_text(
+ json.dumps({"weight_map": {"a": "model.safetensors"}})
+ )
+ else:
+ (export_dir / "adapter_config.json").write_text("{}")
+ if leftover != "fresh":
+ _age(export_dir)
+
+ success, message, output_path = _reexport(backend, export_dir, lambda folder: None)
+
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit", "merged_16bit"]
+ assert seen["folder"] != output_path
+ assert seen["uploaded"] == ["model.safetensors"]
+
+
+def test_reexport_uploads_a_file_copied_with_the_leftovers_times(tmp_path, monkeypatch):
+ # copy2 / copystat give a rewritten file its source's times, however old.
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ export_dir.mkdir()
+ (export_dir / "config.json").write_text('{"a": 1}')
+ _age(export_dir)
+ source = tmp_path / "base" / "config.json"
+ source.parent.mkdir()
+ source.write_text('{"a": 2}')
+ os.utime(source, ns = (_OLD_NS, _OLD_NS))
+
+ def copy_from_base(folder):
+ staged = folder / ".unsloth-copy"
+ shutil.copy2(source, staged)
+ os.replace(staged, folder / "config.json")
+
+ success, message, output_path = _reexport(backend, export_dir, copy_from_base)
+
+ assert success is True, message
+ assert backend.current_model.merges == ["merged_16bit"]
+ assert Path(output_path, "config.json").stat().st_mtime_ns == _OLD_NS
+ assert seen["uploaded"] == ["config.json", "model.safetensors"]
+
+
+def test_reexport_never_touches_the_leftovers_while_saving(tmp_path, monkeypatch):
+ # Cancel terminates the export worker mid-save, so nothing may be changed to undo later.
+ calls: list[str] = []
+ seen: dict = {}
+ backend = _non_mlx_backend(monkeypatch, "test_export_hub_push_merged_backend", calls, seen)
+ export_dir = tmp_path / "export"
+ (export_dir / "sub").mkdir(parents = True)
+ (export_dir / "notes.txt").write_text("user")
+ (export_dir / "sub" / "data.bin").write_bytes(b"x")
+ _age(export_dir)
+
+ def stat_all():
+ return {
+ path: (path.stat().st_mtime_ns, path.stat().st_atime_ns, path.stat().st_ctime_ns)
+ for path in export_dir.rglob("*")
+ if path.is_file()
+ }
+
+ before = stat_all()
+ during = {}
+ success, message, _ = _reexport(backend, export_dir, lambda folder: during.update(stat_all()))
+
+ assert success is True, message
+ assert {path: during[path] for path in before} == before
+ assert {path: stat_all()[path] for path in before} == before
+ assert seen["uploaded"] == ["model.safetensors"]
diff --git a/studio/backend/tests/test_export_imatrix_compressed.py b/studio/backend/tests/test_export_imatrix_compressed.py
index 79ab65f2760..c04562377fb 100644
--- a/studio/backend/tests/test_export_imatrix_compressed.py
+++ b/studio/backend/tests/test_export_imatrix_compressed.py
@@ -113,7 +113,7 @@ def test_export_merged_maps_compressed_to_save_method():
def test_compressed_hub_push_uploads_local_dir_without_recompressing():
m = _func_src("core/export/export.py", "export_merged_model")
assert "if output_path and Path(output_path).is_dir():" in m
- assert "if not (is_compressed or is_torchao or save_dir_was_empty):" in m
+ assert "is_compressed or is_torchao or save_dir_was_empty" in m
assert "hf_api.upload_folder(" in m and "folder_path = upload_dir" in m