Skip to content

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

Draft
gphuang wants to merge 2 commits 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 2 commits 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a dtype mismatch at the Flux attention kernel boundary where Q/K could become fp32 (via for_qk RMSNorm and/or RoPE) while V remains bf16, which can crash AITER FlashAttention due to its requirement that Q, K, and V share the same dtype.

Changes:

  • Cast query and key to value.dtype immediately after RoPE and right before the core attention call in JointSelfAttention.forward.
  • Apply the same boundary cast in FluxSingleAttention.forward to cover the single-stream path as well.

@gphuang
gphuang requested a review from amital-amd July 24, 2026 13:22
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.

2 participants