Repository navigation
Studio: whole-model offload points weights back at their host tensors instead of copying them - #11764
Conversation
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. |
…d while offloaded
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
… instead of copying them (unslothai#11764) * Studio: whole-model offload points weights back at their host tensors instead of copying them * Match the parameter object, not only its version, before reusing the host copy * Forget a kept host tensor when its parameter is replaced or re-pointed while offloaded * Take the stock offload copy when a weight's device storage was swapped through .data * Trim comments * Trim comments in keep-cpu offload * Skip the keep-cpu rescan on modules with nothing kept * Drop a comment the owner check already states
Summary
Whole-model CPU offload (
memory_mode="low_vram", and the fallback for other tiers) spends most of each render copying weights back from the GPU. accelerate'sCpuOffloadhook offloads withmodule.to("cpu"), which copies every weight device-to-host into newly allocated pageable memory, even though the weights did not change and an identical host copy existed a moment earlier. On the B200 box that copy runs at about 1 GiB/s. A cProfile of a warm Z-Image-Turbo render shows 15.8 s of 25 s in the offload.to("cpu")alone.With this change, each parameter keeps the host tensor it had before it was onloaded, and the offload points the parameter back at it instead of copying.
How
diffusion_memory.keep_cpu_weights_on_offload(pipe), called right afterpipe.enable_model_cpu_offload(...)inapply_memory_plan(both the model tier and the group/sequential fallback):CpuOffloadhook:pre_forwardrecords each parameter object and its version counter at onload.init_hookswaps unchanged parameters back to their kept host tensors, then runs the stockmodule.to("cpu")for buffers and anything not kept.torch.Tensorparameters are kept; torchao and GGUF subclasses stay on the stock path.enable_model_cpu_offloadafter every call (maybe_free_model_hooks), which builds new hooks. The pipeline's method is wrapped so the new hooks are wrapped too, and the kept tensors live on each module.torch.compiler.disabled, like accelerate's ownpre_forward.UNSLOTH_DIFFUSION_OFFLOAD_KEEP_CPU=0.Results (B200,
memory_mode="low_vram", speed default, 3 renders after a warm-up)Output:
speed_mode="off". With cuDNN benchmark on, the stock path is not reproducible across processes on a contended GPU either (its algorithm choice depends on timing).Host memory: the kept tensors are the loaded weights themselves. For safetensors loads those stay memory-mapped file pages, which the kernel can reclaim, where the stock path turned them into anonymous copies on the first offload.
On FLUX and Z-Image total RSS after rendering is unchanged, and the peak is 4 to 5 GiB lower.
Why not switch low_vram to group offload
I measured that too, as group offload with streamed text encoders. It is faster still (FLUX 2.3 s, Qwen-Image-2.1 9.4 s) with a 2 to 4 GiB VRAM peak. However, it holds 1.3 to 1.6x more host RAM at steady state (FLUX 50 vs 34 GiB, Qwen-Image-2.1 32 vs 25 GiB), which is the wrong trade on the small machines
low_vramis for. This PR keeps the policy and removes the waste inside it.Tests
tests/test_diffusion_offload_keep_cpu.py:apply_memory_planwiring.tests/test_diffusion*.py+tests/test_video*.pyrun shows 15 failures, the same 15 that fail on main in this environment.