Repository navigation
Studio diffusion: INT8 attention routes hold their modules weakly, so unload returns the card - #18841
Conversation
…le weakly A raced INT8 attention route kept a strong reference to its attention module in the process-wide _ROUTES list, and only the next load's forget_plans() cleared it. After unload() every routed attention module, with its Q / K / V weights and the merged QKV payload, stayed on the card (1774 MiB on FLUX.3 klein INT8, L4), so the next load planned against 2.3 GB less free memory. The route closure now holds the processor and module through weak references and dead routes are pruned on register and on a route drop. A live route behaves as before.
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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: Didn't find any major issues. What shall we delve into next? 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". |
A prune reads _ROUTES, filters it and writes the copy back. Two prunes on different threads (a video load registering while an image render drops its routes) could interleave: the one switched out mid-iteration resumed over a list the other had shortened, skipped entries and wrote its copy back, dropping live routes. Measured with four registering threads and dead routes present: 68 live routes lost in 10 runs without the lock, 0 with it. Before the prune, a register was a bare append and lost nothing. raise_floor still runs outside the lock.
…18846) ## Summary `Source lint` (Lint CI) and the risky loader test in `workflow-trigger lint` are red on `main` since #18807, and on every PR based after it (#18840, #18841): ``` 1 risky loader call site(s) not in the baseline: unsloth/_cpu_init.py:54 [dynamic-import] spec_from_file_location ``` #18807 (`UNSLOTH_MEMORY_REPORT` reports host RAM on the CPU backend) loads `unsloth/utils/run_report.py` with `importlib.util.spec_from_file_location` instead of importing `unsloth.utils`, because that package's `__init__` pulls in the GPU model stack the CPU route skips. The path is fixed, relative to `_cpu_init.py` inside the installed package, so nothing external chooses what gets executed. The call is fine and only needs recording. ## Fix `python scripts/lint_risky_loaders.py --update`, with the new entry's reason filled in. ## Testing - `python scripts/lint_risky_loaders.py`: all 124 call sites recorded (exits 1 on `main`). - `tests/security/test_lint_risky_loaders.py`: 46 passed.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
[skip ci]
Studio diffusion: unloading a model now frees its INT8 attention modules
Problem
When you unload a FLUX.3 klein pipeline (INT8, raced INT8 attention), about 1.77 GB stays allocated on the card until the next load. That load then sees about 2.3 GB less free memory and picks a worse placement. On a 16 GiB card, the text encoder kept on the GPU drops from 2954 to 696 MiB, and the time before the first denoise step goes from 0.240 to 0.410 s. Anyone who switches models and comes back pays this.
Cause
Every raced INT8 attention route is recorded in the process-wide
diffusion_int8_attention._ROUTESlist. That way a later torch-wins verdict can give the module back its stock processor. The closure stored there (_raise_processor_floor.<locals>.raise_floor) held strong references to the processor and to theFlux3Attentionmodule. Only the next load'sforget_plans()cleared the list, so afterunload()every routed attention module stayed alive, and with it:merge_payloadsfrommerge_qkv,fast_uploadfromload_prequantized_transformer.I found this with
torch.cuda.memory._record_memory_history, plus a referrer walk from the surviving CUDA tensors afterunload():Fix
_raise_processor_floornow holds the processor and the module through weak references. If the processor object can't be weak-referenced, it falls back to a strong reference.register_routeand_drop_routesprune routes whose processor is gone.Results (Colab L4, 16 GiB simulated card, three loads per process, base = merge base)
FLUX.3 klein (16 steps, INT8 auto, 1024x1024), 3 reps per arm. Medians, with [min-max] across reps:
Qwen-Image-2.1-Turbo (8 steps), 1 rep per arm, as a second family. Nothing pins its pipeline after unload even on base, which already returns to 8 MiB. Head is the same (8 MiB after every unload, same placement, identical images), so this change costs nothing there.
What stays allocated after unload on head is 8 MiB:
Tests
There are three new CPU tests in
tests/test_diffusion_int8_attention_auto.py. All three fail on the merge base and pass here:install_autois garbage collected once it is dropped (checked by weakref on every routed module and processor), and_ROUTESends up empty.register_routeprunes dead routes and keeps live ones, including routes without analiveprobe (the dispatch route).The existing INT8 attention suites,
test_diffusion_backend.pyand the eject and slot-release tests fail on the same test IDs on base and head: fiveminimax_h3_imagedownload-plan tests, which fail before this change too.