Skip to content

Studio: compile the SDXL U-Net per block so the 4th image no longer waits for the whole compile - #16085

Merged
danielhanchen merged 6 commits into
mainfrom
studio-sdxl-compile-no-wait
Oct 30, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-sdxl-compile-no-wait

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On a fresh Studio home, SDXL's 4th image waited for the whole background compile of the U-Net: 119 to 126 s on
A100 and 140 to 142 s on L4. An eager render takes 2.8 s and 9.6 s. This PR compiles the U-Net per block and lets a
render pause that compile between blocks instead of waiting for all of it. With it, the 4th image takes 6.9 s on
A100 and 17.5 s on L4. Steady compiled speed and image quality match main.

Change

  1. Per-block U-Net compile (diffusion_speed._compile_unet_blocks, diffusion_autoopt/targets.py)
    • On the default tier, UNet2DConditionModel compiles each BasicTransformerBlock, ResnetBlock2D,
      Downsample2D and Upsample2D with the same settings as before (fullgraph, dynamic=False, same inductor
      knobs). Before, it compiled the whole module.
    • The blocks sit behind the existing regional compile guard, so a compile failure falls back to eager and is
      reported. The outer forward stays eager inside the existing denoiser CUDA graph.
    • SDXL at 1024^2 gets 19 graphs, the largest being one block.
    • The compile cache key gains unet_graph=blocks. The dynamo recompile limit is 256 on this path.
    • Max-autotune keeps the whole-module compile.
    • Kill switch: UNSLOTH_DIFFUSION_UNET_REGIONAL=0 restores the whole-module compile.
  2. A render pauses the background compile (diffusion_bg_compile.BackgroundCompile.pause / resume /
    yield_point, diffusion._wait_or_pause_bg_compile)
    • A render that arrives mid-compile lets it run for one slice, which is this load's fastest eager render. It then
      parks the compile thread at the next block boundary, renders eager, and resumes the compile afterwards. The park
      point is in the compile guard, between two graphs, never inside a trace.
    • The pause is requested one measured block graph before the slice ends.
    • A resumed compile must finish at least one graph before it can park again, so back-to-back renders cannot
      starve it.
    • A render that requests or keeps LoRA adapters waits for the whole compile, as before.
    • Unload ends a parked compile.
    • Only the per-block U-Net is marked as yielding; DiT background compiles are unchanged.
    • Kill switch: UNSLOTH_DIFFUSION_BG_COMPILE_YIELD=0 waits for the whole compile, as before.
      UNSLOTH_DIFFUSION_BG_COMPILE_SLICE_S overrides the slice.
  3. Compile workers start on the 2nd image (diffusion_bg_compile.warm_compile_pool)
    • AsyncCompile.warm_pool() and wakeup() start inductor's compile workers in the background on the 2nd image,
      once per process. Until those workers are ready, inductor compiles kernels in-process, so the first block graph
      used to take 7.0 to 7.7 s instead of 4.2 to 4.4 s.
    • Kill switch: UNSLOTH_DIFFUSION_BG_COMPILE_WARM_POOL=0.

Results

The setup for every table below:

  • SDXL base, 1024^2, 25 steps, dpmpp_2m karras, CFG 7.
  • Each arm is a fresh process with a fresh Studio home and inductor cache: 1 warm-up, then 7 new prompts back to
    back.
  • One VM per card: A100-SXM4-40GB and L4. Times are render wall clock in seconds.
  • Base is the merge base 9a1ea09dc3. "Head before warm-up" is the branch without change 3.

4th image (the first render after the deferred profile engages)

arm A100 4th image A100 wait L4 4th image L4 wait
base 118.8 / 122.2 / 122.8 / 126.0 116 to 123 140.3 / 140.0 / 141.7 / 140.3 130 to 132
head 6.91 / 6.84 / 6.89 4.2 17.5 / 17.3 / 17.7 (before warm-up) 7.8 to 8.2
head before warm-up (same VM as the head row) 9.71 / 10.28 / 9.96 7.0 to 7.6
head, UNSLOTH_DIFFUSION_BG_COMPILE_YIELD=0 56.6 53.9 71.5 61.5
head, UNSLOTH_DIFFUSION_UNET_REGIONAL=0 120.5 117.8 140.6 130.6
eager (speed_mode=off) 2.80 9.6

How the head rows behave around the 4th image:

  • On A100, renders 5 to 7 take 3.1 to 6.3 s while the compile finishes in the gaps. Each wait after the 4th image
    is 0.3 to 4.6 s.
  • The warm-up costs 0.3 s on the 2nd image: 3.06 to 3.14 s against 2.78 to 2.79 s. Over the first seven renders it
    is still 3 s faster: 32.3 to 32.5 s against 34.7 to 35.6 s, leaving out the first cell on the VM.

23 back-to-back renders, no think time (A100, same VM)

arm sum of 23 renders sum of renders 1 to 7 slowest render
base r6 281.2 s 137.9 s 121.3 s
base r21 285.6 s 139.9 s 123.3 s
head before warm-up 103.2 s 35.6 s 10.1 s
all eager (2.8 s x 23) about 64.5 s about 19.6 s 2.9 s

What the sums mean:

  • Each base run includes one 109 to 112 s render at prompt 21: a prompt longer than 77 tokens changes the text
    shape, and the whole module recompiles inline. Without that render, base sums to 172 to 174 s.
  • Head recompiles only the two transformer-block graphs for that prompt, in 5 s.
  • Head beats base in total time as well as on the 4th image.
  • Over a short back-to-back burst, head is still slower than never compiling: about 54 s of compile CPU against a
    saving of 0.55 s per compiled render.
  • With 5 s of think time between renders, the waits drop to 0.1 to 2.2 s.

Steady state once compiled

arm A100 renders 4 to 7 A100 long renders L4 renders 4 to 7 L4 long renders
base 2.257 to 2.276 2.217 to 2.221 8.63 to 8.88 8.51 to 8.54
head (all 19 graphs compiled before prompt 4, ..._YIELD=0) 2.264 to 2.269 2.221 to 2.227 8.64 to 8.77 8.48 to 8.53

Recompiles: dynamo's unique graph count stays at 19 on every prompt after the compile completes. Base stays at 1
until the long prompt.

Accuracy

LPIPS on full-resolution images, A100, the 11 prompts that head rendered compiled in a 23-prompt run:

pair mean max
head vs base 0.056 / 0.059 0.115 / 0.178
base vs base (another process) 0.064 0.173
eager vs base 0.060 / 0.066 0.110 / 0.184

Over the first 7 prompts, head vs base is 0.023 to 0.035 on A100 and 0.025 to 0.034 on L4. Base vs base on those
prompts is 0.032 to 0.036 and 0.028 to 0.030. Images 1 and 2 are bit-identical in every arm.

Tests

  • studio/backend/tests/test_diffusion_unet_block_compile.py adds 37 CPU tests:
    • the per-block compile on a tiny diffusers U-Net, the kill switches, the max-autotune path, the guard and fallback
      report, and the cache key
    • pause and resume between blocks, the early pause request, no starvation, a nested pause, closing a parked
      compile
    • wait vs pause on the render side, the LoRA wait, the fastest-eager slice, the worker warm-up
  • The new tests fail on the merge base.
  • The existing test files touching the background compile, the speed layer or the compile cache show the same
    failing test IDs before and after this change. Those failures are all outside this change.

Not proven

  • The 4th image on L4 was measured before change 3. The warm-up was measured on A100 only.
  • Steady state with change 3 was not re-measured. It only starts compile workers earlier, and the compiled code is
    the same.
  • Not run on a GPU:
    • SD 1.5 / SD 2.x / SDXL refiner U-Nets
    • img2img, inpaint, ControlNet, hires
    • the LoRA wait path
    • a resolution change after the compile; per the microbench this recompiles 19 graphs inline, about 55 s against
      about 120 s for the whole module
    • fp16 or offloaded U-Nets
    • the max tier
    • a returning home loading a unet_graph=blocks bundle
  • ROCm, Windows and macOS did not run. ROCm stays out of the background compile, as before.

…ackground compile

The deferred profile compiled UNet2DConditionModel as one module (118-154 s, CPU bound) and the 4th
render waited for all of it. The default tier now compiles the U-Net's repeated blocks
(BasicTransformerBlock, ResnetBlock2D, Downsample2D, Upsample2D) behind the regional compile guard,
and a render that finds that background compile in flight lets it run one slice (about one eager
render), then parks it at the next block boundary and runs eager; the compile resumes after the
render. UNSLOTH_DIFFUSION_UNET_REGIONAL=0 restores the whole-module compile,
UNSLOTH_DIFFUSION_BG_COMPILE_YIELD=0 the wait, UNSLOTH_DIFFUSION_BG_COMPILE_SLICE_S sets the slice.
…e slice ends

A render waited its slice plus the block graph in flight (A100: 4.4-7.4 s against a 2.8 s eager
render). The compile now times its block graphs and the render asks for the pause that much earlier,
so the wait stays about one slice; a released compile owes one graph before it parks again, so
back-to-back renders cannot starve it.
…he deferred compile

Until the worker pool is ready inductor compiles kernels in-process, so the first block graph of the
background compile took 7.2 s against 0.8-3.6 s for the rest. AsyncCompile.warm_pool + wakeup on
the 2nd image starts the workers in the background. UNSLOTH_DIFFUSION_BG_COMPILE_WARM_POOL=0 disables.
@danielhanchen danielhanchen self-assigned this Oct 30, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 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-10-30T08:32:14.265807Z ff6e37a 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

1 similar comment
@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: ff6e37ae7c

ℹ️ 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 added a commit that referenced this pull request Oct 30, 2026
…#16088)

`Repo tests (CPU, rest-2)` is red on main since #16062 (MXFP4 torch
decode fallback: run ldexp on the operands' GPU). It fails on unrelated
PRs such as #16085 and #16074:

```
tests/test_mxfp4_compressed_linear.py::test_torch_fallback_runs_ldexp_on_the_operands_gpu[dtype0..2]
unsloth/models/mxfp4_compressed_linear.py:99: in _dequantize_torch_impl
    out[..., 0::2] = lut[(blocks & 0x0F).long()]
RuntimeError: PyTorch is not linked with support for cuda devices
```

The test builds its operands on `cuda:1` inside `FakeTensorMode`. That
works on any CUDA-linked torch, with or without a GPU, but a CPU-only
build cannot dispatch a fake CUDA tensor. The CPU shards install
CPU-only torch. I checked the same few lines on both builds:
`2.13.0+cu130` with no visible GPU runs them, and `2.10.0+cpu` raises
the error above. The product's device guard is fine; the test assumed a
CUDA build.

## Changes

- `test_torch_fallback_runs_ldexp_on_the_operands_gpu` now skips when
`torch.backends.cuda.is_built()` is false. It still runs on CUDA builds,
GPU or not.
- New `test_the_torch_decode_runs_inside_its_operands_device_guard` runs
on any build, so the CPU shards keep covering the guard. It checks three
things: `_dequantize_torch` decodes with the operand's device current,
puts the previous device back, and leaves the current device alone for a
CPU operand or an index-less `cuda` operand.
- The fake `torch.cuda.device` guard is shared by both tests.

## Verification

- CPU-only torch 2.10: the file has 20 passed and 38 skipped, and the
fake-tensor test skips with the reason above. On main it had 3 failures.
- CUDA torch 2.13 with no visible GPU: the three fake-tensor cases and
the new test pass (4 passed).
- Sabotage: dropping `_operand_device_guard` from `_dequantize_torch`
fails the new test on CPU-only torch.
danielhanchen added a commit that referenced this pull request Oct 30, 2026
#16069 (fill locale gaps and translate the sign-in, consent and Desktop
startup screens) moved the update banners onto `t()`. Two checks that
pinned the old English literals went red on main. They fail on unrelated
PRs, for example #16085:

```
Repo tests (CPU, studio)
  test_update_release_notes.py::test_banners_toggle_inline_release_notes[banner1]
  test_update_release_notes.py::test_notes_toggle_shares_the_action_row[banner1-tauri-update-release-notes-toggle-Remind me later]
  test_desktop_reliability_frontend_contract.py::test_collapsed_tauri_keeps_history_arrows_and_adds_new_chat_by_model_picker
Chat UI Tests
  [banner] FAIL 1440x900: three pending runtimes share the one card summary='llama.cpp, whisper.cpp, and audio.cpp · No restart needed'
  (same at all 6 viewports)
```

In both cases the product is right and the tests read the old text.

## Release notes toggle contract

The Tauri banner now renders `t("desktopUpdate.banner.showNotes")`,
`hideNotes` and `remindLater`. Their English, "Show release notes",
"Hide release notes" and "Remind me later", is unchanged. A new
`_shown_at(src, label)` helper finds a label either as literal text,
which is how the web banner still renders it, or as a `t("key")` call
whose `en.ts` value is that label. It uses the shared
`tests/studio/_en_catalog.py` reader. The ordering check (toggle before
the action, in the same row) uses the position of whichever form is
present. A unit test pins the helper: literal, through the catalog, and
a key whose English differs.

## Titlebar history arrows

The titlebar's back and forward buttons now carry
`aria-label={t("desktopShell.goBack")}` and `goForward`, and their
English is still "Go back" and "Go forward". `_aria_label_count` counts
a label written literally or through a `t()` key whose English matches,
and a unit test pins it.

## Runtime card summary

The card now joins runtime names with `Intl.ListFormat(locale, { type:
"conjunction" })`, which is correct for every locale and gives English a
serial comma. `playwright_update_banner_layout.py` now matches
`llama\.cpp, whisper\.cpp,? and audio\.cpp`. The names must still appear
in that order and joined as one list.

## Verification

- `tests/studio/test_update_release_notes.py`: 2 failed on main, 203
passed here.
- `tests/studio/test_desktop_reliability_frontend_contract.py`: 1 failed
on main, 45 passed here. Pointing the back button at `goForward` fails
it.
- Sabotage: rendering nothing for the Tauri notes toggle fails
`test_banners_toggle_inline_release_notes`. Switching the dismiss button
from `remindLater` to `later` fails
`test_notes_toggle_shares_the_action_row`.
- The new matcher accepts the formatted summary and the old form. It
rejects a missing or reordered runtime.
- The tests that import the layout driver pass (218 passed with the
release notes file).

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@danielhanchen
danielhanchen merged commit 8a02150 into main Oct 30, 2026
32 of 45 checks passed
@danielhanchen
danielhanchen deleted the studio-sdxl-compile-no-wait branch October 30, 2026 11:22
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