Repository navigation
Studio diffusion: return the heap a finished image load freed to the OS - #16934
Conversation
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. |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
Summary
After an image load, Studio now gives the heap the load freed back to the OS. glibc keeps freed chunks below its mmap threshold in its arenas. The staging copies and temporaries a load frees once the weights are pinned or on the GPU therefore stayed counted as used host RAM for the life of the model.
On Qwen-Image-2.1 with an 8 GiB VRAM budget:
MemAvailabledrop) goes from 19.77 to 19.16 GiB (median).Images are md5-identical, and the load, first image and per-image times are unchanged.
What / why
trim_host_heap_after_loadindiffusion_memory.py, called once at the end ofDiffusionBackend.load_pipeline, after the load-time pins have finished.malloc_trim(0)on Linux,malloc_zone_pressure_reliefon macOS,_heapminon Windows.UNSLOTH_DIFFUSION_LOAD_HEAP_TRIM=0.mallinfo2.free()alone cannot shrink a heap whose free chunks sit between live ones. Only a trim walks the arenas and releases the free pages inside them.MALLOC_ARENA_MAX=2did not change anything (19.73 GiB).Evidence (Colab RTX PRO 6000 G4, one VM, base
992185ae8evs head)Qwen-Image-2.1 t2i, 1024^2, 25 steps, VRAM capped by ballast, a fresh process per cell. Cells were interleaved B H H B B H, then H B. Host RAM is the
MemAvailabledrop from before the load to its minimum over the cell (load plus 6 renders).Tests
New
studio/backend/tests/test_diffusion_load_heap_trim.py(CPU only):free()cannot shrink the heap. The trim takes RssAnon from 385 MiB to 59 MiB. With the kill switch, it stays at 385.load_pipelinecalls the trim afterfinish_encoder_pins_at_load, as the last statement before it returns.test_diffusion_memory.py: 156 passed, 6 skipped.Validation
71877d47a9): NO_REGRESSION. 67 steps SAME, 2 DOM-only differences with 0 changed pixels (crawl export / data recipes pages). The diffusion generate / video journeys, the tiny diffusion bench suites (HTTP, in-process, matrix), the diffusion imports and the planner matrix all match base.Not proven
malloc_zone_pressure_relief) and Windows (_heapmin) were not measured on a GPU. Both are the calls unload already makes on those platforms.