Skip to content

Load path: read an adapter's base config through the probe memo, skip the remote mapper for unknown LoRA bases in Studio - #16793

Merged
danielhanchen merged 5 commits into
mainfrom
perf/r28-loader-memo
Nov 6, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
perf/r28-loader-memo

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Two repeated lookups on the LoRA load path.

1. FastModel.from_pretrained: adapter base config read through the probe memo

When an adapter is loaded through FastModel (Gemma 3 / 4, Qwen-VL, gpt-oss and other FastModel families, and Studio LoRA loads), the diffusion dispatch check read the base model's config with a direct AutoConfig.from_pretrained call. The probes just before and after it in the same load already ask for the same config through _memo_config_probe, so this one repeated the config.json HEAD and the resolve-cache HEAD on every adapter load.

It now goes through _memo_config_probe with the same arguments and the same except Exception fallback. The memo's existing rules apply unchanged: it returns a copy, and never stores trust_remote_code results or errors.

Local LoRA on a tiny Gemma 3 Hub base:

main this branch
HTTP requests per load, with a token 7 5
HTTP requests per load, without a token 9 7
AutoConfig calls, with / without a token 3 / 4 2 / 3

The saved probe costs 85 ms median online here (53 to 143 ms, n = 10); on a home connection at 150 ms per request it is about 300 ms per adapter load. The final weight-load call and the order of the load events are identical in all 20 runs, and a missing base raises the same error with the same text.

2. Studio worker: skip the remote mapper for a LoRA base the installed mapper does not know

_load_download_repos (which lists the repos a load will download) called get_model_name(base) for every LoRA base. For a base that is not in the installed mapper tables, that fetches and parses mapper.py from GitHub, and the answer is either the base itself (already in the list) or an exception (ignored), so the repo list never changes. The load then fetches the remote mapper again on its own. The worker now calls get_model_name only for names in the installed mapper tables or BAD_MAPPINGS (the check _loader_repo in core/inference/inference.py makes, except that BAD_MAPPINGS names still go through the mapper here, as on main). Any other base resolves to itself, so the pre-quantized suffix strip applied when ALLOW_PREQUANTIZED_MODELS is off (AMD Instinct on bitsandbytes < 0.49.2) still lists the stripped repo the loader downloads.

Saves one GitHub GET per Studio LoRA load with such a base: 58 ms median here (40 to 89 ms, n = 5), and seconds when GitHub is slow.

Tests

  • New tests/python/test_load_probe_memo.py::test_fastmodel_adapter_load_probes_base_config_once (string token and False): fails on main, passes here, on transformers 4.57.6 and 5.22.1. A missing-base case passes on both and keeps the same error.
  • New studio/backend/tests/test_load_download_repos_mapper.py::test_unmapped_lora_base_skips_the_remote_mapper: fails on main (2 mapper fetches), passes here; the returned repo lists are identical on both.
  • New studio/backend/tests/test_load_download_repos_mapper.py::test_unmapped_prequantized_base_still_lists_the_stripped_repo: passes on main, pins that an unmapped -bnb-4bit base with pre-quantized models disabled still lists the stripped repo.
  • studio/backend/tests/test_load_downloads.py: its loader stub gained the mapper tables the guard reads; passes on both.
  • 51 neighbouring test files (about 2950 tests): the same 10 pre-existing failures on main and on this branch (diffusion masked, MLX bnb substitution, native audio), nothing new.

Not covered

  • No real GPU weight load (runs stop right before weights load), and no public adapter download (a local adapter pointing at a tiny Hub base).
  • Windows / macOS were not run; both changes are pure Python on the request path.

…the probe memo

FastModel.from_pretrained probed the adapter base's config.json directly for
the diffusion check, so every adapter load reaching FastModel repeated a probe
the per-load memo already answers (or answers right after). Same arguments,
same except-Exception fallback; the memo returns a deep copy and never stores
trust_remote_code results or network errors.
_load_download_repos called get_model_name on the adapter base unguarded, so a
base the installed mapper does not know fetched mapper.py from GitHub before
the load, and from_pretrained fetched it again. For such names get_model_name
only returns the base itself or raises, so the repo list is unchanged; reuse
the guard core/inference/inference.py already applies.
@danielhanchen
danielhanchen marked this pull request as ready for review November 6, 2026 07:48
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T07:50:32.392737Z 0c8e556 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. Can't wait for the next one!

Reviewed commit: 0c8e5560fa

ℹ️ 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 90e1e73 into main Nov 6, 2026
@danielhanchen
danielhanchen deleted the perf/r28-loader-memo branch November 6, 2026 09:40
danielhanchen added a commit that referenced this pull request Nov 6, 2026
…e stubbable package (#16898)

## Summary

Since #16793 (`90e1e733c4`), Backend CI `(Python 3.13, g-r)` fails on
unrelated PRs. The last hour's staging runs for #16887, #16886 and
#16882 all show the same two tests:

```
tests/test_load_download_repos_mapper.py:11: in test_unmapped_lora_base_skips_the_remote_mapper
    from unsloth.models import loader_utils
E   ModuleNotFoundError: No module named 'unsloth.models'; 'unsloth' is not a package
```

What #16793 tests (skipping the remote mapper for unknown LoRA bases) is
fine. The two new tests guard with `pytest.importorskip("unsloth")` and
then `from unsloth.models import ...`. In the backend job, several test
files stub `unsloth` at import time as a plain module
(`_stub_if_missing` in `test_audio_type_inconclusive.py` and others)
when the real package cannot be loaded. If one of those runs earlier in
the same xdist worker, `importorskip("unsloth")` returns the stub
instead of skipping, and the submodule import fails. Run alone, or in a
worker where nothing stubbed it, the tests pass. That is why it depends
on how the shard is laid out.

## Change

- `studio/backend/tests/test_load_download_repos_mapper.py`:
`importorskip` the submodules the tests use
(`unsloth.models.loader_utils`, `unsloth.models.loader`). With a stub in
place they now skip, the same as with no unsloth at all. With the real
package they run unchanged.
- `studio/backend/tests/test_backend_tests_stub_heavy_imports.py`:
`test_no_test_importorskips_bare_unsloth` fails if any backend test
calls `pytest.importorskip("unsloth")` on the bare package, and pins its
own detector both ways. No other backend test does this today. The rule
is narrowed to `unsloth`, since the `trl` stubs carry the attributes
their tests read.

## Testing

On `studio/backend`, with a one-line test file that stubs
`sys.modules["unsloth"]` as a plain module run first (what the shard
does):

| | main | this branch |
|---|---|---|
| stub first, then `test_load_download_repos_mapper.py` | 2 failed (the
error above) | 2 skipped |
| `test_load_download_repos_mapper.py` alone, real unsloth | 2 passed |
2 passed |
| `test_backend_tests_stub_heavy_imports.py` | 6 passed | 7 passed |

Putting back the original `importorskip("unsloth")` makes the new guard
fail and name `test_load_download_repos_mapper.py`.
danielhanchen added a commit that referenced this pull request Nov 6, 2026
… memo tests (#16908)

## Summary

`Repo tests (CPU, studio)` has been failing on unrelated PRs since
#16793 landed, always in the same test with a different parameter each
time:

```
tests/python/test_preference_chunked_logprobs.py:278: in test_matches_trl
    torch.testing.assert_close(g, w, rtol = 1e-4, atol = 1e-6)
E   AssertionError: Tensor-likes are not close!
E   Mismatched elements: 1 / 32 (3.1%)
E   Greatest absolute difference: 3.814697265625e-06 at index (0, 6) (up to 1e-06 allowed)
```

Seen on the staging runs for #16109, #16886, #16882 and #16877 today
(`cpo-kwargs1`, `kwargs2`, `kwargs3`, `kwargs5`, `kwargs6`, always
`qwen3_lora`).

The cause is test pollution. #16793 added
`test_load_probe_memo.py::test_fastmodel_adapter_load_probes_base_config_once`,
which runs `FastModel.from_pretrained` up to the weights. That load does
what it does in real use: it swaps PEFT's LoRA forwards
(`peft.tuners.lora.layer.Linear` and 8 other LoRA layer classes) for
Unsloth's, process-wide, and sets `UNSLOTH_FORCE_FLOAT32`,
`UNSLOTH_FULLGRAPH`, `UNSLOTH_RETURN_LOGITS` and `UNSLOTH_MODEL_NAME`.
The test never puts them back, so when xdist schedules the CPO parity
test on the same worker later, it compares the chunked path against TRL
on Unsloth's LoRA forward instead of stock PEFT, and its tight gradient
tolerance trips. The swapped forward is the part that matters: restoring
only the environment variables still fails with the same numbers,
restoring only the forwards passes.

Neither side's product code is wrong. #16793's loader change and its
probe-count assertions are correct; only the test leaks process state.

## Change

Tests only.

- `tests/python/test_load_probe_memo.py`: an autouse fixture records
every PEFT LoRA layer class's own `forward` and the `UNSLOTH_*`
variables before each test and puts them back after. A PEFT module first
imported during the load falls back to the `_unsloth_original_forward`
the wrapper carries.
- `tests/python/test_preference_chunked_logprobs.py`:
`test_matches_trl[...-qwen3_lora]` pins stock PEFT forwards for its
duration (through `monkeypatch`), since its reference is TRL on stock
PEFT. This keeps it deterministic whatever else ran earlier in the
worker.

## Testing

The failing worker's exact history from a local rerun of the CI shard
(`-n 4 --dist loadgroup`) replayed serially reproduces the CI failure
with the same numbers, and bisects to the #16793 test. With that test
followed by `test_matches_trl[cpo-kwargs1-qwen3_lora]`:

| | result |
|---|---|
| main | 1 failed (`3.814697265625e-06 at index (0, 6)`, as in CI) |
| this branch | 2 passed |
| this branch, parity test pin removed | 2 passed |
| this branch, probe memo fixture disabled | 2 passed |
| this branch, both removed | 1 failed (same numbers) |

The same failing worker's history replayed serially (1595 tests) on this
branch: `test_matches_trl[cpo-kwargs1-qwen3_lora]` passes. On main the
same replay fails it. One other failure in both arms,
`test_cached_paths_hold_no_credentials.py::test_model_cache_variables_are_not_treated_as_credential_homes`,
comes from this machine's environment; it passes in CI and is unchanged
here.

Both touched files pass in full: `tests/python/test_load_probe_memo.py`
plus `tests/python/test_preference_chunked_logprobs.py`, 33 passed
(torch 2.10.0+cpu, transformers 5.17.0, TRL 1.15.0, PEFT 0.21.2, the CI
shard's versions).
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