Skip to content

Studio Windows ROCm: decode compile only off for wide-head VAEs, fp64 kept, nested-safe SDPA guard - #18582

Merged
danielhanchen merged 3 commits into
mainfrom
fix-recheck-windows-rocm-sdpa
Nov 20, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-recheck-windows-rocm-sdpa

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Three fixes to the Windows ROCm attention routing added in #17953 and #17960, found in a recheck of the kernel PRs. Each is its own commit.

1. VAE decode compile is dropped only for wide-head VAEs

#17953 keeps the Windows ROCm VAE decode eager so a compiled decode cannot skip the attention routing that keeps head dim 512 off the fused SDPA kernels. But _vae_decode_compile_allowed returned False on the platform alone, before looking at the VAE or the UNSLOTH_DIFFUSION_COMPILE_VAE setting. A VAE with no attention head wider than 256 (attention-free video VAEs, or a narrow AutoencoderKL) lost decode compilation too, even when the env asked for it.

  • diffusion_vae_attn_chunked.py: new vae_has_untrusted_head(vae). It walks the same attention modules the routing patches and reads each head dim (inner_dim // heads on diffusers Attention). Any head wider than 256, any attention module whose head dim it cannot read (the single-head blocks of Wan, Qwen-Image and HunyuanImage), and any VAE it cannot inspect count as wide.
  • diffusion_speed.py: on Windows ROCm _vae_decode_compile_allowed returns False only when vae_has_untrusted_head is True. The check still runs before the U-Net shortcut and the env override, so a wide-head VAE never compiles past the routing there.

Studio's own AutoencoderKL and AutoencoderKLFlux2 VAEs have a single-head mid-block at 512, so they stay eager on Windows ROCm exactly as before.

2. fp64 stays fp64 in the wide-head math path

fp32_math_sdpa ran .float() on Q, K and V and cast only the result back, so an fp64 call was silently reduced to fp32. With fp64 [1, 1, 3, 512] operands filled with 1e20, stock SDPA returns finite output while the routed path overflowed and returned NaN. It now upcasts only fp16 and bf16; fp32 and fp64 run in their own precision. Half inputs keep the existing fp32 upcast, so UNSLOTH_VAE_ATTN_HALF_MATH=0 is unchanged. Studio loads VAEs in fp16, bf16 or fp32, so this only matters to code handing a double VAE to the routed attention.

3. The SDPA guard checks the device before the query shape

The Windows ROCm SDPA wrapper's _wide_cuda_head read query.shape before checking the device. A strided NestedTensor raises NestedTensorImpl doesn't support sizes on .shape, so a valid CPU nested SDPA call failed inside the wrapper. It now returns False for any non-CUDA query without touching the shape, judges a nested CUDA query by size(-1), and sends the call to the stock dispatch if even that raises. Dense CUDA queries are judged as before.

Tests

CPU only, Windows ROCm faked:

  • tests/test_diffusion_windows_rocm_wide_head.py:
    • test_windows_rocm_keeps_decode_compile_unless_the_vae_has_a_wide_head, with tiny real diffusers AutoencoderKL VAEs: the head-64 and no-attention cases fail on main and pass here. The two head-512 cases (with and without the env forcing compile) are still refused, passing on both.
    • test_windows_rocm_fp64_wide_head_keeps_fp64_precision[1073741824|16] (unchunked and chunked): fail on main (NaN), pass here.
    • The existing test_vae_decode_compile_stays_eager_on_windows_rocm (an uninspectable vae=object() stays eager) passes on both.
  • tests/test_rocm_windows_wide_head_sdpa.py: test_a_nested_cpu_query_reaches_the_stock_dispatch and test_a_nested_cuda_query_is_judged_by_its_head[128-False|512-True] fail on main and pass here.

Base vs head over both files: 7 tests fixed, 31 unchanged, 0 failing on this branch. Wider suite (test_diffusion_vae_attn_chunked.py, test_diffusion_speed.py, test_diffusion_vae_fused.py, test_diffusion_vae_regional_compile.py): no failures on main or here apart from the new tests on main.

Windows gfx1151 (AMD CI)

Real Windows ROCm (torch 2.11.0+rocm7.14.0, gfx1151), base = main, head = this branch, two tiny AutoencoderKL VAEs (head dim 64 and 512) on the real GPU:

state narrow VAE: U-Net / env forced wide VAE: U-Net / env forced compiled narrow decode finite rel L2 to eager
base refused / refused refused / refused yes 8.7e-7
head allowed / allowed refused / refused yes 8.7e-7

CONFIRMED: main refuses decode compile for the narrow VAE, this branch allows it, the compiled decode matches eager, and the head-512 VAE is still refused.

Not proven here

  • The speed gain for an unaffected VAE was not measured.
  • fp64 and nested CUDA tensors on a real Windows card were not run.

…de-head VAEs

The platform check returned False for every Windows ROCm VAE before looking at it, so a VAE with no attention head wider than 256 also lost decode compile. The gate now inspects the VAE's attention modules (an uninspectable VAE or module still counts as wide), keeping the guarantee that a wide-head VAE never compiles past the routing, env override included.
fp32_math_sdpa upcast every input with .float(), so an fp64 call ran fp32 scores and overflowed where stock SDPA stays finite. Only fp16 / bf16 upcast now; fp32 and fp64 run in their own precision.
The guard read query.shape first, which raises on a strided NestedTensor, so a CPU nested SDPA call failed in a Windows ROCm process instead of dispatching. The device is checked first and a nested CUDA query is judged by size(-1).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T07:01:09.274162Z f9f35f9 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. Keep them coming!

Reviewed commit: f9f35f96ee

ℹ️ 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 9604898 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the fix-recheck-windows-rocm-sdpa branch November 20, 2026 08:59
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