Skip to content

Studio: run FLUX.3 in float32 on fp16-only GPUs (T4 renders were black) - #17124

Merged
danielhanchen merged 5 commits into
mainfrom
studio-flux3-fp16-black
Nov 8, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-flux3-fp16-black

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

FLUX.3 Klein (distilled and base) renders an all-black image on fp16-only NVIDIA cards (T4 and other pre-Ampere GPUs). Studio picks float16 there (diffusion_device.py), and the FLUX.3 family entries do not carry fp16_incompatible, so _resolve_diffusion_compute_dtype keeps float16 for the whole pipeline.

This PR marks both FLUX.3 entries (flux.3-klein, flux.3-klein-base) fp16_incompatible, the existing flag the Qwen-Image and Z-Image families use for the same failure. On a float16-only card the load is promoted to float32 (logged as diffusion.dtype_promoted); bf16 cards are untouched. Kill switch: UNSLOTH_STUDIO_FLUX3_FP16=1 keeps the old float16 load.

What goes wrong in float16

Kaggle T4, merge base 8a11e11, every module output checked for non-finite values by a forward hook:

  • Dense denoiser with the automatically picked int8 text encoder: Qwen3VLTextModel.layers.0 returns no finite value at all, from an input of absmax 0.128, and every later layer is NaN.
  • unsloth/flux-3-image-klein-GGUF Q4_K_M with a dense text encoder (automatic, or text_encoder_quant="none"): no module returns a non-finite value, but one GGUF linear in the denoiser reaches 56384 (fp16 max 65504), and the decoded image is still black.

Every base render was pure black (pixel mean 0.0, 9 of 9 images over 1024^2 and 768^2 arms). The float32 head renders the prompt (pixel means 49-117).

Results

Kaggle T4, 1024x1024, 16 steps, unsloth/flux-3-image-klein-GGUF Q4_K_M, fresh process per arm, 3 new-prompt renders per arm:

arm output s / image
base (float16) black (pixel mean 0.0) 37.1 / 37.8 (void: black frames)
head (float32), whole card correct 223.4 / 227.7 / 225.0
head (float32), 6 GiB budget correct 224.8 / 225.6

Float32 on a T4 is slower than the black float16 render; it is the precision this model can run at on that card. The head's 6 GiB arms keep peak VRAM at 5.59 GiB.

Tests

tests/test_diffusion_flux3.py:

  • both FLUX.3 families are fp16_incompatible with no fp16 guard, and _resolve_diffusion_compute_dtype promotes float16 to float32 while leaving bf16 and float32 alone;
  • the kill switch (UNSLOTH_STUDIO_FLUX3_FP16) is read at import, checked in a subprocess for 1, true, 0 and unset.

test_differential.py: 3 FIXED (fail on base, pass on head), 114 unchanged, 0 head failures; the two kill-switch-on cases pass on both by design.

Not covered

  • Other fp16-only GPUs (V100, consumer Turing) were not run; they take the same dtype path as the T4.
  • Apple Silicon and ROCm pick bf16 or float32 already and do not reach this flag's branch.

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

chatgpt-codex-connector Bot commented Nov 8, 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-08T14:58:04.885039Z bbaaea2 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. Breezy!

Reviewed commit: 3af859541b

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

…p16-incompatible plan

The 12 FLUX.3 float16 cases run in float32 with this PR, so like every other fp16-incompatible family they take no
torch.compile on the default and max tiers (diffusion_speed.default_compile_eligible). The legacy sequence and the
optimizer agree on all 12; no other case changes.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: bbaaea2e2f

ℹ️ 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 c6fb555 into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-flux3-fp16-black branch November 8, 2026 17:54
Chroma01 pushed a commit to Chroma01/unsloth that referenced this pull request Nov 9, 2026
…ute in float32 per layer (unslothai#17173)

## Summary

FLUX.3 renders black in float16, so since unslothai#17124 fp16-only GPUs (T4 and
other Turing cards) run it in float32. The float32 path widened every
bf16 weight on the host:
`Flux3Transformer2DModel.from_pretrained(torch_dtype=float32)` and the
Qwen3-VL encoder both materialise float32 copies, about 40 GB for klein.
On a Kaggle T4 (31 GB host RAM) the dense klein and klein-base loads are
killed before the first step, for Off and for any other dense request,
run alone on the VM.

This PR keeps the checkpoint's bf16 weights as stored and upcasts each
layer's weight to float32 at call time ("manual cast"), which is what
the baseline stack does on these cards (it stores the DiT in bf16 and
runs a float32 manual cast when bf16 compute is unavailable). Host RAM,
VRAM and every offloaded transfer stay at bf16 size while every matmul
still runs in float32. Upcasting bf16 to float32 is exact, so the math
is the same as the float32 load.

- `diffusion_manual_cast.py`: `apply_manual_cast_(module,
compute_dtype)` swaps every plain `nn.Linear` / `nn.Embedding` stored in
bf16 / fp16 for a twin whose forward upcasts the weight (and bias) to
the compute dtype, and converts every other half tensor (norms, small
embedders, the owner of the first floating parameter so `module.dtype`
still answers float32) up front. Quantised, GGUF and other `nn.Linear`
subclasses are left alone. The forward is plain `F.linear` on a casted
weight, so it composes with group offload, LoRA wrapping and
`torch.compile` (traced with `fullgraph=True` in the tests).
- `diffusion_flux3.py`: `flux3_storage_dtype(target, dtype)` is bf16
only for a CUDA float32 load (the promoted fp16-only case).
`load_flux3_pipeline` then loads the text encoder and the transformer at
bf16 and applies the manual cast, but only when the safetensors headers
show every float tensor of that component is stored as bf16 (else
today's widened load, since loading wider data at bf16 would round it).
An explicit or native transformer quant keeps the widened transformer,
because its quantiser rebuilds the dense Linears it reads.
- All three FLUX.3 load sites (dense pipeline, single file / GGUF,
assembled pipe) pass the storage dtype; on the GGUF path this is the
text encoder only.
- `diffusion_speed._fuse_qkv` skips a manual-cast denoiser: a fused
`to_qkv` would be a plain bf16 Linear fed float32 activations.

bf16 GPUs (they load FLUX.3 at bf16), CPU, MPS and XPU are unchanged:
`flux3_storage_dtype` answers None for every non-float32 or non-CUDA
load. Kill switch: `UNSLOTH_STUDIO_MANUAL_CAST=0` restores the widened
float32 load.

## Results (Kaggle T4, 31 GB host, 1024x1024, fresh process per arm,
same seeds and prompts, one arm at a time on the VM)

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

| model | arm | loads | s / image | peak VRAM | process tree PSS | image
|
|---|---|---|---|---|---|---|
| klein, dense (16 steps) | base | no, killed at 1220 s | - | - | - | -
|
| klein, dense | head, run 1 | yes | 206.5 | 11.46 GiB | 25.0 GiB |
finite |
| klein, dense | head, run 2 | yes | 206.0 | 11.46 GiB | 24.6 GiB |
finite |
| klein-base, dense (50 steps, CFG 3.5) | base | no, killed at 1162 s |
- | - | - | - |
| klein-base, dense | head, run 1 | yes | 1195.2 | 9.68 GiB | 25.3 GiB |
finite |
| klein-base, dense | head, run 2 | yes | 1196.7 | 9.68 GiB | 25.3 GiB |
LPIPS 0.000 vs run 1 |
| klein, GGUF Q8_0 | base, run 1 / run 2 | yes | 241.6 / 241.4 | 11.01
GiB | 26.5 / 27.0 GiB (RSS 46.7 / 27.5) | LPIPS 0.000 run 2 vs run 1 |
| klein, GGUF Q8_0 | head, run 1 / run 2 | yes | 228.7 / 240.4 | 10.29
GiB | 20.2 / 20.8 GiB (RSS 20.7 / 20.8) | LPIPS 0.000 vs base
(pixel-identical) |
| klein, dense | the baseline stack, bf16 weights, float32 manual cast |
yes | 203.5 | 14.99 GiB | 15.0 GiB | finite |

Quality: the T4 dense head render (float32 per layer) against a bf16
Studio render of the same prompts and seeds on an L4 is LPIPS 0.013 mean
/ 0.017 max (3 prompts), the GGUF Q8_0 render 0.014 / 0.020 on both base
and head, and head run 2 against run 1 is 0.000. No image is black
(per-image pixel means 49 to 157). Speed of the dense head is within
1.5% of the baseline stack on the same VM (206.0 vs 203.5 s / image) at
3.5 GiB less VRAM. GGUF speed is unchanged within noise; its peak host
RSS drops from 46.7 / 27.5 GiB to 20.7 / 20.8 GiB because the text
encoder is no longer widened.



On head the load log shows the cast engaged on both components
(`text_encoder keeps 8.99 GiB of bfloat16 weights as stored and computes
in float32`, `transformer keeps 10.33 GiB ...`); the transformer streams
with 4.7 GiB resident instead of being priced and moved at float32 size.

### Does only part of the model need float32?

Measured on the same T4: the text encoder in float32 (manual cast) with
the transformer in float16 still renders black (pixel mean 0.0, all
three prompts, two runs), so the transformer's residual stream needs
float32 as well, not just the encoder. The NaN probe from that run saw
the encoder's first decoder layer go non-finite in float16, and a
float16 DiT Linear peak of 56448, near the float16 limit. Both
components therefore keep float32 compute.

## Tests

`studio/backend/tests/test_diffusion_flux3_manual_cast.py` (CPU, tiny
random models):
- A tiny `Flux3Transformer2DModel` and a tiny
`Qwen3VLForConditionalGeneration` stored in bf16 with the manual cast
give outputs bit-identical (`torch.equal`) to the same weights loaded in
float32, `module.dtype` answers float32, every remaining Linear weight
is bf16.
- The manual-cast Linear traces with `torch.compile(fullgraph=True)`;
subclassed Linears are left alone.
- `flux3_storage_dtype` is bf16 only for CUDA float32 loads and honours
the kill switch.
- The stored-dtype check: all-bf16 shards take the cast, any F32 / F16
tensor or missing files keep the widened load.
- Every `load_flux3_pipeline` call site passes the storage dtype, and
the dense site keeps a quantised transformer widened.
- `_fuse_qkv` skips a manual-cast denoiser.

These fail on `main` (the module does not exist) and pass here. The
FLUX.3, FLUX.3 encoder prequant and QKV-drop suites pass unchanged.

## Not covered

- Other Turing cards (RTX 20xx, Quadro RTX) take the same path but were
not run.
- An explicit INT8 request on a T4 (unslothai#17167) keeps the widened float32
transformer here, so its load is unchanged by this PR.
- LoRA on a manual-cast FLUX.3 was not exercised on a GPU.
Chroma01 pushed a commit to Chroma01/unsloth that referenced this pull request Nov 9, 2026
…nslothai#17167)

## Summary

On a T4 (and every other Turing GPU, sm75) Studio refuses
`transformer_quant="int8"` before the load:

```
transformer_quant='int8' could not be used: this device cannot run a dense torchao quant (it needs a CUDA GPU in bf16).
```

The refusal comes from `dense_transformer_supported`, which requires a
bf16 pipeline: every torchao dynamic scheme is gated on bf16. A T4 has
no bf16, so Studio runs pipelines there in float16 (or float32 for
fp16-incompatible families), and the int8 request never reaches a
quantiser. The T4 is the free Colab / Kaggle card, and the baseline
stack runs INT8 on it.

Studio already ships a torchao-free int8 linear
(`NativeWeightOnlyLinear`, per-row int8 codes, W8A8 through
`torch._int_mm`) that is dtype-agnostic. `torch._int_mm` has no
capability gate (`aten/src/ATen/native/cuda/Blas.cpp`,
`_int_mm_out_cuda` only checks shapes and calls
`at::cuda::blas::int8_gemm`, i.e. cuBLASLt int8), and on a T4 it
dispatches to Turing int8 tensor-core kernels
(`cutlass_75_tensorop_i8816gemm_s8_128x128_tn_align16`, measured below).
Inductor never picks its Triton int8 template on a T4 (`is_big_gpu`
needs 68 SMs, the T4 has 40), so a compiled `_int_mm` stays on the same
cuBLASLt kernel.

This PR routes an explicit int8 request on an fp16-only Turing card to
that path:

- `fp16_card_int8_host(target)`: CUDA, NVIDIA (not ROCm, not the torchao
stub), pipeline dtype float16 or float32, compute capability in [7.5,
8.0). Every bf16 GPU, sm70 and older, CPU, MPS and XPU answer False.
- `fp16_card_int8_scheme(target, requested, family)`: `int8` only for an
explicit int8 request on such a host, honouring the family deny list.
AUTO, fp8, mxfp8 and nvfp4 are unchanged.
- The image precision gate admits the request instead of refusing it;
the loader builds the native int8 linears in the pipeline's own dtype
(`make_filter_fn(require_dtype=...)` in place of the bf16 filter,
`dense_quant_blocker(allow_float16=True)` for that host only).
- When the small-host route has already stored the denoiser as per-row
int8 weights (hosts with little RAM, e.g. Kaggle's 31 GB VM for
Qwen-Image-2.1), those weights are the int8 build: nothing is
re-quantised, and W8A8 is armed on them for float32 compute
(`enable_int8_act_`, gated per call exactly as before).
- `native_quant_scheme` / `native_quant_host` are untouched, so the
single-file int8 loader and the video routes keep their answers on every
GPU.

Kill switch: `UNSLOTH_DIFFUSION_FP16_CARD_INT8=0` restores the refusal.
`UNSLOTH_NATIVE_INT8_ACT=0` keeps the int8 linears weight-only.

## Results (Kaggle T4, 1024x1024, fresh process per arm, 3 prompts, same
seeds)

Base = merge base `0efc5fc3c`, head = this branch.

| model | arm | output | s / image | peak VRAM | LPIPS vs float16 dense
|
|---|---|---|---|---|---|
| Z-Image-Turbo (8 steps) | base, INT8 | refused (message above) | - | -
| - |
| Z-Image-Turbo | base, Auto (today's default) | image | 40.6 | 6.4 GiB
| 0.000 |
| Z-Image-Turbo | Off (float16 dense, group offload), 2 runs | image |
29.3 / 50.7 | 6.4 GiB | 0.000 (A/A) |
| Z-Image-Turbo | head, INT8 (W8A8 `_int_mm`), 2 runs | image | 24.4 /
24.5 | 11.6 GiB | 0.083 (max 0.128); run 2 vs run 1: 0.000 |
| Z-Image-Turbo | head, INT8 weight-only (`UNSLOTH_NATIVE_INT8_ACT=0`) |
image | 29.2 | 11.6 GiB | 0.060 (max 0.106) |
| Qwen-Image-2.1-Turbo (8 steps) | base, INT8 | refused | - | - | - |
| Qwen-Image-2.1-Turbo | Off (small-host int8 weights, float16 compute)
| image | 39.6 (warm page cache) | 14.7 GiB | - |
| Qwen-Image-2.1-Turbo | head, INT8, 2 runs | image | 75.6 (cold page
cache) / 38.2 | 14.7 GiB | - |
| Qwen-Image-2.1-Turbo | GGUF Q8_0 | image | 74.6 | 8.5 GiB | - |

Z-Image INT8 renders the same compositions as the float16 dense
reference; it is 17% faster than Off and 40% faster than today's Auto
default on this card, at the cost of a resident int8 denoiser (11.6 GiB
VRAM instead of a streamed 6.4 GiB). Host RAM (process tree PSS) is
unchanged: 23.4 / 22.6 GiB vs 22.9 / 21.8 GiB.

On Kaggle's 31 GB host, Qwen-Image-2.1-Turbo already takes the
small-host route with Off (its denoiser is stored as int8 weights), so
the INT8 request now lands on the same build: same speed as Off instead
of a refusal.

### Kernel check (Kaggle T4, torch 2.13.0+cu130, torchao 0.18.0)

`torch._int_mm` returns the exact integer product (4096x3072x3072,
compared with an fp64 matmul). Median ms per linear, M=4096:

| K x N | float16 dense | float32 dense | W8A8 eager, fp32 in | W8A8
compiled, fp16 in | `_int_mm` alone |
|---|---|---|---|---|---|
| 3072 x 3072 | 1.79 | 19.03 | 4.25 | 3.66 | 1.79 |
| 3072 x 12288 | 13.39 | 75.61 | 12.26 | 9.03 | 8.21 |
| 12288 x 3072 | 15.85 | 78.84 | 13.86 | 7.39 | 7.99 |

No graph breaks and no recompiles in the compiled W8A8 function (dynamo
counters: 4 frames, 0 recompiles, 0 graph breaks over 4 shapes x 2
dtypes).

## Tests

`studio/backend/tests/test_diffusion_fp16_card_int8.py` (CPU, capability
monkeypatched):
- Turing fp16 / fp32 pipelines take int8; fp8, mxfp8, AUTO and unset do
not.
- Every other capability (sm61, sm70, sm80, sm86, sm89, sm90, sm100,
sm120, unknown) x every dtype is unchanged, as are bf16 on Turing, CPU /
MPS / XPU, ROCm and the torchao stub.
- `native_quant_host` / `native_quant_scheme` answers are unchanged on
Turing.
- `quantize_transformer` builds native int8 linears in float16 and
float32 (output within 5% of the dense one) and keeps deliberate float32
linears dense in a float16 pipeline; Ampere float16 still declines.
- The image precision gate admits int8 on Turing for Qwen-Image-2.1 and
FLUX.3 (default and `low_vram` memory), still refuses fp8 / mxfp8, and
refuses again under the kill switch or on sm70 / sm86.
- `dense_quant_blocker` accepts float16 only when asked and still
refuses fp8 storage; `enable_int8_act_` arms the small-host int8 linears
and honours its kill switch.

## Not covered

- FLUX.3: on `main` it renders black on a T4 in float16 (unslothai#17124 moves it
to float32). With unslothai#17124 applied, the float32 dense load was killed on
Kaggle's 31 GB host for Off and for INT8 alike (the float32 GGUF Q8_0
load works, 240 s / image), so FLUX.3 INT8 on a T4 is not measured here.
- Other Turing cards (RTX 20xx, Quadro RTX) take the same capability
branch but were not run.
- A T4 on a host with enough RAM for a float16 dense Qwen-Image-2.1 load
(where the native build, not the small-host weights, would be used for
that model) was not available.
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