Skip to content

Studio: keep the LoRA fuse host backup off integrated GPU pins and inside the host RAM reserve - #17086

Merged
danielhanchen merged 3 commits into
mainfrom
studio-lora-nov7-fuse
Nov 8, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-lora-nov7-fuse

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

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:

  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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 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-08T04:49:46.908200Z bb3c577 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.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: bb3c5770dc

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

@danielhanchen
danielhanchen merged commit ffeb8f7 into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-lora-nov7-fuse branch November 8, 2026 20:52
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).
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