Skip to content

Studio diffusion: Automatic arms CFG-parallel on an idle card of the same model - #16680

Merged
danielhanchen merged 8 commits into
mainfrom
mg3-auto-cfg-parallel
Nov 5, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
mg3-auto-cfg-parallel

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Automatic placement (no gpu_ids, the UI default) never armed CFG-parallel, so a guided model that fits one card left an identical idle second card unused.

Change

  • Automatic arms CFG-parallel on a secondary that is the same card model and idle at load: at most 1 GiB in use and 5% utilisation, read from nvidia-smi so no CUDA context is opened on it. The idle check runs again when the replica is built.
  • Compile arguments are recorded under Automatic too, so a compiled model's replica builds instead of silently turning CFG-parallel off.
  • Kill switch UNSLOTH_DIFFUSION_CFG_PARALLEL_AUTO=0.
  • The 4 tests that pinned the old rule (Automatic never pairs) are updated, with the reason in a comment.

Evidence

  • Qwen-Image-2.1, CFG 4, Kaggle 2x T4: Automatic 269.3 -> 165.0 s (1.63x), bitwise against one card.
  • SDXL Automatic (with the base PR): 23.13 -> 13.18 s.

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.
…same model

With no gpu_ids (the UI's Automatic) CFG-parallel always declined, so a guided
render on a two-card box left the second card idle unless the user picked both
cards. Automatic now arms it when a secondary is the same card model and idle at
load (at most 1 GiB in use); a busy or different card is never borrowed.
UNSLOTH_DIFFUSION_CFG_PARALLEL_AUTO=0 restores the old behaviour.
Record compile arguments under Automatic too (a compiled denoiser's replica needs
them), and re-check at replica build time that the card Automatic borrowed is
still idle.
@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-04T17:35:13.514711Z 6490905 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6490905be4

ℹ️ 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".

Comment on lines +71 to +75
def _idle_card(probe: Any) -> bool:
free, total = getattr(probe, "free_mib", None), getattr(probe, "total_mib", None)
if free is None or total is None:
return False
return int(total) - int(free) <= AUTO_CFG_PARALLEL_IDLE_USED_MIB

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check GPU utilization before automatically borrowing a card

On a shared host, a secondary GPU running a compute-heavy workload that uses at most 1 GiB of VRAM passes this idle check, so Automatic now places a denoiser replica on an actively used card. Neither DeviceProbe nor smi_free_total_mib() preserves utilization telemetry, and the replica-build recheck uses this same memory-only predicate. This can slow both the existing workload and generation despite the intended idle-only behavior. Preserve the nvidia-smi utilization reading and enforce the stated 5% utilization limit alongside the memory threshold when selecting and rechecking an automatic partner.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The utilisation limit lands in the next PR of this stack, #16682: automatic_partner reads nvidia-smi utilization.gpu per card and refuses a partner above 5%, and as of e128b3b the replica-build recheck reads it again before building on that card (test_automatic_replica_is_not_built_on_a_card_another_job_keeps_busy). The stack is merged in order, so this PR never ships without it.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 6490905be4

ℹ️ 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".

Base automatically changed from mg3-sdxl-unet-cfg-parallel to main November 5, 2026 03:25
@danielhanchen
danielhanchen merged commit 3ea5429 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the mg3-auto-cfg-parallel branch November 5, 2026 03:26
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