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 MPS: opt-in fp16 compute with fixed headroom for Z-Image (#13375)
Daniel Hand3e21f3ee277
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex dbb28739b9b..7babce3cf48 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -7967,6 +7967,10 @@ class DiffusionBackend:getattr(target, "dtype", None),logger = logger,)+ # After quantization, so int8 weight-only layers keep their storage.+ from .diffusion_mps_fp16 import install_mps_fp16++ install_mps_fp16(getattr(pipe, "transformer", None), target, logger = logger)self._raise_if_load_cancelled(_load_token)# Pre-warmed torch.compile cache: a per-fingerprint inductor dir plus a bundle loaded before thediff --git a/studio/backend/core/inference/diffusion_mps_fp16.py b/studio/backend/core/inference/diffusion_mps_fp16.pynew file mode 100644index 00000000000..caeea52938b--- /dev/null+++ b/studio/backend/core/inference/diffusion_mps_fp16.py@@ -0,0 +1,296 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Opt-in (``UNSLOTH_DIFFUSION_MPS_FP16=1``) fp16 compute for Z-Image attention / FFN internals on MPS.++Plain fp16 NaNs (FFN output reaches ~5e5), so the two overflowing sites run at power-of-two headroom (exact, as the+projections are bias-free and attention is linear in v), then are clamped to the fp16 range and rescaled in bf16:++ feed-forward: w2(silu(w1 x) * (w3 x / 32)) * 32+ attention: to_out(sdpa(q, k, v / 4)) * 4++Patched per instance, never class-level, so other pipes / models are untouched.+"""++from __future__ import annotations++import os+import types+from typing import Any, Optional++MPS_FP16_ENV = "UNSLOTH_DIFFUSION_MPS_FP16"+FFN_HEADROOM = 32.0+ATTN_HEADROOM = 4.0+_FP16_MAX = 65504.0+_MARK = "_unsloth_mps_fp16"+# Tests set float32 to check the headroom identities exactly.+COMPUTE_DTYPE = "float16"++_SUPPORTED_TRANSFORMERS = ("ZImageTransformer2DModel",)+_PROCESSOR_CLS = "ZSingleStreamAttnProcessor"+# The processor re-implements diffusers' Z-Image attention: if its source changes, the patch stays off.+_PROCESSOR_TOKENS = (+ "attn.to_q(hidden_states)",+ "attn.to_k(hidden_states)",+ "attn.to_v(hidden_states)",+ "attn.norm_q(query)",+ "attn.norm_k(key)",+ "apply_rotary_emb(",+ "dispatch_attention_fn(",+ "attn.to_out[0](hidden_states)",+)+_FFN_TOKENS = ("self.w1(x)", "self.w3(x)", "self.w2(", "silu")+_ATTN_LINEARS = ("to_q", "to_k", "to_v")+_SOURCE_OK: dict[str, bool] = {}+++def mps_fp16_requested() -> bool:+ return (os.environ.get(MPS_FP16_ENV) or "").strip().lower() in ("1", "on", "true", "yes")+++def _source_ok(key: str, obj: Any, tokens: tuple[str, ...]) -> bool:+ if key not in _SOURCE_OK:+ try:+ import inspect+ src = inspect.getsource(obj)+ _SOURCE_OK[key] = all(tok in src for tok in tokens)+ except Exception: # noqa: BLE001 - unreadable source: keep stock bf16+ _SOURCE_OK[key] = False+ return _SOURCE_OK[key]+++def _compute_dtype() -> Any:+ import torch+ return getattr(torch, COMPUTE_DTYPE)+++def _to_stream(x: Any, dtype: Any, headroom: float) -> Any:+ # Clamp so a stray overflow saturates one value instead of sending inf into the stream.+ return x.clamp_(-_FP16_MAX, _FP16_MAX).to(dtype).mul_(headroom)+++def _ffn_forward(self, x):+ import torch.nn.functional as F++ dtype = x.dtype+ h = x.to(_compute_dtype())+ gate = getattr(self, "_forward_silu_gating", None)+ up = self.w3(h) * (1.0 / FFN_HEADROOM)+ h = gate(self.w1(h), up) if callable(gate) else F.silu(self.w1(h)) * up+ return _to_stream(self.w2(h), dtype, FFN_HEADROOM)+++_PROCESSOR: Optional[type] = None+++def processor_cls() -> type:+ global _PROCESSOR+ if _PROCESSOR is not None:+ return _PROCESSOR+ from diffusers.models.transformers import transformer_z_image as zmod++ class ZImageMpsFp16AttnProcessor(zmod.ZSingleStreamAttnProcessor):+ def __call__(+ self,+ attn,+ hidden_states,+ encoder_hidden_states = None,+ attention_mask = None,+ freqs_cis = None,+ ):+ dtype = hidden_states.dtype+ x = hidden_states.to(_compute_dtype())+ query = attn.to_q(x).unflatten(-1, (attn.heads, -1))+ key = attn.to_k(x).unflatten(-1, (attn.heads, -1))+ value = (attn.to_v(x) * (1.0 / ATTN_HEADROOM)).unflatten(-1, (attn.heads, -1))+ if attn.norm_q is not None:+ query = attn.norm_q(query)+ if attn.norm_k is not None:+ key = attn.norm_k(key)+ if freqs_cis is not None:+ query = _rope(query, freqs_cis)+ key = _rope(key, freqs_cis)+ # A bf16 qk-norm weight (a LoRA or load that bypassed the cast) must not mix dtypes in the kernel.+ query, key = query.to(value.dtype), key.to(value.dtype)+ if attention_mask is not None and attention_mask.ndim == 2:+ attention_mask = attention_mask[:, None, None, :]+ out = zmod.dispatch_attention_fn(+ query,+ key,+ value,+ attn_mask = attention_mask,+ dropout_p = 0.0,+ is_causal = False,+ backend = self._attention_backend,+ parallel_config = self._parallel_config,+ )+ out = attn.to_out[0](out.flatten(2, 3).to(value.dtype))+ if len(attn.to_out) > 1:+ out = attn.to_out[1](out)+ return _to_stream(out, dtype, ATTN_HEADROOM)++ _PROCESSOR = ZImageMpsFp16AttnProcessor+ return _PROCESSOR+++def _rope(x_in: Any, freqs_cis: Any) -> Any:+ import torch+ x = torch.view_as_complex(x_in.float().reshape(*x_in.shape[:-1], -1, 2))+ return torch.view_as_real(x * freqs_cis.unsqueeze(2)).flatten(3).type_as(x_in)+++def _is_native_quant(mod: Any) -> bool:+ return type(mod).__name__ == "NativeWeightOnlyLinear" and hasattr(mod, "weight_q")+++def _castable(mod: Any) -> bool:+ # Quantized storage (GGUF, torchao, bnb) is refused: casting it to fp16 would dequantize or corrupt it.+ import torch++ if mod is None:+ return False+ if _is_native_quant(mod):+ return getattr(mod, "scheme", "int8") == "int8" and not getattr(mod, "act_int8", False)+ for sub in mod.modules():+ if _is_native_quant(sub) or hasattr(sub, "compute_dtype"):+ return False+ for t in (*mod.parameters(), *mod.buffers()):+ if type(t.data) is not torch.Tensor or not t.is_floating_point():+ return False+ if t.dtype not in (torch.bfloat16, torch.float16, torch.float32):+ return False+ return True+++def _cast_compute(mod: Any) -> None:+ if _is_native_quant(mod):+ return+ dtype = _compute_dtype()+ for t in (*mod.parameters(), *mod.buffers()):+ if t.dtype is not dtype:+ t.data = t.data.to(dtype)+++def _block_targets(block: Any) -> Optional[tuple[Any, Any, list]]:+ attn = getattr(block, "attention", None)+ ffn = getattr(block, "feed_forward", None)+ if attn is None or ffn is None:+ return None+ proc = getattr(attn, "processor", None)+ if type(proc).__name__ != _PROCESSOR_CLS and block.__dict__.get(_MARK) is None:+ return None+ to_out = getattr(attn, "to_out", None)+ leaves = [getattr(attn, n, None) for n in _ATTN_LINEARS]+ leaves += [to_out[0] if to_out is not None and len(to_out) else None]+ leaves += [getattr(ffn, n, None) for n in ("w1", "w2", "w3")]+ # The headroom is exact only through bias-free projections.+ if any(m is None or getattr(m, "bias", None) is not None for m in leaves):+ return None+ norms = [+ n for n in (getattr(attn, "norm_q", None), getattr(attn, "norm_k", None)) if n is not None+ ]+ return attn, ffn, leaves + norms+++def unsupported_reason(root: Any, target: Any) -> Optional[str]:+ if not mps_fp16_requested():+ return f"{MPS_FP16_ENV} not set"+ if getattr(target, "device", None) != "mps":+ return "not an MPS device"+ try:+ import torch+ except Exception: # noqa: BLE001+ return "torch unavailable"+ if getattr(target, "dtype", None) is not torch.bfloat16:+ return "stream dtype is not bfloat16"+ if root is None or type(root).__name__ not in _SUPPORTED_TRANSFORMERS:+ return "transformer is not Z-Image"+ try:+ from diffusers.models.transformers import transformer_z_image as zmod+ except Exception: # noqa: BLE001+ return "diffusers has no Z-Image transformer"+ if not _source_ok("processor", zmod.ZSingleStreamAttnProcessor.__call__, _PROCESSOR_TOKENS):+ return "installed diffusers Z-Image attention changed"+ if not _source_ok("ffn", zmod.FeedForward, _FFN_TOKENS):+ return "installed diffusers Z-Image feed-forward changed"+ blocks = 0+ for _name, block in root.named_modules():+ if not (hasattr(block, "attention") and hasattr(block, "feed_forward")):+ continue+ found = _block_targets(block)+ if found is None:+ return f"block {_name} does not match the Z-Image layout"+ if not all(_castable(m) for m in found[2]):+ return f"block {_name} holds quantized or non-float weights"+ blocks += 1+ if not blocks:+ return "no Z-Image block found"+ return None+++def install_mps_fp16(+ root: Any,+ target: Any,+ logger: Any = None,+) -> int:+ # All or nothing: one mismatched block keeps the whole model bf16.+ reason = unsupported_reason(root, target)+ if reason is not None:+ if logger is not None and mps_fp16_requested():+ logger.info("diffusion.mps_fp16: not engaged (%s)", reason)+ return 0+ cls = processor_cls()+ blocks = 0+ for _name, block in root.named_modules():+ if not (hasattr(block, "attention") and hasattr(block, "feed_forward")):+ continue+ attn, ffn, leaves = _block_targets(block)+ if block.__dict__.get(_MARK) is None:+ dtypes = {id(t): t.dtype for m in leaves for t in (*m.parameters(), *m.buffers())}+ for m in leaves:+ _cast_compute(m)+ old = attn.processor+ new = cls()+ new._attention_backend = getattr(old, "_attention_backend", None)+ new._parallel_config = getattr(old, "_parallel_config", None)+ attn.set_processor(new)+ ffn.forward = types.MethodType(_ffn_forward, ffn)+ block.__dict__[_MARK] = (old, leaves, dtypes)+ blocks += 1+ if logger is not None:+ logger.info(+ "diffusion.mps_fp16: blocks=%d (attention + FFN in float16 with x%g / x%g headroom; residual stream, "+ "norms and modulation bf16)",+ blocks,+ ATTN_HEADROOM,+ FFN_HEADROOM,+ )+ root.__dict__[_MARK] = blocks+ return blocks+++def is_installed(root: Any) -> bool:+ return bool(root is not None and root.__dict__.get(_MARK))+++def remove_mps_fp16(root: Any) -> int:+ if root is None:+ return 0+ removed = 0+ for _name, block in root.named_modules():+ mark = block.__dict__.pop(_MARK, None)+ if mark is None or _name == "":+ continue+ old, leaves, dtypes = mark+ block.attention.set_processor(old)+ block.feed_forward.__dict__.pop("forward", None)+ for m in leaves:+ if _is_native_quant(m):+ continue+ for t in (*m.parameters(), *m.buffers()):+ want = dtypes.get(id(t))+ if want is not None and t.dtype is not want:+ t.data = t.data.to(want)+ removed += 1+ root.__dict__.pop(_MARK, None)+ return removeddiff --git a/studio/backend/tests/test_diffusion_mps_fp16.py b/studio/backend/tests/test_diffusion_mps_fp16.pynew file mode 100644index 00000000000..4e148ff0d64--- /dev/null+++ b/studio/backend/tests/test_diffusion_mps_fp16.py@@ -0,0 +1,388 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""MPS fp16-headroom compute for Z-Image: engages only on MPS + bf16 + the env opt-in, the headroom identities are exact+(fp32 compute reproduces the stock forward bit for bit), fp16 compute stays finite where plain fp16 overflows, and+CUDA / CPU targets are never touched. Real diffusers Z-Image modules on CPU."""++from __future__ import annotations++import types++import pytest++torch = pytest.importorskip("torch")+zmod = pytest.importorskip("diffusers.models.transformers.transformer_z_image")+nn = torch.nn++from core.inference import diffusion_mps_fp16 as mf # noqa: E402++MPS_BF16 = types.SimpleNamespace(device = "mps", dtype = torch.bfloat16)+CFG = dict(+ all_patch_size = (2,),+ all_f_patch_size = (1,),+ in_channels = 16,+ dim = 128,+ n_layers = 2,+ n_refiner_layers = 1,+ n_heads = 4,+ n_kv_heads = 4,+ norm_eps = 1e-5,+ qk_norm = True,+ cap_feat_dim = 32,+ rope_theta = 256.0,+ t_scale = 1000.0,+ axes_dims = [8, 12, 12],+ axes_lens = [256, 64, 64],+)+++def _fp16_ok():+ try:+ nn.Linear(4, 4).half()(torch.ones(1, 4, dtype = torch.float16))+ return True+ except Exception: # noqa: BLE001+ return False+++needs_fp16 = pytest.mark.skipif(not _fp16_ok(), reason = "this torch has no CPU float16 matmul")++[email protected](autouse = True)+def _opt_in(monkeypatch):+ monkeypatch.setenv(mf.MPS_FP16_ENV, "1")+++def _model(dtype = torch.bfloat16, seed = 0):+ torch.manual_seed(seed)+ model = zmod.ZImageTransformer2DModel(**CFG).eval()+ with torch.no_grad():+ for p in model.parameters():+ if p.dim() == 2:+ p.normal_(0, 0.05)+ return model.to(dtype)+++def _inputs(dtype):+ g = torch.Generator().manual_seed(1)+ x = torch.randn(16, 1, 16, 16, generator = g).to(dtype)+ cap = torch.randn(24, 32, generator = g).to(dtype)+ return [x], torch.tensor([0.5]).to(dtype), [cap]+++def _run(model, dtype):+ x, t, cap = _inputs(dtype)+ with torch.no_grad():+ out = model(x, t, cap, return_dict = False)[0]+ return (out[0] if isinstance(out, (list, tuple)) else out).float()+++def _snapshot(model):+ procs = [type(m.processor) for m in model.modules() if hasattr(m, "processor")]+ ffn = [("forward" in m.__dict__) for m in model.modules() if isinstance(m, zmod.FeedForward)]+ sd = {k: (v.dtype, v.clone()) for k, v in model.state_dict().items()}+ return procs, ffn, sd+++def _same(a, b):+ assert a[0] == b[0] and a[1] == b[1]+ assert a[2].keys() == b[2].keys()+ for k in a[2]:+ assert a[2][k][0] == b[2][k][0], k+ assert torch.equal(a[2][k][1], b[2][k][1]), k+++ "target",+ [+ types.SimpleNamespace(device = "cuda", dtype = torch.bfloat16),+ types.SimpleNamespace(device = "cpu", dtype = torch.bfloat16),+ types.SimpleNamespace(device = "xpu", dtype = torch.bfloat16),+ types.SimpleNamespace(device = "mps", dtype = torch.float32),+ types.SimpleNamespace(device = "mps", dtype = torch.float16),+ ],+)+def test_never_engages_off_mps_bf16(target):+ model = _model()+ before = _snapshot(model)+ assert mf.unsupported_reason(model, target) is not None+ assert mf.install_mps_fp16(model, target) == 0+ _same(before, _snapshot(model))+ assert not mf.is_installed(model)++[email protected]("value", [None, "", "0", "off", "false"])+def test_needs_the_env_opt_in(monkeypatch, value):+ if value is None:+ monkeypatch.delenv(mf.MPS_FP16_ENV, raising = False)+ else:+ monkeypatch.setenv(mf.MPS_FP16_ENV, value)+ model = _model()+ before = _snapshot(model)+ assert mf.install_mps_fp16(model, MPS_BF16) == 0+ _same(before, _snapshot(model))+++def test_other_transformers_are_never_patched():+ root = nn.Sequential(nn.Linear(8, 8).to(torch.bfloat16))+ assert mf.unsupported_reason(root, MPS_BF16) == "transformer is not Z-Image"+ assert mf.install_mps_fp16(root, MPS_BF16) == 0+++def test_engages_on_mps_bf16_and_keeps_the_stream_bf16():+ model = _model()+ n_blocks = CFG["n_layers"] + 2 * CFG["n_refiner_layers"]+ assert mf.unsupported_reason(model, MPS_BF16) is None+ assert mf.install_mps_fp16(model, MPS_BF16) == n_blocks+ assert mf.install_mps_fp16(model, MPS_BF16) == n_blocks # idempotent+ assert mf.is_installed(model)+ assert model.dtype is torch.bfloat16+ fp16 = (+ "attention.to_q",+ "attention.to_k",+ "attention.to_v",+ "attention.to_out.0",+ "attention.norm_q",+ "attention.norm_k",+ "feed_forward.w1",+ "feed_forward.w2",+ "feed_forward.w3",+ )+ for name, p in model.named_parameters():+ want = torch.float16 if any(f"{s}." in name for s in fp16) else torch.bfloat16+ assert p.dtype is want, name+ blk = model.layers[0]+ assert type(blk.attention.processor) is mf.processor_cls()+ assert "forward" in blk.feed_forward.__dict__+++def test_remove_restores_the_stock_model():+ model = _model()+ before = _snapshot(model)+ assert mf.install_mps_fp16(model, MPS_BF16) > 0+ assert mf.remove_mps_fp16(model) == CFG["n_layers"] + 2 * CFG["n_refiner_layers"]+ after = _snapshot(model)+ assert before[0] == after[0] and before[1] == after[1] and not mf.is_installed(model)+ for k, (dtype, t) in before[2].items():+ assert after[2][k][0] == dtype, k+ # bf16 -> fp16 -> bf16 is exact except below fp16's normal range (|w| < 2**-14), off by < 2**-24 there.+ assert (after[2][k][1].float() - t.float()).abs().max() <= 2**-24, k+++def test_attention_backend_survives_the_processor_swap():+ model = _model()+ for m in model.modules():+ if hasattr(m, "processor"):+ m.processor._attention_backend = "native"+ mf.install_mps_fp16(model, MPS_BF16)+ procs = [m.processor for m in model.modules() if hasattr(m, "processor")]+ assert procs and all(p._attention_backend == "native" for p in procs)+++def test_quantized_storage_keeps_the_whole_model_bf16():+ """GGUF / torchao / bnb leaves would get fp16 activations against bf16 dequant: refuse the model, all or nothing."""+ model = _model()++ class _Gguf(nn.Linear):+ compute_dtype = torch.bfloat16++ old = model.layers[1].feed_forward.w2+ new = _Gguf(old.in_features, old.out_features, bias = False).to(torch.bfloat16)+ model.layers[1].feed_forward.w2 = new+ before = _snapshot(model)+ assert "quantized" in mf.unsupported_reason(model, MPS_BF16)+ assert mf.install_mps_fp16(model, MPS_BF16) == 0+ _same(before, _snapshot(model))+++def test_diffusers_source_matches_the_reimplemented_attention():+ mf._SOURCE_OK.clear()+ assert mf._source_ok(+ "processor", zmod.ZSingleStreamAttnProcessor.__call__, mf._PROCESSOR_TOKENS+ )+ assert mf._source_ok("ffn", zmod.FeedForward, mf._FFN_TOKENS)+++def test_changed_diffusers_attention_keeps_bf16(monkeypatch):+ monkeypatch.setattr(mf, "_SOURCE_OK", {"processor": False})+ model = _model()+ assert mf.install_mps_fp16(model, MPS_BF16) == 0+++def test_headroom_identities_are_exact_in_fp32(monkeypatch):+ """Power-of-two headroom is exact: fp32 compute reproduces the stock model bit for bit."""+ monkeypatch.setattr(mf, "COMPUTE_DTYPE", "float32")+ model = _model(torch.float32)+ ref = _run(model, torch.float32)+ assert mf.install_mps_fp16(model, MPS_BF16) > 0+ assert torch.equal(_run(model, torch.float32), ref)+++def test_ffn_headroom_identity_exact_with_large_values(monkeypatch):+ monkeypatch.setattr(mf, "COMPUTE_DTYPE", "float32")+ torch.manual_seed(0)+ ffn = zmod.FeedForward(64, 160)+ with torch.no_grad():+ ffn.w1.weight.mul_(40.0)+ ffn.w3.weight.mul_(40.0)+ ffn.w2.weight.mul_(400.0)+ x = torch.randn(3, 9, 64)+ ref = ffn(x)+ assert ref.abs().max() > 65504+ patched = mf._ffn_forward(ffn, x)+ assert torch.equal(patched, ref)+++def _overflowing(model):+ with torch.no_grad():+ for blk in (*model.layers, *model.noise_refiner, *model.context_refiner):+ blk.feed_forward.w1.weight.mul_(30.0)+ blk.feed_forward.w3.weight.mul_(30.0)+ blk.feed_forward.w2.weight.mul_(300.0)+ blk.attention.to_v.weight.mul_(200.0)+ blk.attention.to_out[0].weight.mul_(200.0)+ return model+++@needs_fp16+def test_fp16_compute_finite_where_plain_fp16_overflows():+ ref = _run(_overflowing(_model(torch.float32)), torch.float32)+ # The branch outputs exceed fp16's range (plain fp16 is not finite) ...+ blk = _overflowing(_model(torch.float32)).layers[0]+ with torch.no_grad():+ branch = blk.feed_forward(torch.randn(1, 64, CFG["dim"]))+ assert branch.abs().max() > 65504+ plain = _run(_overflowing(_model(torch.float32)).half(), torch.float16)+ assert not torch.isfinite(plain).all()+ # ... and the headroom keeps them in range, as close to fp32 as the bf16 model is.+ bf16 = _run(_overflowing(_model(torch.float32)).to(torch.bfloat16), torch.bfloat16)+ model = _overflowing(_model(torch.float32)).to(torch.bfloat16)+ assert mf.install_mps_fp16(model, MPS_BF16) > 0+ got = _run(model, torch.bfloat16)+ assert torch.isfinite(got).all()+ rel = float((got - ref).norm() / ref.norm())+ rel_bf16 = float((bf16 - ref).norm() / ref.norm())+ assert rel < max(2 * rel_bf16, 1e-2), (rel, rel_bf16)+++@needs_fp16+def test_attention_processor_matches_stock_in_fp32_and_fp16(monkeypatch):+ torch.manual_seed(0)+ blk = zmod.ZImageTransformerBlock(0, 64, 4, 4, 1e-5, True, modulation = False).float()+ with torch.no_grad():+ blk.attention.to_v.weight.mul_(300.0)+ blk.attention.to_out[0].weight.mul_(600.0)+ attn = blk.attention+ x = torch.randn(2, 12, 64)+ freqs = torch.polar(torch.ones(2, 12, 8), torch.randn(2, 12, 8))+ with torch.no_grad():+ ref = attn(x, freqs_cis = freqs)+ assert ref.abs().max() > 65504+ monkeypatch.setattr(mf, "COMPUTE_DTYPE", "float32")+ proc = mf.processor_cls()()+ assert torch.equal(proc(attn, x, freqs_cis = freqs), ref)+ monkeypatch.setattr(mf, "COMPUTE_DTYPE", "float16")+ attn.half()+ got = proc(attn, x.to(torch.bfloat16), freqs_cis = freqs).float()+ assert got.dtype is torch.float32 and torch.isfinite(got).all()+ assert float((got - ref).norm() / ref.norm()) < 1e-2+++def test_ffn_output_saturates_instead_of_inf(monkeypatch):+ """A value past the headroom saturates at 65504 x headroom in the bf16 stream instead of injecting inf."""+ ffn = zmod.FeedForward(8, 16).to(torch.float16)+ with torch.no_grad():+ ffn.w1.weight.fill_(1.0)+ ffn.w3.weight.fill_(1.0)+ ffn.w2.weight.fill_(1e4)+ out = (+ mf._ffn_forward(ffn, torch.full((1, 8), 30.0, dtype = torch.bfloat16))+ if _fp16_ok()+ else None+ )+ if out is None:+ pytest.skip("this torch has no CPU float16 matmul")+ assert out.dtype is torch.bfloat16 and torch.isfinite(out).all()+ # 65504 rounds to 65536 in bf16.+ assert (+ float(out.abs().max()) == float(torch.tensor(65504.0).to(torch.bfloat16)) * mf.FFN_HEADROOM+ )+++class NativeWeightOnlyLinear(nn.Module):+ def __init__(self, lin):+ super().__init__()+ w = lin.weight.detach().float()+ scale = w.abs().amax(dim = 1).clamp(min = 1e-12) / 127.0+ self.register_buffer(+ "weight_q", (w / scale[:, None]).round().clamp(-127, 127).to(torch.int8)+ )+ self.register_buffer("weight_scale", scale)+ self.scheme, self.act_int8, self.bias, self.seen = "int8", False, None, []++ def forward(self, x):+ self.seen.append(x.dtype)+ w = self.weight_q.float() * self.weight_scale[:, None]+ return nn.functional.linear(x.float(), w).to(x.dtype)+++@needs_fp16+def test_int8_weight_only_layers_keep_storage_and_get_fp16_activations():+ model = _model()+ q = []+ for blk in (*model.layers, *model.noise_refiner, *model.context_refiner):+ for parent, names in (+ (blk.attention, ("to_q", "to_k", "to_v")),+ (blk.feed_forward, ("w1", "w2", "w3")),+ ):+ for n in names:+ layer = NativeWeightOnlyLinear(getattr(parent, n))+ setattr(parent, n, layer)+ q.append(layer)+ assert mf.install_mps_fp16(model, MPS_BF16) > 0+ assert all(+ layer.weight_q.dtype is torch.int8 and layer.weight_scale.dtype is torch.float32+ for layer in q+ )+ out = _run(model, torch.bfloat16)+ assert torch.isfinite(out).all()+ assert q[0].seen and all(d is torch.float16 for layer in q for d in layer.seen)+++class _LoRALinear(nn.Module):+ def __init__(self, base):+ super().__init__()+ self.base = base+ self.lora_A = nn.Linear(base.in_features, 4, bias = False).to(base.weight.dtype)+ self.lora_B = nn.Linear(4, base.out_features, bias = False).to(base.weight.dtype)++ def forward(self, x):+ return self.base(x) + self.lora_B(self.lora_A(x))+++@needs_fp16+def test_lora_injected_after_install_rides_the_headroom():+ """Projections replaced after install (LoRA) are still called at run time and scaled as a whole."""++ def adapt(model):+ torch.manual_seed(1)+ for name, m in list(model.named_modules()):+ if isinstance(m, nn.Linear) and name.rsplit(".", 1)[-1] in (+ "w1",+ "w2",+ "w3",+ "to_q",+ "to_k",+ "to_v",+ ):+ parent, _, child = name.rpartition(".")+ setattr(model.get_submodule(parent), child, _LoRALinear(m))+ return model++ ref = _run(adapt(_overflowing(_model(torch.float32))), torch.float32)+ model = _overflowing(_model(torch.float32)).to(torch.bfloat16)+ assert mf.install_mps_fp16(model, MPS_BF16) > 0+ adapt(model)+ got = _run(model, torch.bfloat16)+ assert torch.isfinite(got).all()+ assert float((got - ref).norm() / ref.norm()) < 2e-2