Skip to content

Loader: refuse a network-share base model named by a Hub adapter - #18923

Merged
danielhanchen merged 2 commits into
mainfrom
s1122b/U3/adapter-unc-base
Nov 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
s1122b/U3/adapter-unc-base

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

When FastLanguageModel / FastModel load a Hub adapter, they follow the base_model_name_or_path from its adapter_config.json. On Windows a UNC value such as \\host\share\model makes the load touch an SMB host the repo author chose, which sends the user's NTLM hash.

_refuse_network_share_adapter_base refuses a network-share base for Hub adapters in both loaders, using unsloth_zoo's _is_network_share (falls back to a plain UNC check with an older unsloth_zoo). Local adapter folders are the user's own and are left alone, and on Linux / macOS nothing changes.

Test: tests/test_adapter_unc_base_guard.py (UNC base refused, Hub and local adapters unchanged, both loaders check before the base is used, older unsloth_zoo fallback).

On Windows a UNC base_model_name_or_path from a downloaded adapter_config.json
makes the load open an SMB connection to a host the repo chose. Local adapter
folders are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 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-22T16:24:00.750668Z a7eb032 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: a7eb032b58

ℹ️ 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 added a commit to NilayYadav/unsloth that referenced this pull request Nov 22, 2026
@danielhanchen
danielhanchen merged commit 019980d into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the s1122b/U3/adapter-unc-base branch November 22, 2026 21:19
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…, and read checked-in sources as UTF-8 (main reds) (#18952)

## Summary

Two checks are red on `main` and fail on every PR that runs them, for
example the staging run for #18941:

| Failing check | Introduced by | Which side was wrong | Fix |
|---|---|---|---|
|
`tests/studio/install/test_mlx_install.py::test_mlx_image_engine_pins_leave_the_mlx_stack_alone`
(`studio-install` shard): `assert
'mflux==0.24.0'.startswith('mflux==0.22.')` | #18933 bumped mflux to
0.24.0 (0.22.0 kept for Python 3.10) | Test. The bump is intended. The
test pinned a version literal of its own, so every mflux bump breaks it.
| The test now checks the installer pin against the MLX engine itself:
`_MLX_DIFFUSION_PINS` must be exactly `mflux==<MFLUX_VERSION>` for the
running interpreter, and that version must be in
`MFLUX_COMPATIBLE_VERSIONS`. Both are read from
`mlx_diffusion_convert.py`'s source, since importing it needs numpy and
the installer tests do not have it. The check is stricter than before:
an installer pin that drifts from what the engine accepts at runtime now
fails. |
|
`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
(`rest-2` shard): 3 reads with the platform default encoding | #18923
(`tests/test_adapter_unc_base_guard.py:30` and `:91`) and
`tests/test_inkling2_support.py:367` | Tests. On Windows a bare
`read_text()` uses the locale code page. | `encoding = "utf-8"` on those
three reads. |

Only tests change.

## Testing

- `tests/studio/install/test_mlx_install.py`,
`tests/test_source_read_encoding.py`,
`tests/test_adapter_unc_base_guard.py`,
`tests/test_inkling2_support.py`: 3 failed on `main`. With this change,
92 passed, 4 skipped.
- Sabotage: with `_MFLUX_PIN` changed to `mflux==0.23.0`, a version the
engine does not expect, the mflux test fails with `assert
'mflux==0.23.0' == 'mflux==0.24.0'`.
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 23, 2026
…er (unslothai#18960)

unslothai#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 unslothai#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
unslothai#18961.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01Pa8JdyYMHkqNpdksTfdmRG

---------

Co-authored-by: Claude Fable 5.2 <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
danielhanchen added a commit to lonexreb/unsloth that referenced this pull request Nov 23, 2026
unslothai#19014)

`unsloth eval` resolves the base model of a Hub adapter from its
`adapter_config.json` and passes `base_model_name_or_path` to lm-eval,
vLLM or `AutoConfig`. On Windows a UNC value such as
`\\host\share\model` makes that load open an SMB connection to a host
the repo author chose, which sends the user's NTLM hash. The Python
loaders already refuse this (unslothai#18923), but the hf and vllm eval backends
never go through them.

The CLI now refuses a network-share base named by a Hub adapter right
after resolving it, with exit code 2 and a message to pass
`--base-model`. It uses the same rule as
`unsloth_zoo.vision_utils._is_network_share`, kept import-free because
the hf and vllm backends do not import unsloth. Local adapter folders,
an explicit `--base-model`, normal Hub bases, and Linux / macOS are
unchanged.

Test: `unsloth_cli/tests/test_eval_unc_adapter_base.py` (Windows flag
forced).
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 added a commit to Imagineer99/unsloth that referenced this pull request Nov 23, 2026
…export MoE probe (unslothai#19060)

Two more places where a Hub repo's adapter config can point a load at a
Windows network share (`\\host\share\...`), which makes Windows
authenticate to that host and leak the user's NTLM hash. Same class as
unslothai#18923 and unslothai#19014.

- **Clef adapters (`unsloth/models/decision.py`)**: the Hub repo is
downloaded to a local snapshot folder first, so the loader's
`_refuse_network_share_adapter_base` saw a local folder and skipped the
check. `_load_clef` now runs that helper against the Hub `model_name`
for both the decision config `base_model` and the adapter config
`base_model_name_or_path`.
- **Studio export MoE probe (`GET /api/export/moe-info`)**: for a Hub
LoRA the route read the base from the repo's `adapter_config.json` and
probed it (`Path.exists()`, a `config.json` read). It now returns 400
for a network-share base using Studio's existing `is_network_share`,
before any filesystem call.

Local adapter folders and paths the user types are left alone, and
nothing changes on Linux / macOS. An export of such an adapter already
failed in the loader guard (unslothai#18923), so no working flow is lost.

Tests: `tests/test_clef_adapter_unc_base_guard.py` and
`studio/backend/tests/test_export_moe_info_unc_base.py` (Windows flag
forced; fail on main, pass here).
yermakoffivan pushed a commit to yermakoffivan/unsloth that referenced this pull request Nov 23, 2026
…ransformers routes (unslothai#19101)

Two more places where a Hub repo's metadata reached a Windows filesystem
probe as a UNC path before any check (same class as unslothai#18923, unslothai#19014,
unslothai#19060):

- **`UNSLOTH_PARALLEL=auto` sizing (`parallel_auto.py`)**: a Hub
adapter's `base_model_name_or_path` went to `get_model_name` and
`_checkpoint_tensors` (`os.path.isdir`). It now runs the loader's
existing `_refuse_network_share_adapter_base` right after reading the
base.
- **sentence-transformers Router routes**: `modules.json` folders were
already refused when they leave the model, but Router route keys from
`router_config.json` were joined onto the folder and probed. That rule
is now a shared helper (`_folder_escapes_model`), applied to Hub routes
too: UNC, absolute, drive and `..`-escaping routes are refused. Local
folders and normal routes are unchanged.

Tests: `tests/test_parallel_auto.py`,
`tests/test_sentence_transformer_route_unc.py`.
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant