Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(layer-stream): a snapshot copy is reused only while its layout would still be copied (#1666)

Alpamys Makazhan2d874539f308
diff --git a/changelog.d/0.75.1/1666.fixed.md b/changelog.d/0.75.1/1666.fixed.md
new file mode 100644
index 00000000..f31475ba
--- /dev/null
+++ b/changelog.d/0.75.1/1666.fixed.md
@@ -0,0 +1,6 @@
+- Layer streaming reuses its regular-file copy of a Hugging Face snapshot only while that
+ snapshot would still be copied. When the repo's `blobs` directory has become a link (a
+ symlink, or a junction on Windows) since the copy was made, or the snapshot has gained a
+ linked directory or a file whose link leaves the blob store, the run now stops with the
+ same message as a first copy instead of returning the earlier copy. That copy is left in
+ place, and an unchanged snapshot is reused without copying, as before (#1666).
diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.md
index 7fbb3089..1a43ec18 100644
--- a/docs/performance-and-quantization.md
+++ b/docs/performance-and-quantization.md
@@ -878,7 +878,9 @@ When it is itself a link (a symlink, or a junction on Windows), Soup stops with
naming it instead of following it, so a link in the cache that points outside the cache is
never followed. To keep a model's files on another disk, move the whole cache or the whole
repo folder and link that: the snapshot path is resolved first. A junction inside a snapshot
-directory is refused like a directory symlink.
+directory is refused like a directory symlink. A copy made earlier is reused only while the
+snapshot still passes these checks: when its layout changes into a refused one afterwards,
+the next run stops with the same message and leaves the copy in place.
## Correctness First (v0.36.0)
diff --git a/src/soup_cli/utils/spectrum_scan.py b/src/soup_cli/utils/spectrum_scan.py
index 02258f46..8a26376a 100644
--- a/src/soup_cli/utils/spectrum_scan.py
+++ b/src/soup_cli/utils/spectrum_scan.py
@@ -599,7 +599,15 @@ def _materialized_matches_hf_snapshot(
*,
source_revision: Optional[str] = None,
) -> Optional[Tuple[Tuple[str, int, int], ...]]:
- """Return the real-file manifest when HF metadata proves blob identity."""
+ """Return the real-file manifest when HF metadata proves blob identity.
+
+ The comparison reads link names and sizes only, so the layout is checked
+ last, by the copy step's own listing: a copy is reused only for a snapshot
+ that would be copied now. A layout that listing refuses (a linked blob
+ store, a linked directory inside the snapshot, a file that leaves the blob
+ store or has no blob) answers "no match": the plan then asks for a copy,
+ and the copy step raises its refusal before it touches the earlier copy.
+ """
try:
existing = _weight_file_manifest(materialized_dir, permit_symlinks=False)
except FileNotFoundError:
@@ -632,6 +640,10 @@ def _materialized_matches_hf_snapshot(
return None
if source_revision is not None and lines[0].strip() != source_revision:
return None
+ try:
+ _snapshot_materialization_entries(source_dir, source_revision=source_revision)
+ except (ValueError, OSError):
+ return None
return existing
diff --git a/tests/test_hf_cache_link_containment.py b/tests/test_hf_cache_link_containment.py
index 62101682..64f3ec7d 100644
--- a/tests/test_hf_cache_link_containment.py
+++ b/tests/test_hf_cache_link_containment.py
@@ -7,6 +7,10 @@ has to be a real directory: when it is a link (a symlink, or a junction on Windo
refused by name, wherever it points. A junction inside a snapshot is refused like a
directory symlink, because ``os.walk`` descends into one.
+A copy made earlier is reused only while its snapshot would still be copied: when the
+layout changes into one of the refused ones after the first copy, the next run is refused
+the same way and the copy is left as it is.
+
A test that needs no symlink builds its snapshot from regular files and a hand-made copy
plan, so it also runs on a Windows account that cannot create symlinks. The junction check
is also tested with a supplied answer, so that it is covered on every OS.
@@ -447,3 +451,243 @@ def test_local_snapshot_path_with_a_junction_inside_is_refused(tmp_path, monkeyp
resolve_model_weights(str(snapshot))
assert not _weights_root(tmp_path).exists()
+
+
+# --- a copy made earlier is reused only while its snapshot would still be copied ----
+#
+# The reuse answer compares link names and sizes, so these need a snapshot of symlinks.
+
+
+def _copy_once(monkeypatch, tmp_path: Path, snapshot: Path) -> Path:
+ """Copy a snapshot whose layout is accepted and return the copy's directory."""
+ from soup_cli.utils.spectrum_scan import materialize_model_weights
+
+ resolved = Path(materialize_model_weights(_plan(monkeypatch, tmp_path, snapshot)))
+ _assert_copied(resolved)
+ return resolved
+
+
+def _record_copies(monkeypatch) -> list[str]:
+ """Every file the copy step writes from now on."""
+ from soup_cli.utils import spectrum_scan
+
+ copied: list[str] = []
+ copy_file = spectrum_scan._copy_regular_file
+
+ def _recording(source: str, destination: str) -> None:
+ copied.append(os.path.basename(destination))
+ copy_file(source, destination)
+
+ monkeypatch.setattr(spectrum_scan, "_copy_regular_file", _recording)
+ return copied
+
+
+def _refusal_of_the_second_call(monkeypatch, resolved: Path, error, match: str, model="org/model"):
+ """Plan and copy again: the copy is planned afresh, refused, and the old one is kept."""
+ from soup_cli.utils.spectrum_scan import materialize_model_weights, plan_model_weights
+
+ stamp = (resolved / "model.safetensors").stat().st_mtime_ns
+ copied = _record_copies(monkeypatch)
+ plan = plan_model_weights(model)
+ assert plan.materialize_bytes == len(WEIGHTS), "the earlier copy was planned for reuse"
+
+ with pytest.raises(error, match=match) as refusal:
+ materialize_model_weights(plan)
+
+ assert copied == []
+ _assert_copied(resolved)
+ assert (resolved / "model.safetensors").stat().st_mtime_ns == stamp
+ return refusal
+
+
[email protected]("layout", ["repo-blob-store", "shared-blob-store", "real-subdirectory"])
+def test_copy_of_an_unchanged_snapshot_is_reused_without_copying(
+ tmp_path, monkeypatch, layout
+) -> None:
+ from soup_cli.utils.spectrum_scan import materialize_model_weights
+
+ repo = _repo(tmp_path)
+ if layout == "shared-blob-store":
+ _use_shared_store(repo, repo.parent / "blobs")
+ (repo / "blobs" / WEIGHT_BLOB).symlink_to(
+ Path("../../blobs") / XET_HASH[:2] / XET_HASH
+ )
+ snapshot = _link_snapshot(repo)
+ if layout == "real-subdirectory":
+ (snapshot / "onnx").mkdir()
+ (snapshot / "onnx" / "config.json").write_text(CONFIG, encoding="utf-8")
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ stamp = (resolved / "model.safetensors").stat().st_mtime_ns
+ copied = _record_copies(monkeypatch)
+
+ plan = _plan(monkeypatch, tmp_path, snapshot)
+
+ assert plan.materialize_bytes == 0
+ assert plan.materialized_copy_bytes == len(WEIGHTS)
+ assert Path(materialize_model_weights(plan)) == resolved
+ assert copied == []
+ assert (resolved / "model.safetensors").stat().st_mtime_ns == stamp
+
+
+def test_copy_is_replaced_when_a_weight_links_to_another_blob(tmp_path, monkeypatch) -> None:
+ """Same size, another blob name: the name is what the reuse answer compares."""
+ from soup_cli.utils.spectrum_scan import materialize_model_weights
+
+ repo = _repo(tmp_path)
+ snapshot = _link_snapshot(repo)
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ other = WEIGHTS[::-1]
+ (repo / "blobs" / ("f" * 64)).write_bytes(other)
+ (snapshot / "model.safetensors").unlink()
+ (snapshot / "model.safetensors").symlink_to(Path("../../blobs") / ("f" * 64))
+ copied = _record_copies(monkeypatch)
+
+ plan = _plan(monkeypatch, tmp_path, snapshot)
+
+ assert plan.materialize_bytes == len(other)
+ assert Path(materialize_model_weights(plan)) == resolved
+ assert sorted(copied) == ["config.json", "model.safetensors"]
+ assert (resolved / "model.safetensors").read_bytes() == other
+
+
[email protected]("where", LINK_TARGETS)
+def test_copy_is_not_reused_once_the_blob_store_is_a_symlink(tmp_path, monkeypatch, where) -> None:
+ repo = _repo(tmp_path)
+ resolved = _copy_once(monkeypatch, tmp_path, _link_snapshot(repo))
+ _link_blob_store(repo, _link_target(tmp_path, repo, where))
+
+ refusal = _refusal_of_the_second_call(monkeypatch, resolved, ValueError, IS_A_LINK)
+
+ assert repr(_blob_store_link(repo)) in str(refusal.value)
+
+
+@needs_junction
[email protected]("where", LINK_TARGETS)
+def test_copy_is_not_reused_once_the_blob_store_is_a_junction(tmp_path, monkeypatch, where) -> None:
+ repo = _repo(tmp_path)
+ resolved = _copy_once(monkeypatch, tmp_path, _link_snapshot(repo))
+ _link_blob_store(repo, _link_target(tmp_path, repo, where), junction=True)
+
+ refusal = _refusal_of_the_second_call(monkeypatch, resolved, ValueError, IS_A_LINK)
+
+ assert repr(_blob_store_link(repo)) in str(refusal.value)
+
+
+def test_copy_is_not_reused_once_the_blob_store_is_reported_as_a_junction(
+ tmp_path, monkeypatch
+) -> None:
+ """The junction half of the reuse answer on every OS: a supplied answer."""
+ from soup_cli.utils import spectrum_scan
+
+ repo = _repo(tmp_path)
+ resolved = _copy_once(monkeypatch, tmp_path, _link_snapshot(repo))
+ monkeypatch.setattr(
+ spectrum_scan, "_is_junction", lambda path: os.path.basename(path) == "blobs"
+ )
+
+ refusal = _refusal_of_the_second_call(monkeypatch, resolved, ValueError, IS_A_LINK)
+
+ assert repr(_blob_store_link(repo)) in str(refusal.value)
+
+
+def test_copy_is_not_reused_once_the_snapshot_holds_a_directory_symlink(
+ tmp_path, monkeypatch
+) -> None:
+ snapshot = _link_snapshot(_repo(tmp_path))
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ (snapshot / "extra").symlink_to(_outside_directory(tmp_path), target_is_directory=True)
+
+ _refusal_of_the_second_call(
+ monkeypatch, resolved, ValueError, "directory symlink is not allowed: 'extra'"
+ )
+
+
+@needs_junction
+def test_copy_is_not_reused_once_the_snapshot_holds_a_junction(tmp_path, monkeypatch) -> None:
+ import _winapi
+
+ snapshot = _link_snapshot(_repo(tmp_path))
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ _winapi.CreateJunction(str(_outside_directory(tmp_path)), str(snapshot / "extra"))
+
+ _refusal_of_the_second_call(
+ monkeypatch, resolved, ValueError, "directory junction is not allowed: 'extra'"
+ )
+
+
+def test_copy_is_not_reused_once_a_snapshot_directory_is_reported_as_a_junction(
+ tmp_path, monkeypatch
+) -> None:
+ """The snapshot walk's half of the reuse answer on every OS: a supplied answer."""
+ from soup_cli.utils import spectrum_scan
+
+ snapshot = _link_snapshot(_repo(tmp_path))
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ (snapshot / "extra").mkdir()
+ monkeypatch.setattr(
+ spectrum_scan, "_is_junction", lambda path: os.path.basename(path) == "extra"
+ )
+
+ _refusal_of_the_second_call(
+ monkeypatch, resolved, ValueError, "directory junction is not allowed: 'extra'"
+ )
+
+
+def test_copy_is_not_reused_once_a_weight_links_outside_the_blob_store(
+ tmp_path, monkeypatch
+) -> None:
+ """Same link name and size as the recorded blob, kept in another directory."""
+ repo = _repo(tmp_path)
+ snapshot = _link_snapshot(repo)
+ resolved = _copy_once(monkeypatch, tmp_path, snapshot)
+ (repo / "elsewhere").mkdir()
+ (repo / "elsewhere" / WEIGHT_BLOB).write_bytes(WEIGHTS[::-1])
+ (snapshot / "model.safetensors").unlink()
+ (snapshot / "model.safetensors").symlink_to(Path("../../elsewhere") / WEIGHT_BLOB)
+
+ _refusal_of_the_second_call(
+ monkeypatch, resolved, ValueError, "points outside the Hugging Face blob store"
+ )
+
+
+def test_copy_is_not_reused_once_a_snapshot_file_has_no_blob(tmp_path, monkeypatch) -> None:
+ """A layout the copy step reports as incomplete is planned afresh, not refused early."""
+ repo = _repo(tmp_path)
+ resolved = _copy_once(monkeypatch, tmp_path, _link_snapshot(repo))
+ (repo / "blobs" / CONFIG_BLOB).unlink()
+
+ _refusal_of_the_second_call(
+ monkeypatch, resolved, FileNotFoundError, "'config.json' is missing its blob"
+ )
+
+
[email protected]("change", ["blob-store-symlink", "directory-symlink-in-the-snapshot"])
+def test_local_snapshot_path_copy_is_not_reused_once_a_link_appears(
+ tmp_path, monkeypatch, change
+) -> None:
+ from soup_cli.utils.spectrum_scan import resolve_model_weights
+
+ repo = _repo(tmp_path)
+ snapshot = _link_snapshot(repo)
+ monkeypatch.setenv("SOUP_SPECTRUM_CACHE_DIR", str(tmp_path / "soup-cache"))
+ resolved = Path(resolve_model_weights(str(snapshot)))
+ _assert_copied(resolved)
+ if change == "blob-store-symlink":
+ _link_blob_store(repo, _link_target(tmp_path, repo, "outside-the-cache"))
+ match = IS_A_LINK
+ else:
+ (snapshot / "extra").symlink_to(_outside_directory(tmp_path), target_is_directory=True)
+ match = "directory symlink is not allowed: 'extra'"
+
+ _refusal_of_the_second_call(monkeypatch, resolved, ValueError, match, model=str(snapshot))