Skip to content

Studio sd.cpp: --mmap for CPU renders on builds whose --mmap keeps the CPU repack - #16758

Merged
danielhanchen merged 4 commits into
mainfrom
studio-sdcpp-linux-mmap
Nov 5, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-sdcpp-linux-mmap

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

On Linux and Windows CPU hosts Studio starts sd.cpp without --mmap, so every weight is copied into anonymous memory. Z-Image Turbo Q4_K_M with the BF16 Qwen3-4B encoder peaks at about 12.9 GB of anonymous RSS at 512 px. The current pin's --mmap is not a fix: it skips the CPU weight repack for mapped weights, which changes the pixels.

unslothai/stable-diffusion.cpp#43 makes --mmap keep the repack (repackable weights are loaded and repacked, everything else stays file-backed, copied ranges are released) and says so in --help. This PR is the Studio half of #43 (the commit prepared alongside it, rebased onto current main). It sends --mmap for CPU renders only on builds whose --help carries that text, so it stays off until the pin includes #43, then turns on by itself.

  • mmap_flags() replaces mac_mmap_flags() at the three command builders (sd-cli image, sd-cli video, sd-server). macOS is unchanged.
  • Off macOS it sends --mmap when the render runs on the CPU (the CPU bundle per the install record, or an explicit --backend cpu) and sd-cli --help contains weights the CPU repacks are still loaded. GPU bundles are left alone.
  • UNSLOTH_DIFFUSION_SD_CPP_MMAP=1/0 now forces it on or off on every OS.

Evidence (Colab CPU High-RAM, 4 cores x 2 SMT, 50 GB, same VM, interleaved)

Z-Image Turbo Q4_K_M + Qwen3-4B BF16 + FLUX.1 VAE, 512 px, 2 steps, Studio's CPU argv, median [min, max] of 3 timed renders:

build s/img peak anon RSS peak RSS pixels
pin, no --mmap (today) 206.1 [205.3, 216.3] 12.9 GB 12.9 GB reference for the pin
pin, --mmap 222.4 [200.9, 233.7] 1.9 GB 12.2 GB differ (repack skipped)
#43, no --mmap 219.7 [208.6, 225.1] 12.9 GB 12.9 GB reference for #43
#43, --mmap 203.1 [202.3, 221.4] 3.9 GB 12.1 GB identical to #43 without --mmap

FLUX.2 klein 4B Q4_K_M + Qwen3-4B BF16, 512 px, 2 steps, 2 timed renders: #43 232.1 [223.8, 240.4] s, 10.3 GB anonymous, vs #43 --mmap 226.0 [215.2, 236.7] s, 3.4 GB anonymous, identical pixels.

Weight load time (sd.cpp's loading tensors completed, median [min, max] over first + timed runs): Z-Image #43 18.0 [5.2, 34.3] s vs #43 --mmap 4.3 [3.8, 7.9] s; klein 6.4 [4.0, 9.4] s vs 2.6 [2.6, 3.0] s. Studio keeps sd-server resident, so this is paid once per model load.

Speed is a tie (the spreads overlap). Anonymous memory drops 12.9 to 3.9 GB (Z-Image) and 10.3 to 3.4 GB (klein); the rest is clean page cache the kernel can drop under pressure. The pin bundle and a source build differ in pixels from each other (compiler), which is why each build is compared with itself.

Tests

  • New tests in test_sd_cpp_args.py: CPU bundle with the help text gets --mmap; a build without it, a GPU bundle, an explicit --mmap in extra args and the env override are covered. They fail on main.
  • pytest -o addopts= tests/test_*sd_cpp*.py with no GPU visible: 1558 passed, 3 skipped (main: 1554 passed, 3 skipped).

Not covered

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

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T14:14:59.124414Z ecab7a0 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.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: ecab7a09c8

ℹ️ 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 c014c8b into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the studio-sdcpp-linux-mmap branch November 5, 2026 20:31
danielhanchen added a commit that referenced this pull request Nov 6, 2026
…t navigate (#16858)

## Summary

`Chat UI Tests` on Windows fails now and then in
`tests/studio/playwright_browser_bookmarks.py`, on the step that follows
a link inside the browser panel's page. In the last day it hit main
itself (run 42143788184, after #16856) and #16835 and #16758, none of
which touch the browser panel:

```
FAIL Save, followed=True: Locator expected to have Value 'https://bookmark.test/followed'
Actual value: https://bookmark.test/first
```

The failure screenshot from the main run shows the typed page still
loaded with "Follow link" on it. The click never produced a navigation,
so the address bar stayed on the first page for the whole 5 s wait. The
same Save and Enter cases pass in the same run and on the next run. All
three failures were on the Windows lane. It looks like a click lost on a
loaded Windows runner, not a regression: the page's link handler is
installed before the page's own content, and the panel accepts the first
same-tab navigation a page reports.

## Change

`tests/studio/playwright_browser_bookmarks.py` only. A small
`follow_link` helper clicks "Follow link" and waits 5 s for the address
to change, as before. If it has not, and the address is still the typed
page and the tab still says "Typed page", it clicks again, up to three
clicks in total, and prints a line when it retries.

It does not retry if the address moved anywhere else, so a wrong
navigation still fails at once. A second click cannot double a slow
navigation either: once a page has reported a same-tab navigation the
panel ignores any further one from it (`left` in `page-frame.tsx`).
Every later assertion (bookmark contents, Back to the first page,
Forward to the followed one) is unchanged, so a link that really stops
navigating still fails after the third click.

## Testing

- A fake page and address bar drive `follow_link` through five cases:
navigates on the first, second or third click (passes, 1/2/3 clicks),
never navigates (fails after 3 clicks), lands on another page (fails
after 1 click).
- `tests/studio/test_playwright_suites_run_in_ci.py`: 9 passed.
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