Skip to content

fix(loader): honour HF_HUB_OFFLINE/TRANSFORMERS_OFFLINE env vars - #5598

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
xodn348:fix/hf-hub-offline-env-respected
May 19, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
xodn348:fix/hf-hub-offline-env-respected

Conversation

@xodn348

@xodn348 xodn348 commented May 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #5316 — offline environment variables (HF_HUB_OFFLINE, TRANSFORMERS_OFFLINE) were silently ignored by FastLanguageModel.from_pretrained, causing unexpected network attempts (and failures in air-gapped environments) even when users had explicitly opted into offline mode.

Root causes fixed (3 sites):

  1. unsloth/models/loader.py — FastLanguageModel.from_pretrained
    local_files_only defaulted to False without consulting the standard HF offline env vars. The fix reads those vars and sets local_files_only = True + propagates it through kwargs so every downstream HF call (AutoConfig, AutoModel, tokenizer) skips the network.

  2. unsloth/models/_utils.py — get_statistics()
    Already guarded on the local_files_only parameter but didn't check the env vars directly. A call that never passed the param would still attempt a network request. Added the same env-var guard.

  3. unsloth/dataprep/synthetic.py
    Module-level os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1" was unconditional. hf_transfer is a Rust-based downloader that bypasses the Python-side offline flag. The assignment is now gated so it only runs when offline mode is not active.

What is NOT changed: behaviour when offline env vars are absent is identical to before.

Test plan

=== LOCAL_TEST_PASSED ===
loader.py logic: PASS (HF_HUB_OFFLINE=1 -> local_files_only=True)
_utils.py get_statistics logic: PASS
synthetic.py logic: PASS (hf_transfer NOT enabled in offline mode)
  • Inline logic tests confirm all three fix sites behave correctly under HF_HUB_OFFLINE=1, TRANSFORMERS_OFFLINE=1, and normal (online) mode.
  • ruff check passes on all three changed files with no warnings.
  • Existing test suite unaffected (no public test infrastructure to run without the full GPU environment).

Checklist

  • Conventional-commit title (fix(loader): …)
  • Fixes root cause, not a symptom
  • No behaviour change when offline env vars are absent
  • ruff lint passes

…trained

When HF_HUB_OFFLINE or TRANSFORMERS_OFFLINE is set, propagate
local_files_only=True through kwargs in FastLanguageModel.from_pretrained
so every downstream HF call (AutoConfig, AutoModel, tokenizer) skips the
network.  Also guard get_statistics() and the unconditional
HF_HUB_ENABLE_HF_TRANSFER assignment in dataprep/synthetic.py.

Fixes #5316
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e17c3b557d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/loader.py Outdated
Comment on lines +452 to +453
local_files_only = True
kwargs["local_files_only"] = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Set offline kwargs before FastModel delegation

When FastLanguageModel.from_pretrained(..., load_in_8bit=True), full_finetuning=True, or qat_scheme is used under HF_HUB_OFFLINE=1/TRANSFORMERS_OFFLINE=1, the method returns through the earlier FastModel.from_pretrained branch before this new block runs, so kwargs["local_files_only"] is never populated. FastModel.from_pretrained still defaults local_files_only to False at its own config probes, so these supported loading modes continue to make network-backed AutoConfig/PeftConfig calls despite the offline env vars.

Useful? React with 👍 / 👎.

The existing block landed after the early FastModel.from_pretrained
delegation at loader.py:311, so load_in_8bit / full_finetuning / qat_scheme
loading modes never received local_files_only when only HF_HUB_OFFLINE or
TRANSFORMERS_OFFLINE was set. Hoist the check above that delegation and
mirror it inside FastModel.from_pretrained so direct callers
(FastVisionModel / FastTextModel) honour offline mode too.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f6a07e7b60

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/loader.py
Comment on lines +1068 to +1070
# Mirror env-var fallback for direct callers (FastVisionModel / FastTextModel).
if not local_files_only:
_offline = {"1", "true", "yes", "on"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Move offline detection before model-name resolution

When a direct FastModel.from_pretrained/vision/text caller uses HF_HUB_OFFLINE=1 with an arbitrary repo id that is not in the static mapper, this fallback runs only after get_model_name(...) has already executed at line 1013. get_model_name can call _get_new_mapper() in unsloth/models/loader_utils.py, which performs a requests.get to GitHub for unknown owner/model names, so offline mode still makes a network request before local_files_only is set. The env fallback needs to happen before model-name resolution or the mapper refresh needs to be skipped when offline.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks this works!

@danielhanchen danielhanchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the additional FastModel delegation paths and unsloth-zoo sibling fix. Squash merging.

@danielhanchen
danielhanchen merged commit 94026fc into unslothai:main May 19, 2026
1 check passed
Sekinal pushed a commit to Sekinal/unsloth-zoo that referenced this pull request May 19, 2026
Detects offline state once at unsloth_zoo/__init__.py import using {"1","true","yes","on"} on HF_HUB_OFFLINE / TRANSFORMERS_OFFLINE / HF_DATASETS_OFFLINE and skips HF_HUB_ENABLE_HF_TRANSFER=1 when set. The existing offline cross-sync block shares the same detection so the wider truthy parsing applies there too. Pairs with unslothai/unsloth#5598.
rsd-darshan pushed a commit to rsd-darshan/unsloth that referenced this pull request Jun 3, 2026
…trained (unslothai#5598)

Reads HF_HUB_OFFLINE / TRANSFORMERS_OFFLINE in FastLanguageModel.from_pretrained and FastModel.from_pretrained, forcing local_files_only=True so all delegation paths (load_in_4bit, load_in_8bit, full_finetuning, qat_scheme) and direct FastModel callers (FastVisionModel, FastTextModel) honour offline mode. Also gates HF_HUB_ENABLE_HF_TRANSFER in unsloth/dataprep/synthetic.py and adds an early return in get_statistics. Pairs with unslothai/unsloth-zoo#675. Fixes unslothai#5316.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Offline environment variables ignored - model downloads despite HF_HUB_OFFLINE=1

2 participants