Fix fp32 dtype leak from QK-norm in Flux2 attention processors - #3002
Merged
Conversation
torch.nn.RMSNorm is on autocast's fp32 promotion list, so under autocast(bf16) or autocast(fp16) the QK-norm layers in Flux2Attention return fp32 tensors while the value tensor remains in half precision. apply_rotary_emb preserves the wider dtype, so query/key reach the attention backend as fp32 alongside a half-precision value. Backends that require a uniform half dtype reject this outright -- flash-attn raises on mismatched input dtypes rather than casting. Cast the normalised query/key back to the dtype of the corresponding value tensor. Using value.dtype rather than a hardcoded bfloat16 keeps the fix correct under fp16 autocast and in full-precision runs, where the cast becomes a no-op. Applies to both Flux2AttnProcessor (including the added_kv_proj_dim encoder path) and Flux2ParallelSelfAttnProcessor.
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.
Problem
torch.nn.RMSNormis on autocast's fp32 promotion list. Underautocast(bfloat16)(or fp16), the QK-norm layers inFlux2Attentiontherefore return fp32 tensors, while the
valuetensor stays in halfprecision:
apply_rotary_embpreserves the wider dtype, soquery/keyarrive at theattention backend as fp32 alongside a half-precision
value.Attention backends that require a uniform half dtype reject this outright.
flash-attn raises on mismatched input dtypes rather than casting, so training
Flux.2 with
mixed_precision=bf16and a flash-attn backend fails inside theattention call.
This affects both attention processors:
Flux2AttnProcessor, including theadded_kv_proj_dimencoder branch(
norm_added_q/norm_added_k)Flux2ParallelSelfAttnProcessorFix
Cast the normalised
query/keyback to the dtype of the correspondingvaluetensor, immediately after the norm.Using
value.dtyperather than a hardcodedbfloat16:encoder_value.dtype.Placing the cast directly after the norm (rather than after
apply_rotary_emb) also covers themaybe_metal_flash_rope_attentionpath,which applies rotary embeddings internally.
Reproduction
Self-contained: builds a
Flux2Attentiondirectly and calls it underautocast(bfloat16). No checkpoint, no dataset, ~30 lines.On
main(37d6fb2), A100-SXM4-80GB, torch 2.13.0+cu126:With this patch applied, the same script returns
[(1, 256, 256), (1, 64, 256)].Verified alongside it:
value.dtypeis bit-identical to casting explicitly to bf16(
torch.equal→ True), so the fix changes no numericsautocast(float16), which a hardcodedbfloat16 cast would have broken