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.

Model loading: skip the root index probe when the repo listing shows no index (#15662)

Daniel Han87c3a31ce505
diff --git a/tests/test_prefetch_listing_skips_404_probes.py b/tests/test_prefetch_listing_skips_404_probes.py
new file mode 100644
index 00000000000..41911864f6c
--- /dev/null
+++ b/tests/test_prefetch_listing_skips_404_probes.py
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Listing the repo first skips only the root index probe it proves absent; patterns and model_info calls match
+the unconditional probes, and a failed or empty listing probes as before."""
+
+import json
+import sys
+import types
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+
+try:
+ import unsloth.models._utils as U
+except ImportError as exc:
+ pytest.skip(f"unsloth is not importable here: {exc}", allow_module_level = True)
+
+ST_MODULES = [
+ {"idx": 0, "name": "0", "path": "", "type": "sentence_transformers.models.Transformer"},
+ {"idx": 1, "name": "1", "path": "1_Pooling", "type": "sentence_transformers.models.Pooling"},
+ {"idx": 2, "name": "2", "path": "2_Dense", "type": "sentence_transformers.models.Dense"},
+]
+COSMOS_INDEX = {
+ "weight_map": {"a": "transformer/m-00001-of-00002.safetensors", "b": "vision/model.safetensors"}
+}
+
+PLAIN = [
+ "config.json",
+ "model.safetensors",
+ "pytorch_model.bin",
+ "tokenizer.json",
+ "extra/model.safetensors",
+]
+SHARDED = [
+ "config.json",
+ "model.safetensors.index.json",
+ "transformer/m-00001-of-00002.safetensors",
+ "vision/model.safetensors",
+ "pytorch_model.bin",
+]
+ST_REPO = [
+ "modules.json",
+ "config.json",
+ "model.safetensors",
+ "pytorch_model.bin",
+ "1_Pooling/config.json",
+ "2_Dense/pytorch_model.bin",
+]
+CONTENT = {"modules.json": ST_MODULES, "model.safetensors.index.json": COSMOS_INDEX}
+
+
+def hub(tmp_path, monkeypatch):
+ """A fake Hub serving *listing*: hf_hub_download 404s on unlisted files, model_info lists them."""
+ import huggingface_hub
+
+ for flag in ("HF_HUB_OFFLINE", "TRANSFORMERS_OFFLINE"):
+ monkeypatch.delenv(flag, raising = False)
+ state = {}
+
+ def fake_hf_hub_download(repo_id, filename, *a, **k):
+ state["downloads"].append((filename, k.get("revision"), k.get("token")))
+ if state["hub_down"] and not state["cached"]:
+ raise OSError("connection refused")
+ if filename in state["listing"] or (state["hub_down"] and filename in state["cached"]):
+ p = tmp_path / filename
+ p.write_text(json.dumps(CONTENT[filename]), encoding = "utf-8")
+ return str(p)
+ raise OSError(f"404 {filename}")
+
+ class _Api:
+ def model_info(self, repo_id, *a, **k):
+ state["infos"].append((k.get("revision"), k.get("token")))
+ if state["hub_down"]:
+ raise OSError("connection refused")
+ listing = state["listing_override"]
+ if listing is None:
+ listing = state["listing"]
+ if listing == "none":
+ return types.SimpleNamespace(siblings = None)
+ return types.SimpleNamespace(
+ siblings = [types.SimpleNamespace(rfilename = f) for f in listing]
+ )
+
+ def fake_snapshot(repo_id, **kwargs):
+ state["snapshot"] = kwargs
+ return str(tmp_path / "snapshot")
+
+ zoo = types.ModuleType("unsloth_zoo.hf_xet_fallback")
+ zoo.snapshot_download_with_xet_fallback = fake_snapshot
+ zoo.DownloadStallError = type("DownloadStallError", (RuntimeError,), {})
+ monkeypatch.setitem(sys.modules, "unsloth_zoo.hf_xet_fallback", zoo)
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_hf_hub_download)
+ monkeypatch.setattr(huggingface_hub, "HfApi", _Api)
+
+ def run(
+ listing,
+ *,
+ hub_down = False,
+ cached = (),
+ listing_override = None,
+ **kwargs,
+ ):
+ state.update(
+ listing = listing,
+ hub_down = hub_down,
+ cached = tuple(cached),
+ listing_override = listing_override,
+ downloads = [],
+ infos = [],
+ snapshot = None,
+ )
+ U.maybe_prefetch_hf_snapshot("org/repo", **{"weights_at_root": True, **kwargs})
+ return dict(state)
+
+ return run
+
+
+def _unconditional(monkeypatch):
+ """The pre-change probe behaviour: the listing never proves a file absent."""
+ monkeypatch.setattr(U, "_listing_lacks", lambda siblings, filename: False)
+
+
+KWARGS = [
+ {},
+ {"revision": "abc123", "token": "hf_x"},
+ {"variant": "fp16"},
+ {"use_safetensors": True},
+ {"use_safetensors": False},
+ {"from_tf": True},
+ {"from_flax": True},
+ {"weights_at_root": False},
+ {"subfolder": "transformer"},
+]
+
+
[email protected]("listing", [PLAIN, SHARDED, ST_REPO], ids = ["plain", "sharded", "st"])
+ "kwargs", KWARGS, ids = lambda k: ",".join(f"{a}={b}" for a, b in k.items()) or "auto"
+)
+def test_patterns_match_the_unconditional_probes(hub, monkeypatch, listing, kwargs):
+ head = hub(listing, **kwargs)
+ with monkeypatch.context() as m:
+ _unconditional(m)
+ base = hub(listing, **kwargs)
+ assert head["snapshot"] == base["snapshot"]
+ assert head["infos"] == base["infos"]
+ dropped = [d for d in base["downloads"] if d not in head["downloads"]]
+ assert all(name not in listing for name, _, _ in dropped), dropped
+ assert [d for d in base["downloads"] if d not in dropped] == head["downloads"]
+
+
+def test_a_plain_repo_skips_only_the_index_probe(hub):
+ got = hub(PLAIN)
+ assert got["downloads"] == [("modules.json", None, None)]
+ assert got["infos"] == [(None, None)]
+
+
+def test_modules_json_is_probed_even_when_the_listing_lacks_it(hub):
+ """Its 404 writes the .no_exist marker FastSentenceTransformer._modules_json_for_gating reads offline."""
+ for listing in (PLAIN, SHARDED):
+ got = hub(listing)
+ assert [d[0] for d in got["downloads"]][:1] == ["modules.json"], listing
+
+
+def test_listed_files_are_still_probed_with_the_load_ref(hub):
+ got = hub(SHARDED + ["modules.json"], revision = "abc123", token = "hf_x")
+ assert got["downloads"] == [
+ ("modules.json", "abc123", "hf_x"),
+ ("model.safetensors.index.json", "abc123", "hf_x"),
+ ]
+ assert got["infos"] == [("abc123", "hf_x")]
+
+
+def test_an_explicit_format_adds_no_model_info_call(hub):
+ for kwargs in ({"use_safetensors": True}, {"use_safetensors": False}, {"from_tf": True}):
+ got = hub(PLAIN, **kwargs)
+ assert got["infos"] == [], kwargs
+ assert [d[0] for d in got["downloads"]] == ["modules.json", "model.safetensors.index.json"]
+
+
+def test_a_hub_outage_still_probes_the_cache(hub, monkeypatch):
+ """model_info fails offline-by-accident while hf_hub_download can still answer from the local cache."""
+ head = hub(ST_REPO, hub_down = True, cached = ("modules.json",))
+ with monkeypatch.context() as m:
+ _unconditional(m)
+ base = hub(ST_REPO, hub_down = True, cached = ("modules.json",))
+ assert [d[0] for d in head["downloads"]] == ["modules.json", "model.safetensors.index.json"]
+ assert head["downloads"] == base["downloads"]
+ assert head["infos"] == base["infos"] == [(None, None)]
+ assert head["snapshot"] == base["snapshot"]
+ assert "*/*.safetensors" not in head["snapshot"]["ignore_patterns"]
+
+
[email protected]("override", [[], "none"], ids = ["empty", "none"])
+def test_an_unusable_listing_probes_as_before(hub, override):
+ got = hub(ST_REPO, listing_override = override)
+ assert [d[0] for d in got["downloads"]] == ["modules.json", "model.safetensors.index.json"]
+ assert got["infos"] == [(None, None)]
+
+
+def test_listing_lacks_is_conservative():
+ sib = [
+ types.SimpleNamespace(rfilename = "a\\modules.json"),
+ types.SimpleNamespace(rfilename = "config.json"),
+ ]
+ assert U._listing_lacks(sib, "modules.json") is True
+ assert (
+ U._listing_lacks([types.SimpleNamespace(rfilename = "modules.json")], "modules.json") is False
+ )
+ assert U._listing_lacks(None, "modules.json") is False
+ assert U._listing_lacks([], "modules.json") is False
+ assert U._listing_lacks(U._SIBLINGS_NOT_FETCHED, "modules.json") is False
+ assert U._listing_lacks([object()], "modules.json") is False
+
+
+if __name__ == "__main__":
+ raise SystemExit(pytest.main([__file__, "-q"]))
diff --git a/tests/test_st_subfolder_weights_are_fetched.py b/tests/test_st_subfolder_weights_are_fetched.py
index fac6de443b5..db980d592b9 100644
--- a/tests/test_st_subfolder_weights_are_fetched.py
+++ b/tests/test_st_subfolder_weights_are_fetched.py
@@ -330,7 +330,7 @@ def test_a_module_path_is_not_read_as_a_glob(modules_json, monkeypatch):
"""Repo filenames go into ignore_patterns verbatim, so a "[" in a name would silently become a
character class and stop matching itself."""
modules_json(EMBEDDINGGEMMA)
- files = ["model.safetensors", "weird[1].bin", "2_Dense/pytorch_model.bin"]
+ files = ["modules.json", "model.safetensors", "weird[1].bin", "2_Dense/pytorch_model.bin"]
patterns = _ignores("org/st-bin-dense", monkeypatch, siblings = files)
kept = _kept(files, patterns)
assert "weird[1].bin" not in kept, patterns
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index e22f4a762b5..d5671a52656 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -3333,6 +3333,33 @@ def _adapter_repo_has_safetensors(
return False
+_SIBLINGS_NOT_FETCHED = object()
+
+
+def _fetch_repo_siblings(
+ model_name,
+ *,
+ token = None,
+ revision = None,
+):
+ """Repo file listing from one model_info call, or None on any failure."""
+ try:
+ from huggingface_hub import HfApi
+ return HfApi().model_info(model_name, revision = revision, token = token).siblings
+ except Exception:
+ return None
+
+
+def _listing_lacks(siblings, filename):
+ """True only when a fetched, non-empty listing lacks *filename*; no listing means unknown."""
+ if siblings is _SIBLINGS_NOT_FETCHED or not siblings:
+ return False
+ try:
+ return all(sibling.rfilename.replace("\\", "/") != filename for sibling in siblings)
+ except Exception:
+ return False
+
+
def _st_weighted_subfolder_paths(
model_name,
*,
@@ -3387,6 +3414,7 @@ def _root_index_weight_subfolders(
token = None,
revision = None,
cache_dir = None,
+ siblings = _SIBLINGS_NOT_FETCHED,
):
"""Subfolders the root safetensors index maps weights into (Cosmos3 keeps every shard under transformer/), else ()."""
try:
@@ -3394,6 +3422,8 @@ def _root_index_weight_subfolders(
from huggingface_hub import hf_hub_download
+ if _listing_lacks(siblings, "model.safetensors.index.json"):
+ return ()
path = hf_hub_download(
model_name,
"model.safetensors.index.json",
@@ -3462,6 +3492,7 @@ def _prefetch_ignore_patterns(
variant = None,
weights_at_root = False,
st_module_paths = (),
+ siblings = _SIBLINGS_NOT_FETCHED,
):
"""ignore_patterns for the prewarm snapshot: the static skip list, minus the checkpoint guard when loading from a checkpoint-* subfolder, minus the weight format the load will not read. use_safetensors is a format allowlist (True -> skip *.bin, False -> skip *.safetensors); auto (None) skips *.bin only when in-scope safetensors are shipped, and from_tf/from_flax keep *.h5/*.msgpack. Suppressed for a whole multi-component snapshot (ST/diffusers repos with per-subfolder weights), since "*" spans "/" so dropping "*.bin" would strip a module's only weight."""
ignore_patterns = [
@@ -3503,18 +3534,20 @@ def _prefetch_ignore_patterns(
else:
# Auto: skip .bin only once in-scope safetensors are confirmed (best-effort; any failure keeps both).
try:
- from huggingface_hub import HfApi
-
- siblings = (
- HfApi()
- .model_info(
- model_name,
- revision = revision,
- token = token,
+ if siblings is _SIBLINGS_NOT_FETCHED:
+ from huggingface_hub import HfApi
+ siblings = (
+ HfApi()
+ .model_info(
+ model_name,
+ revision = revision,
+ token = token,
+ )
+ .siblings
)
- .siblings
- or []
- )
+ elif siblings is None:
+ raise RuntimeError("repo listing unavailable")
+ siblings = siblings or []
# Count only in-scope model-weight safetensors, not adapters or sidecars, variant-matching when a variant is requested, else canonical, to prove the .bin redundant.
has_safetensors = any(
_is_model_weight_safetensors(sibling.rfilename)
@@ -3605,11 +3638,20 @@ def maybe_prefetch_hf_snapshot(
# A sentence-transformers repo can hold weights at the root AND in declared module subfolders. Probed once and shared, since the subdir prune and the format prune both span "/" and would drop those weights.
st_module_paths = ()
index_subfolders = ()
+ repo_listing = {}
if (
weights_at_root
and not (tokenizer_only or adapter_only or gguf_file)
and not (isinstance(subfolder, str) and subfolder.strip("/"))
):
+ # List once up front (the auto format prune would anyway) so the index probe skips a 404. modules.json is
+ # always probed: its 404 writes the .no_exist marker FastSentenceTransformer's offline gate reads.
+ if use_safetensors is None and not (from_tf or from_flax):
+ repo_listing["siblings"] = _fetch_repo_siblings(
+ model_name,
+ token = token,
+ revision = revision,
+ )
st_module_paths = _st_weighted_subfolder_paths(
model_name,
token = token,
@@ -3621,6 +3663,7 @@ def maybe_prefetch_hf_snapshot(
token = token,
revision = revision,
cache_dir = cache_dir,
+ **repo_listing,
)
# tokenizer-only / adapter-only warms allow-list exact files below, so the weight-format ignore list and its model_info call are skipped.
ignore_patterns = (
@@ -3637,6 +3680,7 @@ def maybe_prefetch_hf_snapshot(
variant = variant,
weights_at_root = weights_at_root,
st_module_paths = st_module_paths,
+ **repo_listing,
)
)
# Narrow the warm to what the load reads; every branch still warms root tokenizer, config and custom code so those never fall in-process.