Skip to content

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.

9.7 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Loads INT8 .pt on torchao >= 0.18 via inert stand-ins for deleted classes; weights_only kept on."""

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 ckpt


def convert_legacy_int8_weights(module: Any) -> int:
    """Rebuilds v1 int8 weights as pinnable Int8Tensor in place (torchao <= 0.17); all or nothing."""
    import torch

    api = _int8_tensor_api()
    classes = {name: _resolve(name) for name in (*LEGACY_INT8_CLASS_NAMES, _ACT_QUANT)}
    if api is None or any(value is None for value in classes.values()):
        return 0
    rebuilt = []
    try:
        for name, submodule in module.named_modules():
            # Registered parameters only: PadToMinM forwards ``weight`` to its inner Linear.
            weight = getattr(submodule, "_parameters", {}).get("weight")
            if isinstance(weight, classes[_LAQT]):
                rebuilt.append((submodule, _rebuild_weight(name, weight, classes, api)))
    except Exception:  # noqa: BLE001 -- an unrecognised weight keeps the v1 class everywhere
        return 0
    for submodule, weight in rebuilt:
        submodule.weight = torch.nn.Parameter(weight, requires_grad = False)
    return len(rebuilt)