From dd0dbc476fd68426cc2e8da43c3cfd692cf34604 Mon Sep 17 00:00:00 2001 From: Disty0 Date: Wed, 28 May 2025 17:25:38 +0300 Subject: [PATCH] SDNQ fix asym quant formula for dtypes with non zero minimums --- modules/model_quant_sdnq.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/model_quant_sdnq.py b/modules/model_quant_sdnq.py index 022b132e4..a2490b7b2 100644 --- a/modules/model_quant_sdnq.py +++ b/modules/model_quant_sdnq.py @@ -187,11 +187,11 @@ def apply_sdnq_to_module(model, weights_dtype="int8", torch_dtype=None, group_si def get_scale_asymmetric(weight: torch.FloatTensor, reduction_axes: List[int], weights_dtype: str) -> Tuple[torch.FloatTensor, torch.FloatTensor]: zero_point = torch.amin(weight, dim=reduction_axes, keepdims=True) - scale = torch.amax(weight, dim=reduction_axes, keepdims=True).sub_(zero_point).div_(dtype_dict[weights_dtype]["max"]) + scale = torch.amax(weight, dim=reduction_axes, keepdims=True).sub_(zero_point).div_(dtype_dict[weights_dtype]["max"] - dtype_dict[weights_dtype]["min"]) eps = torch.finfo(scale.dtype).eps # prevent divison by 0 scale = torch.where(torch.abs(scale) < eps, eps, scale) if dtype_dict[weights_dtype]["min"] != 0: - zero_point.add_(dtype_dict[weights_dtype]["min"]) + zero_point.sub_(torch.mul(scale, dtype_dict[weights_dtype]["min"])) return scale, zero_point