Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio FLUX.3 on fp16-only GPUs: an explicit INT8 / FP8 quant reads the bf16 checkpoint as stored instead of widening the transformer to float32 (#17228)
Daniel Han184cdb8bef47
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex a41c88ce8f8..a0b05040ed5 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -532,6 +532,7 @@ from .diffusion_transformer_quant import (family_denies_scheme,fp16_card_int8_host,fp16_card_int8_scheme,+ fp16_card_quant_storage_enabled,mark_source_precision,nvfp4_gated_base,native_int8_act,@@ -9705,10 +9706,24 @@ class DiffusionBackend:transformer = pipe_kwargs.get("transformer"),family_name = fam.name,storage_dtype = flux3_storage_dtype(target, dtype),- # A transformer quant rebuilds the dense Linears it reads.- transformer_storage = transformer_quant_pinned is None- and native_scheme is None- and native_offload_scheme is None,+ # fp16-card native build reads Linears as stored (bf16, manual cast).+ transformer_storage = (+ transformer_quant_pinned is None+ and native_scheme is None+ and native_offload_scheme is None+ )+ or (+ fp16_card_quant_storage_enabled()+ and native_scheme is not None+ and native_offload_scheme is None+ and fp16_card_int8_host(target)+ and native_quant_scheme(+ target,+ transformer_quant_pinned,+ family = getattr(fam, "name", None),+ )+ is None+ ),)finally:self._stop_load_prefetch()diff --git a/studio/backend/core/inference/diffusion_native_quant.py b/studio/backend/core/inference/diffusion_native_quant.pyindex 89e07d41ae3..9d1eb55cfda 100644--- a/studio/backend/core/inference/diffusion_native_quant.py+++ b/studio/backend/core/inference/diffusion_native_quant.py@@ -501,7 +501,8 @@ def native_linear_class():self.in_features = int(linear.in_features)self.out_features = int(linear.out_features)self.scheme = scheme- self.compute_dtype = linear.weight.dtype+ cast = getattr(linear, "_unsloth_compute_dtype", None)+ self.compute_dtype = cast if isinstance(cast, torch.dtype) else linear.weight.dtypeself.act_int8 = bool(act_int8) and scheme == NATIVE_INT8self.rot_group = (int(rot_group)@@ -567,7 +568,14 @@ def native_linear_class():self.register_buffer("weight_scale", scale.squeeze(1).to(torch.float32).contiguous().view(torch.int32))- self.bias = linear.bias+ bias = linear.bias+ if (+ bias is not None+ and bias.dtype != self.compute_dtype+ and isinstance(cast, torch.dtype)+ ):+ bias = nn.Parameter(bias.detach().to(self.compute_dtype), requires_grad = False)+ self.bias = bias# Registered here, in eager, so a compiled forward only reads the handle (see _row_major_op).self._row_major = _row_major_op() if self.act_int8 and not self.rot_group else Noneself._int8pack_scales: dict = {}diff --git a/studio/backend/core/inference/diffusion_transformer_quant.py b/studio/backend/core/inference/diffusion_transformer_quant.pyindex c26395d13f3..4a158cc5bf1 100644--- a/studio/backend/core/inference/diffusion_transformer_quant.py+++ b/studio/backend/core/inference/diffusion_transformer_quant.py@@ -2091,6 +2091,26 @@ def _make_quant_config(scheme: str, fast_accum: Optional[bool] = None) -> Any:raise ValueError(f"unknown transformer quant scheme '{scheme}'")+def linear_compute_dtype(module: Any) -> Any:+ """Manual-cast ``_unsloth_compute_dtype`` if set, else the weight dtype."""+ import torch++ cast = getattr(module, "_unsloth_compute_dtype", None)+ if isinstance(cast, torch.dtype):+ return cast+ weight = getattr(module, "weight", None)+ return getattr(weight, "dtype", None)+++FP16_CARD_QUANT_STORAGE_ENV = "UNSLOTH_DIFFUSION_FP16_CARD_QUANT_STORAGE"+++def fp16_card_quant_storage_enabled() -> bool:+ """fp16-card native quant reads bf16 as stored instead of widening to float32; env ``=0`` widens."""+ raw = (_os.environ.get(FP16_CARD_QUANT_STORAGE_ENV) or "").strip().lower()+ return raw not in ("0", "false", "no", "off")++def make_filter_fn(min_features: int,exclude_name_tokens: tuple[str, ...] = (),@@ -2100,7 +2120,7 @@ def make_filter_fn(require_dtype: Any = None,):"""A non-bf16 Linear aborts the whole quantise pass; an unaligned one crashes the first matmul later.- ``require_dtype``: keep only Linears of that dtype (the fp16-card int8 path's compute dtype)."""+ ``require_dtype``: keep only Linears computing in that dtype (by compute dtype, not stored bf16 weight)."""def filter_fn(module: Any, fqn: str = "") -> bool:try:@@ -2130,7 +2150,7 @@ def make_filter_fn(return Falseif require_dtype is not None:weight = getattr(module, "weight", None)- if weight is None or weight.dtype != require_dtype:+ if weight is None or linear_compute_dtype(module) != require_dtype:return Falsereturn Truediff --git a/studio/backend/tests/test_diffusion_fp16_card_quant_storage.py b/studio/backend/tests/test_diffusion_fp16_card_quant_storage.pynew file mode 100644index 00000000000..4ea01c07f79--- /dev/null+++ b/studio/backend/tests/test_diffusion_fp16_card_quant_storage.py@@ -0,0 +1,154 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""fp16-card native quant of bf16 Linears as stored matches the float32-widened build."""++from __future__ import annotations++import ast+import copy+import inspect+import types++import pytest++torch = pytest.importorskip("torch")++import core.inference.diffusion_native_quant as nq # noqa: E402+import core.inference.diffusion_transformer_quant as tq # noqa: E402+from core.inference import diffusion_manual_cast as mc # noqa: E402++KILL_SWITCH = "UNSLOTH_DIFFUSION_FP16_CARD_QUANT_STORAGE"+++def turing(monkeypatch):+ monkeypatch.setattr(tq, "torch_is_rocm", lambda: False)+ monkeypatch.setattr(tq, "is_stubbed", lambda name: False)+ monkeypatch.setattr(tq, "_capability", lambda ordinal = None: (7, 5))+ for env in (KILL_SWITCH, "UNSLOTH_DIFFUSION_FP16_CARD_INT8", "UNSLOTH_NATIVE_INT8_ROT"):+ monkeypatch.delenv(env, raising = False)+ # CPU: no ConvRot (its rotation is a CUDA W8A8 detail), so both builds are plain per-row int8+ monkeypatch.setenv("UNSLOTH_NATIVE_INT8_ROT", "0")+++def _tiny_flux3():+ fmod = pytest.importorskip("diffusers.models.transformers.transformer_flux3")+ torch.manual_seed(0)+ model = fmod.Flux3Transformer2DModel(+ in_channels = 16,+ cond_in_channels = 16,+ num_layers = 2,+ num_single_layers = 2,+ attention_head_dim = 32,+ num_attention_heads = 4,+ joint_attention_dim = 32,+ timestep_channels = 32,+ axes_dims_rope = (8, 8, 8, 8),+ ).eval()+ with torch.no_grad():+ for p in model.parameters():+ p.data = p.data.to(torch.bfloat16).to(torch.float32)+ g = torch.Generator().manual_seed(1)+ kw = {+ "hidden_states": torch.randn(1, 64, 16, generator = g),+ "encoder_hidden_states": torch.randn(1, 9, 32, generator = g),+ "timestep": torch.tensor([0.7]),+ "img_ids": torch.randint(0, 16, (64, 4), generator = g).float(),+ "txt_ids": torch.randint(0, 16, (9, 4), generator = g).float(),+ "return_dict": False,+ }+ if "target_timestep" in inspect.signature(model.forward).parameters and model.config.get(+ "flow_map", False+ ):+ kw["target_timestep"] = torch.tensor([0.5])+ return model, kw+++def _stored_bf16(model):+ keep = tuple(getattr(model, "_keep_in_fp32_modules", None) or ())+ with torch.no_grad():+ for name, p in model.named_parameters():+ if not any(k in name for k in keep):+ p.data = p.data.to(torch.bfloat16)+ mc.apply_manual_cast_(model, torch.float32)+ return model+++def _quantise(model, scheme):+ target = types.SimpleNamespace(device = "cuda", dtype = torch.float32)+ return tq.quantize_transformer(+ types.SimpleNamespace(transformer = model),+ target,+ mode = scheme,+ family = "flux.3-klein",+ min_features = 16,+ )+++def _native(model):+ return {n: m for n, m in model.named_modules() if nq.is_native_linear(m)}++[email protected]("scheme", ["int8", "fp8"])+def test_manual_cast_source_builds_the_same_layers_as_the_widened_load(turing, scheme):+ if scheme == "fp8" and not hasattr(tq, "fp16_card_fp8_scheme"):+ pytest.skip("fp8 on Turing is a separate change")+ widened, kw = _tiny_flux3()+ stored = _stored_bf16(copy.deepcopy(widened))+ assert _quantise(widened, scheme) == scheme+ assert _quantise(stored, scheme) == scheme+ a, b = _native(widened), _native(stored)+ assert a and set(a) == set(b)+ for name in a:+ assert b[name].compute_dtype == torch.float32 == a[name].compute_dtype, name+ assert torch.equal(a[name].weight_q, b[name].weight_q), name+ assert torch.equal(a[name].weight_scale, b[name].weight_scale), name+ if a[name].bias is not None:+ assert b[name].bias.dtype == torch.float32 and torch.equal(+ a[name].bias, b[name].bias+ ), name+ dense = [m for m in stored.modules() if type(m).__name__ == "ManualCastLinear"]+ assert dense and all(m.weight.dtype == torch.bfloat16 for m in dense)+ with torch.no_grad():+ want = widened(**kw)[0]+ got = stored(**kw)[0]+ assert got.dtype == torch.float32 and torch.equal(got, want)+++def test_filter_counts_a_manual_cast_linear_by_its_compute_dtype():+ # the first floating owner is converted up front (module.dtype answers float32); the next one keeps bf16+ wrapper = torch.nn.Sequential(torch.nn.Linear(64, 64), torch.nn.Linear(64, 64)).to(+ torch.bfloat16+ )+ mc.apply_manual_cast_(wrapper, torch.float32)+ cast = wrapper[1]+ assert cast.weight.dtype == torch.bfloat16+ assert tq.linear_compute_dtype(cast) == torch.float32+ assert tq.make_filter_fn(16, require_dtype = torch.float32)(cast, "x")+ assert not tq.make_filter_fn(16, require_dtype = torch.float32)(+ torch.nn.Linear(64, 64).to(torch.bfloat16), "x"+ )+ assert tq.make_filter_fn(16, require_dtype = torch.float32)(torch.nn.Linear(64, 64), "x")+++def test_the_loader_keeps_bf16_storage_only_for_the_fp16_card_route():+ import core.inference.diffusion as d++ src = ast.unparse(ast.parse(inspect.getsource(d.DiffusionBackend)))+ i = src.index("transformer_storage=")+ gate = src[+ i : src.index("load_flux3_pipeline", i) if "load_flux3_pipeline" in src[i:] else i + 600+ ]+ assert "fp16_card_quant_storage_enabled()" in gate+ assert "fp16_card_int8_host(target)" in gate and "native_quant_scheme(" in gate+ assert "native_offload_scheme is None" in gate++[email protected]("raw", ["0", "off", "false", "no"])+def test_kill_switch(monkeypatch, raw):+ monkeypatch.setenv(KILL_SWITCH, raw)+ assert not tq.fp16_card_quant_storage_enabled()+ monkeypatch.delenv(KILL_SWITCH)+ assert tq.fp16_card_quant_storage_enabled()+ assert tq.FP16_CARD_QUANT_STORAGE_ENV == KILL_SWITCH