Repository navigation
Studio ROCm: half-math VAE attention for bf16 only; fp16 takes the fp32 upcast (#18016) - #18077
Merged
danielhanchen merged 4 commits intoNov 17, 2026
Conversation
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
Member
|
@codex review |
1 of 5 tasks
Codex Review Summary: ✅ CompletedThis 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: Didn't find any major issues. Swish! 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". |
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
UNSLOTH_VAE_ATTN_HALF_MATH=0.Scores per VAE (real latents, decoder attention)
Verification
CPU tests in
test_diffusion_vae_attn_chunked.py:AMD CI, gfx1151, real Z-Image VAE:
UNSLOTH_VAE_ATTN_HALF_MATH=0(LPIPS 4e-6 against fp32).NVIDIA T4: this path is ROCm-only and is not installed there; the output is identical before and after.
Not covered