Skip to content

fix(flux): cast Q/K to value dtype before core attention#914

Draft
gphuang wants to merge 1 commit into
AMD-AGI:mainfrom
gphuang:fix/flux/qk-attention-dtype-agi
Draft

fix(flux): cast Q/K to value dtype before core attention#914
gphuang wants to merge 1 commit into
AMD-AGI:mainfrom
gphuang:fix/flux/qk-attention-dtype-agi

Conversation

@gphuang

@gphuang gphuang commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

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 raises FlashAttention only support fp16, bf16 and fp8_e4m3, so the Flux 12B local-spec / MXFP4 recipes cannot reach iteration 1 on rocm/primus:v26.4.

Fix: cast query/key to value.dtype at the attention-kernel boundary in both JointSelfAttention.forward and FluxSingleAttention.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.dtype in both forwards.

Test plan

  • 2-step smoke of flux_12b_ddp_energon_schnell_resample_local_spec_mxfp4.yaml on 8 GPUs with rocm/primus:v26.4: exits rc=0 with finite loss and no NaN; the attention forward runs (previously crashed before iteration 1).
  • Maintainer review of the cast placement.

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.
@gphuang gphuang self-assigned this Jul 23, 2026
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