Skip to content

Studio Images: run Detailer inpaints eager and uncounted - #15063

Merged
danielhanchen merged 3 commits into
mainfrom
studio-detailer-compile-shapes
Oct 24, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-detailer-compile-shapes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The Detailer (face fix) pass inpaints every detection at its own crop size. Today those inpaints go through the normal render path, so on a compiled load they:

  • compile the static SDXL U-Net again for every new face size, and capture a CUDA graph for each one until the per-module cap (4) is full; after that a main render at a new resolution runs without a graph,
  • count as generations, so the deferred speed profile can engage in the middle of a Detailer pass,
  • feed the background compiler's recorder, which then precompiles crop shapes instead of the user's main resolution.

This change runs Detailer inpaints as an auxiliary pass: eager, with no CUDA graph capture, nothing recorded for the background compile, no compile shape registered and no generation counted. The working resolution and sizing are unchanged (same guide_size / max_size / crop_factor math). ComfyUI-Impact-Pack's FaceDetailer also renders each crop at its own size (core.enhance_detail: new_w = int(w * upscale), then tensor_resize, with no bucketing), and ComfyUI runs it eager, so this keeps output parity while removing the per-face compiles.

Changes

  • diffusion_bg_compile.py: auxiliary_pass() context (sets force-eager and no-record, plus an auxiliary flag) and gate_compiled_eager(modules), which routes a whole-module compiled U-Net to its eager call while force-eager is set. Without the gate, force-eager only reached the guarded regional blocks and the graph wrappers, whose eager path still calls the compiled U-Net.
  • diffusion.py generate: under an auxiliary pass, install that gate for the call, skip deferred speed engagement, skip the background compile note/kick and compile-cache shape registration, and do not bump generation_count.
  • diffusion_detailer.py: every crop inpaint runs inside auxiliary_pass().

The SAM 3 park path is unchanged: skipping empty_cache after parking is intentional (the comment records that the flush took minutes on a busy card, and the freed blocks stay available to the render in the same process).

Tests

  • test_diffusion_detailer.py::test_every_inpaint_runs_as_an_auxiliary_eager_unrecorded_pass: each inpaint sees force-eager, no-record and the auxiliary flag; all are cleared afterwards.
  • test_diffusion_cold_start.py::test_a_detailer_pass_neither_compiles_a_whole_compiled_unet_nor_feeds_the_recorder: a whole-module compiled net with an armed recorder takes 0 dynamo compiles and records no samples for 3 new shapes, and compiles normally once the gate is removed.
  • test_diffusion_cold_start.py::test_a_whole_compiled_unet_under_the_gate_takes_no_cuda_graph: no capture through GraphedCompiledCall, and the slot is restored.
  • test_diffusion_cold_start.py::test_force_eager_alone_still_reaches_a_whole_compiled_module: control showing why the gate is needed.
  • test_diffusion_backend.py::test_detailer_passes_neither_count_nor_engage_deferred_speed: 3 Detailer passes after 2 renders leave generation_count at 2 and do not engage; the next main render engages.

The 4 new behaviour tests fail on main and pass with this change.

GPU check on a tiny SDXL pipeline (hf-internal-testing/tiny-stable-diffusion-xl-pipe, default compiled tier with CUDA graphs): 2 main renders at 256x256, then 4 inpaints at 192x256, 320x256, 128x192 and 256x384, then 2 more main renders.

main this change
dynamo graphs added by the 4 inpaints 4 0
CUDA graph captures after the inpaints 4 (cap full, 3 cap skips) 1
generation_count after the inpaints 6 2
graphs added by the follow-up main renders 0 0

Each Detailer detection inpaints at its own crop size. On a compiled load
that recompiled the static SDXL U-Net per face, filled the per-module CUDA
graph cap, counted as a generation (engaging the deferred compile mid pass)
and fed crop shapes to the background compiler's recorder.

Run every crop inpaint inside diffusion_bg_compile.auxiliary_pass(): force
eager (with a gate so a whole-module compiled U-Net honours it), no graph
capture, no recorded samples, no compile shape registered and no
generation_count bump. Sizing and working resolution are unchanged.
@danielhanchen danielhanchen self-assigned this Oct 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T03:25:56.374466Z 0dcf23a 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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 0dcf23abf8

ℹ️ 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 b276691 into main Oct 24, 2026
38 of 42 checks passed
@danielhanchen
danielhanchen deleted the studio-detailer-compile-shapes branch October 24, 2026 05:26
danielhanchen added a commit that referenced this pull request Oct 27, 2026
…eager peak, honor force-eager (#15456)

Two regressions from #15183 in
`studio/backend/core/inference/diffusion_native_quant.py`.

**Peak memory above eager.** The compiled W8A8 linear used a chunk
budget of `1 << 27` elements, 4x the eager `1 << 25`, on the reasoning
that it only holds the int32 accumulator. It also holds the output chunk
at the same time, and the chunk outputs were then joined with
`torch.cat`, so a large linear peaked above the eager path. For an
8192-row bf16 linear 3072 -> 14336 (Wan 2.2 5B sized) with rotation 256:
558 MiB before #15183, 672 MiB on main, so it runs out of memory with
650 MiB of headroom where the eager path fits.

Now the fused budget equals the eager one and the chunks are written
into one preallocated output with `copy_` (no list of parts plus a
`torch.cat` copy).

**Force-eager bypass.** `_forward_fused` checked Dynamo tracing and CUDA
graph capture but not the shared `eager_forced()` flag from
`diffusion_bg_compile`, so a cold native int8 linear inside the
Detailer's `auxiliary_pass()` compiled a graph despite the #15063
contract. It now returns to the eager kernels while the flag is set. The
same check also applies while a background compile is running, so those
generations run the int8 linears eagerly for that window.

Per-linear peak (MiB) and time (ms), shared card, 20-iteration means, so
timings are indicative only:

| shape (rows, in -> out), rotation | before #15183 | main | this PR |
before ms | main ms | this PR ms |
|---|---:|---:|---:|---:|---:|---:|
| 8192, 3072 -> 14336, 256 | 558.06 | 672.02 | 392.00 | 1.58 | 0.58 |
0.74 |
| 8192, 3072 -> 14336, 0 | 510.06 | 672.02 | 392.00 | 1.57 | 0.58 | 0.73
|
| 8192, 14336 -> 3072, 256 | 400.01 | 336.02 | 132.00 | 1.80 | 0.55 |
0.67 |
| 20000, 3072 -> 14336, 256, no bias | 1210.94 | 1093.75 | 729.23 | 3.76
| 1.56 | 1.70 |
| 4096, 3072 -> 3072, 256 | 180.03 | 72.01 | 72.01 | 0.26 | 0.15 | 0.15
|

Outputs are bit-identical across all arms on every shape. Inside
`auxiliary_pass()`: main makes 1 fused call and 1 compiled graph, this
PR makes 0 and 0, and the layer goes back to the fused path after the
context exits.

Tests in `test_diffusion_native_quant_fused.py`: chunks fill one output
in row order, the fused peak never exceeds eager across widths, no fused
call under `auxiliary_pass()` / `force_eager()`, and a CUDA test for
peak at or below eager plus bit-equality. The CUDA skip guards use
`has_real_cuda()`. Against main's module the new CPU tests give 5
failed, with this PR 21 passed, 4 skipped on CPU and all 25 passed on a
GPU.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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