Repository navigation
Studio: fold an unchanged LoRA set into a resident denoiser, restore it exactly before a change - #16675
Merged
Conversation
…store it bit-exactly before a change
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. |
Member
Author
|
@codex review |
|
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". |
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 8, 2026
…side the host RAM reserve (unslothai#17086) ## Summary Follow-up to unslothai#16675. When a resident dense denoiser renders the same LoRA set twice, Studio folds the adapters into the weights and keeps a backup of the original weights so it can restore them bit-exactly. If the card has no room for the backup, it goes to host memory. That host allocation had two gaps: 1. It was always pinned on a CUDA / ROCm device, including integrated GPUs, where Studio's own policy (`host_pinning_allowed`) skips pinning because the card's memory is the same RAM. 2. Nothing checked host RAM. The backup is the full set of adapted weights (several GiB for an SDXL or larger DiT), so the second identical render could add a multi-GiB allocation the first render did not need. The `except` fallback only covers an outright allocation failure, not memory pressure after a successful allocation. ## Changes - `diffusion_lora_fuse.fuse` now pins the host backup only when `host_pinning_allowed(device)` says so, asking about the weights' own device. - Before allocating a host backup, it checks the small-host reserve (`max(3 GiB, 10% of RAM)`, the same reserve and cgroup-aware reading the low-memory load route uses). If the backup would cut into that reserve, the set stays unfused. Unfused output is exactly what PEFT computes without the fold, so the only cost is the two extra low-rank GEMMs per adapted layer. - An unreadable host reading still allows the allocation, so behaviour there is unchanged. - Kill switch: `UNSLOTH_DIFFUSION_LORA_FUSE_HOST_GUARD=0` restores the previous allocation, pinned on every CUDA / ROCm device with no RAM check. - The device-backup path and the restore path are unchanged. ## Evidence (CPU, tiny UNet + real PEFT, base = merge base) | Check | main | this PR | |---|---|---| | Fold with `UNSLOTH_DIFFUSION_HOST_RAM_LIMIT_MIB=2048`, host backup | folded (backup allocated below the reserve) | stays unfused, weights untouched | | `pin_memory` requested for the backup on an integrated GPU | `True` | `False` | | `test_every_pinning_site_asks_the_integrated_gpu_predicate_or_is_reached_through_one` | lists `diffusion_lora_fuse.py:136 fuse` | the fuse site no longer appears | The structural test still fails on both sides because of an unrelated site, `diffusion_transformer_quant.py _upload_staged`. `test_the_h3_block_pin_helper_is_reached_only_through_the_gated_deferral` also fails on both sides. Neither is touched here. New tests in `tests/test_diffusion_lora_fuse.py` cover: - the reserve arithmetic - the unfused fallback through `_apply_loras` together with the kill switch - the pin decision on integrated, discrete and CPU devices All 17 tests in the file pass; the 2 GPU-only tests are skipped on CPU. ## GPU (Kaggle T4, sm75, torch 2.10 / peft 0.19.1 / diffusers 0.42.1) Base (merge base) and head ran one per T4 in the same kernel, 3 reps each. Tiny UNet in fp16, host backup forced: | Check | main | this PR | |---|---|---| | fold with a normal host | folded, pinned backup, restore bit-exact | same | | fold with `UNSLOTH_DIFFUSION_HOST_RAM_LIMIT_MIB=2048` | folded, pinned backup | stays unfused, output bit-equal to unfused | The fused output differs from the unfused one by max abs 1.6e-3 in fp16. That difference is identical on both arms, because the fold itself is unchanged. In every rep, the restore was bit-exact and the output after the restore was bit-equal to the unfused output.
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
With a LoRA attached, PEFT runs every adapted layer as
base(x) + B(A(x)) * scale: two extra small GEMMs per layer on every step. For SDXL with a constant-strength LoRA that made a render about 35% slower than the same model without one.When a render uses the same LoRA set and strengths as the previous one, this folds the adapters into the weights of a resident, unquantised denoiser, so PEFT runs only the base layer. The original weights are restored exactly before any change.
Change
diffusion_lora_fuse.py:fusecomputesscale * (B @ A)in fp32, adds it to the weight in fp32 and rounds to the weight dtype once, then marks each layer merged.UNSLOTH_DIFFUSION_LORA_FUSE_BACKUP=host|deviceforces either.unfusecopies the originals back before any strength, set or removal change, so the existing weights-only path (set_adapters) runs on exactly the unfused model.UNSLOTH_DIFFUSION_LORA_FUSE=0.diffusion.py: an unchanged set folds (a no-op once folded); a change, or clearing the LoRAs, restores first. CUDA graphs are reset and a CFG replica is invalidated after a fold or a restore.Evidence
SDXL + a pixel-art LoRA at 0.8, 1024x1024, 25 steps, CFG 7, one G4, interleaved:
Accuracy against an fp32 reference (UNet + LoRA in fp32, unfused, same seeds, 4 prompts; every arm reproduces exactly across repeats):
The folded path is closer to fp32 on the mean, the max and 3 of 4 prompts; on the fourth both are within about 0.01.
Backup on the host (L4, where the card would keep under 8 GiB free):
Tests
test_diffusion_lora_fuse.py: 12 tests, including a GPU test that pins the fold bit for bit to(W.float() + scale * (B.float() @ A.float())).to(bf16)and a bit-exact restore with the backup on the card and on the host. 10 CPU tests pass here; 6 fail on main.test_diffusion_lora.py: 677 passed; the one failure (test_moss_image_has_no_lora_loader_so_lora_is_gated_off) fails identically on main in a venv without the pinned huggingface_hub.Not covered