Repository navigation
Studio diffusion: Automatic prefers one card plus a second-card copy over a layer split for FLUX when the primary holds the whole denoiser - #16687
Conversation
A U-Net pipeline sends both guidance branches through one call at batch 2B, so
CFG-parallel declined it ('no DiT denoiser') and the second card stayed idle.
UnetCfgParallelRouter cuts that call in half along the batch, runs the second
half on the replica card through run_pair, and concatenates. Odd batches,
unknown tensor inputs and replica failures stay on the source card.
UNSLOTH_DIFFUSION_CFG_PARALLEL_UNET=0 turns it off.
The primary half goes through _primary_noting (waits on a cold replica, marks its signature warm); a module compiled whole after the router was installed, or a call under torch.compile, runs whole on the source; PAG-style processors that chunk the batch themselves keep the U-Net off the router.
…same model With no gpu_ids (the UI's Automatic) CFG-parallel always declined, so a guided render on a two-card box left the second card idle unless the user picked both cards. Automatic now arms it when a secondary is the same card model and idle at load (at most 1 GiB in use); a busy or different card is never borrowed. UNSLOTH_DIFFUSION_CFG_PARALLEL_AUTO=0 restores the old behaviour.
Record compile arguments under Automatic too (a compiled denoiser's replica needs them), and re-check at replica build time that the card Automatic borrowed is still idle.
…tner per load, utilisation-aware) and batch split for distilled FLUX denoisers
…ks already wrap it) and records why a batch did not split
… change (mirror the scales) and fall back to CFG batching after a render-time replica failure
…eights sit in host memory cfg_parallel_decline read the denoiser's first parameter device, so a group-offloaded DiT whose first group lives in host memory between calls (Wan2.2-TI2V-5B on a 15 GiB card) was refused as 'not on a CUDA card' while one whose first group stayed resident (Qwen-Image-2.1) armed. The execution device now falls back to the offload groups' onload card: the primary branch keeps streaming, the replica runs resident on the second card.
An automatic load on the current card onloads its offload groups to bare 'cuda' (no index), so the CFG-parallel replica search found no card model to compare and declined every secondary as 'a different model'.
…utilisation too A card under 1 GiB of memory but computing for another process passed the memory-only recheck; the 5% utilisation limit the selection applies now holds at build time as well. [skip ci]
…over a layer split for FLUX.1 / FLUX.2 when the primary holds the whole denoiser (a streamed denoiser keeps main's split)
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: Didn't find any major issues. 🚀 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". |
…build The replica is built lazily during generation, after the load's selection scope ended, so the scoped reader answered None and a busy card passed.
…g3-placement-order-v2-pr
|
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". |
… after a LoRA scale mirror A batch split gives each half its own activation amax under per-call NVFP4 scaling, so it is refused like CFG batching. Mirrored Python-valued scales guard compiled regions: the warm sets are cleared so the next calls compile one side at a time. [skip ci]
# Conflicts: # studio/backend/tests/test_diffusion_auto_multigpu_policy.py [skip ci]
|
Codex Review: Didn't find any major issues. 🎉 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". |
…cks it (#16704) ## Summary `studio/backend/tests/test_diffusion_native_quant.py::test_peft_wrapped_base_layer_runs_native` depends on test order. It failed twice in a row in `(Python 3.13, a-f)` on the staging runs for #16687, which only touches `diffusion_multigpu.py` and its own test: ``` tests/test_diffusion_native_quant.py:707: in test_peft_wrapped_base_layer_runs_native wrapped = peft.inject_adapter_in_model(cfg, model) core/inference/diffusion_torchao_patches.py:328: in dispatch_torchao return original(*args, **kwargs) peft/tuners/lora/torchao.py:151: in dispatch_torchao from torchao.quantization import LinearActivationQuantizedTensor E ModuleNotFoundError: import of torchao.quantization halted; None in sys.modules ``` The test calls `_block_torchao`, which sets `sys.modules["torchao"]` and `sys.modules["torchao.quantization"]` to `None` to prove the native path never touches torchao. Then it injects a LoRA with peft. peft's LoRA dispatcher only imports `torchao.quantization` if `peft.import_utils.is_torchao_available()` says yes, and that function is `lru_cache`d for the whole process. When an earlier test in the same xdist worker has already asked it with torchao installed, the cached `True` sends the dispatcher straight into the import the test blocked. Run alone, or in a worker where nothing asked first, the test passes. That is why it shows up only for some shard layouts, and #16687's added test file happens to produce one. The product code is fine. With torchao really missing, `is_torchao_available()` is `False` from the start. Only the test's simulation was incomplete: it hid torchao from imports but not from peft's cached probe. ## Change `studio/backend/tests/test_diffusion_native_quant.py`, `_block_torchao` only. While torchao is blocked, it also points `is_torchao_available` in `peft.import_utils` and `peft.tuners.lora.torchao` at `lambda: False`, through `monkeypatch`, so peft treats torchao as absent and the patch is undone after the test. A module peft has not loaded yet is left alone. `test_diffusion_native_quant_mps.py` has its own `_block_torchao` but never calls peft, so it is unchanged. ## Testing A one-line test that calls `peft.import_utils.is_torchao_available()` first stands in for the earlier test in the worker: | | peft 0.18.1 (backend CI) | peft 0.21.2 | |---|---|---| | main, probe then this test | 1 failed (the error above) | 2 passed | | this branch, probe then this test | 2 passed | 2 passed | | this branch, probe, this test, probe again | 3 passed (the later probe still sees torchao) | 3 passed | `tests/test_diffusion_native_quant.py` plus `tests/test_diffusion_native_quant_mps.py`: 188 passed, 14 skipped.
…orchao check peft caches is_torchao_available() per process, so a True left by an earlier LoRA test on the same xdist worker sent its torchao dispatcher into the blocked import (Backend CI a-f).
|
Codex Review: Didn't find any major issues. 🚀 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". |
…stead of capturing it The router is the value of a weak-keyed map on the pipeline, so a fallback partial holding the pipeline kept a load rolled back before publication alive. Image and video loads alike. [skip ci]
|
Codex Review: Didn't find any major issues. Breezy! 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". |
… Automatic picks the partner With three or more cards, the idlest card could hold a split component; narrowing to it first left no candidate although another idle card could take the replica.
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
…arallel [skip ci]
…el-streamed-primary [skip ci]
…tigpu-policy [skip ci]
…t-order-v2 [skip ci]
…2.1x, fp-order only) (unslothai#16786) ## Summary With two cards selected, an even batch rendered without guidance branches (a distilled model, or guidance 1) runs half the batch on each card at the same time (unslothai#16682, unslothai#16687). That batch split only covered FLUX.1 and FLUX.2, because the gate checks argument shapes: every per-sample input has to be a dim-0 tensor. This adds `Lumina2Transformer2DModel` with its exact keyword set: `hidden_states`, `timestep`, `encoder_hidden_states`, `encoder_attention_mask`, `return_dict`. Any other input, including non-empty `attention_kwargs`, keeps the call whole on the source card, as before. The kill switch is unchanged: `UNSLOTH_DIFFUSION_BATCH_SPLIT=0`. Lumina 2 pads each sample's caption to the batch's longest caption under its own mask. A half pads to its own longest caption, and the extra positions are masked out of every attention. The math is the same, and only floating-point order changes. ## Audit of the other DiT classes | class | per-sample inputs | verdict | |---|---|---| | `Lumina2Transformer2DModel` | all dim-0 tensors (latents, timestep, caption states, caption mask) | added | | `QwenImageTransformer2DModel` | dim-0 tensors plus `img_shapes`, a Python list with one entry per prompt. With `zero_cond_t` (Edit 2511) it builds a per-sample index from that list, so a half would need the list cut too | not added: needs list-aware splitting, and a 20B replica cannot be timed on any pair available here | | `HiDreamImageTransformer2DModel` | `encoder_hidden_states_llama3` carries the batch on dim 1 (layers first) | unsafe for a dim-0 split, not added | | `ZImageTransformer2DModel` | per-sample lists | already has its own split (`diffusion_zimage_cfg_split`) | | `SD3Transformer2DModel`, `ChromaTransformer2DModel` | all dim-0 tensors, ids unbatched like FLUX | split-safe on tiny models (below), but neither is a Studio family, so not added | ## Evidence **Tiny random diffusers models, Studio's real `CfgParallelRouter` across two cards** (Kaggle 2x T4, cuda:0 + cuda:1). Each model runs a 4-step loop, compared with the same model called whole on cuda:0. Every case engaged: 3 of 4 steps split, and step 1 learns there is one call per step. | class | fp32 batch 2 | fp32 batch 4 | fp16 batch 2 | fp16 batch 4 | |---|---|---|---|---| | Lumina2 (per-sample caption lengths differ) | max abs 2.4e-7 | bitwise | bitwise | bitwise | | QwenImage (audit only) | 2.4e-7 | bitwise | 2.0e-3 | bitwise | | SD3 (audit only) | 2.4e-7 | bitwise | 9.8e-4 | 2.4e-4 | | Chroma (audit only) | 2.4e-7 | bitwise | bitwise | 4.9e-4 | **Real render, `Alpha-VLLM/Lumina-Image-2.0`, Kaggle T4x2** (both T4s in one kernel). Studio was installed with its own `install.sh` (torch 2.13.0+cu130, diffusers 0.42.1). Settings: cards 0,1 selected, 768x768, 12 steps, guidance 1.0, batch 2, three prompts per arm. Studio picked fp32 on T4, plan `group`. Every arm is a fresh process on the same head tree. Base is `UNSLOTH_DIFFUSION_BATCH_SPLIT=0`, which is the code path `main` takes for this class. Values are scheduler-step medians in s/step. | kernel | arm | prompt 1 | prompt 2 | prompt 3 | |---|---|---|---|---| | 1 | split off r1 / r2 / r3 | 7.854 / 8.001 / 7.973 | 7.941 / 7.980 / 8.015 | 8.532 / 8.531 / 8.532 | | 1 | split on r1 / r2 / r3 | 4.614 / 4.630 / 4.660 | 4.408 / 4.405 / 4.427 | 4.051 / 4.056 / 4.083 | | 2 | split off r1 / r2 | n/a | 8.463 / 8.488 | 9.116 / 9.179 | | 2 | split on r1 / r2 | 5.078 / 5.092 | 4.820 / 4.937 | 4.321 / 4.334 | | 3 | split off | 6.885 | 6.887 | 7.412 | | 3 | split on | 3.970 | 3.968 | 3.768 | (The kernel 2 notebook log lost the first prompt's line for the off arms.) - A/A spread inside a kernel: off 1.9% at most, on 2.4% at most. Speed-up per prompt is 1.72x to 2.11x on every kernel, far outside that spread. - Engagement: the second card holds the replica (peak 10.4 GiB on cuda:1 with the split on, 0 with it off). A guidance-1 render shows no guidance branches, and only the batch split builds that replica. - Output: each arm is deterministic (the same image hashes on every repeat and kernel). Split on against off, all six images: not bit-identical, max difference 1 (of 255) per channel, at most 0.01% of pixels differ, PSNR 91.5 to 96.8 dB. That is floating-point order, from batch-1 against batch-2 kernels. Tests (`tests/test_diffusion_auto_multigpu_policy.py` + `tests/test_video_stream_residency.py`, CPU): | tree | result | failing IDs | |---|---|---| | base (main + this test file) | 2 failed | `test_lumina2_batch_splits_with_per_sample_masks_and_matches_one_card`, `test_lumina2_attention_kwargs_and_kill_switch_stay_whole` | | head | all passed (6 CUDA-only skips) | none | ## Not covered - Lumina 2 with guidance runs two calls per step. CFG-parallel already pairs those, and that path is unchanged. Only guidance-1 or truncated-guidance steps take the new split. - Only T4 (fp32) was timed. bf16 cards and the reference runtime were not run. - Automatic placement: a class in this table also counts as able to serve every request type in the automatic layer-split vs streamed choice (`_hidden_stream_reason`). For Lumina 2 that choice was not measured, because on T4x2 the layer split declined on its own (it did not fit).
Summary
For FLUX.1 / FLUX.2 on two cards, Automatic used the layer split even when the main card can hold the whole denoiser. The split runs at the speed of whichever card holds most of the blocks, and it blocks the second-card copy, so an even batch could not use the batch split from #16682.
Change
layer_split_or_plan(Automatic only; explicit splits are unchanged): for FLUX.1 / FLUX.2, when the primary card holds the whole denoiser resident (nothing streams) and an idle same-model partner can hold a copy, keep one card plus the copy instead of the layer split. The copy serves only even batches and guided steps; placement never changes per request.Evidence
Kaggle 2x T4, same VM per row, outputs bit-identical to one card:
FLUX.1-dev on a T4 streams part of its denoiser, so it keeps main's split.
Tests
test_execution_device_reports_the_primary_of_a_split_pipeline) fails identically on main.Stack
This is one of five stacked PRs that make every decision to use a second GPU automatic, merged in order:
Results for the whole stack, Kaggle 2x T4, same VM per row, base A/A first: