Skip to content

Loader: refuse a network-share base model named by a ModelScope adapter - #18960

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
lonexreb:fix/unc-base-guard-modelscope-adapter
Nov 23, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
lonexreb:fix/unc-base-guard-modelscope-adapter

Conversation

@lonexreb

@lonexreb lonexreb commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

#18923 refuses a Hub adapter whose base_model_name_or_path is a Windows network share, and leaves local adapter folders alone. With UNSLOTH_USE_MODELSCOPE=1 the adapter repo has already been swapped for its downloaded snapshot folder when the guard runs, so os.path.isdir(model_name) is true and a remote adapter naming \\host\share\model is followed to the base.

Both loaders keep the original repo id in modelscope_repo_id, so the guard now gets modelscope_repo_id or model_name. Hub adapters and the user's own local adapter folders behave as before.

Reproduced on main with a mocked modelscope.snapshot_download and Windows simulated the way tests/test_adapter_unc_base_guard.py does: FastLanguageModel and FastModel both reach AutoConfig.from_pretrained on the UNC base; with this change both raise the #18923 ValueError. Not run on a real Windows machine.

Tests (both fail on main):

  • tests/test_optimized_precision_conflicts.py::test_modelscope_adapter_with_network_share_base_is_refused runs FastLanguageModel.from_pretrained through the existing extracted-loader harness with a ModelScope adapter snapshot.
  • tests/test_adapter_unc_base_guard.py::test_both_loaders_check_before_the_base_is_used now pins the repo-id argument at both call sites.

The text-diffusion loader has the same gap by a different route; that is #18961.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Pa8JdyYMHkqNpdksTfdmRG

…ts repo id

With UNSLOTH_USE_MODELSCOPE=1 the adapter repo is already a downloaded
folder when _refuse_network_share_adapter_base runs, and the guard leaves
local folders alone, so a remote adapter naming \\host\share\model was
followed. Pass the repo id the loader kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Pa8JdyYMHkqNpdksTfdmRG
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T08:21:19.349269Z e250017 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e250017fde

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 0672707 into unslothai:main Nov 23, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 23, 2026
…ub adapter (unslothai#18961)

unslothai#18923 added `_refuse_network_share_adapter_base` to `FastLanguageModel` and `FastModel`. `FastDiffusionModel.from_pretrained` reads `adapter_config.json` itself and swaps `model_name` for `base_model_name_or_path` without that check, so on Windows a text-diffusion adapter repo naming `\\host\share\model` still sends the load to that host.

`FastModel` reaches this loader before its own guard: `_dispatch_diffusion()` returns as soon as the repo's config is a text-diffusion type, including the early return for an AutoConfig error that mentions `diffusion_gemma`. So the repo author picks the route with its `config.json`.

The diffusion loader now calls the same guard right after its adapter probe, outside the probe's `except Exception: pass`. The import is local because `loader.py` imports this module. Local adapter folders and Hub bases are unchanged.

Reproduced on main with only the Hub fetches mocked and Windows simulated as in `tests/test_adapter_unc_base_guard.py`: `FastModel` (repo config of an unregistered `diffusion_gemma*` type) and `FastDiffusionModel` directly both open the UNC base; with this change both raise the unslothai#18923 `ValueError`. Not run on a real Windows machine.

Test (fails on main): `tests/test_adapter_unc_base_guard.py::test_text_diffusion_loader_refuses_a_hub_adapter_with_unc_base`.

The ModelScope route past the same guard is unslothai#18960.
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 24, 2026
…elf (unslothai#19170)

Follow-up to unslothai#18923, unslothai#18960, unslothai#18961, unslothai#19101.

## Problem

`AutoModel*.from_pretrained("org/repo")` looks for `adapter_config.json` itself, reads `base_model_name_or_path` from the raw JSON and loads that base (`auto_factory.py`, `integrations/peft.py`). The guard runs only where Unsloth parsed the adapter with PEFT, so two kinds of Hub repo still reach a base on a Windows network share:

1. **An `adapter_config.json` PEFT cannot parse.** With a valid `config.json` and e.g. `"peft_type": "NOT_A_PEFT_TYPE"`, `PeftConfig.from_pretrained` raises, the loader sets `is_peft = False`, skips the guard and loads the repo as a plain model. transformers then swaps in the named base. Applies to `FastLanguageModel` and `FastModel`.
2. **`FastSentenceTransformer`**, on the `for_inference = True` route and the fast encoder route. Both hand the repo id to `SentenceTransformer(...)`, whose Transformer module loads through transformers. A plain valid LoRA `adapter_config.json` is enough here. The default route through `FastModel` was already covered.

## Fix

`_refuse_network_share_raw_adapter_base` in `loader.py` finds the file the way transformers does (`transformers.utils.find_adapter_config_file`), reads the base from the JSON and passes it to the existing guard. Called in both loaders when PEFT parsed nothing, and before both `SentenceTransformer(model_name, ...)` loads. Local folders are left alone, as before.

## Tests

End to end, with real `transformers` / `peft` / `sentence-transformers` against an offline Hub cache, Windows simulated as in `tests/test_adapter_unc_base_guard.py`, spying on `os.path.isdir` / `isfile` / `exists` and `open`:

| | main | this PR |
| --- | --- | --- |
| loader, unparseable `adapter_config.json` | UNC base reaches `exists`, `isdir`, `isfile` | `ValueError`, nothing probed |
| ST `for_inference` | same three calls | `ValueError`, nothing probed |
| ST fast encoder route | same three calls | `ValueError`, nothing probed |

Same result on transformers 5.18.0 and 5.23.0 (sentence-transformers 6.3.0). These ran on macOS, so they show the path reaching the filesystem calls, not an SMB connection. The loader run uses the from-source harness of `tests/test_optimized_precision_conflicts.py` with a stand-in for `FastLlamaModel.from_pretrained` that calls `AutoModelForCausalLM.from_pretrained`; the sentence-transformers run executes the real `sentence_transformer.py` with its sibling imports stubbed.

In the repo: `test_raw_adapter_config_with_unc_base_is_refused` (parseable and unparseable config, a Hub base, no adapter file, a local folder) and `test_routes_that_hand_a_hub_id_to_transformers_check_the_raw_config` (the four call sites sit before the load). Both fail on main. The `loader` fixture stubs the new helper so those tests do not ask the Hub.

Follow-ups on this branch: the loader check reads the same pinned snapshot the config probe and weight load use (`_resolved_revision`), and both sentence-transformers checks fall back to `SENTENCE_TRANSFORMERS_HOME`, which `SentenceTransformer` honours and `hf_hub_download` does not.

## Not covered

- A ModelScope snapshot with an unparseable `adapter_config.json`, the fp8 offline-quantize path and `FastDiffusionModel` after a failed PEFT parse: same shape by reading, not run, not changed here.
- A Transformer module in a sentence-transformers subfolder with its own `adapter_config.json`: only the repo root is checked.
- The check adds one metadata request for `adapter_config.json` on a plain Hub model load (measured 40-49 ms per load online, none offline).
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.

2 participants