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: read the prequant source etag from the repo blob under the shared Hub blob store (#14819)

Daniel Hanae013026ba07
diff --git a/studio/backend/core/inference/diffusion_prequant_cache.py b/studio/backend/core/inference/diffusion_prequant_cache.py
index a0b98da8b09..7fce4eea92a 100644
--- a/studio/backend/core/inference/diffusion_prequant_cache.py
+++ b/studio/backend/core/inference/diffusion_prequant_cache.py
@@ -601,11 +601,27 @@ def _hub_coords(path: Any) -> Optional[tuple[str, str, str, str]]:
return (
repo,
"/".join(parts[i + 2 :]),
- os.path.basename(os.path.realpath(str(path))),
+ _repo_blob_name(str(path), os.path.join(*parts[:i], "blobs")),
parts[i + 1],
)
+def _repo_blob_name(path: str, blobs_dir: str) -> str:
+ """Name of the ``<repo>/blobs`` entry a snapshot file links to, i.e. its etag. huggingface_hub >= 1.32 links that
+ entry on to a shared store (``<cache>/blobs/<xx>/<xet hash>``), so only the first hop into ``blobs_dir`` counts."""
+ blobs = os.path.normcase(os.path.realpath(blobs_dir))
+ current = path
+ for _ in range(8):
+ if os.path.normcase(os.path.realpath(os.path.dirname(current))) == blobs:
+ return os.path.basename(current)
+ try:
+ target = os.readlink(current)
+ except (OSError, ValueError):
+ break # not a link: a copied file (no symlinks) or the chain's end
+ current = os.path.join(os.path.dirname(current), target)
+ return os.path.basename(os.path.realpath(path))
+
+
def _is_content_hash(name: str) -> bool:
return len(name) in (40, 64) and all(c in "0123456789abcdef" for c in name)
diff --git a/studio/backend/tests/test_diffusion_prequant_cache.py b/studio/backend/tests/test_diffusion_prequant_cache.py
index a5ae598f09d..6539ee8aa69 100644
--- a/studio/backend/tests/test_diffusion_prequant_cache.py
+++ b/studio/backend/tests/test_diffusion_prequant_cache.py
@@ -1011,6 +1011,76 @@ def test_an_online_hit_on_a_hub_snapshot_is_revalidated_against_the_moved_main(
assert C.lookup(fam, scheme, base_repo = "org/tiny") == path
+def _share_blob(tmp_path: Path, link: str) -> str:
+ """Move ``link``'s repo blob into the huggingface_hub >= 1.32 shared store: ``<repo>/blobs/<etag>`` becomes a
+ relative link to ``<cache>/blobs/<xx>/<xet hash>``, so the chain ends on a 64-hex name that is not the etag."""
+ import hashlib
+
+ blob = Path(os.path.dirname(link), os.readlink(link))
+ hub = tmp_path / "hub"
+ xet_hash = hashlib.sha256(b"xet" + blob.read_bytes()).hexdigest()
+ try:
+ from huggingface_hub.utils._shared_blobs import publish_blob_to_shared_store
+ except ImportError:
+ publish_blob_to_shared_store = None
+ if publish_blob_to_shared_store is not None:
+ assert publish_blob_to_shared_store(
+ blob_path = str(blob),
+ xet_hash = xet_hash,
+ cache_dir = str(hub),
+ expected_size = blob.stat().st_size,
+ )
+ else: # older hub: the same layout by hand
+ store = hub / "blobs" / xet_hash[:2] / xet_hash
+ store.parent.mkdir(parents = True)
+ os.replace(blob, store)
+ os.symlink(os.path.relpath(store, blob.parent), blob)
+ assert blob.is_symlink() and os.path.basename(os.path.realpath(link)) == xet_hash
+ return xet_hash
+
+
[email protected]("relative", [False, True])
+def test_a_shared_blob_store_snapshot_is_judged_by_its_repo_blob_etag(
+ env, tmp_path, scheme, monkeypatch, relative
+):
+ import huggingface_hub
+
+ base, model = _write_base(tmp_path)
+ _quantize(model, scheme)
+ link, blob = _hub_snapshot(tmp_path, base)
+ if relative: # the hub's own snapshot links are relative
+ target = os.readlink(link)
+ os.unlink(link)
+ os.symlink(os.path.relpath(target, os.path.dirname(link)), link)
+ xet_hash = _share_blob(tmp_path, link)
+ assert C._hub_coords(link)[2] == blob != xet_hash
+ monkeypatch.setattr(C, "source_shards", lambda *_a, **_k: [link])
+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_OFFLINE", False)
+ C._REMOTE_MEMO.clear()
+ remote = {"etag": blob}
+ monkeypatch.setattr(
+ huggingface_hub,
+ "get_hf_file_metadata",
+ lambda url, **_k: types.SimpleNamespace(etag = remote["etag"]),
+ )
+ fam = _fam(base)
+ path = C.save_runtime_quant(model, fam, base_repo = "org/tiny")
+ assert path and C.lookup(fam, scheme, base_repo = "org/tiny") == path # main unchanged: a hit
+ C._REMOTE_MEMO.clear()
+ remote["etag"] = "f" * 64 # main moved to new weights
+ assert C.lookup(fam, scheme, base_repo = "org/tiny") is None
+
+
+def test_hub_coords_of_legacy_and_copied_snapshots_keep_their_names(tmp_path):
+ base = tmp_path / "base" / "transformer"
+ base.mkdir(parents = True)
+ (base / "diffusion_pytorch_model.safetensors").write_bytes(b"weights")
+ link, blob = _hub_snapshot(tmp_path, tmp_path / "base")
+ assert C._hub_coords(link) == ("org/tiny", "transformer/diffusion_pytorch_model.safetensors", blob, "c" * 40)
+ copy = _copy_snapshot(tmp_path, tmp_path / "base")
+ assert C._hub_coords(copy)[2] == "diffusion_pytorch_model.safetensors"
+
+
def test_the_freshness_check_of_a_gated_base_carries_the_loads_token(env, monkeypatch):
import huggingface_hub