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.

Fetch get_statistics repos concurrently so from_pretrained does not wait on four HF round trips (#14476)

Daniel Han2eb94bcf478f
diff --git a/tests/test_get_statistics_concurrent.py b/tests/test_get_statistics_concurrent.py
new file mode 100644
index 00000000000..8ad97aa96e4
--- /dev/null
+++ b/tests/test_get_statistics_concurrent.py
@@ -0,0 +1,175 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+"""get_statistics() fetches its stats repos concurrently under ONE 120 s limit: one child per
+repo, one after another, blocked from_pretrained ~76 s behind a slow proxy (#14366).
+Helpers loaded by AST; huggingface_hub, psutil and unsloth_zoo are stubs: CPU only, no network, no deps."""
+
+import ast
+import functools
+import logging
+import os
+import pathlib
+import re
+import socket
+import sys
+import threading
+import time
+import types
+
+import pytest
+
+_UTILS = pathlib.Path(__file__).resolve().parents[1] / "unsloth" / "models" / "_utils.py"
+_DELAY = 0.4
+
+
+class _Props:
+ total_memory = 32 * 1024**3
+
+
+def _load(monkeypatch, limiter, snapshot_download):
+ tree = ast.parse(_UTILS.read_text(encoding = "utf-8"))
+ keep = [
+ node
+ for node in tree.body
+ if isinstance(node, ast.FunctionDef) and re.search(r"statistic|has_internet", node.name)
+ ]
+ ns = {
+ "os": os,
+ "functools": functools,
+ "socket": socket,
+ "psutil": types.SimpleNamespace(cpu_count = lambda logical = True: 4),
+ "logger": logging.getLogger("unsloth"),
+ "torch": types.SimpleNamespace(
+ cuda = types.SimpleNamespace(get_device_properties = lambda i: _Props())
+ ),
+ "DEVICE_TYPE": "cuda",
+ "DEVICE_COUNT": 1,
+ "USE_MODELSCOPE": False,
+ }
+ exec(compile(ast.Module(body = keep, type_ignores = []), str(_UTILS), "exec"), ns)
+ ns["has_internet"] = lambda: True
+ zoo = types.ModuleType("unsloth_zoo")
+ zoo.__path__ = []
+ rl = types.ModuleType("unsloth_zoo.rl_environments")
+ rl.execute_with_time_limit = limiter
+ zoo.rl_environments = rl
+ hub = types.ModuleType("huggingface_hub")
+ hub.__path__ = []
+ hub.snapshot_download = snapshot_download
+ hub_utils = types.ModuleType("huggingface_hub.utils")
+ bars = {"disabled": False}
+ hub_utils.are_progress_bars_disabled = lambda: bars["disabled"]
+ hub_utils.disable_progress_bars = lambda: bars.update(disabled = True)
+ hub_utils.enable_progress_bars = lambda: bars.update(disabled = False)
+ hub.utils = hub_utils
+ monkeypatch.setitem(sys.modules, "huggingface_hub", hub)
+ monkeypatch.setitem(sys.modules, "huggingface_hub.utils", hub_utils)
+ monkeypatch.setitem(sys.modules, "unsloth_zoo", zoo)
+ monkeypatch.setitem(sys.modules, "unsloth_zoo.rl_environments", rl)
+ for k in (
+ "UNSLOTH_DISABLE_STATISTICS",
+ "UNSLOTH_USE_MODELSCOPE",
+ "TRANSFORMERS_OFFLINE",
+ "HF_HUB_OFFLINE",
+ ):
+ monkeypatch.delenv(k, raising = False)
+ return ns
+
+
+class _Recorder:
+ def __init__(self, fail = ()):
+ self.calls = []
+ self.limits = []
+ self.fail = set(fail)
+ self.lock = threading.Lock()
+
+ def snapshot_download(
+ self,
+ repo_id,
+ force_download = False,
+ **kwargs,
+ ):
+ with self.lock:
+ self.calls.append((repo_id, force_download))
+ time.sleep(_DELAY)
+ if repo_id.split("/", 1)[1] in self.fail:
+ raise OSError("simulated HTTP error")
+
+ def limiter(self, seconds, **kwargs):
+ self.limits.append((seconds, kwargs))
+ return lambda f: f
+
+
+def rec():
+ return _Recorder()
+
+
+def test_repos_fetched_concurrently(monkeypatch, rec):
+ ns = _load(monkeypatch, rec.limiter, rec.snapshot_download)
+ t0 = time.perf_counter()
+ ns["get_statistics"](False)
+ elapsed = time.perf_counter() - t0
+ names = sorted(r.split("/", 1)[1] for r, _ in rec.calls)
+ assert len(names) == 4 and "repeat" in names and "vram-40" in names and "1" in names
+ assert all(force for _, force in rec.calls)
+ # Sequential would be 4 * _DELAY.
+ assert elapsed < 2.5 * _DELAY, elapsed
+
+
+def test_one_time_limited_child_with_process_backend(monkeypatch, rec):
+ ns = _load(monkeypatch, rec.limiter, rec.snapshot_download)
+ ns["get_statistics"](False)
+ # SIGALRM cannot interrupt worker threads; only a killed child bounds a hung download.
+ assert rec.limits == [(120, {"backend": "process"})]
+
+
+def test_hf_down_message_kept(monkeypatch, rec):
+ def limiter(seconds, **kwargs):
+ def wrap(f):
+ def run():
+ raise TimeoutError("Timed out after 120s")
+
+ return run
+
+ return wrap
+
+ ns = _load(monkeypatch, limiter, rec.snapshot_download)
+ with pytest.raises(TimeoutError, match = "HuggingFace seems to be down"):
+ ns["get_statistics"](False)
+
+
+def test_failed_repo_is_swallowed_and_others_still_fetched(monkeypatch):
+ rec = _Recorder(fail = {"repeat"})
+ ns = _load(monkeypatch, rec.limiter, rec.snapshot_download)
+ assert ns["get_statistics"](False) is None
+ assert len(rec.calls) == 4
+
+
+ "env, local_files_only",
+ [
+ ({"UNSLOTH_DISABLE_STATISTICS": "1"}, False),
+ ({"HF_HUB_OFFLINE": "1"}, False),
+ ({}, True),
+ ],
+)
+def test_opt_outs_make_no_request(monkeypatch, rec, env, local_files_only):
+ ns = _load(monkeypatch, rec.limiter, rec.snapshot_download)
+ for k, v in env.items():
+ monkeypatch.setenv(k, v)
+ ns["get_statistics"](local_files_only)
+ assert rec.calls == [] and rec.limits == []
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index f0ab8b221cc..08a6688e302 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -4404,8 +4404,8 @@ def has_internet(
return False
-def _get_statistics(statistics = None, force_download = True):
- # Basic stats on which environment is in use: a README.md is downloaded from HF, all data public, so broken envs can be detected. Disable with UNSLOTH_DISABLE_STATISTICS.
+def _statistics_environment(statistics = None):
+ # Which environment is in use (kaggle, colab, aws, ...): os.environ and /sys only, no network.
n_cpus = psutil.cpu_count(logical = False)
keynames = "\n" + "\n".join(os.environ.keys())
global USE_MODELSCOPE
@@ -4465,41 +4465,57 @@ def _get_statistics(statistics = None, force_download = True):
statistics = try_vllm_check()
except Exception:
statistics = "other"
+ return statistics
- if statistics is not None:
- import tempfile
- from huggingface_hub import snapshot_download
- from unsloth_zoo.rl_environments import execute_with_time_limit
- if has_internet():
+def _download_statistics(names):
+ # Basic stats on which environment is in use: a README.md is downloaded from HF, all data public, so broken envs can be detected. Disable with UNSLOTH_DISABLE_STATISTICS.
+ # All repos are fetched concurrently in ONE time-limited child: one child per repo, one after another, cost a slow proxy minutes per load (#14366).
+ import tempfile
+ from concurrent.futures import ThreadPoolExecutor
+ from huggingface_hub import snapshot_download
+ from unsloth_zoo.rl_environments import execute_with_time_limit
+
+ if has_internet():
+
+ def download(statistics):
+ with tempfile.TemporaryDirectory(ignore_cleanup_errors = True) as f:
+ snapshot_download(
+ f"unslothai/{statistics}",
+ force_download = True,
+ cache_dir = f,
+ local_dir = f,
+ )
- def stats_check():
- with tempfile.TemporaryDirectory(ignore_cleanup_errors = True) as f:
- snapshot_download(
- f"unslothai/{statistics}",
- force_download = True,
- cache_dir = f,
- local_dir = f,
- )
+ def stats_check():
+ with ThreadPoolExecutor(max_workers = len(names)) as pool:
+ futures = [pool.submit(download, name) for name in names]
+ for future in futures:
+ future.result()
- time_limited_stats_check = execute_with_time_limit(120)(stats_check)
- try:
- time_limited_stats_check()
- except TimeoutError:
- raise TimeoutError(
- "Unsloth: HuggingFace seems to be down after trying for 120 seconds :(\n"
- "Check https://status.huggingface.co/ for more details.\n"
- "As a temporary measure, use modelscope with the same model name ie:\n"
- "```\n"
- "pip install modelscope\n"
- "import os; os.environ['UNSLOTH_USE_MODELSCOPE'] = '1'\n"
- "from unsloth import FastLanguageModel\n"
- "model = FastLanguageModel.from_pretrained('unsloth/gpt-oss-20b')\n"
- "```"
- )
- except Exception:
- logger.debug("Unsloth: stats_check failed with an exception.")
- # Do not retry without a time limit: it would freeze offline.
+ # The process backend kills a hung download; SIGALRM cannot interrupt the worker threads.
+ time_limited_stats_check = execute_with_time_limit(120, backend = "process")(stats_check)
+ try:
+ time_limited_stats_check()
+ except TimeoutError:
+ raise TimeoutError(
+ "Unsloth: HuggingFace seems to be down after trying for 120 seconds :(\n"
+ "Check https://status.huggingface.co/ for more details.\n"
+ "As a temporary measure, use modelscope with the same model name ie:\n"
+ "```\n"
+ "pip install modelscope\n"
+ "import os; os.environ['UNSLOTH_USE_MODELSCOPE'] = '1'\n"
+ "from unsloth import FastLanguageModel\n"
+ "model = FastLanguageModel.from_pretrained('unsloth/gpt-oss-20b')\n"
+ "```"
+ )
+ except Exception:
+ logger.debug("Unsloth: stats_check failed with an exception.")
+ # Do not retry without a time limit: it would freeze offline.
+
+
+def _get_statistics(statistics = None, force_download = True):
+ _download_statistics([_statistics_environment(statistics)])
def get_statistics(local_files_only = False):
@@ -4529,8 +4545,6 @@ def get_statistics(local_files_only = False):
if not are_progress_bars_disabled():
disable_progress_bars()
disabled = True
- _get_statistics(None)
- _get_statistics("repeat", force_download = False)
if DEVICE_TYPE == "xpu":
total_memory = torch.xpu.get_device_properties(0).total_memory
elif DEVICE_TYPE == "npu":
@@ -4555,8 +4569,14 @@ def get_statistics(local_files_only = False):
vram = 80
else:
vram = 96
- _get_statistics(f"vram-{vram}")
- _get_statistics(f"{DEVICE_COUNT if DEVICE_COUNT <= 8 else 9}")
+ _download_statistics(
+ [
+ _statistics_environment(),
+ "repeat",
+ f"vram-{vram}",
+ f"{DEVICE_COUNT if DEVICE_COUNT <= 8 else 9}",
+ ]
+ )
if disabled:
enable_progress_bars()