Repository navigation
Studio: run INT8 on T4 and other Turing GPUs instead of refusing it - #17167
Conversation
…refusing it On fp16-only NVIDIA cards (sm75) every dense torchao scheme needs bf16, so transformer_quant='int8' was refused before the load. The torchao-free int8 linear (torch._int_mm, cuBLASLt int8 tensor-core kernels on Turing) runs in the pipeline's fp16 or fp32 compute dtype, so an explicit int8 request now builds it there. When the small-host route already stored the denoiser as per-row int8 weights, those are the int8 build and W8A8 is armed on them for float32 compute. Explicit int8 only: AUTO and every bf16 GPU keep their current routing. Kill switch: UNSLOTH_DIFFUSION_FP16_CARD_INT8=0.
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. |
…rted torch has float8_e4m3fn)
…o run against a tree without it [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
Keep both the Turing explicit-int8 block and the checkpoint int8 storage block. native_int8_act now answers False on every fp16 CUDA target (storage stays weight-only), so the explicit-int8 Turing build takes its activation mode from fp16_card_int8_act instead and keeps W8A8; sm70, XPU and the storage path are unchanged. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
…w_float16 keyword
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
…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.
…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.
…unters, follow-up to unslothai#17167) (unslothai#17193) ## Summary Follow-up to unslothai#17167. That PR arms the existing small-host INT8 layers (`Int8WeightLinear.act_int8 = True`) for an explicit Turing INT8 request, so families such as FLUX.1 now run with armed layers too. Every forward of an armed layer increments a module-level diagnostic counter (`_INT8_ACT_COUNTS["dequant"]` on the weight-only branch, `["int8_act"]` on the W8A8 branch). Under `torch.compile`, Dynamo guards on the exact value of that global int, so each call invalidates the last graph and recompiles: - default compile: 8 frames for 12 identical calls, then an eager fallback for that layer - `fullgraph=True`: call 9 raises `FailOnRecompileLimitHit` - with Studio's raised recompile limit (64) this only delays the problem An fp16 pipeline makes it worse. `_int8_act_ok` declines fp16 on the dtype check, so the newly armed layer takes the weight-only dequant branch exactly as before, except that it now also bumps the counter. ## Fix The counters are eager-only diagnostics, and nothing in the backend reads them during a render. `forward` now skips the increment when `torch.compiler.is_compiling()` is true. Inside a trace that check is a constant, so it adds no graph break. Eager counting is unchanged. The math on both branches is unchanged, and `UNSLOTH_DIFFUSION_SMALL_HOST_INT8_ACT=0` still turns W8A8 off. ## Test (CPU, torch 2.12.1) `test_diffusion_small_host_int8_act.py::test_armed_layer_compiles_once_and_still_counts_eager_calls` builds a real armed `Int8WeightLinear` in fp16, so it takes the dequant branch. An eager call bumps the counter by one. The test then compiles the layer with `fullgraph=True` and a frame-counting backend and runs 12 calls. | Tree | Result | |---|---| | main | FAILED: `FailOnRecompileLimitHit: Hard failure due to fullgraph=True` | | this PR | passed: 1 frame for 12 calls, every output bit-equal to eager | All small-host test files (`test_diffusion_small_host*.py`, `test_video_small_host.py`): 127 passed, 7 skipped. ## Not covered - No GPU run. The W8A8 branch (Turing `torch._int_mm`) takes the same counter change, but it was not compiled on a T4 here.
…d of refusing it (unslothai#17218) ## 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). unslothai#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, unslothai#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.
…7229) ## Problem NVFP4 only has a native path on NVIDIA Blackwell (compute capability major >= 10: sm100, sm103, sm110, sm120, sm121). On every older NVIDIA card (T4 sm75, A100 sm80, 3090 sm86, L4 / 4090 sm89, H100 sm90) an explicit NVFP4 pick can only run weight-only: the 4-bit codes are widened to bf16 per layer and the GEMM runs in bf16. Today that means one of two things. An explicit `transformer_quant="nvfp4"` is refused with a 409 at preflight. An NVFP4 single file (such as the LTX-2.5 packed path from unslothai#17067) loads weight-only. Both are worse than the INT8 builds we already ship, which use INT8 tensor cores and are more accurate: | model | INT8 ConvRot LPIPS vs bf16 | best NVFP4 LPIPS vs bf16 (fake-quant) | |---|---|---| | FLUX.2 klein 4B | 0.0335 | 0.128 | | Z-Image | 0.077 | 0.125 | ## Fix One helper, `core/inference/diffusion_nvfp4_route.py`, makes the decision for every entry point: - the image and video `load` and `download-plan` routes. It runs after the disabled-NVFP4 refusal and the account checks, and before any validation, preflight or plan reads the request, so a pre-Blackwell pick never plans or downloads an NVFP4 file. The route's decision, including "leave it as is", is handed to `begin_load`, so the plan and the load always agree; - `DiffusionBackend.begin_load` and `VideoBackend.begin_load` for direct callers that skip the routes. The device is read only for an NVFP4 pick, once per process. When CUDA is not initialised and every visible card has the same capability, the reading comes from nvidia-smi, so a plan previewed during training takes no CUDA context. Otherwise it comes from `torch.cuda.get_device_capability` per ordinal. ROCm is detected with the backend's own `rocm_bf16.is_rocm_torch`, because ROCm reports gfx versions such as 11.x and 12.x through the same call. A failed read is not cached and leaves the request as it is. Tests replace the reading, so the whole matrix runs on CPU. | host | explicit NVFP4 DiT | NVFP4 text encoder | NVFP4 single file / hosted NVFP4 repo of a known model | |---|---|---|---| | NVIDIA sm >= 10 (Blackwell) | unchanged | unchanged | unchanged | | NVIDIA sm80 to sm90 | INT8 | INT8 | that model's pipeline with INT8 | | NVIDIA sm75 (T4), image | INT8 (the fp16-card int8 linear from unslothai#17167), Auto if that is switched off | Auto (no TE int8 below sm80) | that model's pipeline with INT8 | | NVIDIA sm75 (T4), video | Auto (video INT8 needs bf16) | Auto | that model's pipeline with Auto | | ROCm / XPU / MPS / CPU | Auto | Auto | that model's pipeline with Auto | INT8 is the family's normal INT8 resolution: the hosted INT8 ConvRot checkpoint where one is registered, else on-the-fly int8 with the family's rotation policy. A family whose profile denies int8 gets Auto. Off NVIDIA the pick takes whatever Auto picks there, because explicit INT8 on RDNA is slower than bf16. What the caller sees: one INFO line (`diffusion.nvfp4_route: transformer_quant='nvfp4'->'int8' on nvidia sm_89: NVFP4 needs a Blackwell GPU; using INT8`), and the load's `resolved.transformer_quant` (and `resolved.text_encoder_quant`) carries `requested: "nvfp4"`, `value: "int8"`, `status: "fell_back"` and a reason starting `NVFP4 needs a Blackwell GPU; using INT8`. A replaced checkpoint is also named in `replaced` (`nvfp4:<file>`). The frontend already renders `fell_back` with the reason as a tooltip, so no UI change is needed. No hard error is raised for any of these. Rules: - **When a checkpoint is replaced.** An NVFP4 single file, or a hosted NVFP4 repo, is replaced only by the same model's pipeline: - Image: the explicit `base_repo`, else the base the names spell. The longest base any family table lists wins, in its canonical spelling, so FLUX.2 klein 9B, Qwen-Image-2.1-Turbo and FLUX.1 dev map to their own repos. - Video: LTX-2.5 distilled files only, since the pack runs only its distilled DiT. Never an `ltx-2` family file, since 2.0 to 2.3 share a 2.0 pipeline. Other families only when the names spell the pipeline. - Hosted NVFP4 repos: only when one family owns the repo, or the filename or `family_override` tells the families apart. - **When a checkpoint is kept.** It is kept exactly as today, and logged, when any of these holds: - its family or variant cannot be resolved; - the pick names its own text-encoder or VAE files; - the load was not picked by hand (media auto-switch tracks the model by the path it asked for); - the local path does not exist. A recorded safetensors header scheme wins over the name, and a pickle is never opened to ask. - **Mixed boxes.** A load pinned to a card or a `gpu_ids` selection reroutes when any card it places work on is pre-Blackwell. With no selection the backend can land on any card, so it reroutes only when every visible card is pre-Blackwell. - **NVFP4 prequant path.** An NVFP4 `transformer_prequant_path` is dropped on a rerouted load rather than handed to the INT8 path. An explicit non-NVFP4 precision is kept. - **Master switch off.** With `UNSLOTH_NVFP4_DIFFUSION` off (the default build), an explicit NVFP4 scheme keeps its existing 400 on every GPU, and the master switch still decides whether NVFP4 exists at all. NVFP4 single files that load today with the switch off (the LTX-2.5 packed path) are rerouted. - **Auto.** Auto already never picks NVFP4 below sm100: the family `auto_prefer` row has floor (10, 0), and `_AUTO_LADDER` has no NVFP4 rung. A test now checks this for every image and video family, with every gate record and backend probe forced to pass. - **Kill switch.** `UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0` returns every request untouched (the same object, no annotation), which restores today's behaviour exactly. ## Known trade-off and limits - **LTX-2.5 on A100-40.** The priority matrix measured the explicit NVFP4 packed file at 14.6 s/clip against 16.4 s/clip for INT8, with LPIPS 0.212 against 0.137. The single-clip run below has the two within about 2%. INT8 also holds more VRAM (30.9 GiB peak against 23.3 GiB), in exchange for clearly better quality. - **Fine-tunes.** A fine-tuned NVFP4 file whose name matches a stock variant loads that stock variant's INT8 weights. `UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0` keeps the file. - **Mixed boxes with no GPU selection.** On a box with both Blackwell and pre-Blackwell cards, an explicit NVFP4 with no selection is left as is. - **Manual downloads.** The Hub download manager can still fetch an NVFP4 file the user downloads by hand. Only loads and download plans are rerouted. - **MLX.** On Apple Silicon the MLX routing engine takes no route argument, so a rerouted pick shows the INFO line but not the `fell_back` annotation. ## Tests CPU, `tests/test_diffusion_nvfp4_route.py` (308 tests): - **Routing matrix.** Capabilities {7.5, 8.0, 8.6, 8.9, 9.0, 10.0, 12.0} x devices {cuda, rocm, mps, cpu, xpu} x picks {explicit DiT nvfp4, TE nvfp4, hosted NVFP4 source, hosted NVFP4 repo, LTX-2.5 packed NVFP4 file, unknown local NVFP4 file, auto}. - **Switches.** The kill switch arm for every pick, and the NVFP4-switch-off arm. - **Blackwell.** sm100 / sm103 / sm110 / sm120 / sm121 requests come back untouched (the same request object). - **Device reading.** - mixed boxes, and selections that span cards; - ROCm reporting 12.0, including wheels that leave `torch.version.hip` unset; - nvidia-smi answering without initialising CUDA (identical cards only); - an unreadable device that is neither cached nor acted on; - an ordinary request never reading the device; - no CUDA initialisation while training is active. - **Variant safety.** - FLUX.2 klein 9B, Qwen-Image-2.1-Turbo, FLUX.1 dev and LTX-2.5 distilled map to their own pipelines. - An LTX-2.5 dev file, an LTX-2.3 file and an unspelled FLUX.1 variant are kept. - Shared hosted repos (HunyuanVideo-1.5 480p / 720p) are disambiguated or kept. - Component-file picks, hand-free loads and missing paths are kept. - A bare folder or file path decides the same as its (folder, file) pair. - **Entry points.** Every load and download-plan route (image and video) reroutes after its refusals and account checks. `begin_load` reroutes a direct caller before validation. An unchanged route decision is handed to the backend too. - **No NVFP4 download.** The rerouted Wan 2.2 A14B plan asks no `*-NVFP4` repo and stages no NVFP4 file, while the same pick under the kill switch asks `unsloth/Wan2.2-T2V-A14B-NVFP4`. - **Resolved record.** It reports `requested: nvfp4`, `fell_back`, the reason and `replaced`, and is unchanged when no route is active. Three `tests/test_video_routes.py` tests used an explicit nvfp4 on the CPU test host as the example of a refused scheme. On a CPU host that pick now goes to Auto, so they now pin a Blackwell device, where NVFP4 still reaches those refusals. Related suites (52 files: NVFP4, precision, prequant, families, image and video backends and routes, auto policy, load progress, media auto-switch): 3992 passed on head (on the last full run, six timing-sensitive tests failed while the host was loaded and passed when rerun alone, on head and on the merge base). Across the earlier head-vs-merge-base runs (`4a45dbd03e`), the failing test IDs were the same on both sides. Those were `test_diffusion_load_progress.py::test_quantized_tensor_is_counted_only_after_success_and_hooks_restore[False]`, which is flaky under xdist and passes alone, and a `test_diffusion_load_cancel_platform_matrix.py` collection error from a missing `_sparse_file` helper. ## GPU evidence (Colab L4 sm89 and A100 40GB sm80) Every arm ran in a fresh process with `UNSLOTH_NVFP4_DIFFUSION=1`. Base is the merge base `4a45dbd03e`. All runs used an explicit NVFP4 pick unless the arm says "int8". | card | model | arm | result | engaged | resolved.transformer_quant | DiT layers | peak GiB | s/step | |---|---|---|---|---|---|---|---|---| | L4 | Z-Image-Turbo 1024, 8 steps | base | refused (409 at preflight) | - | `'nvfp4' is not usable for family 'z-image' on this GPU` | - | - | - | | L4 | Z-Image-Turbo | head | renders | int8 | fell_back, "NVFP4 needs a Blackwell GPU; using INT8; seeded from the hosted checkpoint ...Z-Image-Turbo-INT8-ConvRot.safetensors" | ConvRotLinear 239 | 14.84 | 0.746 | | L4 | Z-Image-Turbo | head, kill switch | refused, same text as base | - | - | - | - | - | | L4 | Z-Image-Turbo | head, explicit int8 | renders | int8 | applied, same hosted file | ConvRotLinear 239 | 14.84 | 0.755 | | L4 | Qwen-Image-2.1-Turbo 1024, 8 steps | base | refused (409 at preflight) | - | `'nvfp4' is not usable for family 'qwen-image-2.1'` | - | - | - | | L4 | Qwen-Image-2.1-Turbo | head | renders | int8 | fell_back, seeded from ...Qwen-Image-2.1-Turbo-INT8-ConvRot.safetensors | ConvRotLinear 224 | 17.29 | 0.823 | | L4 | Qwen-Image-2.1-Turbo | head, kill switch | refused, same text as base | - | - | - | - | - | | L4 | Qwen-Image-2.1-Turbo | head, explicit int8 | renders | int8 | applied | ConvRotLinear 224 | 17.29 | 0.831 | | A100-40 | Z-Image-Turbo | base / head kill switch | refused, same text | - | - | - | - | - | | A100-40 | Z-Image-Turbo | head | renders | int8 | fell_back, hosted INT8 ConvRot | ConvRotLinear 239 | 16.23 | 0.233 | | A100-40 | Z-Image-Turbo | head, explicit int8 | renders | int8 | applied | ConvRotLinear 239 | 16.23 | 0.226 | | A100-40 | LTX-2.5 NVFP4 distilled file, 768x512x49, 8 steps | base | renders | None (packed W4A16) | "not engaged (dense bf16 DiT loaded)" | PackedBlockLinear 1176 | 23.26 | 1.061 | | A100-40 | LTX-2.5 NVFP4 file | head | renders | int8 | fell_back, "NVFP4 needs a Blackwell GPU; using INT8 from Lightricks/LTX-2.5-Diffusers instead of diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors", `replaced: nvfp4:...` | NativeWeightOnlyLinear 480 (W8A8, g256 ConvRot) | 30.93 | 1.086 | | A100-40 | LTX-2.5 NVFP4 file | head, kill switch | renders, same as base | None | same as base | PackedBlockLinear 1176 | 23.26 | 1.050 | | A100-40 | LTX-2.5-Diffusers pipeline | head, explicit int8 | renders | int8 | applied | NativeWeightOnlyLinear 480 | 30.93 | 1.081 | - Each head NVFP4 arm logged exactly one `diffusion.nvfp4_route:` line, and base, kill-switch and int8 arms logged none. - No arm requested an NVFP4 URL from the Hub. - Each head NVFP4 arm has the same artifact, layer census and render statistics as the explicit int8 arm, so it is the normal INT8 resolution. - The kill switch matches base byte for byte on the refusal text, and on the LTX layer census, peak VRAM and render statistics. - s/step is one steady image or clip per arm with no A/A repeats, so treat it as indicative, not a speed claim. These runs used head `599d6ed542`. The head arms were re-run on `b01df131b`, after the reworked checkpoint-replacement rules, device read and decision order, and matched exactly: - L4 Z-Image-Turbo, head NVFP4: int8, the same hosted INT8 ConvRot file, the same render statistics. - L4 Z-Image-Turbo, kill switch: the same refusal text as base. - A100-40 LTX-2.5 file, head NVFP4: int8 and `replaced`, with the INFO line `diffusion.nvfp4_route: diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors -> Lightricks/LTX-2.5-Diffusers (INT8) on nvidia sm_80`. - A100-40 LTX-2.5 file, kill switch: identical to base (PackedBlockLinear 1176, 23.26 GiB). The commits after that only add the training-active guard on the device read and trim comments. **Not proven on hardware:** - Blackwell (no Blackwell card was available for this run). That path is covered by CPU tests, which show every sm >= 10 request comes back as the same object with no reroute and no annotation. - T4 and the text-encoder NVFP4 pick were not run on a GPU. - The HTTP routes were exercised on CPU only. The GPU arms used `begin_load`.
… of expanding them to float32 (unslothai#17178) ## Summary On a CPU target the single-file quant loader expands every int8 layer of an int8 denoiser to float32 at load time and keeps that copy for the whole session: 4 bytes a weight instead of 1. CPU computes in float32 and has no Studio int8 runtime, and `int8_storage_host` (unslothai#17079) excluded CPU even with `UNSLOTH_DIFFUSION_INT8_STORAGE=1`, so the loader's int8 backend choice came back empty and the loader dequantized. The int8 storage layer unslothai#17079 added for pre-Ampere CUDA and XPU already runs these layers on CPU with outputs identical to the expanded model. This PR turns it on for CPU: - `int8_storage_host`: a float32 CPU target keeps checkpoint int8 codes as weight-only storage. A CPU target is never offloaded, so on CPU this is the default rather than an offload-only choice. `UNSLOTH_DIFFUSION_INT8_STORAGE=0` still expands them (the existing kill switch). - `NativeWeightOnlyLinear`: a float32 CPU forward widens into a reused per-thread float32 buffer (`copy_` from the codes, then `mul_` by the row scale, bit-identical to the existing widen). Allocating a fresh weight-sized tensor every call was most of the cost (page faults: 23 ms vs 5 ms to widen a 12288x3072 layer on 8 threads) and made the stored model about 25% slower per step. With the buffer the step time matches the expanded model within noise. The buffer is not used under autograd (a graph would keep the buffer the next layer overwrites) or while compiling, and `weight` never returns it. Saving: about 2.79 GiB per billion int8 parameters the loader keeps, on CPU-only Linux, Windows and macOS. Unchanged: CUDA (Ampere+ W8A8 and torchao routes, pre-Ampere fp16 storage), the Turing W8A8 path from unslothai#17167, ROCm, XPU, MPS, a CPU target in bf16 / fp16, LoRA loads (storage stays off under a LoRA, as in unslothai#17079) and ConvRot files on every non-CPU target. ## Results Single-file `int8_tensorwise` Qwen-Image checkpoint built from the real `Qwen/Qwen-Image` transformer config (full 3072 width, 4 of the 60 blocks, random weights, 1.40B parameters, 1.44 GB file). Each arm is a fresh process on the Studio CPU target, with Studio's own int8 backend choice and the single-file quant loader, then 5 transformer steps at 1024 image tokens plus 64 text tokens. 8 threads, 3 interleaved runs per arm, step time is the median excluding the first step. | Arm | int8 backend | Layers kept int8 | Weights held | Peak RSS | s/step (per run) | s/step (all) | |---|---|---:|---:|---:|---|---:| | main | none (expanded to float32) | 0 | 5,599,394,048 B | 7,890 MiB | 1.016 / 0.936 / 0.970 | 0.974 | | this PR | native storage | 24 | 4,240,881,920 B | 6,648 MiB | 0.997 / 0.982 / 0.988 | 0.987 | | this PR without the reused buffer | native storage | 24 | 4,240,881,920 B | 6,610 MiB | 1.157 / 1.196 / 1.204 | 1.192 | - The transformer output is bit-identical between main and this PR (`torch.equal` on the final step output). - The +1.3% median difference is inside main's own run-to-run spread (0.936 to 1.016 s). - 24 of the block Linears are kept int8 here; the rest are layers the Qwen-Image int8 filter leaves dense, so the saving on this file is 1.27 GiB. A full 60-block file scales the same way. - A 2-block tiny copy (20M parameters) is 0.030 vs 0.029 s/step, weights 80.6 MB vs 61.8 MB, output bit-identical. Per-layer timing showed why the buffer is needed. `torch._weight_int8pack_mm` on CPU was 15x to 60x slower than float32 GEMM at 256 to 4096 rows, and a single mixed int8 x float32 `torch.mul` was slower than the existing chunked widen. Neither is used. ## Tests New in the single-file quant loader tests (`studio/backend/tests/`), run on CPU: - backend choice table: CPU float32 is stored by default and with `=1`, expanded with `=0`; CPU bf16, MPS fp16 / fp32 and every existing CUDA / XPU row stay as before. - `test_cpu_keeps_int8_codes_resident_and_matches_the_expanded_dit_bitwise`: held bytes drop by exactly 3 bytes a weight minus the row scales, every layer is weight-only int8 (no W8A8, no rotation), and outputs equal the expanded load bitwise at 1, 3 and 17 rows. The kill switch load has no int8 layer. - `test_cpu_forward_widens_into_a_reused_scratch_bitwise`: bitwise against `_stored_weight`, the buffer grows once to the largest layer and is reused, and `weight` is not the buffer. - `test_cpu_forward_under_autograd_keeps_its_own_weight` and `test_cpu_native_linear_compiles_without_a_graph_break` (Dynamo `fullgraph=True`, dynamic shapes). - CPU never takes the int8 GEMM (`native_int8_act` is False). 5 of these fail on main and all pass here. The single-file quant loader tests, `test_diffusion_fp16_card_int8.py`, `test_diffusion_native_quant.py` and `test_diffusion_native_quant_mps.py` pass, apart from one torchao host-copy test that fails on main too. A wider sweep of 78 diffusion quant, memory, device and video quant test files gives the same 11 failures on main and on this branch (environment related: rope fingerprint, vision attention, segment and one MOSS pipeline test), with no new ones. ## Not covered - MPS: an MPS target with bf16 already keeps int8 codes through the native route, so there is no float32 expansion there. Only a macOS 13 MPS target (no bf16, float32) still expands, and that is left unchanged because storage on MPS float32 has not been run on Apple hardware. - The timing above is one x86 Linux host. Windows and macOS CPU take the same code path but were not timed.
Summary
On a T4 (and every other Turing GPU, sm75) Studio refuses
transformer_quant="int8"before the load: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 throughtorch._int_mm) that is dtype-agnostic.torch._int_mmhas no capability gate (aten/src/ATen/native/cuda/Blas.cpp,_int_mm_out_cudaonly checks shapes and callsat::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_gpuneeds 68 SMs, the T4 has 40), so a compiled_int_mmstays 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):int8only for an explicit int8 request on such a host, honouring the family deny list. AUTO, fp8, mxfp8 and nvfp4 are unchanged.make_filter_fn(require_dtype=...)in place of the bf16 filter,dense_quant_blocker(allow_float16=True)for that host only).enable_int8_act_, gated per call exactly as before).native_quant_scheme/native_quant_hostare untouched, so the single-file int8 loader and the video routes keep their answers on every GPU.Kill switch:
UNSLOTH_DIFFUSION_FP16_CARD_INT8=0restores the refusal.UNSLOTH_NATIVE_INT8_ACT=0keeps the int8 linears weight-only.Results (Kaggle T4, 1024x1024, fresh process per arm, 3 prompts, same seeds)
Base = merge base
0efc5fc3c, head = this branch._int_mm), 2 runsUNSLOTH_NATIVE_INT8_ACT=0)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_mmreturns the exact integer product (4096x3072x3072, compared with an fp64 matmul). Median ms per linear, M=4096:_int_mmaloneNo 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):native_quant_host/native_quant_schemeanswers are unchanged on Turing.quantize_transformerbuilds 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.low_vrammemory), still refuses fp8 / mxfp8, and refuses again under the kill switch or on sm70 / sm86.dense_quant_blockeraccepts 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
mainit renders black on a T4 in float16 (Studio: run FLUX.3 in float32 on fp16-only GPUs (T4 renders were black) #17124 moves it to float32). With Studio: run FLUX.3 in float32 on fp16-only GPUs (T4 renders were black) #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.