metal-flash-attention: causal training ungated; fused RoPE + Attention for Flux1/Flux2, Z-Image and Krea - #2811
Merged
Conversation
…n for Flux1/Flux2, Z-Image and Krea
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.
This pull request introduces support for Metal Flash Attention with fused RoPE (Rotary Position Embedding) in the Flux and Flux2 model attention implementations. The main change is the integration of the
maybe_metal_flash_rope_attentionfunction, which enables the use of a fast, fused attention backend when possible, falling back to the standard rotary embedding logic otherwise. Documentation in multiple languages is updated to clarify the behavior and statistics of the fused RoPE path, especially distinguishing between no-grad and gradient calls.The most important changes are:
Model Code Integration:
maybe_metal_flash_rope_attentioninto the attention call paths ofFluxAttnProcessor3_0,FluxSingleAttnProcessor3_0, and bothflux/transformer.pyandflux2/transformer.py, allowing eligible calls to use Metal Flash Attention with fused RoPE for improved performance. If the fused backend is unavailable or ineligible, the code falls back to the standard RoPE application. (simpletuner/helpers/models/flux/attention.py[1] [2] [3] [4];simpletuner/helpers/models/flux/transformer.py[5] [6];simpletuner/helpers/models/flux2/transformer.py[7] [8] [9]Documentation Updates:
rope_instream, while gradient calls are counted underrope_autograd. Also clarified that eligible no-grad calls avoid eager rotation passes and FP32 materializations, improving clarity for users and developers. (documentation/experimental/METAL_FLASH_ATTENTION.md[1]METAL_FLASH_ATTENTION.es.md[2]METAL_FLASH_ATTENTION.hi.md[3]METAL_FLASH_ATTENTION.ja.md[4]METAL_FLASH_ATTENTION.pt-BR.md[5]METAL_FLASH_ATTENTION.zh.md[6]These changes collectively enable more efficient attention computation on supported hardware, improve code maintainability by centralizing the fused attention logic, and enhance documentation for both users and contributors.