Skip to content

Studio ROCm layer split: probe secondary cards read-only, at the attention dtype - #14655

Merged
danielhanchen merged 9 commits into
mainfrom
recheck_sdpa_probe_pdd
Nov 9, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
recheck_sdpa_probe_pdd

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Studio ROCm layer split: probe secondary cards without changing the primary's SDPA switches

Follow-up to #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.

…n dtype

A candidate secondary's ROCm SDPA probe applied its answer through
_apply_rocm_guard, flipping the process-wide torch.backends.cuda flash /
mem_efficient switches. A rejected math-only card therefore moved the primary
onto math attention, and the next identical preview read the primary as
math-only and kept the card. Secondary checks now run inside
read_only_sdpa_probe(); the cached answer still guards a card once a model is
loaded onto it.

The probe dtype came from the denoiser's first parameter, which for MiniMax-H3
is the fp32 proj_in stem while attention runs in bf16. It is now read from the
first attention module (to_q / to_qkv), falling back to the old order.

Adds a guard test that Studio's regional fullgraph compile of a PDD H3
denoiser keeps the root pre-hook eager.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T15:54:00.985970Z aacd677 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ection

The attention dtype scan returned the first fp16 / bf16 / fp32 parameter of an
attention module. With bias-free quantized projections (GGUF, packed INT8) and
norm_q / norm_k pinned to fp32 (Krea-2, FLUX.3) that was the norm, so the
primary probed at fp32, looked math-only and every secondary was kept even
when it is math-only at the real bf16 compute dtype.

Only the Q (or fused QKV) projection is read now: a layerwise-casting compute
dtype, a quantized linear's compute_dtype, then its weight or bias. Without
one, the first parameter outside _keep_in_fp32_modules is used (diffusers'
ModelMixin.dtype rule), then the pipeline dtype, then the first parameter.

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: af9f8fe309

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

…ries

The read-only secondary probe also skipped the ROCm fused-SDPA guard for
secondaries the split keeps, so a kept card whose flash launch failed left
flash on process-wide. Once the split settles its cards, apply the guard
over the primary and every kept secondary: only kernels that ran on all of
them stay on. Dropped cards stay read-only.

Tests: a kept flash-failed secondary turns flash off; the Krea-2 tests
import diffusers in a session fixture, before the faked 8-card device
count, so the file passes when run alone.

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot 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

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

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

@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: 6e30beb87f

ℹ️ 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 +445 to +446
common = tuple(k for k in answers[0] if all(k in a for a in answers[1:]))
return _apply_rocm_guard(common, logger)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reject splits whose cards have no common fused SDPA backend

On a three-card ROCm selection where the primary supports both fused backends, one secondary supports only flash, and another supports only memory-efficient attention, _rocm_drop_math_only keeps both secondaries because neither is individually math-only. Their intersection here contains only math, so this new guard disables both fused backends process-wide without rejecting or repricing the split. All cards, including the primary, then materialize the quadratic attention matrix despite the plan's fused-attention memory budget, potentially causing generation OOMs. With cached probe answers, the parent commit leaves both switches enabled and each card can dispatch its supported backend; this change disables both. Filter the selection to retain a common fused backend, or decline the split before applying a math-only intersection.

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.

Good catch, fixed in 163b828. _rocm_drop_math_only now walks the secondaries in order and keeps a card only if the fused kernels it shares with the primary and the cards already kept are not empty. A card that would empty that set gets dropped through the same logged path as a math-only card, so the guard can never leave math as the only common kernel under a split priced for fused attention. A card whose probe did not answer adds no constraint. If the primary has no fused kernel, or the guard is turned off, the behaviour is unchanged. test_rocm_kept_secondaries_share_a_fused_sdpa_kernel covers the 3-card case: primary flash plus mem_efficient, cuda:1 flash only, cuda:2 mem_efficient only. cuda:2 is dropped, cuda:1 is kept, and flash stays enabled.

…ernel

The fused SDPA switches are process-wide, so the kept cards are now chosen
so their common fused set with the primary never empties. A secondary that
would leave math as the only shared kernel is dropped like a math-only one
instead of turning every fused backend off under a plan priced for fused
attention.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 163b828aef

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

gfx1151 runs mem_efficient SDPA at fp32 (torch 2.11 + ROCm 7.13), so the old
comment claiming fp32 never runs fused kernels was not accurate.
@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: aacd677133

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

dayanruben pushed a commit to dayanruben/unsloth that referenced this pull request Nov 10, 2026
…lit fits (unslothai#17347)

## Summary

On ROCm, the layer split's common SDPA kernel guard now runs only after
the split has been placed, and only on the cards the split actually
uses. If the split is declined on memory and the load falls back to the
one-card plan, the primary's process-wide flash / memory-efficient SDPA
switches stay as they were.

## Root cause

unslothai#14655 added a guard to `_rocm_drop_math_only` that keeps only the fused
SDPA kernels shared by the primary and the kept secondaries. It does
this by turning off the other kernels through the process-wide
`torch.backends.cuda` switches. The guard ran while
`refine_plan_for_layer_split` was still filtering candidate cards,
before `plan_layer_split` checked memory. When the split did not fit,
the function returned the original one-card plan, but the switches
stayed changed. A primary that runs flash SDPA then lost flash for its
single-card load because of a secondary that ended up with no work. With
the same cached kernel answers, the PR's parent left both switches on.

## Fix

- `_backend_cards`, `_rocm_backend_cards` and `_rocm_drop_math_only`
take a keyword `guard` (default `True`, so other callers such as
`_secondary_runs_denoiser` behave as before).
- `refine_plan_for_layer_split` filters with `guard = False`. Once
`plan_layer_split` returns a split, it applies the guard to
`split.cards`, under the same conditions as before: ROCm, native SDPA,
and a primary that is not math-only.
- If the split is declined, the switches are left alone.

## Flip test


`tests/test_diffusion_layer_split_plan.py::test_rocm_split_declined_on_memory_keeps_the_primarys_fused_sdpa`
uses the real probe, guard and torch switches (only ROCm detection and
the child probe answers are faked). The primary has flash and
memory-efficient SDPA, the secondary has only memory-efficient SDPA.

- With 4000 MiB cards the split is declined, and flash must stay on.
- With 20000 MiB cards the split is placed on cards (0, 1), and flash
must go off.

Results:

- origin/main: `At index 0 diff: False != True` (flash is turned off
although the split was declined)
- this branch: `1 passed`

## Tests run

- Standalone repro with the PR parent's modules and with origin/main:
parent `(flash, mem_efficient) = (True, True)`, main `(False, True)`,
both on the same declined one-card plan.
- `pytest -o addopts= -m "not server and not native_mxc"` over the layer
split, multi-GPU, diffusion attention and diffusion ROCm test files: 815
passed, 27 skipped. There are 3 failures, and the same 3 test IDs fail
on clean main:
`test_diffusion_attention_install.py::test_an_imported_old_kernels_is_not_papered_over`,
`test_diffusion_multigpu.py::test_execution_device_reports_the_primary_of_a_split_pipeline`
and
`test_diffusion_rocm_fused.py::test_teardown_survives_a_later_eager_layer`.
- `prepush_gate.py`: PASS.

## Not proven

- Not run on a mixed ROCm host. The switch state is proven with the real
torch switches on CPU, and the throughput effect is not measured.
- A split that is placed but later fails in `apply_layer_split_plan`
(falling back to `fallback_plan`) still keeps the guard applied. This
change does not touch that path.
- The companion-split path (`_secondary_runs_denoiser`) still guards
while it evaluates, unchanged here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant