Skip to content

Studio diffusion: return the heap a finished image load freed to the OS - #16934

Merged
danielhanchen merged 4 commits into
mainfrom
studio-diffusion-trim-heap-after-load
Nov 7, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-diffusion-trim-heap-after-load

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

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:

  • Warm load (prequant cache hit): the trim returns about 650 MiB. Real host RAM use (MemAvailable drop) goes from 19.77 to 19.16 GiB (median).
  • Cold first load (int8 build): the trim returns about 1.9 GiB.

Images are md5-identical, and the load, first image and per-image times are unchanged.

What / why

  • What: new trim_host_heap_after_load in diffusion_memory.py, called once at the end of DiffusionBackend.load_pipeline, after the load-time pins have finished.
    • It reuses the allocator reclaimer that unload already uses: malloc_trim(0) on Linux, malloc_zone_pressure_relief on macOS, _heapmin on Windows.
    • It does not touch live tensors, Python GC, torch's pinned host cache or device caches.
    • It logs the MiB returned and the time taken (19-50 ms here).
    • Kill switch: UNSLOTH_DIFFUSION_LOAD_HEAP_TRIM=0.
  • Why:
    • Host RAM was broken down by owner on the 8 GiB budget run: smaps per mapping, every live CPU tensor mapped to the pinned slab or registered buffer holding it, and glibc mallinfo2.
    • The weights are not duplicated: 15.6 GiB is pinned (the DiT slabs plus the registered encoder copy), and every pinned slab holds live weights.
    • On top of that, the main process held 2.8 GiB of private anonymous memory, about 650 MiB of it freed heap that glibc had not returned.
    • 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.
    • A second trim after the first and second renders returns only 4-8 MiB, so the load is where that memory comes from.
    • MALLOC_ARENA_MAX=2 did not change anything (19.73 GiB).
  • Failure handling: the trim never fails a load. A missing allocator API is a no-op, and a failing call returns False with one warning per process.

Evidence (Colab RTX PRO 6000 G4, one VM, base 992185ae8e vs 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 MemAvailable drop from before the load to its minimum over the cell (load plus 6 renders).

case arm MemAvailable drop GiB main-process anon after 6 renders MiB trim log
8 GiB budget, warm load base 19.79 / 19.79 / 19.73 / 19.74 2820 / 2820 / 2797 / 2797 -
8 GiB budget, warm load head 19.20 / 19.13 / 19.14 / 19.17 2240 / 2174 / 2218 / 2218 returned 646-664 MiB (19-31 ms)
8 GiB budget, cold first load base 21.36 / 21.45 4447 (anon at load end 3879) -
8 GiB budget, cold first load head 20.28 2520 (anon at load end 1939) returned 1964 MiB (50 ms)
16 GiB budget base / head 12.77 / 12.17 2783 / 2161 returned 646 MiB
resident base / head 5.16 / 3.29 2784 / 2146 returned 646 MiB
  • Base A/A spread at 8 GiB is 0.06 GiB (19.73-19.79, plus 19.81 / 19.75 / 19.80 in an earlier run on the same VM). The head delta is 10x that.
  • The 16 GiB and resident rows are one pair each.
  • Speed (same cells):
base head
steady s/image, 8 GiB 3.305 / 3.300 / 3.302 / 3.322 3.300 / 3.302 / 3.291 / 3.290
first image s (warm load) 12.02 / 11.37 / 11.25 / 11.32 11.98 / 11.81 / 11.31 / 11.36
load s (warm) 10.52 / 11.10 / 10.40 / 10.91 10.92 / 10.88 / 10.47 / 10.91 (two more head cells)
  • Two more head loads took 12.12 and 12.00 s. Each was the cell straight after a cold first load on the VM, so the position in the run, not the arm, explains them. The trim itself took 19-31 ms by its own log.
  • Outputs: all three images are md5-identical across every base and head cell, at every budget.

Tests

New studio/backend/tests/test_diffusion_load_heap_trim.py (CPU only):

  • Real glibc check in a subprocess: 375 MiB of 64 KiB chunks, with every 8th one kept alive so free() cannot shrink the heap. The trim takes RssAnon from 385 MiB to 59 MiB. With the kill switch, it stays at 385.
  • load_pipeline calls the trim after finish_encoder_pins_at_load, as the last statement before it returns.
  • Kill switch values, no allocator API (quiet no-op), and a failing reclaimer (returns False, warns once, never raises).
  • test_diffusion_memory.py: 156 passed, 6 skipped.

Validation

  • Studio regression switchboard (merge base vs head 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.
  • Cross-platform CI: ubuntu-latest 10 passed; windows-latest 9 passed, 1 skipped; macos-15 9 passed, 1 skipped. The skip is the Linux-only glibc test.

Not proven

  • macOS (malloc_zone_pressure_relief) and Windows (_heapmin) were not measured on a GPU. Both are the calls unload already makes on those platforms.
  • Video loads are unchanged. This PR only trims after an image load.
  • Measured on one model, Qwen-Image-2.1. The trim is model independent, but the MiB returned depends on how much the load staged on the heap.

@danielhanchen danielhanchen self-assigned this Nov 6, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 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-07T07:52:48.160782Z abadbda 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

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: abadbda9bb

ℹ️ 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 a3ed62e into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-trim-heap-after-load branch November 7, 2026 07:53
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