Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

fix UT check for int zp (#745)

Weiwei1f79db9bd11d
diff --git a/auto_round/export/export_to_autoround/export_to_fp.py b/auto_round/export/export_to_autoround/export_to_fp.py
index a3c724ad9..a845d2396 100644
--- a/auto_round/export/export_to_autoround/export_to_fp.py
+++ b/auto_round/export/export_to_autoround/export_to_fp.py
@@ -29,7 +29,6 @@ from auto_round.utils import (
check_start_with_block_name,
check_to_quantized,
filter_quantization_config,
- get_block_names,
get_module,
is_mx_fp,
is_nv_fp,
diff --git a/auto_round/export/export_to_autoround/qlinear_fp.py b/auto_round/export/export_to_autoround/qlinear_fp.py
index bd70a43ba..af188fda1 100644
--- a/auto_round/export/export_to_autoround/qlinear_fp.py
+++ b/auto_round/export/export_to_autoround/qlinear_fp.py
@@ -38,7 +38,7 @@ import transformers
from auto_round.data_type.mxfp import FP32_EXPONENT_BIAS, FP32_MIN_NORMAL
from auto_round.data_type.nvfp import cast_to_fp4, get_reciprocal
from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad
-from auto_round.utils import is_mx_fp, is_nv_fp, is_standard_fp
+from auto_round.utils import is_mx_fp, is_nv_fp
# from auto_round.utils import get_weight_compress_dtype
logger = getLogger(__name__)
@@ -228,31 +228,3 @@ class QuantLinear(nn.Module):
packed = (indices[:, 0] | (indices[:, 1] << 4)).to(torch.uint8)
return packed.reshape(m, n // 2)
-
- # def get_fp_scale(self, scale_e8m0):
- # E8M0_EXPONENT_BIAS = 127
- # E8M0_EXPONENT_NAN_VAL = 255
-
- # scale_e8m0 = scale_e8m0.view(torch.uint8)
- # s_offset = scale_e8m0.to(torch.int16) - E8M0_EXPONENT_BIAS
- # # TODO(later): it would be nice if there was a way to do the 2^x operation
- # # in PyTorch without creating a tensor of twos
- # two = torch.full(s_offset.size(), 2.0, device=scale_e8m0.device)
- # # pow(two, s_offset) can be out of range of floating point formats.
- # # TODO(later): handle this for float16 if we decide to support float16
- # # scales.
- # s_fp = torch.pow(two, s_offset)
-
- # # If a block exponent was 255, set values of that block to NaN
- # s_fp = torch.where(scale_e8m0 != E8M0_EXPONENT_NAN_VAL, s_fp, float("nan"))
-
- # return s_fp
-
- # def get_compressed_weight(self, tensor, bits, data_type):
- # data_type = str(data_type)
- # compress_dtype = None
- # if self.is_mx:
- # if bits == 4:
-
- # if self.is_nv:
- # pass ## TODO
diff --git a/auto_round/export/export_to_itrex/export.py b/auto_round/export/export_to_itrex/export.py
index ef64af77b..6fb33b010 100644
--- a/auto_round/export/export_to_itrex/export.py
+++ b/auto_round/export/export_to_itrex/export.py
@@ -213,6 +213,8 @@ def pack_model(
m = get_module(compressed_model, k)
fp_weight = m.weight.data
scale, zp = m.scale, m.zp
+ if isinstance(zp, int | float):
+ zp = torch.full_like(scale, zp)
convert_dtype = scale_dtype
if not isinstance(scale, torch.Tensor):
scale = torch.tensor(scale, dtype=convert_dtype)
diff --git a/test/test_cpu/test_woq_linear.py b/test/test_cpu/test_woq_linear.py
index fcdfeb27f..e077c7a21 100644
--- a/test/test_cpu/test_woq_linear.py
+++ b/test/test_cpu/test_woq_linear.py
@@ -35,6 +35,8 @@ class TestWeightOnlyLinear:
origin_shape = weight.shape
weight = weight.reshape(-1, group_size)
qdq, scale, zp = quant_tensor_sym(weight, -1)
+ if isinstance(zp, int | float):
+ zp = torch.full_like(scale, zp)
int_weight = qdq.div(scale).add(zp).clamp(0, 2 ** (bits) - 1).to(torch.int32).reshape(origin_shape)
scale = scale.reshape(origin_shape[0], -1)
if isinstance(zp, torch.Tensor):