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: load the hosted INT8 pre-quant checkpoints on torchao 0.18 and later (#11884)
Daniel Han4786affa6702
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex 7b3842e2410..bf4321e8d0c 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -185,6 +185,7 @@ from .diffusion_prequant import (hosted_fast_accum_conflict,load_prequantized_transformer,prequant_checkpoint_cached,+ prequant_unreadable_reason,resolve_prequant_source,usable_prequant_source,)@@ -1499,6 +1500,25 @@ def _clear_exception_frames(exc: BaseException) -> None:errors.extend(cause for cause in (error.__cause__, error.__context__) if cause is not None)+def _dense_fast_path_reason(+ fam: Any,+ scheme: Optional[str],+ base: Optional[str],+ kind: str,+ path_override: Optional[str],+ loras: Any = None,+) -> str:+ """Name an unreadable hosted checkpoint only when it was in play (not override/GGUF/LoRA bake)."""+ note = (+ prequant_unreadable_reason(fam, scheme, base_repo = base)+ if kind == "pipeline" and not path_override and not _has_active_lora(loras)+ else None+ )+ if note:+ return f"engaged on the dense fast path; {note}, so the dense bf16 transformer was quantized instead"+ return "engaged on the dense fast path"++class DiffusionBackend:"""Holds at most one loaded diffusers pipeline. All mutations are serialised."""@@ -5723,7 +5743,14 @@ class DiffusionBackend:if transformer_quant_artifact is not Noneelse "re-planned resident for the quantised artifact"if quant_plan is not None- else "engaged on the dense fast path",+ else _dense_fast_path_reason(+ fam,+ transformer_quant_engaged,+ base,+ kind,+ transformer_prequant_path,+ loras,+ ),# Honored when the quant engaged AND when the ask was "off" (a request NOT to# quantise, which the GGUF build satisfies)RESOLVED_APPLIEDdiff --git a/studio/backend/core/inference/diffusion_prequant.py b/studio/backend/core/inference/diffusion_prequant.pyindex 61c369603dc..ce44c4dcf09 100644--- a/studio/backend/core/inference/diffusion_prequant.py+++ b/studio/backend/core/inference/diffusion_prequant.py@@ -269,8 +269,15 @@ def restricted_prequant_load_supported(return safetensors_prequant_supported()if not _register_prequant_safe_globals():return False- required = _SCHEME_REQUIRED_GLOBALS.get((scheme or "").strip().lower())- return True if required is None else required <= _RESOLVED_SAFE_GLOBALS+ key = (scheme or "").strip().lower()+ required = _SCHEME_REQUIRED_GLOBALS.get(key)+ if required is None or required <= _RESOLVED_SAFE_GLOBALS:+ return True+ if key == "int8":+ # torchao 0.18 deleted the v1 int8 classes every hosted INT8 pickle names; see prequant_legacy_int8.+ from .prequant_legacy_int8 import legacy_int8_decode_supported+ return legacy_int8_decode_supported()+ return Falsedef _torch_load_prequant(path: str, **kwargs: Any) -> Any:@@ -289,7 +296,20 @@ def _torch_load_prequant(path: str, **kwargs: Any) -> Any:"a pre-quant checkpoint cannot be deserialized without allowing arbitrary pickle ""globals")- return torch.load(path, weights_only = True, **kwargs)+ from .prequant_legacy_int8 import (+ legacy_int8_decode_supported,+ load_legacy_int8_pickle,+ names_legacy_int8_class,+ rebuild_stray_standins,+ )++ try:+ ckpt = torch.load(path, weights_only = True, **kwargs)+ except Exception as exc: # noqa: BLE001 - only the deleted-int8-classes refusal is retried+ if names_legacy_int8_class(exc) and legacy_int8_decode_supported():+ return load_legacy_int8_pickle(path, **kwargs)+ raise+ return rebuild_stray_standins(ckpt)def _load_prequant_checkpoint(path: str, **kwargs: Any) -> Any:@@ -794,6 +814,7 @@ def load_prequantized_transformer(checkpoint, unsupported meta-init, or a rotation this build cannot apply exactly) so the callerfalls back to dense-quantise. Best-effort: never raises for an unavailable artifact."""+ _LAST_FAILURE.text = Nonetry:# A request-supplied local path names arbitrary WEIGHTS, a different question from the deserialization one# below: allowlisted or not, the file is read weights_only.@@ -1387,6 +1408,76 @@ def _has_meta_tensors(module: Any) -> bool:return False+_LAST_FAILURE = _threading.local()++def _warn(logger: Any, what: str, exc: Exception) -> None:+ _LAST_FAILURE.text = f"{type(exc).__name__}: {exc}"[:300]if logger is not None:logger.warning("diffusion.prequant: %s failed: %s", what, exc)+++def last_prequant_failure() -> Optional[str]:+ text = getattr(_LAST_FAILURE, "text", None)+ _LAST_FAILURE.text = None+ return text+++def _unreadable_why(scheme: str) -> str:+ try:+ import torch+ import torchao+ ao = getattr(torchao, "__version__", "unknown")+ except Exception: # noqa: BLE001+ return "torch or torchao is unavailable"+ if not _tuple_safe_globals_supported():+ return f"torch {torch.__version__} is older than 2.6"+ try:+ from core._torchao_stub import is_stubbed+ if is_stubbed("torchao"):+ return "torchao is not available on this platform"+ except Exception: # noqa: BLE001+ pass+ missing = _SCHEME_REQUIRED_GLOBALS.get(scheme, frozenset()) - _RESOLVED_SAFE_GLOBALS+ if missing:+ return (+ f"torchao {ao} no longer ships {len(missing)} class(es) the checkpoint was saved with"+ )+ return f"torch {torch.__version__} / torchao {ao} cannot deserialize it"+++def prequant_unreadable_reason(+ fam: Any,+ scheme: Optional[str],+ *,+ base_repo: Optional[str] = None,+ task: Optional[str] = None,+) -> Optional[str]:+ """Status line when ``fam`` hosts a ``scheme`` checkpoint this install cannot open; never raises."""+ if not scheme:+ return None+ try:+ src = resolve_prequant_source(fam, scheme, base_repo = base_repo, task = task)+ if src is None or getattr(src, "kind", None) != "repo":+ return None+ from .prequant_safetensors import is_safetensors_checkpoint++ readable = [+ n for n in candidate_filenames_of(src) if restricted_prequant_load_supported(scheme, n)+ ]+ # A derived safetensors name is a guess, as in usable_prequant_source: only a declared or cached one counts.+ if readable and all(is_safetensors_checkpoint(n) for n in readable):+ declared = set(getattr(src, "declared_filenames", ()) or ())+ if (+ not declared.intersection(readable)+ and cached_checkpoint_path(src, names = readable) is None+ ):+ readable = []+ if readable:+ return None+ return (+ f"the hosted {scheme} checkpoint ({src.location}) cannot be read here: "+ f"{_unreadable_why(scheme)}"+ )+ except Exception: # noqa: BLE001 - a status nicety must never break a load+ return Nonediff --git a/studio/backend/core/inference/prequant_legacy_int8.py b/studio/backend/core/inference/prequant_legacy_int8.pynew file mode 100644index 00000000000..d3eb42d247d--- /dev/null+++ b/studio/backend/core/inference/prequant_legacy_int8.py@@ -0,0 +1,252 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Read hosted INT8 ``.pt`` pre-quants on torchao >= 0.18, which deleted the v1 int8 classes they pickle.++Still ``weights_only = True``: inert stand-ins (dispatch raises) are registered under the deleted names+only for that load, and each weight is validated as symmetric per-row int8, then rebuilt bit-identical+as the installed torchao's ``Int8Tensor``. A torchao that still ships the classes loads as before.+"""++from __future__ import annotations++import enum+import threading+from typing import Any, Optional++_LAQT = "torchao.quantization.linear_activation_quantized_tensor.LinearActivationQuantizedTensor"+_AQT = "torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor"+_IMPL = "torchao.dtypes.uintx.plain_layout.PlainAQTTensorImpl"+_LAYOUT = "torchao.dtypes.utils.PlainLayout"+_ACT_QUANT = "torchao.quantization.quant_api._int8_symm_per_token_reduced_range_quant"+_ZERO_POINT_DOMAIN = "torchao.quantization.quant_primitives.ZeroPointDomain"++# All or nothing: a partial torchao would mix real and stand-in objects.+LEGACY_INT8_CLASS_NAMES = (_LAQT, _AQT, _IMPL, _LAYOUT)+++def _resolve(name: str) -> Any:+ import importlib+ module, _, attr = name.rpartition(".")+ try:+ return getattr(importlib.import_module(module), attr)+ except Exception: # noqa: BLE001 - absent module or attribute both mean "not shipped"+ return None+++def legacy_int8_classes_missing() -> bool:+ return all(_resolve(n) is None for n in LEGACY_INT8_CLASS_NAMES)+++def names_legacy_int8_class(exc: BaseException) -> bool:+ text = str(exc)+ return any(name in text for name in (*LEGACY_INT8_CLASS_NAMES, _ACT_QUANT))+++def _int8_tensor_api() -> Optional[tuple]:+ try:+ from core._torchao_stub import is_stubbed+ if is_stubbed("torchao"):+ return None+ except Exception: # noqa: BLE001 - no stub module means nothing is stubbed+ pass+ try:+ from torchao.quantization import PerRow+ from torchao.quantization.quantize_.workflows.int8.int8_tensor import (+ Int8Tensor,+ QuantizeTensorToInt8Kwargs,+ )+ except Exception: # noqa: BLE001+ return None+ return Int8Tensor, QuantizeTensorToInt8Kwargs, PerRow+++_SUPPORTED: Optional[bool] = None+++def legacy_int8_decode_supported() -> bool:+ global _SUPPORTED+ if _SUPPORTED is not None:+ return _SUPPORTED+ ok = False+ try:+ import torch++ parts = str(torch.__version__).split("+")[0].split(".")+ torch_ok = (int(parts[0]), int(parts[1])) >= (2, 6)+ api = _int8_tensor_api()+ if torch_ok and api is not None and legacy_int8_classes_missing():+ # Probe the constructor now, not after a plan has dropped the dense shards.+ _to_int8_tensor(+ torch.zeros(2, 4, dtype = torch.int8),+ torch.ones(2, dtype = torch.bfloat16),+ torch.bfloat16,+ api,+ )+ ok = True+ except Exception: # noqa: BLE001+ ok = False+ _SUPPORTED = ok+ return ok+++_STANDINS: Optional[tuple] = None+_STANDINS_LOCK = threading.Lock()+++def _standins() -> dict:+ """Keyed per torch module: tests swap ``sys.modules["torch"]``."""+ global _STANDINS+ import torch++ cached = _STANDINS+ if cached is not None and cached[0] is torch:+ return cached[1]+ with _STANDINS_LOCK:+ if _STANDINS is not None and _STANDINS[0] is torch:+ return _STANDINS[1]++ class _Inert(torch.Tensor):+ # _rebuild_wrapper_subclass needs a dispatch hook to exist; any op reaching one is a bug.+ __torch_function__ = torch._C._disabled_torch_function_impl++ @classmethod+ def __torch_dispatch__(+ cls,+ func,+ types,+ args = (),+ kwargs = None,+ ):+ raise RuntimeError(f"legacy int8 stand-in cannot run {func}")++ class LegacyLinearActivationQuantized(_Inert):+ pass++ class LegacyAffineQuantized(_Inert):+ pass++ class LegacyPlainImpl(_Inert):+ pass++ class LegacyPlainLayout:+ pass++ def legacy_int8_act_quant(*_args: Any, **_kwargs: Any) -> Any:+ raise RuntimeError("legacy int8 activation quant stand-in is never called")++ class LegacyZeroPointDomain(enum.Enum):+ INT = 1+ FLOAT = 2+ NONE = 3++ standins = {+ _LAQT: LegacyLinearActivationQuantized,+ _AQT: LegacyAffineQuantized,+ _IMPL: LegacyPlainImpl,+ _LAYOUT: LegacyPlainLayout,+ _ACT_QUANT: legacy_int8_act_quant,+ _ZERO_POINT_DOMAIN: LegacyZeroPointDomain,+ }+ _STANDINS = (torch, standins)+ return standins+++def _to_int8_tensor(qdata: Any, scale: Any, dtype: Any, api: tuple) -> Any:+ Int8Tensor, QuantizeTensorToInt8Kwargs, PerRow = api+ n, k = qdata.shape+ # Scale kept in its stored dtype so dequant is bit-identical to v1.+ return Int8Tensor(+ qdata,+ scale.reshape(n, 1),+ [1, k],+ dtype,+ act_quant_kwargs = QuantizeTensorToInt8Kwargs(granularity = PerRow()),+ )+++def _rebuild_weight(name: str, w: Any, standins: dict, api: tuple) -> Any:+ import torch++ def bad(why: str) -> ValueError:+ return ValueError(f"legacy int8 weight {name!r}: {why}")++ state = getattr(w, "__dict__", {})+ act = state.get("input_quant_func")+ if (+ act is not standins[_ACT_QUANT]+ and getattr(act, "__name__", None) != _ACT_QUANT.rsplit(".", 1)[1]+ ):+ raise bad("activation quant is not the per-token int8 one")+ if state.get("quant_kwargs"):+ raise bad("unexpected activation quant kwargs")+ aqt = state.get("original_weight_tensor")+ if not isinstance(aqt, standins[_AQT]):+ raise bad("inner weight is not an AffineQuantizedTensor")+ impl = getattr(aqt, "tensor_impl", None)+ if not isinstance(impl, standins[_IMPL]):+ raise bad("inner storage is not the plain layout")+ if not isinstance(getattr(impl, "_layout", None), standins[_LAYOUT]):+ raise bad("inner layout is not PlainLayout")+ qdata = getattr(impl, "int_data", None)+ scale = getattr(impl, "scale", None)+ if type(qdata) is not torch.Tensor or qdata.dtype != torch.int8 or qdata.dim() != 2:+ raise bad("int data is not a 2-D int8 tensor")+ n, k = qdata.shape+ if tuple(w.shape) != (n, k) or tuple(aqt.shape) != (n, k):+ raise bad(f"shape mismatch {tuple(w.shape)} vs data {(n, k)}")+ if tuple(getattr(aqt, "block_size", ()) or ()) != (1, k):+ raise bad(f"block size {getattr(aqt, 'block_size', None)} is not per-row")+ if type(scale) is not torch.Tensor or not scale.is_floating_point() or scale.numel() != n:+ raise bad("scale is not one float per output row")+ zero_point = getattr(impl, "zero_point", None)+ if zero_point is not None:+ if type(zero_point) is not torch.Tensor or zero_point.device.type == "meta":+ raise bad("zero point present")+ if bool(torch.any(zero_point != 0)):+ raise bad("asymmetric (non-zero zero point)")+ domain = getattr(aqt, "zero_point_domain", None)+ if getattr(domain, "name", None) not in ("NONE", "INT"):+ raise bad(f"zero point domain {domain!r}")+ return _to_int8_tensor(qdata, scale, w.dtype, api)+++_LOAD_LOCK = threading.Lock()+++def load_legacy_int8_pickle(path: str, **kwargs: Any) -> Any:+ """Lock stops overlapping loads unregistering each other; plain loads in the window: ``rebuild_stray_standins``."""+ import torch++ api = _int8_tensor_api()+ if api is None:+ raise RuntimeError("this torchao has no Int8Tensor to rebuild legacy int8 weights into")+ standins = _standins()+ pairs = [(obj, name) for name, obj in standins.items() if _resolve(name) is None]+ with _LOAD_LOCK:+ with torch.serialization.safe_globals(pairs):+ ckpt = torch.load(path, weights_only = True, **kwargs)+ return _rebuild_standins(ckpt, standins, api)+++def rebuild_stray_standins(ckpt: Any) -> Any:+ cached = _STANDINS+ api = _int8_tensor_api() if cached is not None else None+ if cached is None or api is None:+ return ckpt+ return _rebuild_standins(ckpt, cached[1], api)+++def _rebuild_standins(ckpt: Any, standins: dict, api: tuple) -> Any:+ state_dict = ckpt.get("state_dict") if isinstance(ckpt, dict) else None+ if not isinstance(state_dict, dict):+ return ckpt+ legacy = standins[_LAQT]+ stray = (standins[_AQT], standins[_IMPL])+ for key in list(state_dict.keys()):+ value = state_dict[key]+ if isinstance(value, legacy):+ state_dict[key] = _rebuild_weight(key, value, standins, api)+ elif isinstance(value, stray):+ raise ValueError(f"legacy int8 weight {key!r} is not wrapped for activation quant")+ return ckptdiff --git a/studio/backend/core/inference/prequant_safetensors.py b/studio/backend/core/inference/prequant_safetensors.pyindex 23790cdfa24..2307fca8d0d 100644--- a/studio/backend/core/inference/prequant_safetensors.py+++ b/studio/backend/core/inference/prequant_safetensors.py@@ -101,9 +101,21 @@ def _torchao_helpers() -> Optional[tuple]:)except Exception: # noqa: BLE001 - torchao absent, too old, or the module movedreturn None+ # 0.14 has this module but no Int8Tensor: unflatten fails (KeyError '_data') after planning.+ if _version_tuple(_torchao_version()) < MIN_TORCHAO_VERSION:+ return Nonereturn flatten_tensor_state_dict, unflatten_tensor_state_dict+def _version_tuple(version: Optional[str]) -> tuple:+ """Unparseable reads as new enough (the feature import decides)."""+ try:+ parts = str(version).split("+")[0].split(".")+ return (int(parts[0]), int(parts[1]))+ except Exception: # noqa: BLE001+ return (999, 0)++def _torchao_version() -> Optional[str]:try:import torchao@@ -285,6 +297,47 @@ def _drop_field(value: Any, name: str, removed: list) -> Any:return value+def _header_without_inert_tensor_field(+ header: dict, tensors: dict, name: str, *, path: str+) -> Optional[dict]:+ import torch++ pruned = dict(header)+ victims = []+ for key, value in header.items():+ if key in (UNSLOTH_FORMAT_KEY, UNSLOTH_METADATA_KEY, UNSLOTH_ROOT_KEYS_KEY):+ continue+ try:+ parsed = json.loads(value)+ except Exception: # noqa: BLE001 - not every header entry is JSON+ continue+ names = parsed.get("_tensor_data_names") if isinstance(parsed, dict) else None+ if not isinstance(names, list) or name not in names or "." not in key:+ continue+ module_fqn, weight_name = key.rsplit(".", 1)+ flat_key = f"{module_fqn}._{weight_name}_{name}"+ tensor = tensors.get(flat_key)+ if tensor is None:+ raise ValueError(+ f"{path} lists {name!r} for {key} but has no {flat_key!r} tensor; the checkpoint "+ "is incomplete or was edited"+ )+ if bool(torch.any(tensor != 0)):+ raise ValueError(+ f"{path} records a non-zero {name!r} for {key}, which this torchao "+ f"({_torchao_version() or 'unknown'}) cannot construct. Upgrade torchao to read "+ "this checkpoint."+ )+ parsed["_tensor_data_names"] = [n for n in names if n != name]+ pruned[key] = json.dumps(parsed)+ victims.append(flat_key)+ if not victims:+ return None+ for flat_key in victims:+ tensors.pop(flat_key, None)+ return pruned++def _header_without_unconstructible_fields(unflatten: Any,tensors: Any,@@ -334,7 +387,13 @@ def _header_without_unconstructible_fields(continuepruned[key] = json.dumps(_drop_field(parsed, name, removed))if not removed:- break+ # Tensor field, e.g. 0.18's all-zero ``zero_point`` that 0.16 cannot take.+ pruned = _header_without_inert_tensor_field(header, tensors, name, path = path)+ if pruned is None:+ break+ dropped.append(name)+ header = pruned+ continuelive = [v for v in removed if v not in _INERT_FIELD_VALUES]if live:raise ValueError(diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex e9954a5450c..786dc1b210b 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -1102,6 +1102,7 @@ def _h3_auto_denoiser_scheme(base_repo: Optional[str],speed_mode: Optional[str] = None,free_reader: Any = None,+ on_unreadable: Any = None,) -> Optional[str]:"""The hosted scheme an UNSET ``transformer_quant`` resolves to, or None to keep bfloat16.@@ -1157,13 +1158,6 @@ def _h3_auto_denoiser_scheme(# Asked per (scheme, PARTITION): a partition with no hosted checkpoint has no fallback, and serving the other# partition's would generate the wrong thing.return None- from .diffusion_prequant import restricted_prequant_load_supported-- if not restricted_prequant_load_supported(H3_AUTO_FALLBACK_SCHEME):- # An install that cannot restrict the deserialization cannot open a checkpoint at all, and this runs BEFORE the- # download plan: choosing one would drop the dense denoiser shards for an artifact the loader is going to- # refuse.- return None# And the replacement has to fit BEFORE it is chosen. A torchao denoiser cannot ride the offload rotation at all (it# does not survive the mid-block move), so taking it means pinning it, which turns the memory floor from a max into# a sum: 20.3 GB resident PLUS whatever runs beside it. Under text_encoder_quant="none" that sum is larger than the@@ -1172,6 +1166,14 @@ def _h3_auto_denoiser_scheme(hosted_bytes = int(h3_transformer_resident_gb(H3_AUTO_FALLBACK_SCHEME) * 1000.0**3)if not _h3_dense_denoiser_fits((hosted_bytes, sizes[1]), free_bytes):return None+ from .diffusion_prequant import restricted_prequant_load_supported++ if not restricted_prequant_load_supported(H3_AUTO_FALLBACK_SCHEME):+ # Runs BEFORE the download plan, so never pick an artifact the loader will refuse. Asked last so+ # ``on_unreadable`` fires only when this alone kept bfloat16.+ if on_unreadable is not None:+ on_unreadable()+ return Nonereturn H3_AUTO_FALLBACK_SCHEME@@ -4797,6 +4799,7 @@ class VideoBackend:# re-deciding here against a reading that has moved could ask for a component this load can no longer open.# Otherwise decide now, against live free memory, which is the reading that describes the card once the# previous pipeline is gone (the plan only had the card's capacity to go on).+ unreadable_blocked: list = []auto_fallback_scheme = _h3_auto_denoiser_planned or _h3_auto_denoiser_scheme(fam,target = umem_target,@@ -4808,7 +4811,18 @@ class VideoBackend:task = workflow,base_repo = base,speed_mode = speed_mode,+ on_unreadable = lambda: unreadable_blocked.append(True),)+ if auto_fallback_scheme is None and unreadable_blocked:+ from .diffusion_prequant import prequant_unreadable_reason++ unreadable = prequant_unreadable_reason(+ fam, H3_AUTO_FALLBACK_SCHEME, base_repo = base, task = workflow+ ) or (+ f"the hosted {H3_AUTO_FALLBACK_SCHEME} checkpoint cannot be read by this install"+ )+ transformer_quant_reason = f"released bfloat16 components ({unreadable})"+ logger.warning("video.transformer_quant: %s", transformer_quant_reason)if auto_fallback_scheme is not None:scheme = auto_fallback_schemelogger.info(@@ -4897,9 +4911,18 @@ class VideoBackend:if transformer is None:# Best-effort by contract (missing / mismatched / unreadable checkpoint), so the load continues# dense rather than failing after the teardown.+ from .diffusion_prequant import (+ last_prequant_failure,+ prequant_unreadable_reason,+ )+ why = (+ prequant_unreadable_reason(fam, scheme, base_repo = base, task = workflow)+ or last_prequant_failure()+ )transformer_quant_reason = (- f"hosted pre-quantized {scheme} checkpoint unusable; "- f"loaded the released bfloat16 denoiser instead"+ f"hosted pre-quantized {scheme} checkpoint unusable"+ + (f" ({why})" if why else "")+ + "; loaded the released bfloat16 denoiser instead")else:# Seeding is what actually saves the download: load_components(names=None) skips every componentdiff --git a/studio/backend/tests/test_diffusion_pipeline_prequant.py b/studio/backend/tests/test_diffusion_pipeline_prequant.pyindex fcfb93a6b61..a6f4f9bb458 100644--- a/studio/backend/tests/test_diffusion_pipeline_prequant.py+++ b/studio/backend/tests/test_diffusion_pipeline_prequant.py@@ -56,6 +56,13 @@ DENOISER_BYTES = 22_000 * MIBALL_BYTES = sum(size for _name, size in Z_IMAGE_FILES)[email protected](autouse = True)+def _safetensors_readable_regardless_of_the_installed_torchao(monkeypatch):+ """Pin the torchao floor so planning tests ignore the installed release."""+ import core.inference.prequant_safetensors as prequant_safetensors+ monkeypatch.setattr(prequant_safetensors, "_torchao_version", lambda: None)++def _family():fam = detect_family_for_pick(Z_IMAGE_REPO, None, None)assert fam is not None and fam.name == "z-image"diff --git a/studio/backend/tests/test_diffusion_prequant.py b/studio/backend/tests/test_diffusion_prequant.pyindex 33bead5ccc0..39db4d73ec2 100644--- a/studio/backend/tests/test_diffusion_prequant.py+++ b/studio/backend/tests/test_diffusion_prequant.py@@ -910,6 +910,9 @@ def test_support_is_answered_per_scheme(monkeypatch):monkeypatch.setattr(pq, "_SAFE_GLOBALS_REGISTERED", None)monkeypatch.setattr(pq, "_RESOLVED_SAFE_GLOBALS", set())monkeypatch.setattr(pq, "_prequant_safe_globals", lambda: [(object(), n) for n in fp8_only])+ import core.inference.prequant_legacy_int8 as legacy_int8++ monkeypatch.setattr(legacy_int8, "legacy_int8_decode_supported", lambda: False)assert pq.restricted_prequant_load_supported("fp8") is Trueassert pq.restricted_prequant_load_supported("int8") is Falsediff --git a/studio/backend/tests/test_diffusion_te_prequant.py b/studio/backend/tests/test_diffusion_te_prequant.pyindex fc0497372a0..aabe4e9cd9d 100644--- a/studio/backend/tests/test_diffusion_te_prequant.py+++ b/studio/backend/tests/test_diffusion_te_prequant.py@@ -25,6 +25,13 @@ from core.inference.diffusion_te_prequant import ()[email protected](autouse = True)+def _safetensors_readable_regardless_of_the_installed_torchao(monkeypatch):+ """Pin the torchao floor so planning tests ignore the installed release."""+ import core.inference.prequant_safetensors as prequant_safetensors+ monkeypatch.setattr(prequant_safetensors, "_torchao_version", lambda: None)++def _fam(te_prequant_repos = (),name = "ltx-2",diff --git a/studio/backend/tests/test_prequant_legacy_int8.py b/studio/backend/tests/test_prequant_legacy_int8.pynew file mode 100644index 00000000000..24d057c52bc--- /dev/null+++ b/studio/backend/tests/test_prequant_legacy_int8.py@@ -0,0 +1,325 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Legacy v1 INT8 ``.pt`` pre-quants on torchao 0.18+; file tests need a torchao where the rebuild applies."""++from __future__ import annotations++import sys+import types++import pytest++import core.inference.diffusion_prequant as pq+import core.inference.prequant_legacy_int8 as li+++def test_only_the_deleted_int8_names_trigger_the_rebuild():+ refused = (+ "Weights only load failed ... Unsupported global: GLOBAL "+ "torchao.quantization.linear_activation_quantized_tensor.LinearActivationQuantizedTensor "+ "was not an allowed global by default."+ )+ assert li.names_legacy_int8_class(RuntimeError(refused))+ assert not li.names_legacy_int8_class(RuntimeError("Unsupported global: GLOBAL os.system"))+++def test_a_torchao_that_still_ships_the_classes_keeps_the_old_path(monkeypatch):+ monkeypatch.setattr(li, "_resolve", lambda name: object())+ assert not li.legacy_int8_classes_missing()+++def test_int8_support_is_answered_by_the_rebuild_when_the_classes_are_gone(monkeypatch):+ monkeypatch.setattr(pq, "_register_prequant_safe_globals", lambda: True)+ monkeypatch.setattr(pq, "_RESOLVED_SAFE_GLOBALS", set(pq._SCHEME_REQUIRED_GLOBALS["fp8"]))+ monkeypatch.setattr(li, "legacy_int8_decode_supported", lambda: False)+ assert not pq.restricted_prequant_load_supported("int8", "X-INT8.pt")+ monkeypatch.setattr(li, "legacy_int8_decode_supported", lambda: True)+ assert pq.restricted_prequant_load_supported("int8", "X-INT8.pt")+ assert pq.restricted_prequant_load_supported("int8")+ assert pq.restricted_prequant_load_supported("fp8", "X-FP8.pt")+ monkeypatch.setattr(pq, "_RESOLVED_SAFE_GLOBALS", {"torch.torch_version.TorchVersion"})+ assert not pq.restricted_prequant_load_supported("fp8", "X-FP8.pt")+++def test_a_refusal_for_any_other_global_is_not_retried(monkeypatch):+ torch = pytest.importorskip("torch")+ calls = []++ def _load(*a, **k):+ raise RuntimeError("Unsupported global: GLOBAL os.system was not an allowed global")++ monkeypatch.setattr(pq, "_register_prequant_safe_globals", lambda: True)+ monkeypatch.setattr(torch, "load", _load)+ monkeypatch.setattr(li, "legacy_int8_decode_supported", lambda: True)+ monkeypatch.setattr(li, "load_legacy_int8_pickle", lambda *a, **k: calls.append(a))+ with pytest.raises(RuntimeError, match = "os.system"):+ pq._torch_load_prequant("/x.pt", map_location = "cpu")+ assert calls == []+++def test_unreadable_hosted_checkpoint_is_named_for_status(monkeypatch):+ from core.inference.diffusion_families import DiffusionFamily++ fam = DiffusionFamily(+ name = "test-fam",+ base_repo = "org/base",+ pipeline_class = "P",+ transformer_class = "T",+ prequant_repos = (("int8", "org/Model-FP8"),),+ )+ monkeypatch.setattr(pq, "cached_checkpoint_path", lambda *a, **k: None)+ monkeypatch.setattr(pq, "restricted_prequant_load_supported", lambda scheme, name = None: False)+ monkeypatch.setattr(+ pq, "_unreadable_why", lambda scheme: "torchao 9.9 no longer ships 5 class(es)"+ )+ note = pq.prequant_unreadable_reason(fam, "int8")+ assert "org/Model-FP8" in note and "no longer ships" in note+ monkeypatch.setattr(+ pq,+ "restricted_prequant_load_supported",+ lambda scheme, name = None: bool(name) and name.endswith(".safetensors"),+ )+ assert pq.prequant_unreadable_reason(fam, "int8") is not None+ monkeypatch.setattr(pq, "restricted_prequant_load_supported", lambda scheme, name = None: True)+ assert pq.prequant_unreadable_reason(fam, "int8") is None+ assert pq.prequant_unreadable_reason(fam, "fp8") is None+ assert pq.prequant_unreadable_reason(fam, None) is None+++def test_the_dense_fast_path_reason_says_why_the_hosted_checkpoint_was_skipped(monkeypatch):+ import core.inference.diffusion as d++ monkeypatch.setattr(+ d,+ "prequant_unreadable_reason",+ lambda *a, **k: "the hosted int8 checkpoint (r) cannot be read here: x",+ )+ reason = d._dense_fast_path_reason(object(), "int8", "org/base", "pipeline", None)+ assert "cannot be read here" in reason and "dense bf16 transformer" in reason+ assert (+ d._dense_fast_path_reason(object(), "int8", "org/base", "pipeline", "/p.pt")+ == "engaged on the dense fast path"+ )+ assert (+ d._dense_fast_path_reason(object(), "int8", "org/base", "gguf", None)+ == "engaged on the dense fast path"+ )+ assert (+ d._dense_fast_path_reason(+ object(), "int8", "org/base", "pipeline", None, [("org/lora", 1.0)]+ )+ == "engaged on the dense fast path"+ )+++_needs_rebuild = pytest.mark.skipif(+ not li.legacy_int8_decode_supported(),+ reason = "needs a torchao without the v1 int8 classes and with Int8Tensor (0.18+)",+)++[email protected](autouse = True)+def _no_foreign_legacy_registrations():+ """Other test files register placeholders under these names process-wide."""+ torch = pytest.importorskip("torch")+ saved = torch.serialization.get_safe_globals()+ legacy = {*li.LEGACY_INT8_CLASS_NAMES, li._ACT_QUANT}+ torch.serialization.clear_safe_globals()+ torch.serialization.add_safe_globals(+ [g for g in saved if not (isinstance(g, tuple) and g[1] in legacy)]+ )+ yield+ torch.serialization.clear_safe_globals()+ torch.serialization.add_safe_globals(saved)+++def _legacy_modules():+ import enum++ import torch++ class _Base(torch.Tensor):+ __torch_function__ = torch._C._disabled_torch_function_impl++ @staticmethod+ def __new__(cls, shape, dtype):+ return torch.Tensor._make_wrapper_subclass(cls, shape, dtype = dtype)++ @classmethod+ def __torch_dispatch__(+ cls,+ func,+ types,+ args = (),+ kwargs = None,+ ):+ raise RuntimeError("fake")++ def __reduce_ex__(self, proto):+ args = (+ type(self),+ self.dtype,+ tuple(self.size()),+ self.stride(),+ self.storage_offset(),+ self.layout,+ self.device,+ False,+ )+ return (+ torch._tensor._rebuild_from_type_v2,+ (torch._utils._rebuild_wrapper_subclass, type(self), args, dict(self.__dict__)),+ )++ spec = {+ "torchao.quantization.linear_activation_quantized_tensor": "LinearActivationQuantizedTensor",+ "torchao.dtypes.affine_quantized_tensor": "AffineQuantizedTensor",+ "torchao.dtypes.uintx.plain_layout": "PlainAQTTensorImpl",+ }+ mods, made = {}, {}+ for mod, name in spec.items():+ cls = type(name, (_Base,), {"__module__": mod, "__qualname__": name})+ m = types.ModuleType(mod)+ setattr(m, name, cls)+ mods[mod] = m+ made[name] = cls+ layout = type(+ "PlainLayout", (), {"__module__": "torchao.dtypes.utils", "__qualname__": "PlainLayout"}+ )+ mods["torchao.dtypes.utils"] = types.ModuleType("torchao.dtypes.utils")+ mods["torchao.dtypes.utils"].PlainLayout = layout++ def _int8_symm_per_token_reduced_range_quant(x):+ raise RuntimeError("never called")++ _int8_symm_per_token_reduced_range_quant.__module__ = "torchao.quantization.quant_api"+ _int8_symm_per_token_reduced_range_quant.__qualname__ = (+ "_int8_symm_per_token_reduced_range_quant"+ )+ real_api = sys.modules.get("torchao.quantization.quant_api")+ api = types.ModuleType("torchao.quantization.quant_api")+ if real_api is not None:+ api.__dict__.update(real_api.__dict__)+ api._int8_symm_per_token_reduced_range_quant = _int8_symm_per_token_reduced_range_quant+ mods["torchao.quantization.quant_api"] = api++ class ZeroPointDomain(enum.Enum):+ INT = 1+ FLOAT = 2+ NONE = 3++ return mods, made, layout, _int8_symm_per_token_reduced_range_quant, ZeroPointDomain+++def _write_legacy_checkpoint(+ monkeypatch,+ path,+ *,+ zero_point = None,+ extra = None,+):+ import torch++ from torchao.quantization.quant_primitives import ZeroPointDomain++ mods, made, layout, act, _ = _legacy_modules()+ torch.manual_seed(0)+ n, k = 8, 16+ qdata = torch.randint(-128, 128, (n, k), dtype = torch.int8)+ scale = (torch.rand(n) / 50 + 1e-3).to(torch.bfloat16)+ with monkeypatch.context() as m:+ for name, mod in mods.items():+ m.setitem(sys.modules, name, mod)+ impl = made["PlainAQTTensorImpl"]((n, k), torch.int8)+ impl.__dict__.update(int_data = qdata, scale = scale, zero_point = zero_point, _layout = layout())+ aqt = made["AffineQuantizedTensor"]((n, k), torch.bfloat16)+ aqt.__dict__.update(+ tensor_impl = impl,+ block_size = (1, k),+ quant_min = None,+ quant_max = None,+ zero_point_domain = ZeroPointDomain.NONE,+ )+ laqt = made["LinearActivationQuantizedTensor"]((n, k), torch.bfloat16)+ laqt.__dict__.update(original_weight_tensor = aqt, input_quant_func = act, quant_kwargs = {})+ state_dict = {+ "blk.proj.weight": laqt,+ "blk.norm.weight": torch.ones(k, dtype = torch.bfloat16),+ }+ if extra:+ state_dict.update(extra)+ torch.save(+ {+ "format": pq.PREQUANT_FORMAT,+ "metadata": {"scheme": "int8"},+ "state_dict": state_dict,+ },+ str(path),+ )+ return qdata, scale+++@_needs_rebuild+def test_a_legacy_int8_pickle_loads_as_int8tensor_with_the_same_weights(monkeypatch, tmp_path):+ import torch++ path = tmp_path / "Model-INT8.pt"+ qdata, scale = _write_legacy_checkpoint(monkeypatch, path)+ with pytest.raises(+ Exception, match = "LinearActivationQuantizedTensor|was not an allowed global"+ ):+ torch.load(str(path), weights_only = True)+ ckpt = pq._load_prequant_checkpoint(str(path), map_location = "cpu")+ w = ckpt["state_dict"]["blk.proj.weight"]+ assert type(w).__name__ == "Int8Tensor"+ assert torch.equal(w.qdata, qdata)+ assert w.scale.dtype == torch.bfloat16 and torch.equal(w.scale.flatten(), scale)+ assert w.dtype == torch.bfloat16 and tuple(w.shape) == (8, 16)+ assert type(w.act_quant_kwargs.granularity).__name__ == "PerRow"+ assert torch.equal(w.dequantize(), qdata.to(torch.bfloat16) * scale.reshape(8, 1))+ assert type(ckpt["state_dict"]["blk.norm.weight"]) is torch.Tensor+ assert not any(+ isinstance(g, tuple) and g[1] == li._LAQT for g in torch.serialization.get_safe_globals()+ )+++@_needs_rebuild+def test_the_rebuild_does_not_open_the_door_to_other_globals(monkeypatch, tmp_path):+ import os++ import torch++ path = tmp_path / "Model-INT8.pt"+ _write_legacy_checkpoint(monkeypatch, path, extra = {"zz": os.getcwd})+ with pytest.raises(Exception, match = "getcwd|posix|was not an allowed global"):+ pq._load_prequant_checkpoint(str(path), map_location = "cpu")+ del torch+++@_needs_rebuild+def test_an_asymmetric_legacy_weight_is_refused(monkeypatch, tmp_path):+ import torch++ path = tmp_path / "Model-INT8.pt"+ _write_legacy_checkpoint(monkeypatch, path, zero_point = torch.ones(8, dtype = torch.int8))+ with pytest.raises(ValueError, match = "zero point"):+ pq._load_prequant_checkpoint(str(path), map_location = "cpu")+++@_needs_rebuild+def test_a_plain_load_that_overlaps_a_legacy_one_is_rebuilt_too(monkeypatch, tmp_path):+ """A concurrent plain load must not hand inert stand-ins to load_state_dict."""+ import torch++ path = tmp_path / "Model-INT8.pt"+ qdata, _ = _write_legacy_checkpoint(monkeypatch, path)+ standins = li._standins()+ pairs = [(obj, name) for name, obj in standins.items() if li._resolve(name) is None]+ with torch.serialization.safe_globals(pairs):+ raw = torch.load(str(path), weights_only = True)+ assert isinstance(raw["state_dict"]["blk.proj.weight"], standins[li._LAQT])+ monkeypatch.setattr(torch, "load", lambda *a, **k: raw)+ monkeypatch.setattr(pq, "_register_prequant_safe_globals", lambda: True)+ w = pq._torch_load_prequant(str(path), map_location = "cpu")["state_dict"]["blk.proj.weight"]+ assert type(w).__name__ == "Int8Tensor" and torch.equal(w.qdata, qdata)diff --git a/studio/backend/tests/test_prequant_safetensors.py b/studio/backend/tests/test_prequant_safetensors.pyindex a5c01c44e22..74c507d35c9 100644--- a/studio/backend/tests/test_prequant_safetensors.py+++ b/studio/backend/tests/test_prequant_safetensors.py@@ -516,3 +516,81 @@ def test_a_field_carrying_a_real_setting_is_refused_rather_than_dropped():ps._header_without_unconstructible_fields(_unflatten, {}, header, path = "artifact.safetensors")+++def test_an_all_zero_tensor_field_a_newer_torchao_added_is_dropped():+ torch = pytest.importorskip("torch")++ def _unflatten(tensors, header):+ if "zero_point" in json.loads(header["blk.w.weight"])["_tensor_data_names"]:+ raise ValueError(+ "Failed to create instance of Int8Tensor: Int8Tensor.__new__() got an "+ "unexpected keyword argument 'zero_point'"+ )+ return {"blk.w.weight": object()}, {}++ tensors = {+ "blk.w._weight_qdata": torch.zeros(2, 4, dtype = torch.int8),+ "blk.w._weight_scale": torch.ones(2, 1),+ "blk.w._weight_zero_point": torch.zeros(2, 1, dtype = torch.int8),+ }+ header = {+ "blk.w.weight": json.dumps(+ {+ "_type": "Int8Tensor",+ "_data": {},+ "_tensor_data_names": ["zero_point", "qdata", "scale"],+ }+ ),+ ps.UNSLOTH_FORMAT_KEY: "v1",+ }+ pruned = ps._header_without_unconstructible_fields(+ _unflatten, tensors, header, path = "artifact.safetensors"+ )+ assert json.loads(pruned["blk.w.weight"])["_tensor_data_names"] == ["qdata", "scale"]+ assert "blk.w._weight_zero_point" not in tensors+ assert pruned[ps.UNSLOTH_FORMAT_KEY] == "v1"+++def test_a_non_zero_tensor_field_is_refused_rather_than_dropped():+ torch = pytest.importorskip("torch")++ def _unflatten(tensors, header):+ raise ValueError("unexpected keyword argument 'zero_point'")++ tensors = {"blk.w._weight_zero_point": torch.ones(2, 1, dtype = torch.int8)}+ header = {"blk.w.weight": json.dumps({"_data": {}, "_tensor_data_names": ["zero_point"]})}+ with pytest.raises(ValueError, match = "zero_point"):+ ps._header_without_unconstructible_fields(+ _unflatten, tensors, header, path = "artifact.safetensors"+ )+++def test_a_missing_tensor_field_is_refused_rather_than_read_as_zero():+ torch = pytest.importorskip("torch")++ def _unflatten(tensors, header):+ raise ValueError("unexpected keyword argument 'zero_point'")++ names = json.dumps({"_data": {}, "_tensor_data_names": ["zero_point", "qdata", "scale"]})+ tensors = {+ "a.w._weight_zero_point": torch.zeros(2, 1, dtype = torch.int8),+ "b.w._weight_qdata": torch.zeros(2, 4, dtype = torch.int8),+ "b.w._weight_scale": torch.ones(2, 1),+ }+ header = {"a.w.weight": names, "b.w.weight": names}+ with pytest.raises(ValueError, match = "b.w._weight_zero_point"):+ ps._header_without_unconstructible_fields(+ _unflatten, tensors, header, path = "artifact.safetensors"+ )+++def test_torchao_older_than_the_floor_is_not_safetensors_support(monkeypatch):+ assert ps._version_tuple("0.14.0") < ps.MIN_TORCHAO_VERSION+ assert ps._version_tuple("0.16.0+cu130") >= ps.MIN_TORCHAO_VERSION+ assert ps._version_tuple("0.19.0+git492be6c") >= ps.MIN_TORCHAO_VERSION+ assert ps._version_tuple(None) >= ps.MIN_TORCHAO_VERSION+ assert ps._version_tuple("dev") >= ps.MIN_TORCHAO_VERSION+ monkeypatch.setattr(ps, "_torchao_version", lambda: "0.14.0")+ assert ps._torchao_helpers() is None+ assert ps.safetensors_prequant_supported() is Falsediff --git a/studio/backend/tests/test_video_prequant.py b/studio/backend/tests/test_video_prequant.pyindex e2eb608d85e..c1c4033693e 100644--- a/studio/backend/tests/test_video_prequant.py+++ b/studio/backend/tests/test_video_prequant.py@@ -1174,3 +1174,44 @@ def test_an_unreadable_card_keeps_the_rotation(monkeypatch):monkeypatch.setattr(vid, "_h3_dense_denoiser_resident_bytes", lambda fam, **kw: (1, 1))monkeypatch.setattr(vid, "_h3_free_device_bytes", lambda device: None)assert vid._h3_dense_denoiser_fits(vid._h3_dense_denoiser_resident_bytes(None), None) is False+++def test_unreadability_is_reported_only_when_it_is_what_kept_bfloat16(monkeypatch):+ try:+ import importlib+ import importlib.metadata++ importlib.metadata.version("torch")+ assert hasattr(importlib.import_module("torch"), "Tensor")+ except Exception: # noqa: BLE001 - a stand-in torch in sys.modules is not an install+ pytest.skip("needs torch installed")+ fam, torch, vid = _shared_setup_1()+ import core.inference.diffusion_prequant as pq++ monkeypatch.setattr(vid, "_h3_auto_precision_ok", lambda target = None: True, raising = False)+ monkeypatch.setattr(vid, "_h3_free_device_bytes", lambda device: 80 * 1000**3)+ monkeypatch.setattr(pq, "restricted_prequant_load_supported", lambda *a, **k: False)+ fired = []++ def ask(**over):+ kw = dict(+ target = None,+ dtype = torch.bfloat16,+ device = "cuda",+ te_scheme = "int8",+ task = "fl2va",+ base_repo = fam.base_repo,+ on_unreadable = lambda: fired.append(True),+ )+ kw.update(over)+ return vid._h3_auto_denoiser_scheme(fam, **kw)++ assert ask() is None and fired == [True]+ fired.clear()+ assert ask(speed_mode = "off") is None and fired == []+ assert ask(base_repo = "someone/MiniMax-H3") is None and fired == []+ monkeypatch.setattr(vid, "_h3_free_device_bytes", lambda device: 10 * 1000**3)+ assert ask() is None and fired == []+ monkeypatch.setattr(pq, "restricted_prequant_load_supported", lambda *a, **k: True)+ monkeypatch.setattr(vid, "_h3_free_device_bytes", lambda device: 80 * 1000**3)+ assert ask() == vid.H3_AUTO_FALLBACK_SCHEME and fired == []