Repository navigation
Studio: run FP8 on T4 and other Turing GPUs as weight-only fp8 instead of refusing it - #17218
Merged
Merged
Conversation
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. You're on a roll. 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". |
[skip ci]
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.
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
On a T4 (and every other Turing GPU, sm75) Studio refuses
transformer_quant="fp8"before the load: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):fp8for an explicit fp8 request on anfp16_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_hostanswer for both explicit int8 and fp8; the image precision gate and the loader'snative_schemeread them in place of the int8-only helpers.quantize_transformerbuilds native fp8 linears (per-row e4m3 codes, float32 scales) in the pipeline dtype, weight-only (act_int8stays False for fp8). The forward isF.linearon the upcast weight, so a float32 FLUX.3 load computes every matmul in float32 as before.native_quant_schemeandselect_transformer_quant_schemeare untouched, so the hosted fp8 prequant planners and the ROCm / MPS native routes keep their answers.Kill switch:
UNSLOTH_DIFFUSION_FP16_CARD_FP8=0restores the refusal (UNSLOTH_DIFFUSION_FP16_CARD_INT8=0also 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 =
mainat the merge basedcb1eb1ec7, 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.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):quantize_transformerbuilds weight-only native fp8 linears in the pipeline dtype (fp16 and fp32), output within 5% of the dense layer; Ampere fp16 still declines.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, excepttest_unpinned_torchao_host_copy_is_a_distinct_wrapper_over_the_same_data, which fails the same way onmainin this environment (torchao build).Not covered