Repository navigation
Studio diffusion: allow the multi-GPU layer split on discrete ROCm cards - #14479
Conversation
Replace the blanket ROCm decline with ROCm-specific gates: an APU / unified memory primary or secondary declines with a unified memory reason, the selection compares native bf16 support instead of the gfx target as a CUDA capability, and ROCm flash_attn / AITER keep blocks on cards of the primary's gfx target (flash also probed on each secondary). CUDA paths are unchanged; UNSLOTH_DIFFUSION_LAYER_SPLIT=0 still turns it off.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d5b82639ae
ℹ️ 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".
| from utils.hardware.hardware import _rocm_props_unified_status | ||
| return _rocm_props_unified_status(torch.cuda.get_device_properties(int(ordinal))) |
There was a problem hiding this comment.
Treat gfx1153 as unified before enabling the split
When a Windows ROCm gfx1153/Krackan Point 2 device exposes a missing or zero is_integrated field, this classifier returns False: the underlying hardcoded APU set ends at gfx1152, and its name fallback is skipped whenever gcnArchName is present. Because Studio explicitly supports gfx1153 on Windows, such an APU can now pass _rocm_discrete_secondaries and receive denoiser blocks even though this change intends to exclude shared-memory GPUs; extend the unified classifier for gfx1153 (and cover the unset-integrated case) before using its negative result as proof that a card is discrete.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed. gfx1153 is listed as an APU in the LLVM AMDGPUUsage processor table, and so are gfx1033, gfx1035, gfx1036, gfx90c and the older APU targets, but only gfx1103 and gfx1150-1152 were named, so any of them read as discrete when the runtime leaves is_integrated unset. The shared APU arch set in utils.hardware now names every APU-only target (5f54056); its callers all ask "known APU" and none of those targets shipped as a discrete board. Tests cover the classifier and the split skipping such a secondary.
| if backend is None: | ||
| return cards, None |
There was a problem hiding this comment.
Verify fused SDPA on each native-backend secondary
When a mixed-gfx ROCm secondary has no working fused SDPA kernel at the model dtype, this unconditional native-backend path still assigns it denoiser blocks. Studio's own SDPA diagnostics note that the resulting math backend materializes a quadratic attention matrix and can exceed VRAM by many times, while the layer planner reserves only its ordinary block working set; large image or video renders can therefore OOM on the secondary even though the split was selected to avoid offload. Probe each secondary with the existing per-device SDPA capability check and exclude cards that are math-only (or account for that peak) rather than assuming native SDPA has the same memory behavior everywhere.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed. The primary is probed and priced as math-only at load, but on the native path a mixed-gfx secondary was never probed, so math SDPA could build the full score matrix on a card budgeted for a fused kernel. The ROCm native path now runs the per-device SDPA probe from diffusion_attention on each secondary at the model dtype and drops math-only cards when the primary has a fused kernel, recording a math-only decline if none are left (33a90aedd8). CUDA is unchanged, and a test covers both cases.
gfx1153 Krackan Point 2 and the other targets LLVM types APU only were read as discrete when the runtime leaves is_integrated unset, which let the diffusion layer split hand such a part denoiser blocks.
Under native SDPA a mixed-gfx secondary with no fused kernel at the model dtype would run its blocks on math SDPA, whose score matrix the plan never priced for that card. Probe each secondary with diffusion_attention's per-device SDPA check and drop it when the primary has a fused kernel, recording the reason when no secondary is left. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 813b3489d8
ℹ️ 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".
| if _rocm_torch(): | ||
| # ROCm's capability is the gfx target (gfx942 (9, 4)), which does not order; match the primary's load dtype | ||
| # instead (arch-pinned attention kernels are gated per card in diffusion_layer_split_plan). | ||
| others = tuple(o for o in others if _rocm_dtype_parity(int(primary), o)) | ||
| return ( | ||
| MultiGpuSelection(primary = int(primary), secondaries = others, automatic = automatic) | ||
| if others | ||
| else None |
There was a problem hiding this comment.
Gate mixed-gfx cards before moving a second denoiser
When a gfx1100 primary is paired with a gfx942 secondary, this new ROCm branch admits the secondary because both support bf16, whereas the previous capability ordering rejected it. That selection is also consumed by refine_plan_for_multigpu, whose component-assignment path can move transformer_2 wholesale before the new _backend_cards checks run. For a two-expert pipeline using the primary's flash or aiter backend, the second expert can therefore execute an architecture-pinned kernel on an unverified gfx target and fail during generation. Apply the per-gfx backend filtering to secondary denoisers in the component split as well, or limit this relaxed admission to the layer-split path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed: the component assignment could move transformer_2 to a mixed-gfx secondary before the layer split per-card gate ever ran. The second denoiser now moves only to a card that passes the same gate (flash / AITER on the primary exact gfx target with the flash probe, native SDPA not math-only beside a fused primary); otherwise both denoisers stay on the primary and only the companions move (688c899). CUDA is unchanged, and a test covers flash, AITER and the math-only case.
| "gfx1103", | ||
| "gfx1153", |
There was a problem hiding this comment.
Handle gfx1153 when architecture properties are absent
Adding gfx1153 to this arch-only set still misclassifies Krackan Point 2 on the AMD SDK/Radeon-wheel case where the architecture attributes and integrated flag are both unavailable: _rocm_classify_unified_memory then falls back to the device name, but its marker list does not include the 820M name used by this change's own gfx1153 test data. The classification therefore returns ("", False), this set cannot match it, and the APU remains eligible for denoiser blocks even though it shares system memory. Extend the name fallback for gfx1153 as well as the architecture set.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed: with no arch attributes and no integrated flag the name fallback only knew the Strix and Krackan Point names, so a Radeon 820M (Krackan Point 2, gfx1153) read as discrete. It now also matches 820M, 8040S, 780M / 760M / 740M, 680M / 660M and 610M as whole tokens, all integrated-only parts, so discrete mobile names such as RX 7600M or 6800M stay discrete (52649e6). Tests cover the new names and those negatives.
make_selection admits mixed gfx targets on ROCm, and the component assignment could move transformer_2 wholesale to a card the primary's flash_attn / AITER kernel was never verified on, or whose native SDPA is math-only. Apply the layer split's per-card attention gate before offering the second denoiser to the secondary.
With every arch attribute and is_integrated unset (AMD SDK / Radeon wheels), the classifier only knew the Strix and Krackan Point names, so a Radeon 820M Krackan Point 2 and the Phoenix, Rembrandt and Raphael iGPUs read as discrete. Match their integrated-only names as whole tokens so discrete mobile parts such as the RX 7600M stay discrete. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…ntion dtype (unslothai#14655) ## Studio ROCm layer split: probe secondary cards without changing the primary's SDPA switches Follow-up to unslothai#14479. Two issues in how the ROCm layer split checks a candidate secondary card's attention kernels. ### 1. A secondary card's probe turned off fused SDPA for the whole process `_rocm_drop_math_only` checks each candidate card with `diffusion_attention.sdpa_math_only`. The first time a card's ROCm probe finishes, `_probe_rocm_sdpa_kernels` passes its answer to `_apply_rocm_guard`, which flips `torch.backends.cuda.enable_flash_sdp` / `enable_mem_efficient_sdp`. Those switches apply to the whole process, so they also control the primary card the model is already running on. On a host where cuda:0 has fused kernels and cuda:1 is math-only (only the ROCm build check and the child-process answers are faked; the probe, guard and torch switches are real): | | flash | mem_efficient | secondary kept | |---|---|---|---| | before | True | True | | | first `_rocm_drop_math_only` | False | False | no | | identical second call | False | False | yes | The card we reject still moves the primary onto math attention for the rest of the session. The plan was priced assuming the primary's fused kernel. Then, because of the switches the first call flipped, the next identical preview sees the primary as math-only and keeps the card it just rejected. Fix: `diffusion_attention.read_only_sdpa_probe()` (a context variable) stops a completed probe from applying the guard. The planner wraps only the secondary checks in it. The primary check still applies its own answer, as before. The secondary's answer is still cached, so `guard_rocm_fused_sdpa` applies it if a model is later loaded onto that card. Because the switches are process-wide, the cards that are kept and run blocks also need a guard that holds for all of them: - A secondary that shares no fused kernel with the primary and the cards kept before it is dropped like a math-only one. For example, if cuda:1 runs only flash and cuda:2 runs only mem_efficient, keeping both would leave math as the only common kernel under a plan priced for fused attention. - Once the kept cards are known, `guard_rocm_fused_sdpa_cards` applies the guard with the kernels that ran on every one of them (primary plus kept secondaries). ### 2. The probe dtype came from the first registered parameter `_rocm_attention_dtype` used `next(module.parameters()).dtype`. MiniMax-H3 keeps `proj_in` and the other `_keep_in_fp32_modules` in fp32 while its blocks and their attention run in bf16. So the probe ran at fp32 instead of the dtype attention actually runs at. Kernel availability depends on the dtype: on gfx1151, SDPA flash and the flash_attn package run at bf16 but not at fp32 (mem_efficient runs at both, see the results below). A probe at the wrong dtype can therefore reach the wrong keep / drop decision. The flash branch of `_rocm_backend_cards` had the same problem: at fp32 the flash_attn check fails, so a flash-backed MiniMax-H3 split dropped secondaries that run flash at bf16. Fix: `_attention_dtype` reads the compute dtype of the first attention projection (`to_q`, or the fused `to_qkv` that resident INT8 H3 leaves). It uses, in order, a diffusers layerwise-casting hook's compute dtype, the projection's `compute_dtype` (GGUF / packed linears), then its weight or bias dtype. If no projection has a readable fp16 / bf16 / fp32 dtype, it uses the first parameter outside `_keep_in_fp32_modules` (diffusers' own `ModelMixin.dtype` rule), then the pipeline dtype, then the first parameter as before. The flash branch of `_rocm_backend_cards` uses the same dtype. ### Tests - `test_rocm_secondary_probe_leaves_the_primary_sdpa_switches_alone`: runs the real probe and guard against the real torch switches, with only the ROCm build check and the child answers faked. Fails on main (`(False, False) != (True, True)`), passes here. - `test_rocm_kept_secondaries_share_a_fused_sdpa_kernel`: a flash-only and a mem_efficient-only secondary are not both kept. Fails on main, passes here. - `test_rocm_kept_secondary_still_gets_the_fused_sdpa_guard` (guard, passes on main too): a kept secondary whose flash launch failed still turns flash off once it runs blocks. - `test_rocm_probe_runs_at_the_attention_dtype_not_an_fp32_stem`: uses a module with an fp32 stem registered before bf16 attention. Fails on main (`float32 != bfloat16`), passes here. - `test_rocm_probe_dtype_packed_projection_with_fp32_norm_synthetic` and `test_rocm_probe_dtype_skips_fp32_pinned_modules_when_projections_carry_no_dtype`: packed bias-free projections beside an fp32 `norm_q`, with and without a `compute_dtype`. Fail on main, pass here. `test_rocm_probe_reads_a_quantized_projection_not_its_fp32_qk_norm` covers the same with a real Krea-2 denoiser. - `test_studio_regional_fullgraph_compile_keeps_the_pdd_hook_eager` (guard, passes on main too): Studio compiles DiTs only through `compile_repeated_blocks` or per-block forwards below offload hooks. This test checks that `fullgraph=True` on a PDD MiniMax-H3 denoiser leaves the root `__call__`, and with it the PDD pre-hook, eager. Every step's sigmas still reach the banks. On the latest main merged in: `test_diffusion_layer_split_plan.py` and `test_video_minimax_h3_pdd.py` give 5 failed / 88 passed on main and 93 passed here. The related attention, ROCm fused and multi-GPU suites give the same 560 passed on both sides, plus one failure on both sides that comes from the test environment. ### Results on hardware AMD Strix Halo (gfx1151), torch 2.11.0+rocm7.13.0, main vs this branch in the same job. The probe, the guard and the torch switches are real. This host has one GPU, so cuda:1 is either the same card renumbered or, for the mixed case, a child answer replaced with math-only. Real SDPA kernels on gfx1151: fp16 and bf16 run math, flash and mem_efficient; fp32 runs math and mem_efficient. The flash_attn package runs at bf16 but not at fp32. | | main | this PR | |---|---|---| | flash / mem_efficient after rejecting a math-only cuda:1 | False / False | True / True | | cards kept, first / second identical check | none / cuda:1 | none / none | | SDPA op the primary then runs at bf16 | math | flash | | same-arch pair (all real): switches, cards kept | unchanged, cuda:1 | unchanged, cuda:1 | | tiny MiniMax-H3 (fp32 proj_in, bf16 attention): probe dtype | float32 | bfloat16 | | same model, flash branch: dtype / flash_attn runs there | float32 / no | bfloat16 / yes | Studio regression run on this head (`aacd677133`) vs main (`ed3eec632c`): no functional regression across auth, settings, crawl, inference settings, image and video generation, the tiny diffusion bench suites (HTTP, in-process, matrix), the upstream CI suites and the diffusion import check. The two settings screenshots that differ show only the per-run state directory path in the AGENTS.md hint; no frontend file changes here. Not covered: no real mixed ROCm pair and no Windows ROCm host were available, so the effect on speed and activation memory of the old global flip was not measured on hardware.
Summary
Enables the diffusion multi-GPU layer split (#14412) on ROCm. Until now the planner declined every ROCm host with
ROCm (layer split is CUDA only). With this change a denoiser that does not fit one card is cut across discrete ROCm GPUs, on Linux and on Windows, the same way it already is on CUDA. Integrated GPUs and APUs (unified memory) decline with a clear reason, and every ROCm-specific gate is scoped to ROCm only, so nothing changes on CUDA.UNSLOTH_DIFFUSION_LAYER_SPLIT=0still turns the split off (as doesUNSLOTH_DIFFUSION_MULTIGPU=0).What changed
studio/backend/core/inference/diffusion_layer_split_plan.py_rocm_discrete_secondariesruns Studio's existing APU classifier on each card (utils.hardware._rocm_props_unified_status: the driver's integrated flag, then the gfx target). A unified primary declines withunified memory: cuda:N is an integrated GPU (APU) sharing system memory, so cutting the denoiser across cards frees no memory. A unified secondary is dropped, and if that leaves no secondary the split declines with the same reason.note_unified_primary: whenrefine_plan_for_multigpustops on a unified-memory plan, ROCm now records the unified reason instatus()["multi_gpu"]["layer_split"], so a two-APU selection is no longer silent. On CUDA this stays a silent no-op, same as before.flash(flash_attn, Studio: pick ROCm flash attention by a whole-block timing probe instead of a model table #13792) andaiteronly keep secondaries with the primary's exact gfx target.flashmust also pass its own probe on each secondary (_rocm_flash_attn_runsfor that card and the denoiser's dtype), the same probe the primary passes at load. The CUDA branch (_PRIMARY_ARCH_BACKENDScompared by compute capability) is unchanged._is_rocmnow also detects AMD SDK / Radeon wheels, which leavetorch.version.hipunset.studio/backend/core/inference/diffusion_multigpu.pymake_selectionon ROCm no longer orders gfx targets as if they were CUDA capabilities. Before, a 7900 XTX(11, 0)primary dropped an MI300(9, 4)secondary. Now a secondary is kept when it can run the primary's load dtype: if the primary has native bf16, a card that only emulates bf16 (RDNA1/2, GCN, Vega perrocm_bf16) is dropped. The CUDA path is untouched.refine_plan_for_multigpucallsnote_unified_primaryon its existing unified-memory early return. It still returns the plan unchanged.Review follow-ups
utils/hardware: the APU arch set now names every target LLVM's AMDGPUUsage table types as APU (gfx1153 Krackan Point 2, gfx1033, gfx1035, gfx1036, gfx1013, gfx90c, gfx902, gfx909, gfx801, gfx810), so an APU whose runtime leavesis_integratedunset no longer reads as discrete. The device-name fallback (no arch attributes, no flag) also matches 820M, 8040S, 780M / 760M / 740M, 680M / 660M and 610M as whole tokens, so discrete mobile names such as RX 7600M stay discrete.transformer_2) moves to a ROCm secondary only if that card passes the same per-card backend gate; otherwise both denoisers stay on the primary and only the text encoder / VAE move.Already ROCm-safe on main, audited and left alone:
backend != "cuda"is off on ROCm._split_cardsandresident_cuda_cardsreturn nothing on HIP.tensor.to(device). There are no custom streams or events in the split path.trusted_mem_get_info(ordinal)already caps the free figure on Windows ROCm, per card.Tests
tests/test_diffusion_layer_split_plan.py: 8 new tests. They run Studio's real classifiers (_rocm_props_unified_status,rocm_bf16) over faked device properties:test_rocm_discrete_splits_and_explicit_modes_and_legacy_cpu_offload_keep_the_plan._rocm_torch, so the CUDA-shaped tests behave the same on a ROCm host.AMD CI evidence (gfx1151, Linux, device multiplier)
One batched GPU job on the Strix Halo runner, torch ROCm (HIP 7.13),
--spoof-devices 2(3 HIP devices, 2 of them phantom), base = maina74da3f321, head = main + this change, same runner and seeds.ROCm (layer split is CUDA only)unified memory: cuda:0 is an integrated GPU (APU) ...compile_repeated_blocks(fullgraph=True)), tiny FLUX.1 / Qwen-Image / Wan, prompts 2-4invalid device ordinal(101) for the phantom ordinal while precompiling. The multiplier only wraps the HIP calls torch makes, so this is the spoof, not the split; it needs a real second cardUNSLOTH_DIFFUSION_LAYER_SPLIT=0The one head failure,
test_execution_device_reports_the_primary_of_a_split_pipeline, is the runner's diffusers failing to import in the test process (Failed to import diffusers.pipelines.pipeline_utils), and the skip is the same import. Both pass on the CPU sweep above.What the spoof cannot prove
The device multiplier shows extra HIP ordinals that are the same physical gfx1151. So this run proves wiring: selection, planning, placement, the boundary hand-offs, bit identity, and compile with zero recompiles. It does not prove:
gfx1151 is an APU, so reaching the discrete branch at all required a Studio-layer spoof in the harness: the memory kind reported as discrete, and the per-card unified classifier answering discrete. The unmodified host takes the unified decline, which is the intended behaviour on this hardware.
For a tester with two or more discrete AMD GPUs