Repository navigation
Studio: return freed host memory to the OS after diffusion and video unload - #11795
Conversation
…unload Loading a diffusion or video pipeline stages every weight through host memory. Once the model is unloaded, glibc keeps those freed pages mapped, so host RSS grew by about 6 GiB per SDXL-Turbo load / unload cycle and stayed 14+ GiB high after unloading Wan2.2-TI2V-5B. torch allocated memory after unload was about 9 MiB, and a malloc_trim(0) probe dropped RSS from 19.3 GiB to 3.3 GiB, so this is allocator retention, not a live reference. The trim added in #10184 only runs after a generation under the model offload policy, so it never fires for a GPU-resident model or on unload. Add reclaim_host_memory(), the same allocator call without the policy gate, and call it at the end of the image unload (_unload_locked, also used before a replacement load) and the video teardown, after clear_gpu_cache() has run gc.
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 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c79b4ab700
ℹ️ 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".
| clear_gpu_cache() | ||
| # clear_gpu_cache() ran gc, so the pipeline's host staging buffers are freed but still mapped by | ||
| # the allocator; return them, or host RSS grows by several GiB per load / unload cycle. | ||
| reclaim_host_memory(logger = logger) |
There was a problem hiding this comment.
Reclaim host pages even when GPU cache clearing fails
When clear_gpu_cache() raises—an intentional behavior for sticky CUDA faults that is explicitly covered by the existing unload tests—execution never reaches this new reclamation call, even though the model state has already been deleted and its host pages are eligible for trimming. The same ordering exists in VideoBackend._teardown_state_locked, so an unload following such a fault can still retain the multi-GiB host RSS this change is intended to release; invoke the best-effort reclaimer from a finally around the cache-clearing step.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this. clear_gpu_cache() only raises on a sticky CUDA fault, which leaves the CUDA context unusable until the process restarts, and the restart returns every host page anyway. Trimming in that state changes nothing a user can act on, and the unload still surfaces the fault as before.
|
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 security review |
|
Codex Review: Didn't find any major issues. Swish! 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 Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
…unload (unslothai#11795) * Studio: return freed host memory to the OS after diffusion and video unload Loading a diffusion or video pipeline stages every weight through host memory. Once the model is unloaded, glibc keeps those freed pages mapped, so host RSS grew by about 6 GiB per SDXL-Turbo load / unload cycle and stayed 14+ GiB high after unloading Wan2.2-TI2V-5B. torch allocated memory after unload was about 9 MiB, and a malloc_trim(0) probe dropped RSS from 19.3 GiB to 3.3 GiB, so this is allocator retention, not a live reference. The trim added in unslothai#10184 only runs after a generation under the model offload policy, so it never fires for a GPU-resident model or on unload. Add reclaim_host_memory(), the same allocator call without the policy gate, and call it at the end of the image unload (_unload_locked, also used before a replacement load) and the video teardown, after clear_gpu_cache() has run gc. * Trim comments on host memory reclaim
Summary
Unloading an image or video model in Studio leaves most of its host memory mapped. Host RSS grows by about 6 GiB per SDXL-Turbo load / unload cycle and stays 14+ GiB high after unloading Wan2.2-TI2V-5B, until the process exits. This returns those pages to the OS at the end of every unload.
Root cause
Loading a pipeline stages every weight through host memory. After unload the tensors are gone (torch allocated memory is about 9 MiB), but glibc keeps the freed pages in its arenas instead of returning them. A
malloc_trim(0)probe after two cycles dropped anonymous RSS from 19.3 GiB to 3.3 GiB, so nothing is holding a reference; this is allocator retention.#10184 added a trim, but only after a generation under the
modeloffload policy. A GPU-resident load never uses that policy, and unload never trims, so the pages stay mapped.Fix
diffusion_memory.reclaim_host_memory(): the same allocator call and once-only logging asreclaim_offload_host_memory(), without the offload-policy gate.reclaim_offload_host_memory()now calls it after its policy check, so its behaviour is unchanged.DiffusionBackend._unload_locked()(unload, and before a replacement load) andVideoBackend._teardown_state_locked(), afterclear_gpu_cache()has run gc.It never touches live tensors, device caches or generation, and unsupported allocators or failures are non-fatal, as before.
Results
Host anonymous RSS in GiB, from the Studio diffusion edge suite, fast tier, SDXL-Turbo and Wan2.2-TI2V-5B, one B200. Base is
mainat 9a8f246.image.leak_cyclesandswitch.video_to_imagefail onmainand pass with this change on both surfaces. Across the 50 fast-tier checks the summed check time was 881 s onmainand 837 s here, so the trim adds no visible cost to unload.Testing
reclaim_host_memory()ignores the offload policy and stays best-effort, and both the image unload and the video teardown trim afterclear_gpu_cache()with the state already dropped.tests/test_diffusion_memory.py,tests/test_diffusion_backend.py,tests/test_video_backend.py: 910 passed.mainand on this branch: 44 pass / 6 fail onmain, 48 / 2 here. The two left are the mid-render VRAM case and a 1536x1536 render refused for lack of free VRAM on a shared GPU; the same check fails onmainunder the same load.image.leak_cyclesandswitch.video_to_imagepass on both surfaces.image.unload_during_generate(VRAM held after unloading mid-render) is a separate issue, fixed in #11790.Risk
Low. The trim runs once per unload, after the model is gone, on the same code path #10184 already uses after generation, including its Linux, macOS and Windows handling.
Overlap with #11766
#11766 changes the same two lines (it wraps
clear_gpu_cache()in atryand releases pinned host memory in thefinally), so whichever merges second has a small conflict. The resolution is to keep both, withreclaim_host_memory()afterrelease_pinned_host_memory()so the trim runs once the pinned chunks are gone.