Repository navigation
Studio diffusion: keep a model-offloaded denoiser's step eager instead of re-recording its CUDA graph every render - #14497
Conversation
…d of re-recording its CUDA graph every render Under memory_mode=low_vram the accelerate CPU-offload hook re-uploads the denoiser on every render, so its weights land at new addresses and the whole-step graph was recorded again on every render (one capture per prompt on prompts 2+). The per-block graph layer already refuses this placement for the same reason. The whole-step layer now does too by default, with the reason in status; UNSLOTH_DIFFUSION_MODEL_OFFLOAD_CUDA_GRAPH=1 restores the old re-record path. Output is bit-identical; peak reserved drops by the graph pool.
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. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9308921454
ℹ️ 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".
| if not model_offload_graphs_requested(): | ||
| return None, MODEL_OFFLOAD_REASON |
There was a problem hiding this comment.
Preserve the whole-step opt-in in the final status reason
In the normal image-loading paths, diffusion.py calls arm_block_graphs() immediately after arm_graphs_after_placement(). For a model-offloaded denoiser, this new refusal leaves no whole-step handle, so arm_block_graphs() subsequently replaces this reason with diffusion_block_graph.MODEL_OFFLOAD_REASON, which tells users only to set UNSLOTH_DIFFUSION_BLOCK_GRAPHS=1. Consequently, the Studio status never exposes the newly added UNSLOTH_DIFFUSION_MODEL_OFFLOAD_CUDA_GRAPH=1 switch for restoring the prior whole-step behavior; preserve or incorporate this reason when the block layer also declines.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed: arm_block_graphs replaced the whole-step reason with the block layer's, which names only UNSLOTH_DIFFUSION_BLOCK_GRAPHS. A model-offloaded denoiser's final status now names both switches (UNSLOTH_DIFFUSION_MODEL_OFFLOAD_CUDA_GRAPH=1 for the whole step, UNSLOTH_DIFFUSION_BLOCK_GRAPHS=1 per block), with a test that fails before and passes after (e8c89c9).
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e8c89c94d4
ℹ️ 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".
| MODEL_OFFLOAD_REASON + ", or " + BLOCK_GRAPHS_ENV + "=1 to record per block" | ||
| if model_offload | ||
| else OPT_IN_REASON, |
There was a problem hiding this comment.
Preserve non-opt-in whole-step refusal reasons
When UNSLOTH_DIFFUSION_MODEL_OFFLOAD_CUDA_GRAPH=1 is already set but whole-step installation is rejected for another reason—for example, a model-offloaded HunyuanImage denoiser with its capture-safe rewrite disabled—arm_after_placement() stores that actionable refusal, but this branch unconditionally replaces it merely because the denoiser has _hf_hook. The final status consequently tells the user to enable an option that is already enabled and hides the actual capture-safety failure; combine the two opt-ins only when prior is the new model-offload-default refusal, and otherwise preserve the specific prior reason.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed: with the opt-in set, a specific whole-step refusal (for example a capture-safety one) was replaced by the opt-in hint. The model offload status now keeps the whole step's own refusal when arm_after_placement recorded one, and only falls back to the opt-in hint when it did not; the per-block switch is appended either way. A test covers the specific-reason case and fails before (8e287e8).
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
…ss renders and pin torchao int8 host weights (#14666) ## Summary Under `memory_mode=low_vram` (whole-module CPU offload) on a compiled speed tier, the denoiser step now runs as a CUDA graph on every render, recorded once on render 1, and the int8 weights the compiled tier stores stop being copied back and forth through pageable host memory on every render. Four commits: 1. **Stable device range for a model-offloaded denoiser** (`diffusion_stable_arena.py`). The denoiser's onloads land in one CUDA virtual address range reserved for the load (driver VMM: `cuMemAddressReserve` once, then `cuMemCreate` + `cuMemMap` on every onload and `cuMemUnmap` + `cuMemRelease` on every offload). The weights get the same addresses on every render, so render 1's step graph replays on renders 2+ with no new capture. The physical memory is still released at every offload, so the text encoder and VAE phases get the VRAM back as they do today. This is the same idea as ComfyUI's `comfy_aimdo` `model_vbar`. #14497 kept this placement eager because the stock onload moved the weights on every render. `offload_placement()` now arms the graph only when the range attaches; `UNSLOTH_DIFFUSION_MODEL_OFFLOAD_ARENA=0` brings back #14497's eager step. 2. **Keep and pin torchao int8 / fp8 host weights under model offload** (`diffusion_memory.py`). The keep-host-weights offload skipped torchao wrapper weights (`Int8Tensor`). Every render then copied them device-to-host into fresh pageable memory and uploaded them again from there. On FLUX.2-klein 4B only 0.5 of 4 GiB of the denoiser was pinned. Wrapper weights whose inner tensors are plain are now kept: the offload swaps the parameter back onto its kept host tensors (no copy), and their inner tensors are pinned on the first onload. 3. **Int8 / fp8 weights in the stable range.** Each inner tensor is placed in the range, and the weight is rebuilt over the placed tensors and swapped in, the same way `Module.to` moves it. 4. **Survive diffusers re-hooking.** accelerate's `remove_hook_from_module` pops instance `to` overrides (`_accelerate_added_attributes`), and diffusers re-hooks model offload before every render. Without this commit the range served only render 1. A forward pre-hook now puts the override back before the hook's pre-forward onloads the module. The existing offload speed check still judges each recorded key. A replay has to be at least 1% faster than the eager step, or the graph is dropped for the load. That covers the devices where a replay does not pay. ## Numbers Studio `begin_load` + `generate`, `memory_mode=low_vram`, `speed_mode=default` (auto int8), 1024^2, 4 prompts (new text length and seed each time, guidance change on 4), B200. Base = main `465e0c8d27`, head = this branch. The host was heavily loaded during these runs (load average 1100-2000), so wall times are not comparable across processes. The counters are exact, and the same-process eager vs replay timings are fair. | | klein 4B base | klein 4B head | Z-Image base | Z-Image head | |---|---|---|---|---| | step graph armed | no (#14497 reason) | yes, over the stable range | no | yes | | captures, render 1 / renders 2-4 | 0 / 0 | 1 / 0, 0, 0 | 0 / 0 | 1 / 0, 0, 0 | | replays per render, renders 2-4 | 0 | 4 of 4 each | 0 | 0 (new text lengths: #14387 records lengths on render 1 only) | | graph invalidations | - | 0 | - | 0 | | denoiser host weights pinned | 0.5 of 4 GiB | 4.0 GiB | 0.2 of 6 GiB | 6.0 GiB | | same process: eager step vs replay | - | 26.5 vs 20.3 ms | - | 158.5 vs 94.7 ms | | peak allocated / reserved MiB | 6073 / 6088 | 6115 / 6936 | 7704 / 7716 | 7800 / 8442 | | output | | | | same sha256 as base on all 4 renders | Same-process A/B on head Z-Image at 512^2, 8 steps, same prompt and seed. The replay ran at 31.7 ms per denoiser call. The same compiled step with the graph bypassed ran at 86.8 ms (min 56.0). Wall per render was 35.6 s against 57.6 s, and the image sha was identical in both modes. Wan 2.2 5B low_vram (480x832, 17 frames): armed over a 4848 MiB range, speed check 54.1 ms eager vs 21.1 ms replay. The first denoiser call of a render includes the onload. On base it took 26-58 s on every klein render: the int8 weights were uploaded from pageable memory after a device-to-host copy. Head klein render 3 took 0.195 s. Renders 2 and 4 took 109 s and 149 s while the host was stalling, so treat the onload time as unmeasured here. Quiet-host check, Colab A100 40 GB. Studio was installed by `install.sh` at `465e0c8d27`; "new" adds the four commits. FLUX.2-klein-4B, `low_vram`, speed default (auto int8), 1024^2, 4 steps. Renders 2-4 (render 1 compiles): | arm | wall per render | first denoiser call (incl. onload) | ms per denoiser call | captures 2-4 | replays per render | sha vs base | |---|---|---|---|---|---|---| | base (main) | 4.71-4.79 s | 1.27-1.32 s | 114.0 | 0 | 0 | - | | head with `UNSLOTH_DIFFUSION_MODEL_OFFLOAD_ARENA=0` (commit 2 only: int8 kept + pinned) | 1.91-1.92 s | 0.66 s | 114.2-114.6 | 0 | 0 | identical on all 4 | | head (all four commits) | 1.87 s | 0.62 s | 113.1-113.9 | 0 | 4 of 4 | differs (66 dB PSNR vs base on the B200 images; in-process graph vs no graph is identical) | | base, speed off | 2.45-2.74 s | 1.00-1.06 s | 174 | 0 | 0 | - | On main the compiled low_vram tier was slower per render than speed off (4.7 s vs 2.5 s) because of the int8 re-copy. The head is 2.5x faster than main and 1.3x faster than speed off. On a quiet A100 the graph itself adds about 1-2%: in-process, 512^2 is 33.0 vs 33.5 ms per call and 1024^2 is 114.1 vs 114.4 ms, bit-identical. Most of the per-render win comes from commit 2. The graph's large gains on the loaded B200 (Z-Image 512^2: 31.7 vs 86.8 ms) come from CPU launch overhead, which graphs remove. That is real for a CPU-starved machine, but it is not the quiet-host number. The graph pool lives across renders. That costs about 0.85 GiB reserved on klein and 0.6 GiB on Z-Image, held during the text encoder and VAE phases. The range itself is released at every offload. ## Tests - New: `test_model_offload_replays_over_the_stable_range_and_frees_it_between_renders`, `test_model_offload_stable_range_holds_torchao_int8_weights`, `test_torchao_int8_weights_keep_their_host_storage_and_are_pinned[0|1]`. Updated: `test_model_offload_records_nothing_after_the_first_render[False]` (main arms nothing there; it now arms the graph and records 0 captures on renders 2+), and `test_model_offload_keeps_the_step_eager_without_the_stable_range` (#14497's eager test, now with the range turned off). - Base (main) with the head tests: both `test_torchao_int8_weights_keep_their_host_storage_and_are_pinned` cases fail (the host tensors change on every render). The stable-range tests cannot import on main, because the module does not exist there. - Head, GPU: `test_diffusion_offload_keep_cpu.py` 39 passed. `test_diffusion_offload_cuda_graph.py -k "model_offload or stable_range"`: all passed, including re-hooking between renders the way diffusers does, and torchao int8. - CPU sweep over `test_diffusion_memory`, `test_diffusion_block_graph`, `test_diffusion_offload_step_graphs`, `test_diffusion_multigpu`, `test_diffusion_offload_precision` and `test_diffusion_offload_keep_cpu`: base 499 passed / 1 failed / 63 skipped, head 499 passed / 1 failed / 65 skipped. The one failure has the same ID on both (`test_execution_device_reports_the_primary_of_a_split_pipeline`, a diffusers / huggingface_hub import mismatch in that environment). The two extra skips on head are the new CUDA tests.
Summary
With
memory_mode=low_vram(whole-model CPU offload) on a compiled speed tier, Studio recorded the denoiser's whole-step CUDA graph again on every render. The accelerate CPU-offload hook re-uploads the denoiser at the start of each render, and the text encoder and VAE reuse its VRAM in between, so the weights land at new addresses every time.OffloadPlacementnoticed the move, dropped the graph and recorded a new one, so prompts 2+ each paid one capture plus a fresh graph pool.The per-block graph layer (
diffusion_block_graph.MODEL_OFFLOAD_REASON) already refuses this placement for exactly this reason. This PR makes the whole-step layer do the same by default:offload_placement()returns(None, MODEL_OFFLOAD_REASON)for aCpuOffloadhook, soarm_after_placement()arms nothing and status shows why.UNSLOTH_DIFFUSION_MODEL_OFFLOAD_CUDA_GRAPH=1brings back the old re-record path unchanged (its tests now opt in).group) placements are untouched.Why not keep the weights at stable addresses
The only way to keep the addresses stable is to hold the denoiser's device copy between renders. That is a resident placement, not low_vram: the text-encoder phase would then hold both models at once. ComfyUI only replays graphs over weights in a VMM-reserved virtual buffer (comfy_aimdo
model_vbar), checking a fault signature before each replay. Its offload path doesn't replay over weights that have moved.Numbers
Studio
load_pipeline+generate, FLUX.2-klein-base-4B, 1024x1024,memory_mode=low_vram,speed_mode=default, 6 prompts (new text lengths, seeds, guidance change on 4-6), B200, exclusive card. Base = merge base, head = this branch, interleaved runs.A replay saves about 0.4 ms of a 29 ms call (about 1.5%): roughly 6 ms per 8-step render, 22 ms per 28-step render. Recording the step again on renders 2-4 took 255, 2067 and 156 ms (synchronized wall time around the capture, 28 steps, on a busy host). So each render's recording costs 7 to 90 times what its replays save, and its pool adds about 590 MiB to the peak of the placement that is meant to have the lowest VRAM. Wall time per render is dominated by the host-to-device onload, which varies by seconds on a shared host for both arms.
Tests
test_model_offload_keeps_the_step_eager_by_default_with_the_reasonandtest_model_offload_records_nothing_after_the_first_render[False]fail on main (main records a graph on every moved render) and pass here.[True]checks that the opt-in still re-records.test_diffusion_cuda_graph,test_diffusion_offload_cuda_graph,test_diffusion_offload_step_graphs,test_diffusion_block_graph,test_diffusion_qwenimage21_graph,test_diffusion_layer_split,test_diffusion_offload_precision,test_diffusion_plan_load_dtype,test_diffusion_backend,test_video_backend,test_video_ltx25): base 1757 passed / 3 failed, head 1760 passed / 3 failed. The same threetest_diffusion_qwenimage21_graphIDs fail on both, so they already fail on main.