Skip to content

Studio: check local single-file checkpoints without importing the diffusion stack - #15587

Merged
danielhanchen merged 8 commits into
mainfrom
studio-model-scan-no-diffusion
Oct 28, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
studio-model-scan-no-diffusion

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Listing local models (routes/models.py, _scan_models_dir) and catalog classification (hub/services/models/catalog_classification.py) only need resolve_local_single_file, a few lines of path checks, but they imported it from core.inference.diffusion. That pulled in the whole 15k-line diffusion module and about 80 sibling modules the first time a process listed a model folder.

What changes

  • resolve_local_single_file moves unchanged into a new light module, core/inference/diffusion_local_files.py, which only checks file paths.
  • Every reader imports it from the light module: routes/models.py, catalog_classification.py, and the media-load paths (routes/inference.py, routes/video.py, media_locality.py, media_model_index.py). diffusion.py no longer re-exports it (an ordinary-module re-export fails the import-hoist lint).
  • Tests that monkeypatched diffusion.resolve_local_single_file now patch diffusion_local_files.resolve_local_single_file.

Results

First folder scan in a fresh process (n=3 each):

main this PR
CPU 79 to 103 ms 12 to 16 ms
New modules imported 82 (the diffusion stack) 7

This is paid once per process, on the first model list or catalog check.

Tests

  • New studio/backend/tests/test_diffusion_local_files_light.py (2): listing a models folder through _scan_models_dir, and classifying it through _local_family_needles, each in a fresh process, find the checkpoint without importing core.inference.diffusion. Both fail on main.
  • Every test file touching the function or a re-pointed module (43 files): identical failure IDs on main and this PR (7 pre-existing on a CPU-only host).
  • Staging CI on ubuntu-latest, windows-latest and macos-15 (Apple Silicon): the model scan tests ran and passed on each (3 passed per OS).

Follow-up: catalog classification

Catalog classification also imported core.inference.video (which pulls in core.inference.diffusion) before checking whether the model belongs to a video family. Both the local path (_local_model_task) and the cached repo path (_cached_repo_task) now import it only once a video family matches, so classification results are unchanged.

Fresh process main this PR
Classify a local single-file image checkpoint 178 new modules, diffusion loaded 91, diffusion not loaded
Classify a cached non-video repo 349 new modules, diffusion loaded 202, diffusion not loaded

Cached image repos still load the diffusion module for its trust check, as before.

The light-import tests now put the backend on the child's sys.path (so they also pass with PYTHONSAFEPATH=1), have a timeout, and cover both classification paths end to end. The adapter folder comment was restored in the moved function.

@danielhanchen

Copy link
Copy Markdown
Member Author

Source lint fails here on the import-hoist verifier (job 122396597643), not on the code:

[BLOCKER] studio/backend/hub/services/models/catalog_classification.py: TARGET-CHANGED name 'resolve_local_single_file' in <module>._local_family_needles ['from:core.inference.diffusion:resolve_local_single_file'] -> ['from:core.inference.diffusion_local_files:resolve_local_single_file'] (rename re-points module)
[BLOCKER] studio/backend/routes/models.py: TARGET-CHANGED name 'resolve_local_single_file' in <module>._scan_models_dir [...] (rename re-points module)

This is a false positive. resolve_local_single_file moves into diffusion_local_files.py and diffusion.py re-exports it, so both import paths bind the same function. scripts/verify_import_hoist.py only treats a re-point as a benign relocation when the old and new targets are module-level imports removed and added in the same file. Both call sites here import inside the function, so the move reads as a rename clash. The other two lint lines in that job (the ruff format --check count and the shellcheck info notes) also appear on green runs.

Retargeting those two local imports is the point of the PR (they must stop importing the diffusion stack), so nothing in the PR needs to change for this. The lint red is safe to merge past once the rest is green.

@danielhanchen

Copy link
Copy Markdown
Member Author

Correction to my note above: the import-hoist check reports a third blocker here that I missed, and it is not a false positive.

[BLOCKER] studio/backend/core/inference/diffusion.py: HOISTED-IMPORT-UNUSED 'resolve_local_single_file' (['from:.diffusion_local_files:resolve_local_single_file']) added but unused

The two TARGET-CHANGED lines are cleared by #15622, which teaches the verifier that a local import re-pointed after its definition moved binds the same object. This one stays: the verifier only accepts an unused re-export through __all__ in a package __init__.py, deliberately, so the back-compat from .diffusion_local_files import resolve_local_single_file # noqa: F401 in diffusion.py is still flagged. If nothing outside this PR imports resolve_local_single_file from core.inference.diffusion any more, dropping that line clears it.

danielhanchen added a commit that referenced this pull request Oct 28, 2026
… moved is not a clash (#15622)

`Source lint` blocks refactors that move helpers into a new module,
because `scripts/verify_import_hoist.py` reads every re-pointed
function-local import as a rename clash. Two open PRs are stuck on it
today:

- #15596 (moves the GGUF chat and compaction helpers out of
`llama_cpp.py`): 63 `TARGET-CHANGED` blockers, e.g. `'_can_reset_epoch'
... ['from:core.inference.llama_cpp:_can_reset_epoch'] ->
['from:core.inference.chat_compaction:_can_reset_epoch']`.
- #15587 (moves `resolve_local_single_file` into
`diffusion_local_files.py`): 2 of the same.

## Cause

`TARGET-CHANGED` exists to catch a same-name load that now resolves to a
different object. Its only exemption is a module-level import removed
and re-added in the same file. A refactor that moves a definition and
updates `from old import N` inside a function to `from new import N`
binds the same object, but neither import is module-level, so it is
reported as a clash.

## Change

`compare()` takes an optional `moved(old_target, new_target)` predicate.
`main()` builds it from git: modules are resolved to files at the before
and after refs, the same way the importing file would see them (relative
imports against its package, absolute ones against its ancestor
directories). A `TARGET-CHANGED` is downgraded to an INFO `MOVED` line
only when every lost `from:old:N` pairs with exactly one gained
`from:new:N` of the same name, and either:

- both imports reach one definition, following top-level `from ...
import` re-exports at their own ref (the old and new module re-export
it); or
- `N` was defined at the top level of the old module before, is defined
at the top level of the new module after, and the old module no longer
defines it, at most re-importing it from the new module.

Anything else stays a blocker: a re-point to a different `N`, the old
module keeping its own definition, or an alias rename across modules.
The module-level relocation rule, the `HOISTED-IMPORT-UNUSED` check and
its `__init__.py`-only `__all__` exemption are unchanged.

### When a re-point does not count as a move

A re-pointed import keeps its TARGET-CHANGED blocker unless every one of
the following holds:
- the old and new targets name the same symbol, and each lost import
pairs one-to-one with a gained import;
- both modules resolve to exactly one file, meaning an absolute import
is not ambiguous between roots, and a relative import does not climb to
or past the repo root;
- the destination did not bind the name before the change, and binds it
exactly once now, unconditionally at module level.

The last point treats a binding as ambiguous when any of these appears
for the name:
- a second binding site;
- a binding inside an `if` / `try` / `with` / loop / `match` block,
including `TYPE_CHECKING`;
- a bare annotation;
- `del`, `global`, `:=` (including in a lambda default), `except ...
as`, or a match capture;
- `import *`.

## Verification

- `--self-test`: all cases pass, including three new ones (a re-point
after a move, the same re-point when nothing moved, an alias rename
across modules).
- #15596 at its head against its merge base: main's verifier 63
blockers; this branch 0 blockers, 63 `MOVED`.
- #15587: main's verifier 3 blockers; this branch 1, the
`HOISTED-IMPORT-UNUSED` re-export in `diffusion.py`, which the existing
rule keeps on purpose.
- End to end through git refs in a scratch repo: a genuine move and a
re-point through a shared re-export pass; a re-point to a different
function of the same name, a move where the old module keeps its own
definition, and an alias rename across modules all still fail.
danielhanchen added a commit that referenced this pull request Oct 28, 2026
…g (pytest 9.2) (#15638)

## Summary


`studio/backend/tests/test_mcp_session_cache_reset_between_tests.py::test_the_next_test_starts_with_an_empty_cache_and_the_session_closed`
has failed on the `(Python 3.13, g-r)` shard of #15587, #15593, #15601
and #15615 (`assert 0 == 1` on `_Session.closed`).

### Root cause

CI now resolves pytest 9.2.0, and pytest-xdist 3.8.0 (the latest
release) does not work with it:
- `--dist loadgroup` keeps an `xdist_group` on one worker by rewriting
`item._nodeid` to `<id>@<group>`.
- pytest 9.2 builds `Item.nodeid` from a structured `_id` instead, so
that write is lost.
- Every grouped test is therefore scheduled like an ungrouped one.

The MCP pair (and the other `*_reset_between_tests` pairs) only hold
when both tests run on the same worker. With `-v`, the first test ran on
`gw0` and the second on `gw3`.

Minimal reproduction:

```python
pytestmark = pytest.mark.xdist_group("g1")
def test_one(): pass
def test_two(): pass
```

`pytest -n 2 --dist loadgroup -v`:
- pytest 9.1.1 reports `test_one@g1` and `test_two@g1`, both on `gw0`.
- pytest 9.2.0 reports `test_one` on `gw0` and `test_two` on `gw1`.

The same pytest release is behind the `torch._dynamo.config.* does not
exist` failures (#15624 pins Backend CI below 9.2 for that; #15626 makes
the suite tolerate it).

## Change

This builds on #15624 (merged), which pins `'pytest<9.2'` in
`studio-backend-ci.yml`, the only workflow that runs `--dist loadgroup`.

- **`version-compat-ci.yml`**: `'pytest>=8'` becomes `'pytest>=8,<9.2'`.
These lanes run the CPU-spoof fixtures that monkeypatch
`torch._dynamo.config`, which pytest 9.2 deletes on undo.
- **`tests/_shared/xdist_group_check.py`**, installed as a `trylast`
`pytest_collection_modifyitems` hook in `tests/conftest.py` and
`studio/backend/tests/conftest.py`:
- On an xdist worker running `--dist loadgroup`, any test that carries
an `xdist_group` mark but whose node id has no `@group` tag now fails
with the reason.
- Before, it would sometimes pass and sometimes fail depending on
scheduling. If the pin is lifted before pytest-xdist catches up, the
grouped tests fail by name with an explanation instead of flaking.
- Raising a usage error at collection time would be swallowed into an
xdist `INTERNALERROR`, so each affected test fails individually instead.
- **`tests/test_xdist_group_check.py`**: unit tests for the check, the
conftest wiring, and both pins (Backend CI from #15624 and
version-compat from this PR).

## Testing

Run on `studio/backend/tests/test_mcp_*.py` with `-n 4 --dist
loadgroup`:
- **pytest 9.1.1:** 2089 passed, twice.
- **pytest 9.2.0:** the MCP pair failed in both runs.
- **Serially on 9.2.0:** 2089 passed, so the failure needs xdist.

With the check in place, the two grouped pair files under `-n 2 --dist
loadgroup`:
- **pytest 9.1.1:** 4 passed.
- **pytest 9.2.0:** all 4 grouped tests fail with "--dist loadgroup did
not tag this xdist_group test ...".

`tests/test_xdist_group_check.py` with
`tests/test_torch_config_guard.py` (from #15626) after merging main: 18
passed.
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 05:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-10-28T08:05:24.411222Z f3e790e 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: 587313eb2c

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7d6f506074

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

1 similar comment
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7d6f506074

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: f3e790e1b3

ℹ️ 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 fe66481 into main Oct 28, 2026
4 of 49 checks passed
@danielhanchen
danielhanchen deleted the studio-model-scan-no-diffusion branch October 28, 2026 08:22
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