Repository navigation
Studio FLUX.3: run attention in float16 on fp16-only GPUs (T4: -15% per image GGUF, -34% INT8) - #17223
Merged
Conversation
…ded by their RMSNorm, V scaled per head)
…dense loads resolve an unset request to off)
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
GPUs without bf16 (T4 and other Turing / Volta cards) run FLUX.3 in float32, because its residual stream overflows float16. That includes attention, which then runs on the float32 SIMT memory-efficient kernel (
fmha_cutlassF_f32_aligned_32x128_rf_sm75): 42.4 s of a 210 s Q4_K_M render on a T4 in a torch profile, the second largest cost after the float32 GEMMs. Attention cannot overflow float16 here, so this PR runs it on the float16 tensor-core kernel instead:norm_q/norm_k, then RoPE, which keeps each rotated pair's norm), so every entry is at mostsqrt(head_dim) * max|norm weight|. The patch reads that bound from the loaded weights once at install and engages only when it is below 16384 (float16 max is 65504). FLUX.3 klein's largest norm weight is 6.81, a bound of 77.diffusion_flux3_half_attn.pywrapsdispatch_attention_fnin diffusers'transformer_flux3module (the same module-level patch pattern as the FLUX.3 fused RoPE), registered as the autoopt load patchflux3_fp16_attn. It engages only for a FLUX.3 transformer on an sm70 / sm75 GPU, and each call still requires float32 CUDA inputs without autograd and no mask (or a bool one); anything else calls stock. An explicit speedoffkeeps stock float32 attention (exact); an unset request that a dense load resolves to off still engages. bf16 GPUs, 16-bit loads, ROCm, MPS, CPU and every other family are unchanged.Kill switch:
UNSLOTH_DIFFUSION_FLUX3_FP16_ATTN=0(alsoUNSLOTH_DIFFUSION_PATCH_FLUX3_FP16_ATTN=0).Results (Kaggle T4, FLUX.3 klein, 1024x1024, 16 steps, fresh process per arm, same VM, same seeds)
Base =
mainat the merge basedcb1eb1ec7, head = this branch. Order base, head, head, base.fmha_cutlassF_f32(Q8_0 profile on the same VM)fmha_cutlassF_f16_aligned_32x128_rf_sm7515% faster per image. With the INT8 fix of #17217 applied on both arms (another VM, base = #17217 alone): INT8 89.2 s -> 58.8 s per image, LPIPS 0.006 vs the float32-attention render, 0.029 vs Studio's float32 dense render (0.026 before).
The float32 dense reference (
transformer_quant=none, speed off) is unchanged: an explicit off keeps stock attention.Tests
studio/backend/tests/test_diffusion_flux3_half_attn.py:explicit_off.Flux3Transformer2DModel(tiny config): every attention call of a forward takes the float16 path, output within float16 rounding of the float32 forward (diffusers 0.42.1 venv; skipped where diffusers has no FLUX.3).test_diffusion_autoopt_core.py(the fused-DiT group now lists the patch) andtest_diffusion_autoopt_parity.py(the legacy sequence calls it like the other FLUX.3 patch; the fixture gains"flux3_fp16_attn": falseon the 640 image cases, and nothing else) pass.Not covered