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 diffusion: compile MiniMax-H3 with torchao float8 weights (#14621)
Daniel Han22a8b5d6e62c
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.pyindex 849cd062f4d..0ff5c0c0e3e 100644--- a/studio/backend/core/inference/diffusion_dynamic_text.py+++ b/studio/backend/core/inference/diffusion_dynamic_text.py@@ -179,10 +179,31 @@ def unbacked_supported() -> bool:return callable(getattr(builder, "is_unbacked_source", None))+def _torchao_float8_weights(transformer: Any) -> bool:+ """Whether a torchao float8 tensor holds any of ``transformer``'s weights (wrapped in a plain Parameter or not)."""+ try:+ from .diffusion_speed import torchao_weight_tensors+ for t in torchao_weight_tensors(transformer):+ if "float8" in type(t).__name__.lower():+ return True+ # torchao < 0.12: AffineQuantizedTensor + Float8Layout+ layout = getattr(t, "_layout", None)+ if layout is not None and "float8" in type(layout).__name__.lower():+ return True+ except Exception: # noqa: BLE001 - unreadable: keep the family's choice+ return False+ return False++def unbacked_sources_for(transformer: Any) -> tuple[str, ...]:if not unbacked_supported():return ()- return _FAMILY_UNBACKED.get(type(transformer).__name__, ())+ unbacked = _FAMILY_UNBACKED.get(type(transformer).__name__, ())+ if unbacked and _torchao_float8_weights(transformer):+ # torchao float8 activation quant guards on the row count (``_get_reduction_params``): an unbacked+ # ``temb`` is a data-dependent error and the compile falls back to eager. Backed costs one respecialise.+ return ()+ return unbackeddef sources_for(transformer: Any) -> tuple[str, ...]:diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.pyindex 3a652f05bc5..f4ab92ed373 100644--- a/studio/backend/core/inference/diffusion_speed.py+++ b/studio/backend/core/inference/diffusion_speed.py@@ -1388,13 +1388,27 @@ def _carries_torchao_weights(module: Any) -> bool:the same two representations ``transformer_is_quantised`` covers. Only torchao's own types count, so a GGUF orbitsandbytes parameter subclass keeps its existing compile policy."""+ return any(True for _ in torchao_weight_tensors(module))+++def torchao_weight_tensors(module: Any) -> Iterator[Any]:+ """Each torchao tensor subclass holding a parameter of ``module``: the parameter itself, or its ``.data`` when a+ plain ``nn.Parameter`` wraps the subclass. Yields nothing for a module that cannot be read (tests/fakes)."""+def _torchao(t: Any) -> bool:return t is not None and type(t).__module__.startswith("torchao")try:- return any(_torchao(p) or _torchao(getattr(p, "data", None)) for p in module.parameters())+ parameters = list(module.parameters())except Exception: # noqa: BLE001 - not a torch module (tests/fakes)- return False+ return+ for p in parameters:+ if _torchao(p):+ yield p+ else:+ data = getattr(p, "data", None)+ if _torchao(data):+ yield datadef is_compile_failure(exc: BaseException) -> bool:diff --git a/studio/backend/tests/test_diffusion_dynamic_text.py b/studio/backend/tests/test_diffusion_dynamic_text.pyindex c5d257a433d..3b5d6a3bdeb 100644--- a/studio/backend/tests/test_diffusion_dynamic_text.py+++ b/studio/backend/tests/test_diffusion_dynamic_text.py@@ -543,6 +543,50 @@ def test_slice_identity_fix_is_probe_gated_and_reversible(monkeypatch):assert SliceVariable.__dict__.get("get_real_python_backed_value") is stock+class Float8Tensor(torch.Tensor):+ """Stands in for torchao's float8 weight subclass (its module and class name are read)."""+++Float8Tensor.__module__ = "torchao.quantization.quantize_.workflows.float8.float8_tensor"+++def _h3_with_float8_weight():+ m = MiniMaxH3Transformer3DModel()+ m.proj = torch.nn.Linear(8, 8)+ m.proj.weight = torch.nn.Parameter(+ torch.Tensor._make_subclass(Float8Tensor, torch.zeros(8, 8)), requires_grad = False+ )+ assert type(m.proj.weight).__name__ == "Float8Tensor"+ return m+++def test_minimax_h3_float8_weights_keep_temb_backed_dynamic():+ """torchao float8 activation quantisation guards on the row count, so an unbacked temb failed the compile."""+ if not dt.unbacked_supported():+ pytest.skip("torch lacks compiler.config.unbacked_sources")+ m = _h3_with_float8_weight()+ assert dt.unbacked_sources_for(m) == ()+ assert "L['temb']" in dt.sources_for(m)+ assert dt.install(m) is True+ m(12, 2)+ assert "L['temb']" in m.seen.split(",")+ assert "L['temb']" not in (m.seen_unbacked or "").split(",")+ dt.uninstall(m)+ # a plain (bf16 / int8) H3 keeps the unbacked temb+ assert dt.unbacked_sources_for(MiniMaxH3Transformer3DModel()) == ("L['temb']",)+++def test_minimax_h3_float8_inside_a_plain_parameter_keeps_temb_backed_dynamic():+ """Some torchao builds keep ``Linear.weight`` a plain Parameter whose ``.data`` is the float8 subclass."""+ if not dt.unbacked_supported():+ pytest.skip("torch lacks compiler.config.unbacked_sources")+ m = MiniMaxH3Transformer3DModel()+ plain = types.SimpleNamespace(data = torch.Tensor._make_subclass(Float8Tensor, torch.zeros(8, 8)))+ m.parameters = lambda recurse = True: iter([plain])+ assert dt.unbacked_sources_for(m) == ()+ assert "L['temb']" in dt.sources_for(m)++class ZImageTransformer2DModel(torch.nn.Module):"""Z-Image's one block class runs the caption refiner (caption tokens), the noise refiner (image tokens) and themain layers (caption + image), positionally as (x, attn_mask, freqs_cis); the caption is padded to 32-token buckets."""