Repository navigation
Studio diffusion: fold a constant LoRA set when the denoiser is resident, even with other components offloaded - #18872
Merged
Merged
Conversation
…ent under group offload _fuse_constant_loras returned for any offload policy other than none. A pipe whose text encoder is grouped loads with offload_policy=group even when the transformer itself stays whole on the card (FLUX.2 klein 4B bf16 on L4), so its adapters ran unfolded on every render. The gate now folds under group offload when every denoiser sits on one accelerator with no accelerate or diffusers offload hook on it or any submodule, and the memory mode does not keep it streamed. A streamed, partly pinned or demoted transformer carries group hooks or host weights and still stays unfused; model, streaming and sequential offload still refuse. The fold itself, its backup and the unfold on change are the existing path. L4, FLUX.2 klein 4B bf16, 1024 px, 4 steps, rank-32 adapter on 100 layers: repeat renders with the adapter 0.874 -> 0.706 s/step (no adapter 0.696), first render after a change and every no-adapter render byte-identical to before.
Member
Author
|
@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. 👍 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". |
…so CPU legs without diffusers / peft run them [skip ci]
danielhanchen
added a commit
to Etherll/unsloth
that referenced
this pull request
Nov 22, 2026
[skip ci]
danielhanchen
added a commit
that referenced
this pull request
Nov 23, 2026
## Summary #18872 lets `_fuse_constant_loras` fold an unchanged LoRA set under `offload_policy=group` when the denoiser is resident (FLUX.2 klein 4B bf16 on a 24 GB card). The PR body said the fold, `W + scale * (B @ A)` in fp32 rounded once to bf16, is closer to the true result than PEFT's unfolded branch. Checked against an exact fp32 computation of the adapted layers, it is not. The fold rounds the whole merged weight to bf16, which adds an error of about 2^-9 of each weight whatever the adapter's size. PEFT keeps the base weight exact and only rounds the small adapter branch. This change keeps group offload loads on the unfolded path by default. `UNSLOTH_DIFFUSION_LORA_FUSE=1` turns the #18872 fold back on for users who want the speed. Loads with no offload fold as before, and `UNSLOTH_DIFFUSION_LORA_FUSE=0` still turns every fold off. ## Results Colab L4, FLUX.2 klein 4B bf16, 1024 px, 4 steps, seed 1234, guidance 1.0, Studio defaults (text encoder grouped, DiT resident, INT8 attention), rank-32 adapter on 100 layers, the same 4 prompts as #18872. Each arm ran in a fresh process. The reference computes every adapted layer as `x W^T + b + scale * (x A^T) B^T` in fp32 and rounds the output to bf16 once. The rest of the DiT is unchanged. | Prompt | Unfolded PEFT vs reference, LPIPS / PSNR | Folded vs reference, LPIPS / PSNR | Unfolded vs folded, one-step rel. error vs reference | |---|---|---|---| | 0 | 0.0107 / 35.5 dB | 0.0196 / 31.6 dB | 0.037 vs 0.042 | | 1 | 0.0100 / 35.8 dB | 0.0071 / 38.0 dB | 0.038 vs 0.038 | | 2 | 0.0170 / 29.4 dB | 0.0273 / 27.9 dB | 0.021 vs 0.025 | | 3 | 0.0058 / 35.9 dB | 0.0055 / 36.6 dB | 0.021 vs 0.023 | | mean | 0.0109 | 0.0149 | 0.029 vs 0.032 | The one-step error is measured on the first transformer forward of each render, where the inputs are identical across arms. The fold is further from the reference on 3 of 4 prompts by one-step error and on 2 of 4 by LPIPS, with mean LPIPS 37% higher. A CPU check on a 3072x3072 bf16 layer shows the cause. Before the shared output rounding, the folded weight is off by 1.5e-3 to 1.7e-3 (relative) for every adapter size. PEFT's branch is off by 9e-6 to 6e-4, and that error grows with the adapter. Against a whole-transformer fp32 reference (fp32 weights and adapters), the result is the same: | Attention | Prompts where PEFT is closer (LPIPS) | Prompts where PEFT is closer (one-step error) | Mean LPIPS to fp32, PEFT / folded | |---|---|---|---| | INT8 (default) | 4 of 4 | 4 of 4 | 0.0135 / 0.0204 | | native SDPA | 3 of 4 (the fourth within 0.0007) | 4 of 4 | 0.0094 / 0.0193 | Repeat runs on the same VM and on a second VM gave byte-identical images. No-LoRA renders and the first LoRA render after a change are byte-identical across all arms. Cost: on this workload the repeat LoRA render goes back to the unfolded speed measured in #18872 (about 0.87 s/step against 0.71 s/step folded). The 7 GiB pinned host backup the fold keeps on L4 also goes away (process RSS 16.2 GiB folded against 9.1 GiB unfolded). ## Tests - `test_group_fold_is_opt_in`: unset, empty, `auto` and `0` keep group offload unfused; `1`, `on` and `true` fold; no offload always passes the gate. - `test_group_policy_stays_unfused_unless_opted_in`: a group policy load with a resident denoiser leaves the weights untouched by default. - The existing gate and fold tests now set `UNSLOTH_DIFFUSION_LORA_FUSE=1`. Without that, they would test the default. ``` python -m pytest tests/test_diffusion_lora_fuse.py tests/test_diffusion_lora_fold_gate.py tests/test_diffusion_manual_cast_lora.py 49 passed, 2 skipped ``` On the unpatched gate the five new default-off cases fail. ## Update: opt-in on resident cards too A follow-up measurement on a Colab A100-40 with the denoiser resident (offload policy none, same model, LoRA, prompts and seed) shows the same trade-off, so the fold is now opt-in on every path (`UNSLOTH_DIFFUSION_LORA_FUSE=1`): | arm | s/step | s/image (4 steps) | peak alloc / reserved | restore copy | |---|---|---|---|---| | fold (main default) | 0.1742 | 1.109 | 21.42 / 23.39 GiB | 7.19 GiB on the card | | fold, A/A repeat | 0.1748 | 1.119 | 21.42 / 23.39 GiB | same | | unfolded (PEFT) | 0.2092 | 1.254 | 14.23 / 15.79 GiB | none | - The fold is 16.7% faster per step (A/A 0.3%), but holds a 7.19 GiB restore copy on the card (it moves to pinned host RAM when the card has less than about 17 GiB to spare). - Against a whole-transformer fp32 reference, PEFT is closer on 4/4 prompts by one-step error and 3/4 by LPIPS (mean LPIPS 0.0274 folded vs 0.0223 unfolded). - Default renders therefore take PEFT's path everywhere; users who prefer the speed set `UNSLOTH_DIFFUSION_LORA_FUSE=1`. `UNSLOTH_DIFFUSION_LORA_FUSE=0` stays a no-op off switch. - Tests: the fuse suites set the opt-in where they exercise folding; a new test checks a resident denoiser stays unfolded by default. 44 passed, 2 skipped. `test_integrated_gpu_no_host_pin.py` has 3 failures that are identical on the parent commit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_fuse_constant_lorasfolds an unchanged LoRA set into the denoiser weights on its second render, but it returned for every offload policy other thannone. On a 24 GB card FLUX.2 klein 4B bf16 loads withoffload_policy=groupbecause the text encoder is grouped, while the transformer stays whole on the card (denoiser_offload: resident). Its adapters therefore ran through PEFT's unfolded forward on every render, costing about +24% s/step.The gate now also folds under group offload when the denoiser itself does not move:
unet,transformer,transformer_2, ...) has all parameters and buffers on one accelerator (not host, not meta), and_hf_hookand no diffusers offload hook sits on it or any of its submodules, andlow_vram).A streamed transformer, one whose groups are pinned resident but still hooked (those can be released for an oversized request), or one demoted to host all keep refusing. Model, streaming and sequential offload refuse as before. The fold, its backup and the bit-exact unfold before any LoRA change are the existing code, unchanged.
Results
Colab L4, FLUX.2 klein 4B, 1024 px, 4 steps, seed 1234, rank-32 adapter on 100 layers. Each arm ran in a fresh process with 3 timed renders (median, min-max). Base is main at the merge base (no inference code changed on main since).
With the fold, the LoRA overhead on the resident workload goes from +24% to +1.5%. The two base processes differ by about 1%. Peak allocation during the denoise is unchanged. The fold keeps its 7 GiB restore copy in host memory on this card, because the card does not have the 8 GiB free that a device copy needs.
Equivalence:
The folded output is what the existing fold path already produces on loads with no offload. On an A100 40 GB the same model loads with no offload and folds on both base and this PR, and the folded renders are byte-identical between the two. The fold computes
W + scale * (B @ A)in fp32 and rounds once to the weight dtype. PEFT instead rounds the adapter branch to bf16 on every step, so the two can differ by up to about 0.03 LPIPS on some prompts.Smoke runs:
Tests
tests/test_diffusion_lora_fuse.pygains CPU tests: