Skip to content

Studio diffusion: fold a constant LoRA set when the denoiser is resident, even with other components offloaded - #18872

Merged
danielhanchen merged 3 commits into
mainfrom
studio-lora-fold-resident
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-lora-fold-resident

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

_fuse_constant_loras folds an unchanged LoRA set into the denoiser weights on its second render, but it returned for every offload policy other than none. On a 24 GB card FLUX.2 klein 4B bf16 loads with offload_policy=group because 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:

  • every denoiser (unet, transformer, transformer_2, ...) has all parameters and buffers on one accelerator (not host, not meta), and
  • no accelerate _hf_hook and no diffusers offload hook sits on it or any of its submodules, and
  • the memory mode does not keep the denoiser streamed (low_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).

Workload Arm s/step Peak alloc Folded layers
bf16, text encoder grouped, DiT resident base, no LoRA 0.702 [0.700, 0.706] 0
base, no LoRA (repeat process) 0.694 [0.691, 0.699] 0
base, LoRA repeat render 0.874 [0.872, 0.876] 8.39 GiB 0
this PR, no LoRA 0.696 [0.693, 0.699] 0
this PR, LoRA first render after the change 0.866 8.40 GiB 0
this PR, LoRA repeat render 0.706 [0.704, 0.708] 8.46 GiB 100
Q8_0 GGUF (cannot fold) base / this PR, LoRA 0.960 / 0.958 5.20 GiB 0
bf16 low_vram, streamed (cannot fold) base / this PR, LoRA 1.278 / 1.283 2.06 GiB 0

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:

Check Result
No-LoRA renders, base vs this PR (GGUF and streamed: 4 prompts, resident: 1 prompt) byte-identical
First LoRA render after a change, base vs this PR byte-identical
GGUF and streamed bf16 LoRA renders, base vs this PR byte-identical
Folded repeat vs base unfolded render, 4 prompts PSNR 27.3 to 38.3 dB, LPIPS (alex) 0.005 to 0.028

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:

  • T4: fp16, group offload. The transformer kept 26 of 26 groups resident at load, but they stayed hooked, and the next render streamed them again. The gate refused the fold, as intended for a transformer whose groups can stream again.
  • A100 40 GB: no offload. Fold behavior and images are identical to base.

Tests

tests/test_diffusion_lora_fuse.py gains CPU tests:

  • the gate decision across policies, memory modes, hook kinds (top-level, per-block, accelerate, non-offload), and host or meta placement
  • a group-policy load with a resident denoiser folds, matches the unfused output, and restores the base weights bit-exactly on a change
  • a group-policy load with a hooked block stays unfused
python -m pytest tests/test_diffusion_lora_fuse.py tests/test_diffusion_manual_cast_lora.py
41 passed, 2 skipped

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T09:45:38.408867Z c125c49 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: c125c49f41

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

…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
@danielhanchen
danielhanchen merged commit f3df81d into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-lora-fold-resident branch November 22, 2026 17:12
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.
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