Skip to content

Studio: run FP8 on T4 and other Turing GPUs as weight-only fp8 instead of refusing it - #17218

Merged
danielhanchen merged 3 commits into
mainfrom
studio-fp8-on-turing
Nov 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-fp8-on-turing

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

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

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

The baseline stack runs FP8 on the same card: it keeps the fp8 weights and upcasts each layer to the compute dtype at call time (a T4 has no fp8 matmul). #17167 opened the same refusal for INT8 by routing it to Studio's torchao-free native linear. This PR does the same for FP8, weight-only:

  • fp16_card_fp8_scheme(target, requested, family): fp8 for an explicit fp8 request on an fp16_card_int8_host (CUDA, NVIDIA, not the torchao stub, pipeline dtype float16 or float32, compute capability in [7.5, 8.0)), honouring the family deny list. AUTO, int8, mxfp8 and nvfp4 are unchanged.
  • fp16_card_native_scheme / fp16_card_native_host answer for both explicit int8 and fp8; the image precision gate and the loader's native_scheme read them in place of the int8-only helpers.
  • quantize_transformer builds native fp8 linears (per-row e4m3 codes, float32 scales) in the pipeline dtype, weight-only (act_int8 stays False for fp8). The forward is F.linear on the upcast weight, so a float32 FLUX.3 load computes every matmul in float32 as before.
  • native_quant_scheme and select_transformer_quant_scheme are untouched, so the hosted fp8 prequant planners and the ROCm / MPS native routes keep their answers.

Kill switch: UNSLOTH_DIFFUSION_FP16_CARD_FP8=0 restores the refusal (UNSLOTH_DIFFUSION_FP16_CARD_INT8=0 also closes it, since both ride the same Turing host check).

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

Base = main at the merge base dcb1eb1ec7, head = this branch. The baseline stack ran an fp8 file of the same linear set (per-tensor e4m3 weights, float32 manual cast) on the same VM.

arm output s / image (2 renders) peak VRAM process tree PSS LPIPS vs own float32 dense
base, FP8 refused (message above) - - - -
head, FP8 (weight-only, float32 compute) image 206.2 (206.0 / 206.5) 11.15 GiB 22.8 GiB 0.038 (max 0.068)
baseline stack, fp8 file, float32 manual cast image 201.6 (202.2 / 201.1) 10.99 GiB 5.2 GiB 0.037 (max 0.056)

Earlier the same day on another T4 VM the baseline fp8 arm took 212.2 / 210.5 s (A/A), so the head sits inside the baseline's own VM-to-VM spread: both run every matmul in float32 on this card, the fp8 weights only save memory. LPIPS is against each stack's own float32 dense render of the same 2 prompts and seeds (Studio's is bit-identical across the VMs used). Host PSS is higher on Studio because an explicit quant on a float32 load still widens the transformer before quantising it (as INT8 does today, #17173); not changed here.

Tests

studio/backend/tests/test_diffusion_fp16_card_fp8.py (CPU, capability monkeypatched):

  • Turing fp16 / fp32 pipelines take fp8; AUTO, none, int8 (its own route) and mxfp8 do not.
  • Every other capability (sm61, sm70, sm80, sm86, sm89, sm90, sm100, sm120, unknown) x every dtype is unchanged, as are bf16 on Turing and CPU / MPS / XPU; the shared native route and the torchao selector are not widened.
  • Both kill switches, the family deny list, and the switch name.
  • quantize_transformer builds weight-only native fp8 linears in the pipeline dtype (fp16 and fp32), output within 5% of the dense layer; Ampere fp16 still declines.
  • The image precision gate admits fp8 for FLUX.3 (float32 and float16 targets) and Qwen-Image-2.1 on Turing, with and without low_vram, and refuses it with the kill switch or on sm70 / sm86.

test_diffusion_fp16_card_int8.py: the "other schemes still refused" case now lists mxfp8 only (fp8 is the point of this PR). The int8 native, transformer quant and pipeline prequant suites pass unchanged, except test_unpinned_torchao_host_copy_is_a_distinct_wrapper_over_the_same_data, which fails the same way on main in this environment (torchao build).

Not covered

  • Other Turing cards (RTX 20xx, Quadro RTX) take the same path but were not run.
  • 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:27:18.148967Z dc5c8d6 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. You're on a roll.

Reviewed commit: dc5c8d6364

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 9, 2026
…he bf16 checkpoint as stored instead of widening the transformer to float32 (unslothai#17228)

## Summary

unslothai#17173 keeps FLUX.3's bf16 weights as stored on fp16-only GPUs (T4) and
upcasts per layer, but an explicit transformer quant still loads the
transformer widened to float32 first ("a transformer quant rebuilds the
dense Linears it reads"). For the fp16-only card's native build (INT8
from unslothai#17167, FP8 from unslothai#17218) that widened copy is not needed: the
native linear reads `weight.float()` anyway, and widening bf16 to
float32 is exact, so the codes are the same either way. The widened load
costs host RAM on the 31 GB Kaggle T4 VM, where a Studio INT8 load
already sits at 23 GiB PSS.

This PR loads the transformer at bf16 with the manual cast when the
requested scheme takes the fp16-only card's native route, and lets that
build read manual-cast Linears:

- `load_flux3_pipeline(..., transformer_storage=...)` is also true for a
native scheme on an `fp16_card_int8_host` that is not the shared ROCm /
MPS native route (`native_quant_scheme(...) is None`) and not the native
offload build.
- `make_filter_fn(require_dtype=...)` counts a manual-cast Linear by its
compute dtype (`linear_compute_dtype`: `_unsloth_compute_dtype`, else
the weight dtype), so the float32 filter of the fp16-card path still
selects them.
- The native linear takes its compute dtype from that attribute too, and
holds a manual-cast source's bf16 bias at the compute dtype (`F.linear`
needs one dtype).
- Everything the quant skips (the M=1 modulation projections, small
layers) stays a bf16 manual-cast Linear instead of a float32 copy.

bf16 GPUs, explicit-quant loads elsewhere, ROCm / MPS native routes and
the native offload build are unchanged. Kill switch:
`UNSLOTH_DIFFUSION_FP16_CARD_QUANT_STORAGE=0` restores the widened load.

## Results (Kaggle T4, 31 GB host, FLUX.3 klein, 1024x1024, 16 steps,
fresh process per arm, one process on the VM, same seeds)

Both arms carry unslothai#17217 (INT8 renders), unslothai#17218 (FP8 on Turing) and the
other T4 FLUX.3 PRs of this round; head adds this PR. Order head-less,
head, head, head-less.

| format | arm | s / image (2 runs) | load s | process tree PSS | min
MemAvailable | peak VRAM | LPIPS |
|---|---|---|---|---|---|---|---|
| INT8 | widened (today) | 52.8 / 52.9 | 145.7 / 143.6 | 23.1 / 23.0 GiB
| 8.0 / 8.0 GiB | 11.34 GiB | - |
| INT8 | head (stored bf16) | 53.1 / 53.2 | 136.0 / 133.7 | 20.9 / 21.2
GiB | 12.9 / 12.9 GiB | 11.20 GiB | 0.000 vs widened (pixel-identical),
both runs |
| FP8 | widened (today) | 185.2 | 121.9 | 22.0 GiB | 8.3 GiB | 11.05 GiB
| - |
| FP8 | head (stored bf16) | 185.8 | 113.9 | 21.1 GiB | 12.7 GiB | 11.19
GiB | 0.000 vs widened (pixel-identical) |

Same images and the same speed; 0.9 to 2.2 GiB less process PSS, 4.4 to
4.9 GiB more host memory left free at the low point of the load, and an
8 to 10 s shorter load.

## Tests

`studio/backend/tests/test_diffusion_fp16_card_quant_storage.py` (tiny
diffusers `Flux3Transformer2DModel`, Turing capability monkeypatched):
- INT8 (and FP8 where unslothai#17218 is present) built from the manual-cast bf16
transformer gives the same native layers as the widened float32 load:
same layer set, compute dtype float32, `torch.equal` codes, scales and
biases; the rest stay bf16 manual-cast Linears; the forward output is
`torch.equal` to the widened model's.
- The filter counts a manual-cast Linear by its compute dtype; a plain
bf16 Linear is still rejected by the float32 filter.
- The loader gate is the fp16-card native route only; the kill switch
and its name.

The INT8 case fails on `main` (no layer passes the float32 filter). The
fp16-card INT8, FLUX.3 manual cast, native quant, fused native quant and
FLUX.3 suites pass unchanged (407 passed, diffusers 0.42.1).

## Not covered

- Without unslothai#17217, INT8 still fails its first render on a T4 (separate
fix); without unslothai#17218, FP8 is still refused there.
- Other Turing cards were not run.
- The regression switchboard GPU units did not run (its batch notebook
is over Kaggle's size cap); owed.
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