Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(layer-stream): materialize a local path to a Hugging Face cache snapshot (#1512)
Alberto Cuccab99edd61f366
diff --git a/changelog.d/0.75.1/1512.fixed.md b/changelog.d/0.75.1/1512.fixed.mdnew file mode 100644index 00000000..4aaa3d3e--- /dev/null+++ b/changelog.d/0.75.1/1512.fixed.md@@ -0,0 +1,3 @@+- Layer streaming now accepts a local path to a Hugging Face cache snapshot+ (`snapshots/<commit>`): it is copied to regular files like the snapshot behind a Hub id,+ instead of stopping with "no .safetensors weight files found" (#1511 by @bobbonauta in #1512).diff --git a/docs/performance-and-quantization.md b/docs/performance-and-quantization.mdindex b9cd3240..0e765188 100644--- a/docs/performance-and-quantization.md+++ b/docs/performance-and-quantization.md@@ -858,6 +858,14 @@ copy. The layer shards remain under `~/.soup/layer-stream/`. Their index recordsfilename, size, and `mtime_ns`, so a necessary re-shard says which component changed insteadof silently spending minutes rebuilding the cache.+`base:` may also be a local path to a Hugging Face cache snapshot+(`.../models--org--name/snapshots/<commit>`). Soup copies it to regular files in the same+Spectrum cache slot as the Hub id `org/name` and reuses that copy while the commit and blob+ids match. A directory whose `.safetensors` files are symlinks but which is not such a+snapshot (including a regular directory that also holds an alias symlink to a shard) is+refused with a message naming the accepted layouts: pass the Hub id, the snapshot directory,+or a directory of regular files.+This materialisation also works with `HF_HUB_OFFLINE=1` when the standard Hugging Facesnapshot is complete. Soup pins the commit resolved by the initial cache lookup and copiesonly verified snapshot files from that commit's blob store; it does not perform a second Hubdiff --git a/src/soup_cli/utils/spectrum_scan.py b/src/soup_cli/utils/spectrum_scan.pyindex e7b81810..02258f46 100644--- a/src/soup_cli/utils/spectrum_scan.py+++ b/src/soup_cli/utils/spectrum_scan.py@@ -893,6 +893,21 @@ def plan_model_weights(model: str) -> ModelWeightsPlan:if os.path.isdir(model):source = os.path.realpath(model)manifest = _weight_file_manifest(source, permit_symlinks=True)+ if any(os.path.islink(os.path.join(source, name)) for name, _s, _m in manifest):+ # A local path to an HF cache snapshot: its weights are symlinks into+ # blobs/, which the sharder and the scanner skip. Copy it like the+ # snapshot a Hub id resolves to, sharing that copy's cache slot.+ source_revision = _hf_snapshot_revision(source)+ if source_revision is None:+ raise ValueError(+ f"{model} holds symlinked .safetensors files outside a Hugging "+ "Face snapshots/<commit> directory; Soup reads weights only "+ "from regular files: pass the Hub id, the HF snapshot "+ "directory, or a directory of regular files"+ )+ return _hf_snapshot_plan(+ _hf_cache_repo_id(source) or model, source, manifest, source_revision+ )return ModelWeightsPlan(model=model,source_dir=source,@@ -924,7 +939,24 @@ def plan_model_weights(model: str) -> ModelWeightsPlan:source_files=manifest,source_revision=source_revision,)+ return _hf_snapshot_plan(model, source, manifest, source_revision)++def _hf_cache_repo_id(source_dir: str) -> Optional[str]:+ """``org/name`` for a ``models--org--name/snapshots/<sha>`` directory."""+ repo_dir = os.path.basename(os.path.dirname(os.path.dirname(source_dir)))+ if not repo_dir.startswith("models--"):+ return None+ return repo_dir[len("models--"):].replace("--", "/") or None+++def _hf_snapshot_plan(+ model: str,+ source: str,+ manifest: Tuple[Tuple[str, int, int], ...],+ source_revision: Optional[str],+) -> ModelWeightsPlan:+ """Plan the regular-file copy of a symlinked HF snapshot (reused when current)."""materialized = os.path.join(resolve_cache_dir(), "weights", model_slug(model))diff --git a/tests/test_local_hf_snapshot_path.py b/tests/test_local_hf_snapshot_path.pynew file mode 100644index 00000000..03e958af--- /dev/null+++ b/tests/test_local_hf_snapshot_path.py@@ -0,0 +1,93 @@+"""Layer streaming given a local path to a Hugging Face cache snapshot.++``snapshots/<commit>/*.safetensors`` are symlinks into ``blobs/``. The sharder and the+scanner skip symlinks, so a local snapshot path must be copied to regular files the same+way the snapshot behind a Hub id is, and share that copy's cache slot.+"""++from __future__ import annotations++from pathlib import Path++import pytest++COMMIT = "a" * 40+ETAG = "b" * 64+++def _snapshot(tmp_path: Path, *, repo: str = "models--org--model", commit: str = COMMIT) -> Path:+ import torch+ from safetensors.torch import save_file++ root = tmp_path / repo+ blobs = root / "blobs"+ snapshot = root / "snapshots" / commit+ blobs.mkdir(parents=True)+ snapshot.mkdir(parents=True)+ save_file({"model.layers.0.self_attn.q_proj.weight": torch.eye(2)}, str(blobs / ETAG))+ (blobs / ("d" * 40)).write_text('{"model_type":"llama"}\n', encoding="utf-8")+ (snapshot / "model.safetensors").symlink_to(Path("../../blobs") / ETAG)+ (snapshot / "config.json").symlink_to(Path("../../blobs") / ("d" * 40))+ return snapshot++[email protected]_symlink+def test_local_snapshot_path_is_materialized(tmp_path, monkeypatch) -> None:+ from soup_cli.utils.layer_shard import shard_checkpoint+ from soup_cli.utils.spectrum_scan import resolve_model_weights++ snapshot = _snapshot(tmp_path)+ cache_root = tmp_path / "soup-cache"+ monkeypatch.setenv("SOUP_SPECTRUM_CACHE_DIR", str(cache_root))++ resolved = Path(resolve_model_weights(str(snapshot)))++ assert resolved == cache_root / "weights" / "org__model"+ assert not (resolved / "model.safetensors").is_symlink()+ index = shard_checkpoint(+ str(resolved), str(tmp_path / "shards"), dtype="float32", arch="llama"+ )+ assert index.n_layers == 1++[email protected]_symlink+def test_local_path_reuses_the_hub_id_copy(tmp_path, monkeypatch) -> None:+ from soup_cli.utils import hubs+ from soup_cli.utils.spectrum_scan import plan_model_weights, resolve_model_weights++ snapshot = _snapshot(tmp_path)+ monkeypatch.setenv("SOUP_SPECTRUM_CACHE_DIR", str(tmp_path / "soup-cache"))+ monkeypatch.setattr(hubs, "snapshot_download", lambda *_a, **_k: str(snapshot))+ resolve_model_weights("org/model")++ plan = plan_model_weights(str(snapshot))++ assert plan.materialize_bytes == 0+ assert plan.materialized_copy_bytes > 0++[email protected]_symlink+def test_symlinked_weights_outside_a_snapshot_layout_are_refused(tmp_path, monkeypatch) -> None:+ from soup_cli.utils.spectrum_scan import plan_model_weights++ target = _snapshot(tmp_path) / "model.safetensors"+ plain = tmp_path / "plain"+ plain.mkdir()+ (plain / "model.safetensors").symlink_to(target)+ monkeypatch.setenv("SOUP_SPECTRUM_CACHE_DIR", str(tmp_path / "soup-cache"))++ with pytest.raises(ValueError, match="symlinked .safetensors files outside"):+ plan_model_weights(str(plain))++[email protected]_symlink+def test_snapshot_outside_an_hf_cache_name_keeps_a_path_derived_slot(tmp_path, monkeypatch) -> None:+ from soup_cli.utils.spectrum_scan import model_slug, plan_model_weights++ snapshot = _snapshot(tmp_path, repo="my-model-cache")+ monkeypatch.setenv("SOUP_SPECTRUM_CACHE_DIR", str(tmp_path / "soup-cache"))++ plan = plan_model_weights(str(snapshot))++ assert Path(plan.weights_dir).name == model_slug(str(snapshot))+ assert Path(plan.weights_dir).name != "org__model"