Repository navigation
Studio VAE attention: gate ROCm half math on the autocast dtype that actually runs - #18573
Merged
Merged
Conversation
…actually runs A bf16 VAE query under fp16 autocast passed the bf16-only half math gate, was then cast to fp16 and ran fp16 scores, which overflow at real VAE magnitudes and turn the decode NaN. Cast to the autocast dtype first and gate on that, so the call takes the fp32 upcast; an fp16 query under bf16 autocast now keeps bf16 half math.
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. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
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.
#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.dtypebefore 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_runsuses 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_mathfails on main because half math is skipped, and passes here.tests/test_diffusion_vae_attn_chunked.py,tests/test_diffusion_windows_rocm_wide_head.pyandtests/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.