fix(flux): cast Q/K to value dtype before core attention#914
Draft
gphuang wants to merge 1 commit into
Draft
Conversation
Q/K RMSNorm (for_qk) and RoPE can leave Q/K in fp32 while V stays bf16. AITER FlashAttention requires Q/K/V to share one dtype and otherwise raises "FlashAttention only support fp16, bf16 and fp8_e4m3". Align Q/K to value.dtype at the attention-kernel boundary in both JointSelfAttention and FluxSingleAttention forwards; no-op when dtypes already match.
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 Flux joint and single self-attention forwards can reach the core attention call with Q/K in fp32 while V is bf16: the Q/K RMSNorm (
for_qk) and RoPE promote Q/K to fp32, but V is left untouched. AITER FlashAttention requires Q, K and V to share one dtype and otherwise raisesFlashAttention only support fp16, bf16 and fp8_e4m3, so the Flux 12B local-spec / MXFP4 recipes cannot reach iteration 1 onrocm/primus:v26.4.Fix: cast
query/keytovalue.dtypeat the attention-kernel boundary in bothJointSelfAttention.forwardandFluxSingleAttention.forward— right after RoPE, before the core attention computation. This covers both the RMSNorm and RoPE promotion paths and is a no-op when the dtypes already match.Changed
primus/backends/megatron/core/models/diffusion/flux/attention.py— cast Q/K →value.dtypein both forwards.Test plan
flux_12b_ddp_energon_schnell_resample_local_spec_mxfp4.yamlon 8 GPUs withrocm/primus:v26.4: exitsrc=0with finite loss and no NaN; the attention forward runs (previously crashed before iteration 1).