Skip to content

Studio VAE attention: gate ROCm half math on the autocast dtype that actually runs - #18573

Merged
danielhanchen merged 1 commit into
mainfrom
fix-recheck-vae-autocast-dtype
Nov 20, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix-recheck-vae-autocast-dtype

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#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.

…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.
@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-20T05:40:16.141951Z b250952 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

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. Keep it up!

Reviewed commit: b250952a90

ℹ️ 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 3f8e149 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the fix-recheck-vae-autocast-dtype branch November 20, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant