Skip to content

Studio ROCm: half-math VAE attention for bf16 only; fp16 takes the fp32 upcast (#18016) - #18077

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
bezabyte:codex/issue-18016-rocm-vae-wide-head
Nov 17, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
bezabyte:codex/issue-18016-rocm-vae-wide-head

Conversation

@bezabyte

@bezabyte bezabyte commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #18016. ROCm cards without native bf16 (gfx103x and older) run the VAE in fp16. The Z-Image / FLUX.1 VAE's decoder mid-block attention (head dim 512) produces scores of 1e6 to 5e7 on real latents, past fp16's 65504. The half-math SDPA stored every score as inf, and softmax returned NaN for every row.

  • Half-math VAE attention now runs only for bf16, which has fp32's exponent range. fp16 takes the fp32-upcast math SDPA, the same path as UNSLOTH_VAE_ATTN_HALF_MATH=0.
  • The first version of this PR routed every Linux ROCm head wider than 256 to fp32. That also dropped half math for bf16 VAEs on gfx11 / gfx1151, adding about 26% to VAE decode time, so the dtype gate replaces it.
  • Windows ROCm head-width handling (Windows ROCm: run attention heads wider than 256 on SDPA math (Gemma 4 NaN) #17976) is unchanged.

Scores per VAE (real latents, decoder attention)

VAE head dim score range fp16 half math
Z-Image / FLUX.1 512 1e6 to 5e7 all NaN
FLUX.2 512 -18 to 16 finite
SDXL fp16-fix 512 -36 to 125 finite
Qwen-Image 384 -20 to 29 finite
Qwen-Image 2.1 1152 -4 to 12 finite

Verification

CPU tests in test_diffusion_vae_attn_chunked.py:

  • An fp16 call with FLUX.1-scale scores at the real 512 px shape [1, 1, 4096, 512] is finite and equal to the kill-switch output. It fails before the fix, and first checks that the half-math call alone returns all NaN on these inputs.
  • bf16 at the same scores keeps half math. This passes before the fix and fails on the first version of this PR.

AMD CI, gfx1151, real Z-Image VAE:

  • Linux, fp16: before, every output pixel is NaN at 512 and 1024 px. After, the output is finite and bitwise equal to UNSLOTH_VAE_ATTN_HALF_MATH=0 (LPIPS 4e-6 against fp32).
  • Linux, bf16 decode at 1024 px: 819 ms before, 814 ms after, with identical output and within the 3% noise. The fp32 upcast takes 1032 ms.
  • Windows: the same fp16 fix, and the same bf16 decode speed and output.

NVIDIA T4: this path is ROCm-only and is not installed there; the output is identical before and after.

Not covered

  • No gfx103x card was available. The NaN was reproduced on a gfx1151 forced to fp16, and in a CPU emulation.
  • fp16 VAEs whose scores do fit (SDXL, FLUX.2, Qwen-Image on gfx103x) go back to the fp32 upcast they used before half math was added. Their speed on gfx103x was not measured.

@danielhanchen danielhanchen self-assigned this Nov 17, 2026
The FLUX.1 / Z-Image VAE's decoder mid-block attention (head dim 512) produces
scores between 1e6 and 5e7 on real latents. ROCm cards without native bf16
(gfx103x and older) run the VAE in fp16, where half math stored those scores
in fp16: every one overflowed to inf (fp16 max 65504) and softmax returned NaN
for every row, so the final decode failed with "decoded image contains NaN".

bf16 has fp32's exponent range and computes the same scores finite, so the
half-math speedup measured on gfx1151 (bf16) stays. fp16 now runs the fp32
upcast math SDPA, the same path as UNSLOTH_VAE_ATTN_HALF_MATH=0.

This replaces the Linux ROCm wide-head fp32 route, which also dropped half
math for bf16 VAEs on gfx11 / gfx1151.

Fixes unslothai#18016
@danielhanchen danielhanchen changed the title fix(studio): use fp32 VAE attention for wide Linux ROCm heads Studio ROCm: half-math VAE attention for bf16 only; fp16 takes the fp32 upcast (#18016) Nov 17, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T08:03:23.227750Z 3e28272 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

Copy link
Copy Markdown

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

Reviewed commit: 3e28272eb5

ℹ️ 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 added a commit that referenced this pull request Nov 17, 2026
…ne as benign (red since #18103) (#18114)

`tests/studio/test_backend_ci_parallel_isolation.py::test_every_tight_elapsed_bound_is_isolated`
fails on main (workflow-trigger lint, "Workflow guard suites"), seen on
the staging runs of #18077 and #18106:

```
E   assert not {'test_anthropic_admission.py': ['test_anthropic_admission.py:239 one duration against another']}
```

## Cause

#18103 replaced the fixed 0.1 s sleeps in `test_anthropic_admission.py`
with `_until_queued`, which polls the admission queue with `assert
loop.time() < deadline` as a 5 s timeout. The lint flags any clock
comparison in a backend test that runs under `-n 4` until a human
records it. This one is a poll deadline: descheduling only delays the
poll and cannot make the condition false, which is exactly the case
`BENIGN_TIMING` is for. #18103 should have recorded it; this does.

## Fix

Add `("test_anthropic_admission.py", "_until_queued")` to
`BENIGN_TIMING` with a one-line reason, next to the other poll
deadlines.

## Verification

- main: `test_every_tight_elapsed_bound_is_isolated` fails with the CI
message.
- this branch: `tests/studio/test_backend_ci_parallel_isolation.py` 43
passed.
@danielhanchen
danielhanchen merged commit 891ce13 into unslothai:main Nov 17, 2026
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…actually runs (#18573)

#18077 keeps ROCm VAE attention in bf16 half math but sends fp16 through
the fp32 upcast, because fp16 scores overflow at real VAE magnitudes.
The dtype gate reads the caller's `q.dtype` before autocast casts it. A
bf16 query under fp16 autocast passes the bf16-only gate. The mode then
casts Q/K/V to fp16 and computes the scores in fp16 with autocast off.
On FLUX-scale VAE inputs every score overflows and the output is all
NaN. In the other direction, an fp16 query under bf16 autocast missed
bf16 half math.

## Change

The mode casts to the active autocast dtype once, before the half math
gate, so the gate sees the dtype that actually runs. A bf16 query under
fp16 autocast now takes the fp32 upcast and stays finite. An fp16 query
under bf16 autocast takes bf16 half math. Without autocast nothing
changes. The Windows wide-head branch reuses the same cast instead of
repeating it. The stock chunked path now gets pre-cast tensors, which is
what autocast would hand the kernel anyway.

## Tests

`test_bf16_vae_under_fp16_autocast_gates_on_the_fp16_it_runs` uses
FLUX.1 VAE-scale bf16 inputs `[1,1,4096,512]` under fp16 autocast, with
the real dtype / ROCm / env gate and CPU tensors. On main it takes half
math, and a direct run of the same case gives 2,097,152 NaNs out of
2,097,152 outputs. Here it takes the upcast and every output is finite.
`test_fp16_vae_under_bf16_autocast_keeps_half_math` fails on main
because half math is skipped, and passes here.
`tests/test_diffusion_vae_attn_chunked.py`,
`tests/test_diffusion_windows_rocm_wide_head.py` and
`tests/test_diffusion_vae_full_map.py`: main has 2 failures, both of
them the new tests, with 89 passed. This branch: 91 passed.

## Not proven

The tests run on CPU with ROCm eligibility simulated. This PR was not
run on real ROCm hardware. That check would be the cross-dtype autocast
VAE decode on gfx103x and gfx1151 (`python -m pytest -o addopts="" -rs
studio/backend/tests/test_diffusion_vae_attn_chunked.py`, then a 512 px
and 1024 px decode under each autocast direction, checking for finite
output and comparing against fp64). It is also not known how often
shipped pipelines call the VAE with a dtype that differs from the
autocast dtype.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants