Repository navigation
Studio diffusion: overlap the quant scheme probe with the planner import on a cold image load - #16882
Conversation
…'s import on a cold image load The first quantised image load on a card spawns a child that smoke-tests every dense quant scheme (~4.5 s on an L4). It started only after _pipeline_planned_denoiser_scheme had paid the diffusers import (~6 s), so the load thread waited for both in series. The child now starts before that import, on a daemon thread under the same _CHILD_PROBE_LOCK, and _scheme_supported takes its table (None verdicts included) instead of spawning again. It is joined before free VRAM is read and before the planner returns, and only runs when the serial probe would have (no cached or persisted verdict). UNSLOTH_DIFFUSION_PROBE_OVERLAP=0 keeps the serial order.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9d3707e4d6
ℹ️ 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".
| thread.join(_CHILD_PROBE_TIMEOUT + 30.0) | ||
| if discard and not thread.is_alive(): |
There was a problem hiding this comment.
Ensure the probe has stopped before returning from join
When another card's probe holds _CHILD_PROBE_LOCK, the prewarm thread can spend up to 180 seconds waiting for that lock before starting its own 180-second child timeout. This 210-second join can therefore return while the new child still owns GPU memory. _pipeline_planned_denoiser_scheme then proceeds to snapshot_device_memory on its bf16 branch, violating the intended ordering and allowing the probe's temporary CUDA context to affect memory planning. Account for lock-wait time and explicitly ensure the worker has completed or been cancelled before allowing memory reads; a timed join alone does not establish that guarantee.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in the latest push: the early thread takes the probe lock non-blocking and skips when another probe holds it, and the join is untimed (the thread is bounded by the child's own deadline). Added a test for the held-lock case.
…ntimed join A held probe lock (another card's probe) made the early thread queue for up to one child timeout before its own child, so the 210 s timed join could return while that child still held GPU memory and the planner then read free VRAM. The thread now takes the lock non-blocking and gives up when it is held (the load's serial probe runs as before), and the join waits for the thread, which _child_probe_table bounds by its own deadline.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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`.
… 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).
…wning a second child (unslothai#16910) ## Summary Follow-up to unslothai#16882, which starts the quant scheme probe child early on a cold image load. Three cases spawned a second child that the serial probe never needed: 1. **The early child ends with no table** (hit the 180 s `_CHILD_PROBE_TIMEOUT`, or exited without a result). `_scheme_supported` saw no early table and spawned a fresh child, so a wedged probe stalled the first load twice as long (about 360 s instead of 180 s) before the in-process fallback. The early thread now records that its child ran without a table, and the load goes straight to the in-process probe, as it does after a failed serial child. 2. **The startup warm races the early child.** If a load arrives before the startup warm (`prewarm_probe_table`) takes the probe lock, the warm blocks on the lock while the early child runs, then finds `_SMOKE_CACHE` empty and runs its own child. The early thread now writes its definite verdicts into `_SMOKE_CACHE`, as `prewarm_probe_table` does, so the warm sees them. 3. **A plan that never reaches the probe** (the bf16 seed path that returns before `_planned_quant_scheme`) discarded the early table. Where the verdicts cannot be persisted (`UNSLOTH_DIFFUSION_PROBE_CACHE=0`, no card UUID, no storage root, or no passing scheme), every such plan spawned another child. Those verdicts are now kept in `_SMOKE_CACHE`, the same verdicts the serial probe caches. The out-of-memory case is unchanged: a `None` verdict is still not cached, and the load still takes it from the early table without a second child. `UNSLOTH_DIFFUSION_PROBE_OVERLAP=0` still restores the serial probe exactly. ## Evidence CPU only, the probe child replaced by a fake (sleep plus table), same harness on all three trees: base = `f85c190501` (before unslothai#16882), merged = `9c084751ab` (unslothai#16882), this branch. | case | base | merged unslothai#16882 | this PR | |---|---|---|---| | child ends with no table: children / wall (child = 1.0 s) | 1 / 1.20 s | 2 / 2.05 s | 1 / 1.05 s | | startup warm blocked behind the early child: children | 1 | 2 | 1 | | 3 plans on the bf16 seed path, verdicts not persistable: children | 0 | 3 | 1 | | 3 plans on the bf16 seed path, persistence on: children | 0 | 1 | 1 | - 96 enumerated planner cells (auto / explicit x keeps-bf16 x eager x prior verdict state x child outcome): with `UNSLOTH_DIFFUSION_PROBE_OVERLAP=0` all 96 traces match base exactly on both trees; with overlap on, free VRAM is never read, and the planner never returns, while a probe child or the overlap thread is alive. - Remaining extra child versus base: a plan that never reaches the probe still pays one early child per process (disclosed in unslothai#16882), now once instead of per plan. ## Tests - `studio/backend/tests/test_diffusion_probe_overlap.py`: 3 new tests (no-table child spawned once, startup warm reuses the early table, verdicts kept on a plan that never reaches the probe). All 3 fail on `origin/main` and pass here; the 6 existing tests pass on both. The fixture now stubs the in-process probe body (`_run_smoke_probe`) instead of `_smoke_probe`, so a cached verdict is answered the way the real code answers it. - 18 related backend test files (probe, quant, cold start, early probe): 2305 passed, 13 skipped; the one failure (`test_diffusion_comfy_community_files.py::test_moss_pipeline_takes_a_supplied_vae`) fails the same way on `origin/main`. ## Not proven - No GPU run: the timings above use a fake child. The fix only changes which cached table the load reads, not the probe itself.
Summary
On a cold image load the load thread runs two slow serial steps back to back: the planner's diffusers / transformers import (via
_auto_keeps_bf16_reason, about 6 s CPU) and then the quant scheme probe, which spawns a child process and waits on it idle (_child_probe_table, about 4-5 s on a cold card). This PR starts that same child before the import, on a daemon thread, so the two overlap.start_probe_table_prewarm/join_probe_table_prewarmindiffusion_transformer_quant.py;_scheme_supportedconsumes the early table instead of spawning a second child._CHILD_PROBE_LOCK, and produces the same table, so the scheme decision is unchanged.scheme probe ran beside the loadwhen it engages.UNSLOTH_DIFFUSION_PROBE_OVERLAP=0restores the serial probe.Evidence
FLUX.2-klein-4B, one Colab L4 VM (12 vCPU, torch 2.13.0+cu130), base =
origin/mainb75a4cd, head = this branch, 3 repeats per arm, arm order alternated per repeat. Fresh = new process, empty Studio home and caches (weights already downloaded); returning = a second process.Tests
studio/backend/tests/test_diffusion_probe_overlap.py(5 tests): the child starts before the import and overlaps it, the join happens before free VRAM is read, the off switch, a cached verdict skips the early child, an out-of-memory child result is not cached. All pass on head; the overlap and ordering tests fail on base.test_video_cosmos3.py, 3 tests) fail identically on base.Not proven