Repository navigation
Studio: keep the LoRA fuse host backup off integrated GPU pins and inside the host RAM reserve - #17086
Merged
Conversation
…inside the host RAM reserve The fold's host backup (the whole set of adapted weights) was allocated pinned on every CUDA / ROCm device and without a host RAM check. Pin it only where host_pinning_allowed says so, and stay unfused when the backup would leave less than the small-host reserve free. UNSLOTH_DIFFUSION_LORA_FUSE_HOST_GUARD=0 restores the old allocation.
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. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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 9, 2026
…ally holds (unslothai#17191) Follow-up to unslothai#17086. ## What happened unslothai#17086 added a host RAM reserve check before the LoRA fuse allocates its host backup of the original weights. The check is charged `sum(sizes)` of the backup layout. Weights up to 64 MiB share 64 MiB chunks, which the pinned host allocator keeps at their size. A weight larger than 64 MiB, though, got its own slab of exactly its size, and CUDA's caching host allocator rounds every pinned block up to a power of two. A 72 MiB slab therefore holds 128 MiB, and a 126 MiB slab holds 128 MiB. FLUX-sized denoisers hit this: their `3072 x 12288`, `3072 x 21504` and `15360 x 3072` bf16 projections are all over 64 MiB. In the layout below (24 x 72 MiB, 4 x 126 MiB, 4 x 90 MiB, 8 x 18 MiB) the guard was charged 2784 MiB while the pinned allocator held 4288 MiB, so a backup could pass the check and still take about 1.5 GiB out of the reserve it is meant to keep. ## What changed - When the backup is pinned, the weights are packed largest first into 512 MiB chunks, and each chunk is then trimmed to the power of two that holds what it got. Every pinned block is now a power of two, so the size the guard is charged is the size the allocator holds. - The same layout also stops one oversized weight from holding a mostly empty power-of-two block by itself: for the layout above the pinned backup drops from 4288 MiB to 2816 MiB (6 chunks instead of 35), against 2736 MiB of weights. - Device backups and pageable host backups (CPU, integrated GPUs) keep the old exact layout. - `UNSLOTH_DIFFUSION_LORA_FUSE_POW2_PACK=0` restores the previous layout and charge. Placement, copy, fold and restore are unchanged: each weight still gets an aligned view into one chunk. ## Tests `studio/backend/tests/test_diffusion_lora_fuse.py`: - `test_the_host_guard_is_charged_what_the_pinned_allocator_holds` runs `fuse` on the FLUX-like layout with a recording pinned allocation and checks that the guard charge covers the power-of-two rounded blocks. On main it fails (`2919235584 >= 4496293888`); with this change it passes. - `test_power_of_two_layout_places_every_weight_inside_its_chunk` checks that the chunks are powers of two, offsets are aligned, and no weights overlap. - `test_power_of_two_packing_kill_switch_keeps_the_exact_slabs` checks the opt-out. The full file passes on CPU (20 passed, 2 CUDA-only skips).
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
Follow-up to #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:
host_pinning_allowed) skips pinning because the card's memory is the same RAM.exceptfallback only covers an outright allocation failure, not memory pressure after a successful allocation.Changes
diffusion_lora_fuse.fusenow pins the host backup only whenhost_pinning_allowed(device)says so, asking about the weights' own device.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.UNSLOTH_DIFFUSION_LORA_FUSE_HOST_GUARD=0restores the previous allocation, pinned on every CUDA / ROCm device with no RAM check.Evidence (CPU, tiny UNet + real PEFT, base = merge base)
UNSLOTH_DIFFUSION_HOST_RAM_LIMIT_MIB=2048, host backuppin_memoryrequested for the backup on an integrated GPUTrueFalsetest_every_pinning_site_asks_the_integrated_gpu_predicate_or_is_reached_through_onediffusion_lora_fuse.py:136 fuseThe 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_deferralalso fails on both sides. Neither is touched here.New tests in
tests/test_diffusion_lora_fuse.pycover:_apply_lorastogether with the kill switchAll 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:
UNSLOTH_DIFFUSION_HOST_RAM_LIMIT_MIB=2048The 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.