Skip to content

Studio FLUX.3: keep float16 on fp16-only GPUs behind a residual rescale (T4 INT8 -24% per image, GGUF Q8_0 -73%) - #17385

Merged
danielhanchen merged 3 commits into
mainfrom
studio-flux3-fp16-guard
Nov 10, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-flux3-fp16-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

GPUs without bf16 (T4 and other pre-Ampere cards) ran FLUX.3 klein in float32, because plain float16 renders black. Dense, GGUF and the non-GEMM work around INT8 therefore ran on float32 SIMT kernels without tensor cores. This PR keeps FLUX.3 in float16 on those cards behind a new fp16 guard recipe.

On a T4, INT8 goes from 55 to 42 s per image (-24%) and GGUF Q8_0 from 188 to 50 s per image (-73%), with LPIPS 0.006 / 0.018 against the float32 render.

Root cause

Only the text-token residual stream overflows float16 (limit 65504). Activation maxima from a float32 run with forward hooks, real weights, 512px:

site max abs
context_embedder output (first overflow: plain fp16 gives inf here, then NaN everywhere, black image) 114,919
text residual after txt_mode_blocks 646,594
text residual in the joint blocks (block 27) 9,661,311
joint ff.linear_out output (block 25) 271,334
attention to_out.0 output 27,215
SwiGLU product 15,267
image residual 3,347
modulations 30

The image stream, modulations, q/k/v and the dense text encoder in float16 all fit.

Fix

New recipe rescale_residual (S = 1024) on the existing install_fp16_guard mechanism:

  • The whole residual stream is carried at 1/S. The guard scales the inputs of context_embedder and x_embedders, the attention input and the SwiGLU output, and divides every LayerNorm and q/k-norm eps by S squared.
  • This is exact: every projection on the path is bias-free and every norm is scale-invariant. The recipe is checked against the diffusers FLUX.3 source and falls back to the float32 promotion if the source no longer matches.
  • The entry hooks follow later-loaded wrappers (for example LoRA).
  • Logged as diffusion.fp16_guard: recipe=rescale_residual blocks=43.
  • UNSLOTH_STUDIO_FLUX3_FP16_GUARD=0 (FLUX.3 only) or UNSLOTH_STUDIO_FP16_GUARD=0 restores the float32 promotion.

Before / after

Colab T4, 1024x1024, 16 steps, fresh process per arm, order base / head / head / base / base / head. Base is origin/main 00d10566e6.

format base s/image (s/step) head s/image (s/step) spread base / head LPIPS head vs base
INT8 54.93 to 55.20 (3.43) 42.02 to 42.09 (2.63) 0.5% / 0.15% 0.0064 mean / 0.0082 max
GGUF Q8_0 187.9 to 188.2 (11.76) 50.3 to 50.4 (3.14) 0.4% / 0.1% 0.0175 mean / 0.0348 max
  • Base and head arms are each pixel-identical across their repeats; head renders are non-black with the same pixel means as base.
  • The kill-switch arm runs 54.81 s and is pixel-identical to base.
  • Peak VRAM rises on INT8 from 7.9 to 10.1 GiB allocated: at half the activation bytes the transformer now stays fully on the card instead of partly streaming.
  • CPU float16 vs float32: LPIPS 0.0001 at 512px, 0.0115 at 1024px on another prompt.

Tests

  • New tests/test_diffusion_flux3_fp16_guard.py: 7 fail on main, all pass here (one real-class test skips without diffusers 0.42.1 and passes with it).
  • test_diffusion_flux3.py::test_fp16_only_cards_promote_flux3_to_fp32 now covers the kill-switch path.
  • 640 neighbouring diffusion tests pass.

Not proven

  • Only a Colab T4 (high-RAM) ran; memory budgets such as 6 GiB were not run.
  • All arms ran speed off; the default and max speed tiers on T4 were not timed.
  • The int8 text encoder stays unreachable on CUDA without bf16 on both trees, so it is not covered here.
  • fp16-only ROCm and XPU cards, FLUX.3 klein-base, reference images and LoRA on a real checkpoint (only a CPU test covers a wrapped entry).

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

chatgpt-codex-connector Bot commented Nov 10, 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-10T12:02:32.297998Z e501f3b 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. Nice work!

Reviewed commit: e501f3b372

ℹ️ 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 dd43f10 into main Nov 10, 2026
@danielhanchen
danielhanchen deleted the studio-flux3-fp16-guard branch November 10, 2026 16:13
danielhanchen added a commit that referenced this pull request Nov 10, 2026
…17421)

## Summary

`pip scan-packages :: hf-stack` fails on main since unsloth-zoo
2026.11.4 reached PyPI (run 42852360538, on the #17385 merge commit):

```
Summary: 2 CRITICAL, 5 HIGH, 173 MEDIUM
  7 of the 180 finding(s) above are at a site already reviewed in scripts/scan_packages_baseline.json. What differs from the reviewed entry:
    HIGH      unsloth-zoo  unsloth_zoo/compiler.py                     same matched code, file digest outside the pin
    HIGH      unsloth-zoo  unsloth_zoo/mlx/decision.py                 same matched code, file digest outside the pin
    HIGH      unsloth-zoo  unsloth_zoo/saving_utils.py                 1 matched line(s) gone, none added
    HIGH      unsloth-zoo  unsloth_zoo/temporary_patches/moe_utils.py  same matched code, file digest outside the pin
    CRITICAL  unsloth-zoo  unsloth_zoo/vision_utils.py (env + network) same matched code, file digest outside the pin
    CRITICAL  unsloth-zoo  unsloth_zoo/vision_utils.py (IMDS + network) same matched code, file digest outside the pin
    HIGH      unsloth-zoo  unsloth_zoo/vllm_utils.py                   same matched code, file digest outside the pin
```

This is the per-release re-review the digest pins are for, as in #16922
(2026.11.3), #16732 (2026.11.2) and #16624 (2026.11.1).

## Review

I read the six files in the 2026.11.3 and 2026.11.4 wheels downloaded
from PyPI.

- Six of the seven new entries have the same `evidence_hash` as an
already reviewed entry: the matched code is unchanged and only the file
digest moved.
- In `saving_utils.py` one of the two `__import__("transformers",
fromlist = [model_class_name])` lines is gone and nothing was added. Its
entry was unpinned before and stays unpinned.
- None of the changed lines add `exec`, `eval`, `compile`, `marshal`,
`zlib`, `__import__`, environment reads, HTTP or socket calls, or the
metadata address.

| File | What changed in 2026.11.4 | Verdict |
|---|---|---|
| `unsloth_zoo/compiler.py` | MHC projection rewrites become a tuple of
`(old, new)` pairs instead of one source and replacement | benign |
| `unsloth_zoo/mlx/decision.py` | decision loads install the MLX
`model_file` guard, so a repository `config.json` cannot run code |
benign, hardening |
| `unsloth_zoo/saving_utils.py` | safetensors header reads reuse the
mapped header, and previous export shards are snapshotted and kept; one
duplicate `__import__` removed. The "metadata" here is safetensors
header metadata, not IMDS | benign |
| `unsloth_zoo/temporary_patches/moe_utils.py` | MoE backward pads the
stored expert rows instead of the transposed view | benign |
| `unsloth_zoo/vision_utils.py` | data URI parsing accepts any parameter
that prefixes `base64`, as FFmpeg does | benign |
| `unsloth_zoo/vllm_utils.py` | vLLM spawn and FlashInfer fallbacks pop
`VLLM_USE_FLASHINFER_SAMPLER` / `VLLM_USE_V2_MODEL_RUNNER` and reword
the standby hints | benign |

## Change

`scripts/scan_packages_baseline.json` gains the seven entries for the
2026.11.4 files. Each one sits next to the entry it re-reviews. No entry
is removed or widened, so 2026.11.3 installs still match their own pins.

## Testing

- `scripts/scan_packages.py -r` on `unsloth-zoo==2026.11.4`: exits 1
against main's baseline (2 CRITICAL, 5 HIGH) and 0 with this one (24
suppressed, 13 MEDIUM).
- The baseline tests (`git grep -l scan_packages_baseline -- tests`):
218 passed.
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