Skip to content

Studio FLUX.3: run attention in float16 on fp16-only GPUs (T4: -15% per image GGUF, -34% INT8) - #17223

Merged
danielhanchen merged 3 commits into
mainfrom
studio-flux3-fp16-attention-turing
Nov 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-flux3-fp16-attention-turing

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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:

  • Q and K leave an RMSNorm (norm_q / norm_k, then RoPE, which keeps each rotated pair's norm), so every entry is at most sqrt(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.
  • V is unbounded, so each (batch, head) slice is divided by a power of two that puts its largest entry in [2^13, 2^14] (exact in float32) before the cast, and the output, a convex combination of V rows, is multiplied back in float32.
  • Float16 keeps 10 mantissa bits; bf16 GPUs run this same attention at 7.

diffusion_flux3_half_attn.py wraps dispatch_attention_fn in diffusers' transformer_flux3 module (the same module-level patch pattern as the FLUX.3 fused RoPE), registered as the autoopt load patch flux3_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 speed off keeps 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 (also UNSLOTH_DIFFUSION_PATCH_FLUX3_FP16_ATTN=0).

Results (Kaggle T4, FLUX.3 klein, 1024x1024, 16 steps, fresh process per arm, same VM, same seeds)

Base = main at the merge base dcb1eb1ec7, head = this branch. Order base, head, head, base.

arm s / image, run 1 run 2 attention kernel per render (profile) LPIPS
GGUF Q4_K_M, base 226.7 (227.9 / 225.4) 227.1 (228.9 / 225.3) 44.6 s fmha_cutlassF_f32 (Q8_0 profile on the same VM) 0.068 vs float32 dense
GGUF Q4_K_M, head 193.7 (193.6 / 193.9) 191.9 (192.9 / 190.8) 12.4 s fmha_cutlassF_f16_aligned_32x128_rf_sm75 0.0001 vs base (both runs); 0.068 vs float32 dense

15% 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:

  • The scaled float16 path matches a float64 reference to float16 rounding per head for V magnitudes 1e-6 to 1e7 (one head 1000x louder than the rest), where a plain float16 cast overflows.
  • CPU / 16-bit / masked calls run stock; the Q / K bound is read from the RMSNorm weights (inf when a norm is missing or not RMSNorm).
  • Install gating: engaged, idempotent, uninstall restores the symbol; explicit off, a loud norm weight, another denoiser class, a GPU with bf16, the kill switch and an unexpected symbol all stay stock. Only sm70 / sm75 qualify.
  • Registered as a load patch that passes explicit_off.
  • diffusers' real 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) and test_diffusion_autoopt_parity.py (the legacy sequence calls it like the other FLUX.3 patch; the fixture gains "flux3_fp16_attn": false on the 640 image cases, and nothing else) pass.

Not covered

  • V100 (sm70) takes the same path but was not run.
  • FLUX.3 klein-base and reference-image edits take the same path but were not timed.
  • The regression switchboard GPU units did not run (its batch notebook is over Kaggle's size cap); owed.

@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T07:54:36.514099Z e7fe9fc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: e7fe9fcf34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit cc0c4a0 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-flux3-fp16-attention-turing branch November 9, 2026 11:55
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