Skip to content

Refuse a network-share adapter base that transformers resolves by itself - #19170

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
lonexreb:fix/unc-base-guard-raw-adapter-config
Nov 24, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
lonexreb:fix/unc-base-guard-raw-adapter-config

Conversation

@lonexreb

Copy link
Copy Markdown
Contributor

Follow-up to #18923, #18960, #18961, #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.

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 may add one metadata request for adapter_config.json on a plain Hub model load; not measured.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Pa8JdyYMHkqNpdksTfdmRG

transformers reads a Hub repo's adapter_config.json as raw JSON and loads the
base it names. The guard only ran when PEFT parsed the file in the two main
loaders, so a file PEFT rejects, and both FastSentenceTransformer routes that
hand the repo id to sentence-transformers, still reached the named base.
Check the raw file on those routes.

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

chatgpt-codex-connector Bot commented Nov 24, 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-24T13:46:38.659464Z 2f9621a 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[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 2f9621aa8e

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 24, 2026
@danielhanchen
danielhanchen merged commit 96c25f1 into unslothai:main Nov 24, 2026
22 of 47 checks passed
danielhanchen added a commit that referenced this pull request Nov 24, 2026
…9310)

## Problem


`tests/test_prefetch_snapshot_scope.py::test_st_cache_resolutions_honor_explicit_hf_cache_dir`
fails on main since #19170, and with it the Core legs of every PR based
on main (seen first on the staging run for #19181):

```
AssertionError: an ST cache_dir resolution must read an explicit kwargs.get('cache_dir') first
```

## Cause

#19170 added `_refuse_network_share_raw_adapter_base(...)` before both
`SentenceTransformer(...)` loads. On the `for_inference = True` route it
passes

```python
cache_dir = st_kwargs.get("cache_folder")
or os.environ.get("SENTENCE_TRANSFORMERS_HOME"),
```

The code is right. A few lines earlier the same function maps an
explicit HF `cache_dir` onto `st_kwargs["cache_folder"]`:

```python
_st_cache = kwargs.get("cache_dir") or kwargs.get("cache_folder")
if _st_cache is not None:
    st_kwargs["cache_folder"] = _st_cache
```

so `st_kwargs.get("cache_folder")` already reads `cache_dir` first, and
it is the same value the neighbouring `_check_modules_json_types(...,
cache_dir = st_kwargs.get("cache_folder"))` call and the
`SentenceTransformer(**st_kwargs)` load use. The guard only looked for
the literal `'cache_dir'` inside the expression, so it could not see the
mapping. #19170's own CI ran on an older base and the failure showed up
only after it merged.

## Fix

Test only, no product code changes. The guard now accepts a resolution
that either:

- reads `kwargs.get("cache_dir")` as its first operand, or
- reads `<dict>.get("cache_folder")` first, where the same function,
earlier, assigns `<dict>["cache_folder"]` only from expressions whose
first operand reads `cache_dir` (directly or through one local name).

This is stricter than before in one way: `cache_dir` has to come first,
not just appear somewhere in the expression.

## Verification

- `tests/test_prefetch_snapshot_scope.py`: 43 passed (1 failed on main).
- Sabotage, one at a time on `sentence_transformer.py`, each makes the
guard fail:
- swapping the mapping to `kwargs.get("cache_folder") or
kwargs.get("cache_dir")`;
  - dropping `st_kwargs["cache_folder"] = _st_cache`;
- changing the delegated route's resolution to
`kwargs.get("cache_folder") or
os.environ.get("SENTENCE_TRANSFORMERS_HOME")`.
- With `tests/test_adapter_unc_base_guard.py` (the tests #19170 added):
all pass.
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