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.

Studio: read the Int8Tensor constructor signature once per class in the native INT8 rebuild (#15701)

Daniel Han6599e4a95b8c
diff --git a/studio/backend/core/inference/prequant_native.py b/studio/backend/core/inference/prequant_native.py
index 6db246e88b0..638d41603a3 100644
--- a/studio/backend/core/inference/prequant_native.py
+++ b/studio/backend/core/inference/prequant_native.py
@@ -7,6 +7,7 @@ the caller falls back to the torchao reader; UNSLOTH_PREQUANT_NATIVE_REBUILD=0 d
from __future__ import annotations
import dataclasses
+import functools
import json
import os
from typing import Any, Optional
@@ -177,6 +178,12 @@ def _granularity_name(g: Any) -> str:
return type(g).__name__
[email protected]_cache(maxsize = None)
+def _constructor_params(cls: Any) -> frozenset:
+ import inspect
+ return frozenset(inspect.signature(cls.__new__).parameters)
+
+
def _rebuild_int8(
name: str, entry: dict, tensors: dict, used: set, api: dict, v1: Optional[dict]
) -> Any:
@@ -242,9 +249,7 @@ def _rebuild_int8(
if "Int8Tensor" not in api:
raise _Unsupported(f"{name}: this torchao has no Int8Tensor")
- import inspect
-
- params = inspect.signature(api["Int8Tensor"].__new__).parameters
+ params = _constructor_params(api["Int8Tensor"])
kwargs: dict = {}
if act_fields is not None:
kwargs["act_quant_kwargs"] = _decode(act, api, name)
diff --git a/studio/backend/tests/test_prequant_native.py b/studio/backend/tests/test_prequant_native.py
index 45772e94f49..2998502c1ec 100644
--- a/studio/backend/tests/test_prequant_native.py
+++ b/studio/backend/tests/test_prequant_native.py
@@ -144,6 +144,34 @@ def test_native_rebuild_matches_torchao_unflatten(tmp_path, monkeypatch):
assert _same(_canon(native["state_dict"][key]), _canon(sd[key])), key
+@needs_helpers
+@needs_int8tensor
+def test_int8_constructor_signature_is_read_once_not_per_weight(tmp_path, monkeypatch):
+ sd = {f"blk{i}.q.weight": _int8_tensor(seed = i) for i in range(4)}
+ path = _write(tmp_path / "M-INT8.safetensors", sd)
+ monkeypatch.setenv(pn.NATIVE_REBUILD_ENV, "0")
+ stock = ps.load_prequant_safetensors(str(path))["state_dict"]
+ monkeypatch.delenv(pn.NATIVE_REBUILD_ENV)
+ target = _API["Int8Tensor"].__new__
+ real, calls = inspect.signature, []
+
+ def counting(obj, *args, **kwargs):
+ if obj is target:
+ calls.append(obj)
+ return real(obj, *args, **kwargs)
+
+ monkeypatch.setattr(inspect, "signature", counting)
+ cached = getattr(pn, "_constructor_params", None)
+ if cached is not None:
+ cached.cache_clear()
+ for _ in range(2):
+ native = ps.load_prequant_safetensors(str(path))
+ assert native["reader"] == "native"
+ for key in sd:
+ assert _same(_canon(native["state_dict"][key]), _canon(stock[key])), key
+ assert len(calls) == 1
+
+
@needs_helpers
@needs_int8tensor
def test_mapped_read_is_the_same_tensors_as_a_copied_read(tmp_path):