From 81152a079ae381d2ae9a7783edce177efc5a8390 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Wed, 10 Jun 2026 07:42:50 -0700 Subject: [PATCH] Run the HF cache redirect before import fixes that can freeze Hub constants (#6150) * Run the HF cache redirect before import fixes that can freeze Hub constants unsloth-zoo's redirect_hf_cache_if_readonly() repoints HF_HOME, HF_HUB_CACHE, and HF_XET_CACHE at import time when the default cache is read-only. In _gpu_init.py the early fix block can import vllm (disable_broken_vllm), transformers (check_fbgemm_gpu_version), or huggingface_hub (fix_huggingface_hub) before import unsloth_zoo runs the redirect. huggingface_hub computes its cache constants when its constants module is first executed, so those probes can freeze the old read-only paths and the redirect then never reaches Hub. Recent huggingface_hub and vllm releases import lazily, which masks this, but older supported versions import eagerly: older vllm pulls in transformers at top level, and on hub versions where is_offline_mode still exists the hasattr probe materializes the constants module. Load unsloth_zoo/hf_cache.py straight from its file (it is stdlib-only) and run the redirect before the fix calls. The zoo's own call later is an idempotent no-op, and an older unsloth_zoo without hf_cache.py is skipped silently. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: danielhanchen Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- unsloth/_gpu_init.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py index 172c69c5d8..a7b8ac594c 100644 --- a/unsloth/_gpu_init.py +++ b/unsloth/_gpu_init.py @@ -35,6 +35,36 @@ from .import_fixes import ( fix_huggingface_hub, ) +# Redirect a read-only Hugging Face cache before anything below can import +# huggingface_hub / transformers / vllm (disable_broken_vllm probes +# `import vllm`, check_fbgemm_gpu_version imports transformers, and +# fix_huggingface_hub imports huggingface_hub itself), all of which can +# freeze Hub's cache constants with the un-redirected paths. unsloth_zoo +# runs the same redirect at import, but that happens after these probes. +# hf_cache.py is stdlib-only, so load it straight from its file without +# triggering the full unsloth_zoo package init this early; the zoo's own +# call later is an idempotent no-op. Older unsloth_zoo without hf_cache.py +# is skipped silently. +try: + import importlib.util as _importlib_util + from pathlib import Path as _Path + + _zoo_spec = _importlib_util.find_spec("unsloth_zoo") + if _zoo_spec is not None and _zoo_spec.origin: + _hf_cache_file = _Path(_zoo_spec.origin).with_name("hf_cache.py") + if _hf_cache_file.is_file(): + _hf_cache_spec = _importlib_util.spec_from_file_location( + "unsloth_zoo._early_hf_cache", _hf_cache_file + ) + _hf_cache = _importlib_util.module_from_spec(_hf_cache_spec) + _hf_cache_spec.loader.exec_module(_hf_cache) + _hf_cache.redirect_hf_cache_if_readonly() + del _hf_cache, _hf_cache_spec + del _hf_cache_file + del _zoo_spec, _importlib_util, _Path +except Exception: + pass + # Configure libdrm ids table path early so ROCm can resolve AMD GPU names. configure_amdgpu_asic_id_table_path() disable_broken_causal_conv1d()