* Studio: add shared Xet-primary download helper with HTTP stall fallback Xet is the fast default transport in huggingface_hub, but a stalled Xet transfer hangs with no progress and no exception, and a blocked native thread cannot be killed. The safetensors inference path already recovers (subprocess watchdog + respawn with HF_HUB_DISABLE_XET=1); the GGUF and training paths do not. Add a reusable helper that the in-process paths can adopt. utils/hf_xet_fallback.py: - DownloadStallError (moved here from core/inference/orchestrator.py, which now imports it; behavior unchanged, still a RuntimeError subclass). - get_hf_download_state / start_watchdog: a no-progress watchdog built on the sparse-aware hub.utils.hf_cache_state helpers; fires only while a .incomplete is present and the on-disk byte total is unchanged for stall_timeout. - hf_hub_download_with_xet_fallback: cached files short-circuit; otherwise the download runs in a spawn child (own process group) supervised by the watchdog. On a stall it kills the child, makes the partial safe for HTTP via prepare_cache_for_transport, and respawns once with HF_HUB_DISABLE_XET=1. Cancel and deterministic errors (auth/missing/disk) propagate without a fallback. Tests cover the watchdog state machine, the transport decision logic, and a regression lock that HF_HUB_DISABLE_XET is honored in a fresh interpreter. * Studio: route GGUF Chat-Mode downloads through the Xet->HTTP fallback The GGUF load path (_download_gguf main+shards, _download_companion_gguf for mmproj/MTP) called a bare blocking hf_hub_download with no recovery, so a Xet stall hung the Chat-Mode load with no fallback. Route those three calls through hf_hub_download_with_xet_fallback: Xet stays primary, HTTP is used only if Xet stalls, per-file so finished shards stay cached. The existing _cancel_event is threaded through, the Cancelled sentinel is preserved, and companions stay best-effort (a terminal stall is swallowed to None). Cached files short-circuit in the helper with no subprocess, so the fast path is unchanged. The two offline mmproj tests are repointed from huggingface_hub.hf_hub_download to the new call boundary (the helper) since the download now goes through it. * Studio: recover a stalled training model-load via Xet->HTTP respawn Training runs in a spawn subprocess and FastModel.from_pretrained downloads internally, so the download cannot be wrapped per-file like GGUF. Instead the worker now watches the HF cache during the model-load phase (emitting model_load_started / model_load_completed and a stall event), and the parent recovers a stall by terminating the worker and respawning it once with HF_HUB_DISABLE_XET=1. worker.py: set HF_HUB_DISABLE_XET=1 before any HF import when the parent passes disable_xet (respawn), and wrap trainer.load_model with start_watchdog. training.py: plumb disable_xet through the config; track the model-load window; on a first-load stall arm a one-shot respawn (handled on the exiting pump thread, so no pump self-join) that preserves the DB run row (history is not duplicated) and re-runs the load over HTTP. A second stall, or a stall outside model-load, surfaces as a normal error. W&B init happens after model-load, so a pre-load respawn cannot duplicate it; the dataset is re-formatted in the new worker. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: add gated test-only fault-injection hook for the Xet stall path UNSLOTH_HF_XET_FORCE_STALL=1 makes the Xet download attempt write a partial blob and hang, so the no-progress watchdog and the HTTP fallback can be exercised end to end against a real repo (never set in production). Used to verify recovery on real models: a forced Xet stall on a 5.37GB Qwen3.5-35B-A3B shard triggered the watchdog and the HTTP retry downloaded the correct file (sha256 verified). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: tighten Xet-fallback comments and consolidate its tests Trim docstrings and inline comments across the Xet->HTTP fallback code to the non-obvious why (spawn-not-thread, killpg-not-getpgid, the sparse-partial HTTP-resume hazard); drop comments that merely restate the code. Verified comment-only with an AST signature check. Merge the three helper-level test files (watchdog, transport policy, and the HF_HUB_DISABLE_XET regression lock) into tests/test_hf_xet_fallback.py, and prefer the real structlog over a bare stub so test collection order cannot leak an incomplete module to others that log at import. Full backend suite: 3455 passed, 14 pre-existing flash-attn failures only. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
209 lines
6.5 KiB
Python
209 lines
6.5 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
"""Parent-side training Xet->HTTP fallback: a model-load stall respawns the
|
|
worker once with Xet disabled, preserving the DB run row. Driven via
|
|
_handle_event with a fake spawn context; no GPU, no network, no real subprocess.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import logging
|
|
import queue
|
|
import sys
|
|
import threading
|
|
import types as _types
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
|
|
if _BACKEND_DIR not in sys.path:
|
|
sys.path.insert(0, _BACKEND_DIR)
|
|
|
|
# Stub the heavy module-level imports of core/training/training.py so it imports
|
|
# under CPU-only/no-network, then restore them (see the restore loop below).
|
|
_SAVED: dict = {}
|
|
|
|
|
|
def _stub(name, mod):
|
|
_SAVED[name] = sys.modules.get(name)
|
|
sys.modules[name] = mod
|
|
|
|
|
|
_lg = _types.ModuleType("loggers")
|
|
_lg.get_logger = lambda name: logging.getLogger(name)
|
|
_stub("loggers", _lg)
|
|
_stub("structlog", _types.ModuleType("structlog"))
|
|
_mpl = _types.ModuleType("matplotlib")
|
|
_plt = _types.ModuleType("matplotlib.pyplot")
|
|
_plt.Figure = type("Figure", (), {}) # referenced in a class-def annotation
|
|
_mpl.pyplot = _plt
|
|
_stub("matplotlib", _mpl)
|
|
_stub("matplotlib.pyplot", _plt)
|
|
_hw = _types.ModuleType("utils.hardware")
|
|
_hw.prepare_gpu_selection = lambda *a, **k: (None, None)
|
|
_stub("utils.hardware", _hw)
|
|
_npl = _types.ModuleType("utils.native_path_leases")
|
|
_npl.native_path_secret_removed_for_child_start = lambda: contextlib.nullcontext()
|
|
_npl.run_without_native_path_secret = lambda fn: fn
|
|
_stub("utils.native_path_leases", _npl)
|
|
_pth = _types.ModuleType("utils.paths")
|
|
_pth.outputs_root = lambda *a, **k: "/tmp/outputs"
|
|
_stub("utils.paths", _pth)
|
|
|
|
import core.training.training as training_mod
|
|
from core.training.training import TrainingBackend
|
|
|
|
# Restore every stubbed module so this file never pollutes the shared session: a
|
|
# leaked bare ``structlog`` (no ``get_logger``) would break every later module
|
|
# that logs at import. training_mod already bound the stubs it needs at runtime.
|
|
for _name in (
|
|
"loggers",
|
|
"structlog",
|
|
"matplotlib",
|
|
"matplotlib.pyplot",
|
|
"utils.hardware",
|
|
"utils.native_path_leases",
|
|
"utils.paths",
|
|
):
|
|
_prev = _SAVED.get(_name)
|
|
if _prev is None:
|
|
sys.modules.pop(_name, None)
|
|
else:
|
|
sys.modules[_name] = _prev
|
|
|
|
|
|
@pytest.fixture(autouse = True)
|
|
def _stub_worker_module():
|
|
"""Stub ``core.training.worker`` so the respawn's lazy import of the
|
|
torch-heavy worker is never required."""
|
|
prev = sys.modules.get("core.training.worker")
|
|
stub = _types.ModuleType("core.training.worker")
|
|
stub.run_training_process = lambda **kwargs: None
|
|
sys.modules["core.training.worker"] = stub
|
|
yield
|
|
if prev is None:
|
|
sys.modules.pop("core.training.worker", None)
|
|
else:
|
|
sys.modules["core.training.worker"] = prev
|
|
|
|
|
|
class _FakeProc:
|
|
def __init__(self, **kwargs):
|
|
self._alive = True
|
|
self.pid = 4321
|
|
self.kwargs = kwargs
|
|
|
|
def start(self):
|
|
pass
|
|
|
|
def is_alive(self):
|
|
return self._alive
|
|
|
|
def terminate(self):
|
|
self._alive = False
|
|
|
|
def kill(self):
|
|
self._alive = False
|
|
|
|
def join(self, timeout = None):
|
|
self._alive = False
|
|
|
|
|
|
class _FakeQueue:
|
|
def put(self, *a, **k):
|
|
pass
|
|
|
|
def get(self, *a, **k):
|
|
raise queue.Empty
|
|
|
|
|
|
class _FakeCtx:
|
|
def __init__(self):
|
|
self.spawned: list = []
|
|
|
|
def Queue(self):
|
|
return _FakeQueue()
|
|
|
|
def Process(self, **kwargs):
|
|
self.spawned.append(kwargs)
|
|
return _FakeProc(**kwargs)
|
|
|
|
|
|
def _backend_mid_load():
|
|
b = TrainingBackend()
|
|
b._last_full_config = {"model_name": "org/model", "disable_xet": False, "hf_token": "tok"}
|
|
b._in_model_load = True
|
|
b._xet_fallback_used = False
|
|
proc = _FakeProc()
|
|
b._proc = proc
|
|
return b, proc
|
|
|
|
|
|
def test_stall_during_load_arms_respawn_and_terminates_worker():
|
|
b, proc = _backend_mid_load()
|
|
b._handle_event({"type": "stall", "message": "no progress for 180s"})
|
|
assert b._needs_xet_respawn is True
|
|
assert b._xet_fallback_used is True
|
|
assert proc.is_alive() is False, "stalled worker must be terminated"
|
|
|
|
|
|
def test_respawn_uses_disable_xet_and_preserves_run_row(monkeypatch):
|
|
b, _ = _backend_mid_load()
|
|
b._handle_event({"type": "stall", "message": "x"})
|
|
|
|
fake_ctx = _FakeCtx()
|
|
monkeypatch.setattr(training_mod, "_CTX", fake_ctx)
|
|
monkeypatch.setattr(b, "_pump_loop", lambda: None) # neutralize the new pump
|
|
created = {"n": 0}
|
|
finalized = {"n": 0}
|
|
monkeypatch.setattr(
|
|
b, "_ensure_db_run_created", lambda: created.__setitem__("n", created["n"] + 1)
|
|
)
|
|
monkeypatch.setattr(
|
|
b, "_finalize_run_in_db", lambda **k: finalized.__setitem__("n", finalized["n"] + 1)
|
|
)
|
|
|
|
b._respawn_worker_disable_xet()
|
|
|
|
assert len(fake_ctx.spawned) == 1, "respawn must start exactly one worker"
|
|
cfg = fake_ctx.spawned[0]["kwargs"]["config"]
|
|
assert cfg["disable_xet"] is True, "respawned worker must run with Xet disabled"
|
|
assert cfg["model_name"] == "org/model"
|
|
assert created["n"] == 0, "respawn must not recreate the DB run row"
|
|
assert finalized["n"] == 0, "a successful respawn must not finalize the run as error"
|
|
|
|
|
|
def test_second_stall_surfaces_error_without_respawn():
|
|
b, proc = _backend_mid_load()
|
|
b._xet_fallback_used = True # HTTP fallback already spent
|
|
b._handle_event({"type": "stall", "message": "stalled again over http"})
|
|
assert b._needs_xet_respawn is False
|
|
assert b._progress.error and "stalled" in b._progress.error.lower()
|
|
assert proc.is_alive() is False
|
|
|
|
|
|
def test_model_load_completed_disarms_recovery():
|
|
b, _ = _backend_mid_load()
|
|
b._handle_event({"type": "model_load_completed"})
|
|
assert b._in_model_load is False
|
|
# A stall after the load finished is not a transport stall to recover from.
|
|
b._handle_event({"type": "stall", "message": "post-load"})
|
|
assert b._needs_xet_respawn is False
|
|
|
|
|
|
def test_model_load_started_arms_recovery_window():
|
|
b = TrainingBackend()
|
|
assert b._in_model_load is False
|
|
b._handle_event({"type": "model_load_started"})
|
|
assert b._in_model_load is True
|
|
|
|
|
|
def test_child_should_disable_xet_truth_table():
|
|
from utils.hf_xet_fallback import child_should_disable_xet
|
|
|
|
assert child_should_disable_xet({"disable_xet": True}) is True
|
|
assert child_should_disable_xet({"disable_xet": False}) is False
|
|
assert child_should_disable_xet({}) is False
|