Skip to content

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

fix mx_fp issues (#286)

wenhuach2199cff1fbd7b8
diff --git a/auto_round/data_type/mxfp.py b/auto_round/data_type/mxfp.py
index f2ddfbde0..60229218f 100644
--- a/auto_round/data_type/mxfp.py
+++ b/auto_round/data_type/mxfp.py
@@ -61,6 +61,7 @@ def quant_mx(tensor, bits, data_type, v, max_scale, mantissa_rounding="even", **
KeyError: If `data_type` is not found in `MXFP_FORMAT_CACHE`.
"""
ebits, mbits, emax, max_norm, min_norm = MXFP_FORMAT_CACHE[data_type]
+ orig_dtype = tensor.dtype
shared_exp, _ = torch.max(torch.abs(tensor), dim=-1, keepdim=True)
if isinstance(max_scale, torch.Tensor):
shared_exp *= (max_scale.unsqueeze(dim=-1))
@@ -74,6 +75,10 @@ def quant_mx(tensor, bits, data_type, v, max_scale, mantissa_rounding="even", **
shared_exp = floor_ste(shared_exp)
shared_exp[shared_exp > scale_emax] = scale_emax ##changed Nan
shared_exp[shared_exp < -scale_emax] = -scale_emax
+ if (shared_exp.dtype == torch.float16 and (torch.any(shared_exp > 15) or torch.any(shared_exp < -24))) or (
+ shared_exp.dtype == torch.bfloat16 and torch.any((shared_exp < -126))):
+ tensor = tensor.to(torch.float32)
+ shared_exp = shared_exp.to(torch.float32)
tensor = tensor / (2 ** shared_exp)
is_mx_fp4 = data_type == "mx_fp4" or ("mx_fp" in data_type and bits == 4)
multiply = 2 if is_mx_fp4 else 1 ## 2 is a tricky setting
@@ -108,7 +113,7 @@ def quant_mx(tensor, bits, data_type, v, max_scale, mantissa_rounding="even", **
tensor = torch.clamp(tensor, min=-max_norm, max=max_norm)
tensor = tensor * (2 ** shared_exp)
- return tensor, shared_exp, None
+ return tensor.to(orig_dtype), shared_exp.to(orig_dtype), None
for key in MXFP_FORMAT_CACHE.keys():
diff --git a/auto_round/quantizer.py b/auto_round/quantizer.py
index 00dbc4f49..411e1ab1d 100644
--- a/auto_round/quantizer.py
+++ b/auto_round/quantizer.py
@@ -107,7 +107,7 @@ class WrapperWALayer(torch.nn.Module):
self.orig_layer.group_size,
scale_dtype=self.orig_layer.scale_dtype,
q_scale_thresh=self.orig_layer.q_scale_thresh,
- data_type=self.orig_layer.data_type)
+ data_type=self.orig_layer.act_data_type)
return self.orig_layer.forward(x)
@@ -234,7 +234,7 @@ class WrapperLinear(torch.nn.Module):
self.scale_dtype = self.orig_layer.scale_dtype
self.sym = self.orig_layer.sym
self.data_type = self.orig_layer.data_type
- self.weight_quant_func, self.data_type = get_quant_func(self.data_type, self.bits, self.sym)
+ self.weight_quant_func, self.data_type = get_quant_func(self.orig_layer.data_type, self.bits, self.sym)
self.act_bits = self.orig_layer.act_bits
self.act_group_size = self.orig_layer.act_group_size
self.act_sym = self.orig_layer.act_sym
@@ -243,7 +243,8 @@ class WrapperLinear(torch.nn.Module):
self.params = {}
if self.act_quant:
- self.act_quant_func, _ = get_quant_func(self.data_type, self.act_bits, self.act_sym)
+ self.act_quant_func, self.act_data_type = get_quant_func(self.orig_layer.data_type, self.act_bits,
+ self.act_sym)
self.q_scale_thresh = 1e-5
@@ -337,6 +338,7 @@ class WrapperLinear(torch.nn.Module):
self.orig_layer.q_scale_thresh = self.q_scale_thresh
self.orig_layer.data_type = self.data_type
if self.act_quant:
+ self.orig_layer.act_data_type = self.act_data_type
self.orig_layer.act_quant_func = self.act_quant_func
wrapper_layer = WrapperWALayer(self.orig_layer)
return wrapper_layer
@@ -371,7 +373,7 @@ class WrapperLinear(torch.nn.Module):
if self.act_quant:
x, _, _ = quant_tensor(self.act_quant_func, x, self.act_bits, self.act_group_size,
scale_dtype=self.scale_dtype, q_scale_thresh=self.q_scale_thresh,
- data_type=self.data_type)
+ data_type=self.act_data_type)
# pylint: disable=not-callable
bias = self.orig_layer.bias
if bias is not None and bias.device.type == 'meta':
@@ -418,9 +420,10 @@ class WrapperTransformerConv1d(torch.nn.Module):
self.act_sym = self.orig_layer.act_sym
self.act_dynamic = self.orig_layer.act_dynamic
self.act_quant = self.act_bits <= 8
- self.weight_quant_func, self.data_type = get_quant_func(self.data_type, self.bits, self.sym)
+ self.weight_quant_func, self.data_type = get_quant_func(self.orig_layer.data_type, self.bits, self.sym)
if self.act_quant:
- self.act_quant_func, _ = get_quant_func(self.data_type, self.act_bits, self.act_sym)
+ self.act_quant_func, self.act_data_type = get_quant_func(self.orig_layer.data_type, self.act_bits,
+ self.act_sym)
self.q_scale_thresh = 1e-5
weight_dtype = torch.float32
@@ -521,6 +524,7 @@ class WrapperTransformerConv1d(torch.nn.Module):
self.orig_layer.data_type = self.data_type
if self.act_quant:
self.orig_layer.act_quant_func = self.act_quant_func
+ self.orig_layer.act_data_type = self.act_data_type
wrapper_layer = WrapperWALayer(self.orig_layer)
return wrapper_layer
if hasattr(self.orig_layer, 'update'):
@@ -548,7 +552,7 @@ class WrapperTransformerConv1d(torch.nn.Module):
if self.act_quant:
x, _, _ = quant_tensor(self.act_quant_func, x, self.act_bits, self.act_group_size,
scale_dtype=self.scale_dtype, q_scale_thresh=self.q_scale_thresh,
- data_type=self.data_type)
+ data_type=self.act_data_type)
bias = self.orig_layer.bias
if self.enable_norm_bias_tuning:
bias, _, _ = quant_tensor(self.bias_quant_func, bias, self.bias_bits, self.bias_group_size, self.bias_v,