Skip to content

Studio diffusion: CFG-parallel for U-Net families (SDXL) by batch split - #16679

Merged
danielhanchen merged 3 commits into
mainfrom
mg3-sdxl-unet-cfg-parallel
Nov 5, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
mg3-sdxl-unet-cfg-parallel

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

A U-Net denoiser was never CFG-parallel (no DiT denoiser), so SDXL on two GPUs ran at one GPU's speed: the second card sat idle.

Change

  • UnetCfgParallelRouter (diffusion_multigpu.py) splits a batch-2B U-Net call along dim 0 and runs the conditional and unconditional halves on the two cards, then concatenates.
  • Odd batches, unknown inputs, PAG processors, whole-compiled modules and replica failures stay on one card.
  • Kill switch UNSLOTH_DIFFUSION_CFG_PARALLEL_UNET=0 (bitwise identical to main).

Evidence

  • SDXL 1024x1024, CFG 7, Kaggle 2x T4, gpu_ids=[0,1]: 24.47 -> 13.94 s per image (1.75x), LPIPS 0.003 against one card.
  • Compiled (speed_mode="default"): 20.12-20.22 -> 11.05-11.58 s (1.79x), 0 graph breaks, 0 recompiles after the first render, every guided step paired (100 of 100).
  • 9 tests.

Stack

This is one of four stacked PRs that make every decision to use a second GPU automatic, merged in order:

  1. U-Net CFG-parallel (SDXL)
  2. Automatic arms CFG-parallel on an idle card of the same model
  3. CFG-parallel for a group-offloaded denoiser
  4. Automatic partner card, FLUX batch split, LoRA scale mirror, CFG batching fallback

Results for the whole stack, Kaggle 2x T4, same VM per row, base A/A first:

model one card s Automatic s speedup output vs one card
SDXL 1024, CFG 7 23.13 13.18 1.75x LPIPS 0.003, >= 39.97 dB (kill switch bitwise)
SDXL, compiled 20.12-20.16 11.06-11.29 1.79x LPIPS 0.0040 (one-card A/A 0.0043)
SDXL, batch 2 55.83 27.97 2.00x LPIPS 0.0042
Qwen-Image-2.1, CFG 4 256-280 164-165 1.63x bitwise
Wan2.2-TI2V-5B, CFG 5, per clip 211.10 144.49 1.46x LPIPS 0.0025 (kill switch bitwise)
FLUX.2-klein-4B, batch 2 22.61 12.74-13.40 1.69-1.77x LPIPS 0.0007
  • 0 graph breaks and 0 recompiles or CUDA-graph captures on every follow-up render. The first compiled image is 25-30 s slower on two cards, paid once per load while the second card compiles its copy.
  • AMD gfx1151 (Linux, one real GPU plus a faked second device) and Windows 11 CPU: no regressions (same failures on base and head). No change touches a Triton / CUDA kernel.
  • Not covered: no run on more than 2 cards (the 3-8 card rule is unit-tested only); NVLink pairs; a Windows GPU run.

A U-Net pipeline sends both guidance branches through one call at batch 2B, so
CFG-parallel declined it ('no DiT denoiser') and the second card stayed idle.
UnetCfgParallelRouter cuts that call in half along the batch, runs the second
half on the replica card through run_pair, and concatenates. Odd batches,
unknown tensor inputs and replica failures stay on the source card.
UNSLOTH_DIFFUSION_CFG_PARALLEL_UNET=0 turns it off.
The primary half goes through _primary_noting (waits on a cold replica, marks its
signature warm); a module compiled whole after the router was installed, or a call
under torch.compile, runs whole on the source; PAG-style processors that chunk the
batch themselves keep the U-Net off the router.
@danielhanchen danielhanchen self-assigned this Nov 4, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 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-04T16:41:30.825708Z d0a4314 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: d0a431436e

ℹ️ 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 eb805d3 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the mg3-sdxl-unet-cfg-parallel branch November 5, 2026 03:25
danielhanchen added a commit that referenced this pull request Nov 5, 2026
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