Repository navigation
Studio: compute ROCm VAE half-math SDPA per call instead of flipping the global flag - #17350
Merged
Merged
Conversation
…the global flag The fp16/bf16 reduction flag for math SDPA is process-global, so scoping it around a VAE attention call leaked to other threads and could stay enabled after two overlapping calls. Run the same math SDPA ops in the input dtype directly; outputs are bit-identical to SDPA with the flag on.
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. |
[skip ci]
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
1 of 5 tasks
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
The ROCm VAE attention half-reduction math from #17252 now runs per call and no longer touches the process-wide
torch.backends.cuda.allow_fp16_bf16_reduction_math_sdpflag. On gfx1151 the speed-up stays the same and outputs are bit-identical to before (results below).UNSLOTH_VAE_ATTN_HALF_MATH=0still keeps the fp32 upcast.Root cause
#17252 added
half_reduction_math(). This helper saved the flag, set it toTruearound the VAE SDPA call, then restored the saved value. The flag is stored inat::globalContext(), so every thread in the process shares it:False), B enters (savesTrue), A exits (setsFalsewhile B is still inside), B exits (setsTrue). The flag then staysTrueafter both calls have finished.Fix
half_math_sdpa(q, k, v, scale)runs the same ATen ops as PyTorch's math SDPA with the flag on, in the same order and in the input dtype: scale q and k by sqrt(scale), bmm,_safe_softmax, bmm. On torch < 2.5, where_safe_softmaxdoes not exist, it uses plain softmax.UNSLOTH_VAE_ATTN_HALF_MATHkill switch. The fp32 path and the chunking budget are also unchanged.Equivalence on CPU (torch 2.12.1, bf16, 1x1x1024x512, the FLUX VAE head dim):
F.scaled_dot_product_attentionunder the math backend with the flag on.bmmcalls and the same_safe_softmax, with the same shapes, dtypes and strides. The only difference is that the scaling multiply records asmul.Tensorinstead ofmul.Scalar, which is the same kernel with a wrapped scalar. GEMM work and intermediate allocations therefore match the merged version.Flip test
studio/backend/tests/test_diffusion_vae_attn_chunked.py::test_half_math_leaves_other_threads_and_the_flag_aloneThe test runs two VAE attention calls in two threads. A per-thread dispatch mode parks each thread inside its half-math call. While both are parked, the test checks that the global flag still has its original value and that an unrelated math SDPA call in the main thread is bit-identical to its reference. It then releases the threads first in, first out and checks the flag and the unrelated call again.
assert True == False(where True = _HALF_FLAG())Other new tests:
test_half_math_sdpa_matches_the_flag[None|0.3|-0.3]: bit-identical to flag-on math SDPA, including a negative scale.test_half_math_runs_in_the_autocast_dtype: under bf16 autocast with fp32 k / v, the result is bf16 and equal to the direct half path.test_half_math_keeps_dtype_scoped_and_closestill passes. It checks that the unchunked output is bit-equal to flag-on SDPA, differs from the upcast path, and is close to the fp32 reference.Tests run
test_diffusion_vae_attn_chunked.py: 49 passed.test_diffusion_vae_attn_chunked.py,test_diffusion_rocm_tag_detect.py,test_diffusion_speed.py,test_diffusion_device.py,test_video_vae_bf16_decode_rocm.pyandtest_diffusion_attention.py, run with-o addopts= -m "not server and not native_mxc": 502 passed, 1 skipped.gfx1151 results (Strix Halo, Linux, torch 2.11.0+rocm7.13.0, exclusive GPU)
Both arms ran in the same job on the same machine. Base is current main (with #17252), head is main plus this change. Each arm is the median of 7 interleaved reps after 2 warm-up reps. The half path engaged in every unit, the kill switch did not, and no fused SDPA kernel takes head dim 512.
Not proven