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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""HF download subprocess (SIGKILL stops chunk threads); resume needs single-stream writes per file, enforced
by HF_HUB_DISABLE_XET=1, HF_HUB_ENABLE_HF_TRANSFER=0 (http), one writer per blob, purge of unprovable
.incomplete blobs."""
from __future__ import annotations
import argparse
import json
import os
import re
import signal
import sys
import threading
import time
import uuid
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Union
_HERE = Path(__file__).resolve().parent
_BACKEND = _HERE.parent.parent
if str(_BACKEND) not in sys.path:
sys.path.insert(0, str(_BACKEND))
# Fresh interpreter: main.py's process-wide network injections do not survive the spawn.
from utils.native_tls import activate_native_tls
from utils.happy_eyeballs import activate_happy_eyeballs
activate_native_tls()
activate_happy_eyeballs()
from hub.utils.snapshot_filters import (
SNAPSHOT_IGNORE_PATTERNS,
)
from hub.utils.gguf_plan import (
GgufVariantPlan,
plan_from_expected_files,
resolve_variant_plan,
sibling_sha256,
)
from hub.utils.state_dir import RepoType
from utils.download_speed_limit import DOWNLOAD_SPEED_LIMIT_ENV, speed_limit_bytes_per_second
from hub.utils.resumable_partials import (
LAST_STOCK_RESUMABLE_VERSION,
_hub_version,
restore_resumable_partials,
)
# Put huggingface_hub's 1.17 HTTP writer back: the SIGKILL then restart loop reads .incomplete for its resume offset, and 1.18+ leaves nothing to read.
_PARTIALS_RESUMABLE = restore_resumable_partials()
# Each file is still one sequential writer under huggingface_hub's per-blob lock, so overlapping files only hides request latency and per-connection caps.
_HTTP_FILE_WORKERS = 4
def _file_workers(mode: str) -> int:
"""``snapshot_download(max_workers=...)``. Xet stays serial: hf_xet fans out inside a file and a killed Xet file restarts from zero. HTTP overlaps only where a killed partial resumes: the stock writer up to 1.17, past it only the writer restored at import."""
version = _hub_version()
resumes = _PARTIALS_RESUMABLE or (bool(version) and version <= LAST_STOCK_RESUMABLE_VERSION)
return _HTTP_FILE_WORKERS if mode == "http" and resumes else 1
# typing.Union, not `str | bool | None`: an alias is evaluated on import and PEP 604 raises below 3.10.
HfTokenArg = Union[str, bool, None]
# Bound the metadata fetch so a stalled connection fails the worker instead of hanging at 0%; the file download itself is governed by huggingface_hub's own timeout.
_METADATA_REQUEST_TIMEOUT = 10.0
_METADATA_RETRY_TIMEOUT = 30.0
_METADATA_RETRY_DELAY = 1.0
def _on_signal(signum, frame):
# 130 is what `classify_exit` maps to the "cancelled" job state.
sys.exit(130)
def _lift_http_ceiling_on_modelscope() -> None:
"""The hub refuses large HTTP transfers on the client side, for Hugging Face's storage; ModelScope's CDN serves them."""
from huggingface_hub import constants
from utils.hub_settings import MODELSCOPE, active_source
if active_source() == MODELSCOPE:
constants.MAX_HTTP_DOWNLOAD_SIZE = sys.maxsize
class _Pacer:
def __init__(self, bytes_per_second: float):
self._rate = bytes_per_second
self._lock = threading.Lock()
self._ready_at = time.monotonic()
def wait(self, nbytes: int) -> None:
with self._lock:
now = time.monotonic()
# A quarter second of credit, so sleep overshoot (15ms on Windows) does not erode the rate.
self._ready_at = max(self._ready_at, now - 0.25) + nbytes / self._rate
delay = self._ready_at - now
if delay > 0:
time.sleep(delay)
def _paced_stream(inner, pacer: _Pacer):
# iter_raw demands its own library's SyncByteStream: httpx on hub 1.x, httpx2 on 2.x.
base = sys.modules[type(inner).__module__.partition(".")[0]].SyncByteStream
class _PacedStream(base):
def __iter__(self):
for chunk in inner:
pacer.wait(len(chunk))
yield chunk
def close(self) -> None:
inner.close()
return _PacedStream()
def _install_speed_limit() -> None:
bytes_per_second = speed_limit_bytes_per_second(os.environ.get(DOWNLOAD_SPEED_LIMIT_ENV))
if not bytes_per_second:
return
try:
from huggingface_hub import constants, set_client_factory
from huggingface_hub.utils._http import default_client_factory
except ImportError:
print(
"download speed limit needs huggingface_hub 1.x; downloading at full speed",
file = sys.stderr,
)
return
pacer = _Pacer(bytes_per_second)
# http_get writes (and progress reads) per DOWNLOAD_CHUNK_SIZE: 10 MB stalls the bar for minutes at a low limit.
constants.DOWNLOAD_CHUNK_SIZE = max(
64 * 1024, min(constants.DOWNLOAD_CHUNK_SIZE, int(bytes_per_second // 2))
)
def pace(response) -> None:
response.stream = _paced_stream(response.stream, pacer)
def paced_client():
client = default_client_factory()
hooks = client.event_hooks
client.event_hooks = {"request": hooks["request"], "response": [*hooks["response"], pace]}
return client
set_client_factory(paced_client)
def _install_signal_handlers() -> None:
signal.signal(signal.SIGTERM, _on_signal)
signal.signal(signal.SIGINT, _on_signal)
sigpipe = getattr(signal, "SIGPIPE", None)
if sigpipe is not None:
signal.signal(sigpipe, _on_signal)
def _parent_poll_seconds() -> float:
raw = os.environ.get("UNSLOTH_HF_WORKER_PARENT_POLL_SECONDS")
if raw:
try:
value = float(raw)
if value > 0:
return value
except ValueError:
pass
return 2.0
def _protected_blob_hashes() -> frozenset[str]:
"""Blob hashes a concurrent same-repo peer is writing (passed by the backend as a plain env list). Excluded from this worker's purge so a shared ``.incomplete`` (e.g. a bundled mmproj) is never deleted under the peer."""
raw = os.environ.get("UNSLOTH_PROTECTED_BLOB_HASHES", "")
return frozenset(h for h in raw.split(",") if h)
def _parent_is_alive(parent_pid: int) -> bool:
"""Probes the recorded parent PID, never getppid(), so reparenting to init still reads as dead."""
if sys.platform == "win32":
import ctypes
from ctypes import wintypes
SYNCHRONIZE = 0x00100000
WAIT_OBJECT_0 = 0x0
ERROR_INVALID_PARAMETER = 87
kernel32 = ctypes.WinDLL("kernel32", use_last_error = True)
kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
kernel32.OpenProcess.restype = wintypes.HANDLE
kernel32.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
kernel32.WaitForSingleObject.restype = wintypes.DWORD
kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
kernel32.CloseHandle.restype = wintypes.BOOL
ctypes.set_last_error(0)
handle = kernel32.OpenProcess(SYNCHRONIZE, False, parent_pid)
if not handle:
return ctypes.get_last_error() != ERROR_INVALID_PARAMETER
try:
return kernel32.WaitForSingleObject(handle, 0) != WAIT_OBJECT_0
finally:
kernel32.CloseHandle(handle)
try:
os.kill(parent_pid, 0)
except ProcessLookupError:
return False
except OSError:
return True
return True
def _terminate_orphaned_self(heartbeat: str | None = None) -> None:
# Hard exit from the watchdog thread: a self-SIGTERM would be deferred while the main thread is GIL-blocked in a C socket read, and the partial resumes byte-exact with atomic marker writes.
try:
print(
"Parent process exited; stopping orphaned download worker.",
file = sys.stderr,
)
sys.stderr.flush()
except Exception:
pass
# The parent owns heartbeat cleanup and is gone, so nobody else will remove it.
try:
from hub.utils.download_heartbeat import remove
remove(heartbeat)
except Exception:
pass
os._exit(130)
def _install_parent_death_watchdog(parent_pid: int | None, heartbeat: str | None = None) -> None:
if not parent_pid or parent_pid <= 0:
return
interval = _parent_poll_seconds()
def _watch() -> None:
while True:
try:
alive = _parent_is_alive(parent_pid)
except Exception:
alive = True
if not alive:
_terminate_orphaned_self(heartbeat)
return
time.sleep(interval)
threading.Thread(
target = _watch,
name = "parent-death-watchdog",
daemon = True,
).start()
# One job per process: once the Hub has refused the token, every later read skips it.
_REJECTED_TOKEN: str | None = None
def _hf_token_arg(hf_token: str | None) -> HfTokenArg:
if not hf_token or hf_token == _REJECTED_TOKEN:
return False
return hf_token
def _metadata_read(fn, hf_token: str | None, *args, **kwargs):
"""A metadata read that retries once anonymously when the Hub rejects the token (HTTP 401)."""
global _REJECTED_TOKEN
from hub.utils.hf_tokens import (
call_hub_with_anonymous_retry,
collecting_hub_token_rejections,
saved_token_rejected,
)
token = _hf_token_arg(hf_token)
with collecting_hub_token_rejections():
result = call_hub_with_anonymous_retry(fn, token, *args, **kwargs)
rejected = token is not False and saved_token_rejected(token)
if hf_token and rejected:
_REJECTED_TOKEN = hf_token
print(
"Hugging Face rejected the saved token (HTTP 401); downloading without it. "
"Update or remove the token in Settings if it has expired or been revoked.",
file = sys.stderr,
)
return result
def _retry_metadata_fetch(repo_id: str, fetch, *, label: str):
for attempt, timeout in enumerate((_METADATA_REQUEST_TIMEOUT, _METADATA_RETRY_TIMEOUT)):
try:
return fetch(timeout)
except Exception as e:
if attempt == 1:
raise
print(
f"{label} request failed for {repo_id} " f"({type(e).__name__}: {e}); retrying.",
file = sys.stderr,
)
time.sleep(_METADATA_RETRY_DELAY)
raise RuntimeError(f"{label} unavailable for {repo_id}")
_RESOLVED_COMMITS: dict[str, str] = {}
# huggingface_hub's REGEX_COMMIT_HASH: only a full sha is taken as already resolved.
_COMMIT_SHA = re.compile(r"[0-9a-f]{40}")
def _model_info_with_retry(repo_id: str, hf_token: str | None):
from huggingface_hub import model_info as hf_model_info
info = _retry_metadata_fetch(
repo_id,
lambda timeout: _metadata_read(
hf_model_info,
hf_token,
repo_id,
timeout = timeout,
files_metadata = True,
),
label = "Metadata",
)
commit = getattr(info, "sha", None)
if isinstance(commit, str) and commit:
_RESOLVED_COMMITS[repo_id] = commit
return info
def _read_ref(ref: Path):
try:
return ref.read_text(encoding = "utf-8")
except (OSError, UnicodeDecodeError):
return None
def _write_ref(ref: Path, commit: str) -> None:
"""Atomic, with even transient files outside refs/ (scan_cache_dir reads branch names)."""
ref.parent.mkdir(parents = True, exist_ok = True)
tmp = ref.parent.parent / f".studio-{ref.name}.{uuid.uuid4().hex}.tmp"
try:
tmp.write_text(commit, encoding = "utf-8")
os.replace(tmp, ref)
except BaseException:
try:
tmp.unlink()
except OSError:
pass
raise
def _pinned_revision(repo_type: RepoType, repo_id: str, commit) -> dict:
"""Select an immutable revision without exposing an unverified snapshot through refs/main."""
if not isinstance(commit, str) or not _COMMIT_SHA.fullmatch(commit):
return {}
return {"revision": commit}
def _model_manifest_attestation(commit) -> dict:
"""Date copied files only when metadata resolved an immutable revision."""
if isinstance(commit, str) and _COMMIT_SHA.fullmatch(commit):
return {"commit_hash": commit, "metadata_derived": True}
return {}
# Among cooperating workers, the latest-started verified success owns main.
# SHA transfers leave main unchanged; native owner locks identify abandoned attempts.
# Journal/ref writes are not a power-loss transaction and cannot coordinate external writers.
@dataclass(frozen = True)
class _MainRefState:
next_sequence: int = 1
published_sequence: int = 0
published_commit: str | None = None
attempts: tuple[tuple[int, str], ...] = ()
owners: tuple[tuple[int, str], ...] = ()
def _start_main_ref(state: _MainRefState, commit: str, owner: str) -> tuple[_MainRefState, int]:
sequence = state.next_sequence
return _MainRefState(
sequence + 1,
state.published_sequence,
state.published_commit,
(*state.attempts, (sequence, commit)),
(*state.owners, (sequence, owner)),
), sequence
def _finish_main_ref(state: _MainRefState, sequence: int, verified: bool) -> _MainRefState:
attempts = dict(state.attempts)
commit = attempts.pop(sequence)
publish = verified and sequence > state.published_sequence
return _MainRefState(
state.next_sequence,
sequence if publish else state.published_sequence,
commit if publish else state.published_commit,
tuple(attempts.items()),
tuple((seq, owner) for seq, owner in state.owners if seq != sequence),
)
def _prune_main_ref_attempts(state: _MainRefState, owner_is_alive) -> _MainRefState:
for sequence, owner in state.owners:
if not owner_is_alive(owner):
state = _finish_main_ref(state, sequence, False)
return state
def _main_ref_owner_lock(lock_dir: Path, owner: str):
# Soft locks survive SIGKILL and cannot prove ownership.
from filelock import FileLock, SoftFileLock
lock = FileLock(lock_dir / f"studio-main-owner-{owner}.lock")
lock.acquire(timeout = 0)
if isinstance(lock, SoftFileLock):
lock.release()
raise OSError("refs/main attempt ownership requires native file locks")
return lock
def _main_ref_owner_is_alive(lock_dir: Path, owner: str) -> bool:
from filelock import Timeout
try:
lock = _main_ref_owner_lock(lock_dir, owner)
except Timeout:
return True
lock.release()
Path(lock.lock_file).unlink(missing_ok = True)
return False
def _load_main_ref_state(path: Path, previous: str | None) -> _MainRefState:
try:
raw = path.read_text(encoding = "utf-8")
except FileNotFoundError:
return _MainRefState(published_commit = previous)
# Resetting a damaged journal could reuse a live peer's sequence.
data = json.loads(raw)
state = _MainRefState(
data["next_sequence"],
data["published_sequence"],
data["published_commit"],
tuple(tuple(attempt) for attempt in data["attempts"]),
tuple(tuple(owner) for owner in data.get("owners", ())),
)
if (
type(state.next_sequence) is not int
or type(state.published_sequence) is not int
or not 0 <= state.published_sequence < state.next_sequence
or not (state.published_commit is None or isinstance(state.published_commit, str))
or (state.published_sequence > 0 and state.published_commit is None)
or len(dict(state.attempts)) != len(state.attempts)
or any(
type(sequence) is not int
or not 0 < sequence < state.next_sequence
or not isinstance(commit, str)
or not _COMMIT_SHA.fullmatch(commit)
for sequence, commit in state.attempts
)
or len(dict(state.owners)) != len(state.owners)
or set(dict(state.owners)) != set(dict(state.attempts))
or any(
type(sequence) is not int
or not isinstance(owner, str)
or re.fullmatch(r"[0-9a-f]{32}", owner) is None
for sequence, owner in state.owners
)
):
raise ValueError(f"Invalid refs/main publication state: {path}")
return state
def _update_main_ref(
repo_dir: Path,
*,
commit: str | None = None,
sequence: int | None = None,
verified: bool = False,
on_publish = None,
owner: str | None = None,
) -> int | None:
"""Persist one decision under the lock; no network, transfer or verification here."""
from huggingface_hub.utils import WeakFileLock
lock_dir = repo_dir.parent / ".locks" / repo_dir.name
lock_dir.mkdir(parents = True, exist_ok = True)
state_path = lock_dir / "studio-main.json"
ref = repo_dir / "refs" / "main"
with WeakFileLock(lock_dir / "studio-main.lock"):
previous = _read_ref(ref)
before = _prune_main_ref_attempts(
_load_main_ref_state(state_path, previous),
lambda attempt_owner: _main_ref_owner_is_alive(lock_dir, attempt_owner),
)
if commit is not None:
if owner is None:
raise ValueError("refs/main attempt requires a live owner")
state, sequence = _start_main_ref(before, commit, owner)
else:
state = _finish_main_ref(before, sequence, verified)
try:
_write_ref(state_path, json.dumps(asdict(state)))
if verified and previous != state.published_commit:
_write_ref(ref, state.published_commit)
except BaseException:
if verified:
# A failed publication must not disown an older active success.
failed = _finish_main_ref(before, sequence, False)
_write_ref(state_path, json.dumps(asdict(failed)))
if _read_ref(ref) != previous:
if previous is None:
ref.unlink(missing_ok = True)
else:
_write_ref(ref, previous)
raise
# Reclaim under this lock so no new peer loses its snapshot.
if verified and state.published_sequence == sequence and on_publish is not None:
on_publish(state)
return sequence
def _pinned_snapshot_download(
snapshot_download,
commit,
*,
expected_files = (),
verify = None,
on_publish = None,
**kwargs,
):
"""Transfer and verify outside the lock; publish and reclaim under it."""
from hub.utils.snapshot_reuse import repo_cache_dir
repo_type, repo_id = kwargs.get("repo_type") or "model", kwargs["repo_id"]
repo_dir = repo_cache_dir(repo_type, repo_id)
if commit is None:
commit = _read_ref(repo_dir / "refs" / "main")
pin = _pinned_revision(repo_type, repo_id, commit)
if not pin:
raise OSError(
f"Cannot resolve an immutable revision for {repo_id}; retry when metadata is available"
)
lock_dir = repo_dir.parent / ".locks" / repo_dir.name
lock_dir.mkdir(parents = True, exist_ok = True)
owner = uuid.uuid4().hex
owner_lock = _main_ref_owner_lock(lock_dir, owner)
try:
sequence = _update_main_ref(repo_dir, commit = commit, owner = owner)
try:
path = snapshot_download(**kwargs, **pin)
if Path(path).name != commit or not Path(path).is_dir():
raise OSError(
f"Pinned download of {repo_id} returned a snapshot other than {commit}"
)
# Legacy Hub can return an existing incomplete snapshot after metadata
# fails. Check before making it visible to offline repo-ID resolution.
if expected_files:
from hub.utils.download_manifest import Manifest, verify_against_disk
manifest = Manifest(repo_type, repo_id, None, "", tuple(expected_files))
if not verify_against_disk(manifest, Path(path)).ok:
raise OSError(f"Pinned snapshot of {repo_id} is incomplete")
if verify is not None:
verify(path)
except BaseException:
_update_main_ref(repo_dir, sequence = sequence)
raise
_update_main_ref(repo_dir, sequence = sequence, verified = True, on_publish = on_publish)
return path
finally:
owner_lock.release()
Path(owner_lock.lock_file).unlink(missing_ok = True)
def _rebuild_header_only_ggufs(
repo_type: RepoType, repo_id: str, commit_hash, expected_files: list, hf_token: str | None
) -> None:
"""Rebuild each pending image / video GGUF whose new revision changed only its header from the older snapshot's
copy. Chat, audio and other GGUFs keep the plain download; the gate is read only when a rebuild is possible."""
try:
from hub.utils.download_manifest import expected_path_is_safe, normalized_commit_hash
from hub.utils.gguf_header_delta import (
_is_media_gguf,
delta_enabled,
hub_range_fetcher,
rebuild_from_older_snapshot,
)
from hub.utils.snapshot_reuse import repo_cache_dir
commit = normalized_commit_hash(commit_hash)
if not commit or not delta_enabled():
return
repo_dir = repo_cache_dir(repo_type, repo_id)
protected = _protected_blob_hashes()
for item in expected_files:
path = getattr(item, "path", None)
digest = getattr(item, "sha256", None)
size = int(getattr(item, "size", 0) or 0)
if not expected_path_is_safe(path) or not str(path).lower().endswith(".gguf"):
continue
result = rebuild_from_older_snapshot(
repo_dir,
commit,
path,
size,
digest or "",
hub_range_fetcher(repo_id, path, hf_token, repo_type = repo_type, revision = commit),
protected_blob_hashes = protected,
media_gate = lambda old, path = path: _is_media_gguf(old, repo_id, path),
)
if result.placed:
print(
f"Rebuilt {path} from the cached older copy (only its header changed): fetched "
f"{result.fetched_bytes / 1e6:.2f} MB instead of {result.size / 1e9:.2f} GB.",
file = sys.stderr,
)
except Exception as exc: # noqa: BLE001 - an optimisation only: snapshot_download fetches what is left
print(f"GGUF header reuse skipped for {repo_id}: {exc}", file = sys.stderr)
def _reuse_unchanged_files(
repo_type: RepoType, repo_id: str, commit_hash, expected_files: list, hf_token: str | None
) -> list:
"""Link files unchanged since an older snapshot into this commit; return the files still to download."""
from hub.utils.snapshot_reuse import paths_in_snapshot, reuse_unchanged_snapshot_files
if not expected_files or not commit_hash:
return list(expected_files)
result = reuse_unchanged_snapshot_files(
repo_type,
repo_id,
commit_hash,
expected_files,
# Always hash locally: a Hub digest proves what the old commit served, not what is on disk now.
protected_blob_hashes = _protected_blob_hashes(),
)
if result.reused:
print(
f"Reused {len(result.reused)} unchanged file(s) ({result.reused_bytes / 1e9:.2f} GB) "
f"from an older snapshot of {repo_id} instead of downloading them again.",
file = sys.stderr,
)
_rebuild_header_only_ggufs(repo_type, repo_id, commit_hash, expected_files, hf_token)
# Files an earlier attempt placed are skipped by snapshot_download and have no blob for the preflight to discount.
present = paths_in_snapshot(
repo_type, repo_id, commit_hash, [getattr(f, "path", None) for f in expected_files]
)
return [f for f in expected_files if getattr(f, "path", None) not in present]
def _dataset_info_with_retry(repo_id: str, hf_token: str | None):
from huggingface_hub import HfApi
api = HfApi()
return _retry_metadata_fetch(
repo_id,
lambda timeout: _metadata_read(
api.dataset_info,
hf_token,
repo_id,
timeout = timeout,
files_metadata = True,
),
label = "Dataset metadata",
)
# Tied to drain_stderr_excerpt's 500-byte head/tail window: listing every expected file would blow past it and lose the diagnostic.
_VERIFY_PATH_LIST_CAP = 10
def _format_path_list(paths: tuple[str, ...], cap: int = _VERIFY_PATH_LIST_CAP) -> str:
if len(paths) <= cap:
return ", ".join(paths)
head = ", ".join(paths[:cap])
return f"{head}, ... and {len(paths) - cap} more"
def _verify_completed_download(
repo_type: RepoType,
repo_id: str,
variant: str | None,
snapshot_path: str,
*,
metadata_unavailable: bool = False,
) -> None:
"""Verify every manifest file is on disk at its declared size, exiting nonzero with a diagnostic if not. No-op when no manifest exists: the manifest write is best-effort, so absence means "verification unavailable, trust snapshot_download's exit code"."""
from hub.utils import download_manifest
manifest = download_manifest.read_manifest(repo_type, repo_id, variant)
if manifest is None:
return
result = download_manifest.verify_against_disk(
manifest,
Path(snapshot_path),
)
if result.ok:
return
label = f"{repo_id}{f' [{variant}]' if variant else ''}"
if metadata_unavailable:
print(
f"Could not reach Hugging Face for {label} and the copy on disk is "
f"incomplete ({len(result.missing)} file(s) missing, "
f"{len(result.size_mismatched)} the wrong size). Access to a private "
"or restricted repo may have been lost (HF token removed or "
"changed), the connection dropped, or Hugging Face is temporarily "
"unavailable. Set a valid HF token or reconnect, then resume the "
"download.",
file = sys.stderr,
)
else:
print(
f"Verification failed for {label}: snapshot_download completed but "
f"{len(result.missing)} expected file(s) are missing and "
f"{len(result.size_mismatched)} have incorrect size on disk.",
file = sys.stderr,
)
if result.missing:
print(
f"Missing: {_format_path_list(result.missing)}",
file = sys.stderr,
)
if result.size_mismatched:
print(
f"Size mismatched: {_format_path_list(result.size_mismatched)}",
file = sys.stderr,
)
sys.exit(1)
def _preflight_disk_space(repo_type: str, repo_id: str, expected_files: list) -> None:
"""Fail fast when the active HF cache filesystem cannot hold what is left to download. Fail-open: any inability to size the work or read free space skips the check, so a real download is never blocked by an estimation gap."""
import shutil
from hub.utils.download_registry import existing_blob_bytes
from hub.utils.hf_cache_state import hf_cache_root
try:
size_by_hash: dict[str, int] = {}
unhashed_bytes = 0
for expected in expected_files:
size = int(getattr(expected, "size", 0) or 0)
if size <= 0:
continue
blob_hash = getattr(expected, "sha256", None)
if blob_hash:
# Dedup by content hash: a blob listed under two filenames is written once, so count it once.
size_by_hash[blob_hash] = size
else:
unhashed_bytes += size
total_expected = sum(size_by_hash.values()) + unhashed_bytes
if total_expected <= 0:
return
already_have = existing_blob_bytes(
repo_type,
repo_id,
frozenset(size_by_hash),
)
remaining = max(0, total_expected - already_have)
if remaining <= 0:
return
root = hf_cache_root(create = True)
if root is None:
return
free = shutil.disk_usage(root).free
except Exception:
return
if free < remaining:
print(
f"Not enough disk space to download {repo_id}: need about "
f"{remaining / 1e9:.1f} GB free in {root}, but only "
f"{free / 1e9:.1f} GB is available. Free up space and "
"try again.",
file = sys.stderr,
)
sys.exit(1)
def _snapshot_download_plan(info) -> tuple[list[str], list]:
from hub.utils.download_manifest import ExpectedFile
from hub.utils.snapshot_filters import (
resolve_snapshot_ignore_patterns_for_files,
snapshot_download_siblings,
)
filenames = [s.rfilename for s in info.siblings if isinstance(s.rfilename, str)]
filtered = snapshot_download_siblings(info.siblings)
expected_files = [
ExpectedFile(
path = s.rfilename,
size = int(getattr(s, "size", 0) or 0),
sha256 = sibling_sha256(s),
)
for s in filtered
if isinstance(s.rfilename, str)
]
return resolve_snapshot_ignore_patterns_for_files(filenames), expected_files
def _dataset_expected_files(info) -> list:
from hub.utils.download_manifest import ExpectedFile
return [
ExpectedFile(
path = s.rfilename,
size = int(getattr(s, "size", 0) or 0),
sha256 = sibling_sha256(s),
)
for s in info.siblings
if isinstance(s.rfilename, str)
]
def _exact_dataset_snapshot_target(repo_id: str, snapshot_path: str, commit_hash):
from hub.utils import download_manifest
from hub.utils.state_dir import repo_cache_basename
normalized_commit = download_manifest.normalized_commit_hash(commit_hash)
if normalized_commit is None:
return None
try:
snapshot = Path(snapshot_path).expanduser().resolve(strict = True)
repo_dir = snapshot.parent.parent
hub_cache = repo_dir.parent.resolve(strict = True)
except (OSError, RuntimeError, ValueError):
return None
if (
snapshot.name != normalized_commit
or snapshot.parent.name != "snapshots"
or repo_dir.name.casefold() != repo_cache_basename("dataset", repo_id).casefold()
):
return None
return normalized_commit, snapshot, hub_cache
def _write_dataset_completion_from_metadata(
repo_id: str, snapshot_path: str, commit_hash, expected_files, mode: str
) -> bool:
from hub.utils import download_manifest
target = _exact_dataset_snapshot_target(repo_id, snapshot_path, commit_hash)
files = tuple(expected_files)
if target is None or not files:
return False
normalized_commit, snapshot, hub_cache = target
verification_manifest = download_manifest.Manifest(
repo_type = "dataset",
repo_id = repo_id,
variant = None,
started_at = "",
expected_files = files,
transport = mode,
hub_cache = str(hub_cache),
version = 2,
commit_hash = normalized_commit,
metadata_derived = True,
)
if not download_manifest.verify_against_disk(
verification_manifest,
snapshot,
).ok:
return False
return download_manifest.write_dataset_completion(
repo_id,
normalized_commit,
files,
mode,
hub_cache = hub_cache,
)
def _recover_manifest_after_download(
repo_type: RepoType,
repo_id: str,
snapshot_path: str,
mode: str,
*,
fetch_info,
expected_files_from_info,
label: str = "",
) -> None:
from hub.utils import download_manifest
from hub.utils.hf_cache_state import has_active_incomplete_blobs
existing = download_manifest.read_manifest(repo_type, repo_id, None)
if existing is not None:
if repo_type != "dataset":
return
if existing.metadata_derived and existing.commit_hash is not None:
_write_dataset_completion_from_metadata(
repo_id,
snapshot_path,
existing.commit_hash,
existing.expected_files,
mode,
)
return
try:
info = fetch_info()
expected_files = expected_files_from_info(info)
manifest_kwargs = {}
if repo_type == "dataset":
exact_target = _exact_dataset_snapshot_target(
repo_id,
snapshot_path,
getattr(info, "sha", None),
)
if exact_target is not None:
manifest_kwargs = {
"commit_hash": exact_target[0],
"metadata_derived": True,
}
_write_dataset_completion_from_metadata(
repo_id,
snapshot_path,
getattr(info, "sha", None),
expected_files,
mode,
)
if download_manifest.write_manifest(
repo_type,
repo_id,
None,
expected_files,
mode,
**manifest_kwargs,
):
return
reason = "manifest write failed"
except Exception as e:
reason = f"{type(e).__name__}: {e}"
if existing is not None:
return
if has_active_incomplete_blobs(repo_type, repo_id):
print(
f"{label}could not reach Hugging Face for {repo_id} and the copy on "
"disk is still incomplete. Access to a private or restricted repo may "
"have been lost (HF token removed or changed), the connection dropped, "
"or Hugging Face is temporarily unavailable. Set a valid HF token or "
"reconnect, then resume the download.",
file = sys.stderr,
)
sys.exit(1)
fallback_files = download_manifest.expected_files_from_snapshot_dir(Path(snapshot_path))
if fallback_files and download_manifest.write_manifest(
repo_type,
repo_id,
None,
fallback_files,
mode,
):
print(
f"{label}could not record the metadata manifest for {repo_id}, "
"recorded one from the downloaded files so completion "
f"is tracked ({reason})",
file = sys.stderr,
)
else:
print(
f"{label}could not record the metadata manifest for {repo_id}, "
f"{download_manifest.MANIFEST_DEGRADED_MARKER} ({reason})",
file = sys.stderr,
)
def _download_snapshot(
repo_id: str,
hf_token: str | None,
mode: str,
tqdm_class: type | None = None,
) -> None:
from huggingface_hub import snapshot_download
from hub.utils.download_registry import prepare_cache_for_transport
from hub.utils import download_manifest
try:
info = _model_info_with_retry(repo_id, hf_token)
except Exception as e:
print(
f"metadata unavailable, downloading full snapshot for {repo_id} "
f"({type(e).__name__}: {e})",
file = sys.stderr,
)
info = None
commit = getattr(info, "sha", None)
download_manifest.clear_cancel_marker("model", repo_id, None)
if info is not None:
ignore_patterns, expected_files = _snapshot_download_plan(info)
# The manifest verifies the finalized files under snapshots/, which both transports produce identically; XET's block-level dedup lives only in the chunk cache.
download_manifest.write_manifest(
"model",
repo_id,
None,
expected_files,
mode,
**_model_manifest_attestation(getattr(info, "sha", None)),
)
else:
ignore_patterns = list(SNAPSHOT_IGNORE_PATTERNS)
manifest = download_manifest.read_manifest("model", repo_id, None)
expected_files = list(manifest.expected_files) if manifest is not None else []
if manifest is not None and manifest.metadata_derived:
commit = download_manifest.normalized_commit_hash(manifest.commit_hash)
purged = prepare_cache_for_transport("model", repo_id, mode)
if purged:
print(
f"Purged {purged} untrusted partial blob(s) for {repo_id} "
f"before starting {mode} download.",
file = sys.stderr,
)
to_download = _reuse_unchanged_files(
"model", repo_id, getattr(info, "sha", None), expected_files, hf_token
)
_preflight_disk_space("model", repo_id, to_download)
def verify(snapshot_path: str) -> None:
if info is None:
_recover_manifest_after_download(
"model",
repo_id,
snapshot_path,
mode,
fetch_info = lambda: _model_info_with_retry(repo_id, hf_token),
expected_files_from_info = lambda recovered: _snapshot_download_plan(recovered)[1],
)
_verify_completed_download(
"model",
repo_id,
None,
snapshot_path,
metadata_unavailable = info is None,
)
_pinned_snapshot_download(
snapshot_download,
commit,
expected_files = expected_files,
verify = verify,
repo_id = repo_id,
token = _hf_token_arg(hf_token),
ignore_patterns = ignore_patterns,
max_workers = _file_workers(mode),
tqdm_class = tqdm_class,
)
def _gguf_variant_target_plan(
repo_id: str, variant: str, hf_token: str | None
) -> GgufVariantPlan | None:
try:
info = _model_info_with_retry(repo_id, hf_token)
except Exception as e:
print(
f"metadata unavailable, cannot resolve GGUF variant '{variant}' "
f"for {repo_id} ({type(e).__name__}: {e})",
file = sys.stderr,
)
raise RuntimeError(
f"Metadata unavailable while resolving GGUF variant '{variant}' " f"for {repo_id}"
) from e
# plan_for_variant (inside resolve_variant_plan), not .get: a repo filing every variant under one shared container qualifies every key, so a stored pin or an explicit repo:Q4_K_M missed the map and the worker exited with "No GGUF shards matching variant".
return resolve_variant_plan(info.siblings, variant)
def _download_gguf_variant(
repo_id: str,
variant: str,
hf_token: str | None,
mode: str,
tqdm_class: type | None = None,
) -> None:
from huggingface_hub import snapshot_download
from hub.utils.download_registry import prepare_cache_for_transport
from hub.utils.hf_cache_state import has_active_incomplete_blobs
from hub.utils import download_manifest
metadata_unavailable = False
try:
plan = _gguf_variant_target_plan(repo_id, variant, hf_token)
except RuntimeError:
plan = None
metadata_unavailable = True
commit = None if metadata_unavailable else _RESOLVED_COMMITS.get(repo_id)
if not metadata_unavailable:
if plan is None:
print(
f"No GGUF shards matching variant '{variant}' in {repo_id}",
file = sys.stderr,
)
sys.exit(1)
targets = list(plan.target_filenames)
expected_files = list(plan.expected_files)
main_blob_hashes = plan.main_hashes
companion_blob_hashes = plan.companion_hashes
download_manifest.write_manifest(
"model",
repo_id,
variant,
expected_files,
mode,
**_model_manifest_attestation(commit),
)
else:
# Metadata unreachable: resume the exact shards the original attempt recorded so snapshot_download can range over the surviving .incomplete blobs.
manifest = download_manifest.read_manifest("model", repo_id, variant)
if manifest is None or not manifest.expected_files:
print(
f"Metadata unavailable and no manifest to resume GGUF "
f"variant '{variant}' for {repo_id}",
file = sys.stderr,
)
sys.exit(1)
plan = plan_from_expected_files(variant, manifest.expected_files)
if manifest.metadata_derived:
commit = download_manifest.normalized_commit_hash(manifest.commit_hash)
targets = list(plan.target_filenames)
expected_files = list(plan.expected_files)
download_manifest.write_manifest(
"model",
repo_id,
variant,
expected_files,
mode,
**_model_manifest_attestation(commit),
)
main_blob_hashes = plan.main_hashes
companion_blob_hashes = plan.companion_hashes
print(
f"Metadata unavailable; resuming GGUF variant '{variant}' for "
f"{repo_id} from the existing manifest.",
file = sys.stderr,
)
download_manifest.clear_cancel_marker("model", repo_id, variant)
purge_blob_hashes = main_blob_hashes
if not main_blob_hashes:
if has_active_incomplete_blobs("model", repo_id):
print(
f"GGUF variant '{variant}' for {repo_id} has partial cache state "
"but no resolvable blob hashes; delete the partial download or "
"retry when metadata is available.",
file = sys.stderr,
)
sys.exit(1)
purge_blob_hashes = frozenset()
print(
f"GGUF variant '{variant}' for {repo_id} has no resolvable blob "
"hashes; starting without partial cache reuse.",
file = sys.stderr,
)
# Main quant blobs are owned by this variant; the shared mmproj companion has its own marker and is never purged while a concurrent peer is writing it.
purged = prepare_cache_for_transport(
"model",
repo_id,
mode,
variant,
only_blob_hashes = purge_blob_hashes,
companion_blob_hashes = companion_blob_hashes,
protected_blob_hashes = _protected_blob_hashes(),
)
if purged:
print(
f"Purged {purged} untrusted partial blob(s) for {repo_id} "
f"before starting {mode} download.",
file = sys.stderr,
)
to_download = (
expected_files
if metadata_unavailable
else _reuse_unchanged_files("model", repo_id, commit, expected_files, hf_token)
)
_preflight_disk_space("model", repo_id, to_download)
def reclaim(state: _MainRefState) -> None:
try:
from hub.services.models.deletion import reclaim_replaced_gguf_variant
from hub.utils.snapshot_reuse import repo_cache_dir
reclaim_replaced_gguf_variant(
repo_id,
variant,
plan.main_hashes,
hf_token,
hub_cache = repo_cache_dir("model", repo_id).parent,
protected_revisions = frozenset(
[state.published_commit, *(sha for _, sha in state.attempts)]
),
)
except Exception as e:
print(
f"Verified GGUF update for {repo_id} [{variant}], but stale-cache "
f"reclaim failed ({type(e).__name__}: {e})",
file = sys.stderr,
)
snapshot_path = _pinned_snapshot_download(
snapshot_download,
commit,
expected_files = expected_files,
on_publish = reclaim,
repo_id = repo_id,
token = _hf_token_arg(hf_token),
allow_patterns = targets,
max_workers = _file_workers(mode),
tqdm_class = tqdm_class,
)
_verify_completed_download(
"model",
repo_id,
variant,
snapshot_path,
metadata_unavailable = metadata_unavailable,
)
def _download_scoped_snapshot(
repo_id: str,
scope: str,
files: list[str],
hf_token: str | None,
mode: str,
tqdm_class: type | None = None,
) -> None:
"""Fetch exactly ``files`` from ``repo_id``, keyed under ``scope``. For consumers that read a deliberate subset of a repo (the diffusion loader skips the packaged root single, transformer/ shards and fp16 twins). Keyed apart from the repo's full snapshot so neither manifest describes the other, and the repo is not later judged partial against expectations it was never meant to meet."""
from huggingface_hub import HfApi, snapshot_download
from hub.utils.download_registry import prepare_cache_for_transport
from hub.utils import download_manifest
from hub.utils.download_manifest import ExpectedFile
wanted = set(files)
try:
info = _model_info_with_retry(repo_id, hf_token)
except Exception as e:
print(
f"metadata unavailable for scoped download of {repo_id} " f"({type(e).__name__}: {e})",
file = sys.stderr,
)
info = None
commit = getattr(info, "sha", None)
expected_files: list[ExpectedFile] = []
blob_hashes: frozenset[str] = frozenset()
if info is not None:
siblings = [s for s in info.siblings if getattr(s, "rfilename", None) in wanted]
# Every requested file must resolve: dropping an unmatched name would shrink the manifest to the survivors, and snapshot_download also succeeds when an allow pattern matches nothing.
missing = sorted(set(wanted) - {getattr(s, "rfilename", None) for s in siblings})
if missing:
print(
f"Scoped download of {repo_id} cannot resolve "
f"{len(missing)} requested file(s): {_format_path_list(missing)}",
file = sys.stderr,
)
sys.exit(1)
expected_files = [
ExpectedFile(
path = s.rfilename,
size = int(getattr(s, "size", 0) or 0),
sha256 = sibling_sha256(s),
)
for s in siblings
]
from hub.utils.snapshot_filters import blob_hashes_for_siblings
blob_hashes = blob_hashes_for_siblings(siblings)
download_manifest.write_manifest(
"model",
repo_id,
scope,
expected_files,
mode,
**_model_manifest_attestation(getattr(info, "sha", None)),
)
else:
manifest = download_manifest.read_manifest("model", repo_id, scope)
if manifest is not None:
expected_files = list(manifest.expected_files)
if manifest.metadata_derived:
commit = download_manifest.normalized_commit_hash(manifest.commit_hash)
download_manifest.clear_cancel_marker("model", repo_id, scope)
purged = prepare_cache_for_transport(
"model",
repo_id,
mode,
scope,
only_blob_hashes = blob_hashes or None,
protected_blob_hashes = _protected_blob_hashes(),
)
if purged:
print(
f"Purged {purged} untrusted partial blob(s) for {repo_id} [{scope}] "
f"before starting {mode} download.",
file = sys.stderr,
)
to_download = _reuse_unchanged_files(
"model", repo_id, getattr(info, "sha", None), expected_files, hf_token
)
_preflight_disk_space("model", repo_id, to_download)
def verify(snapshot_path: str) -> None:
if info is None:
# A saved manifest may omit files in the current request.
root = Path(snapshot_path)
absent = tuple(f for f in files if not (root / f).exists())
if absent:
print(
f"Could not reach Hugging Face for {repo_id} [{scope}] and the copy on disk "
f"is incomplete ({len(absent)} file(s) missing): {_format_path_list(absent)}. "
"Reconnect (or set a valid HF token) and resume the download.",
file = sys.stderr,
)
sys.exit(1)
_verify_completed_download(
"model",
repo_id,
scope,
snapshot_path,
metadata_unavailable = info is None,
)
_pinned_snapshot_download(
snapshot_download,
commit,
expected_files = expected_files,
verify = verify,
repo_id = repo_id,
token = _hf_token_arg(hf_token),
allow_patterns = files,
max_workers = _file_workers(mode),
tqdm_class = tqdm_class,
)
def _download_dataset(
repo_id: str,
hf_token: str | None,
mode: str,
tqdm_class: type | None = None,
) -> None:
from huggingface_hub import snapshot_download
from hub.utils.download_registry import prepare_cache_for_transport
from hub.utils import download_manifest
try:
info = _dataset_info_with_retry(repo_id, hf_token)
except Exception as e:
print(
f"dataset metadata unavailable, downloading full dataset for {repo_id} "
f"({type(e).__name__}: {e})",
file = sys.stderr,
)
info = None
# Cancel-marker clear and manifest write run on every transport (see _download_snapshot for XET).
download_manifest.clear_cancel_marker("dataset", repo_id, None)
if info is not None:
expected_files = _dataset_expected_files(info)
commit_hash = getattr(info, "sha", None)
download_manifest.write_manifest(
"dataset",
repo_id,
None,
expected_files,
mode,
commit_hash = commit_hash,
metadata_derived = True,
)
else:
expected_files = []
commit_hash = None
purged = prepare_cache_for_transport("dataset", repo_id, mode)
if purged:
print(
f"Purged {purged} untrusted partial blob(s) for {repo_id} "
f"before starting {mode} download.",
file = sys.stderr,
)
to_download = _reuse_unchanged_files("dataset", repo_id, commit_hash, expected_files, hf_token)
_preflight_disk_space("dataset", repo_id, to_download)
download_kwargs = {
"repo_id": repo_id,
"token": _hf_token_arg(hf_token),
"repo_type": "dataset",
"max_workers": _file_workers(mode),
"tqdm_class": tqdm_class,
}
if isinstance(commit_hash, str) and commit_hash.strip():
download_kwargs["revision"] = commit_hash.strip()
snapshot_path = snapshot_download(
**download_kwargs,
)
if info is None:
_recover_manifest_after_download(
"dataset",
repo_id,
snapshot_path,
mode,
fetch_info = lambda: _dataset_info_with_retry(repo_id, hf_token),
expected_files_from_info = _dataset_expected_files,
label = "dataset ",
)
_verify_completed_download(
"dataset",
repo_id,
None,
snapshot_path,
metadata_unavailable = info is None,
)
if info is not None:
_write_dataset_completion_from_metadata(
repo_id,
snapshot_path,
getattr(info, "sha", None),
expected_files,
mode,
)
def _force_stall_for_tests(repo_id: str, repo_type: str) -> None:
"""Test-only fault injection: hang the Xet attempt so the stall watchdog can be exercised. Never set in production. ``unsloth_zoo.hf_xet_fallback`` has the same hook for its own spawns, but the hub worker is a different process launched a different way, so without this there is no way to hang a *real* hub download on demand. A partial has to exist and stay open: the watchdog counts only ``.incomplete`` files held open by the child it is watching."""
from huggingface_hub.constants import HF_HUB_CACHE
blobs = os.path.join(HF_HUB_CACHE, f"{repo_type}s--" + repo_id.replace("/", "--"), "blobs")
handle = None
try:
os.makedirs(blobs, exist_ok = True)
handle = open(os.path.join(blobs, "xet-force-stall.incomplete"), "wb")
handle.write(b"\0" * 4096)
handle.flush()
except OSError:
pass
print("UNSLOTH_HF_XET_FORCE_STALL: hanging the xet attempt", file = sys.stderr, flush = True)
while True:
# `handle` stays referenced by this frame, which never returns, so the partial stays open.
time.sleep(3600)
def _progress_class(heartbeat: str | None) -> type | None:
if not heartbeat:
return None
from huggingface_hub.utils import tqdm as hf_tqdm
from hub.utils.download_heartbeat import HeartbeatWriter
writer = HeartbeatWriter(heartbeat)
class _Heartbeat(hf_tqdm):
def update(self, n = 1):
if n and n > 0:
writer.add(int(n))
return super().update(n)
return _Heartbeat
def main() -> None:
parser = argparse.ArgumentParser(description = "HuggingFace Hub download worker")
parser.add_argument("--repo-id", required = True)
parser.add_argument("--variant", default = None)
parser.add_argument("--dataset", action = "store_true")
parser.add_argument("--transport", choices = ("http", "xet"), default = "http")
parser.add_argument("--parent-pid", type = int, default = None)
parser.add_argument(
"--files-json",
default = None,
help = "Temp JSON file holding a scoped job's exact file list (deleted after reading).",
)
parser.add_argument("--heartbeat", default = None)
args = parser.parse_args()
scoped_files: list[str] = []
if args.files_json:
import json
try:
with open(args.files_json, encoding = "utf-8") as handle:
scoped_files = [str(f) for f in json.load(handle)]
finally:
try:
os.unlink(args.files_json)
except OSError:
pass
_install_signal_handlers()
_install_parent_death_watchdog(args.parent_pid, args.heartbeat)
hf_token = os.environ.get("HF_TOKEN") or None
_lift_http_ceiling_on_modelscope()
_install_speed_limit()
if args.transport == "xet" and os.environ.get("UNSLOTH_HF_XET_FORCE_STALL") == "1":
_force_stall_for_tests(args.repo_id, "dataset" if args.dataset else "model")
progress = _progress_class(args.heartbeat)
try:
if args.dataset:
_download_dataset(args.repo_id, hf_token, args.transport, progress)
elif scoped_files:
_download_scoped_snapshot(
args.repo_id, args.variant, scoped_files, hf_token, args.transport, progress
)
elif args.variant:
_download_gguf_variant(args.repo_id, args.variant, hf_token, args.transport, progress)
else:
_download_snapshot(args.repo_id, hf_token, args.transport, progress)
sys.exit(0)
except SystemExit:
raise
except Exception as e:
# Surface a precise message rather than a generic "worker exited with code 1": huggingface_hub recommends force_download=True to recover, which our Restart maps to purging the partial via prepare_cache_for_transport.
print(f"{type(e).__name__}: {e}", file = sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()