Skip to content

Fix offline training/export: avoid network calls in pre-flight checks and export - #6861

Closed
InfoSage05 wants to merge 2 commits into
unslothai:mainfrom
InfoSage05:fix/offline-training-export-6817
Closed

InfoSage05 wants to merge 2 commits into
unslothai:mainfrom
InfoSage05:fix/offline-training-export-6817

Conversation

@InfoSage05

Copy link
Copy Markdown
Contributor

Description

Fixes #6817

Unsloth Studio makes several network calls during training pre-flight checks, export, and startup that ignore HF_HUB_OFFLINE / TRANSFORMERS_OFFLINE, causing hangs and crashes when running without internet.

What was broken

  1. Export (GGUF/merged/lora) — the export handler had no offline protection.
    save_pretrained_gguf triggers transformers' _patch_mistral_regex → is_base_mistral() → model_info() which makes a hidden network call and crashes with RuntimeError: Unsloth: The tokenizer is weirdly not loaded?

  2. RAG embedder warmup — the daemon thread loaded unsloth/bge-small-en-v1.5 via SentenceTransformer making HEAD requests even with HF_HUB_OFFLINE=1. Retried 5x per file causing massive startup delays.

  3. is_embedding_model() — called hf_model_info() with no offline check, generating Could not determine if X is embedding model warnings.

  4. _get_model_size_bytes() — called HfApi.repo_info() with no offline check, generating Could not get model size for X warnings.

  5. detect_gguf_model_remote() — checked _env_offline() then fell back to cache (good), but when cache lookup returned None it fell into 3 network retries with 1s/2s/4s backoff instead of returning immediately.

What was fixed

File Fix
core/export/worker.py Wrap export handler with _offline_window_if_unreachable (sets HF_HUB_OFFLINE=1 + in-process flags + session reset)
main.py Skip _warm_rag_embedder() when HF_HUB_OFFLINE/TRANSFORMERS_OFFLINE is set
core/rag/embeddings.py Guard SentenceTransformer load with HF_HUB_OFFLINE=1 / TRANSFORMERS_OFFLINE=1 when offline env vars are set
utils/models/model_config.py is_embedding_model(): check _env_offline() before hf_model_info()
utils/models/model_config.py detect_gguf_model_remote(): catch connection/DNS/timeout errors and break retry loop immediately
routes/models.py _get_model_size_bytes(): return None immediately when offline

All fixes follow the pattern already established by _env_offline() checks in sibling functions (e.g. detect_gguf_model_remote, GGUF variant listing) — it was just never applied to these call sites.

… and export

Studio makes network calls during training/export even when offline or
HF_HUB_OFFLINE=1 is set, causing hangs and crashes with DNS errors.

Fixes:
- Wrap export handler with offline window so save_pretrained_gguf etc.
  don't trigger Hub calls via transformers tokenizer init -> model_info
- Skip RAG embedder warmup when HF_HUB_OFFLINE/TRANSFORMERS_OFFLINE is set
- Guard SentenceTransformer load with HF_HUB_OFFLINE=1 when offline
- is_embedding_model(): check _env_offline() before hf_model_info()
- _get_model_size_bytes(): check offline before HfApi.repo_info()
- detect_gguf_model_remote(): catch connection/DNS/timeout errors and
  fall back to local cache immediately instead of retrying 3 times

Fixes unslothai#6817
@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!

@Imagineer99

Copy link
Copy Markdown
Member

Thanks, this targets the right issue, but it needs an update before merge. The PR is currently conflicting with main, and the RAG changes are stale versus newer security code. Please rebase, preserve existing offline env vars instead of popping them, ensure explicit offline mode makes zero Hub metadata calls, and add focused tests for these offline short-circuits.

@Imagineer99 Imagineer99 self-assigned this Jul 9, 2026
Imagineer99 added a commit to Imagineer99/unsloth that referenced this pull request Jul 10, 2026
Credit InfoSage05 for the original unslothai#6861 fix direction and affected call-site analysis.

Co-authored-by: Ayushman <[email protected]>
Imagineer99 added a commit to Imagineer99/unsloth that referenced this pull request Jul 10, 2026
Credit InfoSage05 for the original unslothai#6861 fix direction and affected call-site analysis.

Co-authored-by: Ayushman <[email protected]>
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 19, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gap is still real in studio/backend/core/export/worker.py:678 where the export handler runs outside any offline window, and in routes/models.py:2047 where the size lookup always hits the Hub. Main has since reworked the RAG embedder load and is_embedding_model to use the local cache, so could you rebase and narrow this to just those two call sites, keeping any offline env vars the user already set?

@danielhanchen

Copy link
Copy Markdown
Member

The export path in studio/backend/core/export/worker.py and the size lookup in routes/models.py still reach the Hub offline on main, but the RAG embedder hunks are stale and pop offline env vars the user set. Could you rebase and narrow this to just those two call sites, as asked earlier?

@danielhanchen danielhanchen self-assigned this Oct 4, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for the detailed write-up of #6817! Closing this as superseded: every part of it has since landed on main through other PRs, and #6817 is closed.

Hunk in this PR Status on main
Export handler wrapped in _offline_window_if_unreachable Landed in #11466 (export steps now run inside the offline window when the Hub is unreachable)
SentenceTransformer load honoring offline mode Landed in #7218 (hf_env_offline() capture, local_files_only when supported)
Skip _warm_rag_embedder() at startup when offline Startup warmup no longer exists: #8564 made embeddings stay cold until ingestion or retrieval needs them
is_embedding_model() offline guard Landed in #7218, and it classifies from the local HF cache offline instead of returning False, so cached embedding models stay recognized
detect_gguf_model_remote() breaking the retry loop on connection errors Offline already returns from the local cache before the loop. Online, the retries are deliberate: a transient failure returning None would make a GGUF-only repo look non-GGUF and fall through to MLX on Apple Silicon
_get_model_size_bytes() offline early return With HF_HUB_OFFLINE=1, huggingface_hub raises OfflineModeIsEnabled before any request, which this function already catches and turns into None, so there is no hang. Cached repos also take the local snapshot sizing path

The related tokenizer is_base_mistral probe during GGUF export was fixed separately in #7482. Thanks again for tracking all of these call sites down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Offline Training and Exporting Is broken

3 participants