PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
change act_scale to input_scale for fp8 export (#711)
Heng Guo61f0d828e2fc
diff --git a/auto_round/export/export_to_autoround/export_to_fp8_woq.py b/auto_round/export/export_to_autoround/export_to_fp8_woq.pyindex b5d4f937b..5b6a4c400 100644--- a/auto_round/export/export_to_autoround/export_to_fp8_woq.py+++ b/auto_round/export/export_to_autoround/export_to_fp8_woq.py@@ -61,7 +61,7 @@ class FP8WOQLinear(torch.nn.Module):weight_scale,bias=None,weight_zp=None,- act_scale=None,+ input_scale=None,dtype=torch.bfloat16,):super().__init__()@@ -79,8 +79,8 @@ class FP8WOQLinear(torch.nn.Module):if weight_zp:self.register_buffer("weight_zp", weight_zp.to(dtype))- if act_scale is not None:- self.register_buffer("act_scale", act_scale.to(dtype))+ if input_scale is not None:+ self.register_buffer("input_scale", input_scale.to(dtype))def pack_layer(layer_name, model, data_type, packing_device=None):@@ -148,7 +148,7 @@ def pack_layer(layer_name, model, data_type, packing_device=None):weight_scale=scale,bias=bias,weight_zp=zp,- act_scale=act_scale,+ input_scale=act_scale,dtype=model.dtype,)diff --git a/auto_round/wrapper.py b/auto_round/wrapper.pyindex c817a34ff..156246385 100644--- a/auto_round/wrapper.py+++ b/auto_round/wrapper.py@@ -318,7 +318,8 @@ class WrapperLinear(torch.nn.Module):_, act_scale, _ = self._qdq_act(torch.zeros(tmp_shape).to(self.device), act_max_scale=self.act_max_scale, act_max=act_max)- self.orig_layer.act_max = torch.tensor(self.orig_layer.act_max * act_max_scale.item()).to("cpu")+ self.orig_layer.act_max = self.orig_layer.act_max * act_max_scale.item()+ self.orig_layer.act_max = self.orig_layer.act_max.to("cpu")else:act_scale = torch.ones_like(act_max_scale, dtype=self.act_data_type)self.orig_layer.act_scale = act_scale.to("cpu")diff --git a/test/test_cpu/test_export.py b/test/test_cpu/test_export.pyindex ae780cc44..bbce4036b 100644--- a/test/test_cpu/test_export.py+++ b/test/test_cpu/test_export.py@@ -222,9 +222,9 @@ class TestAutoRound(unittest.TestCase):quantized_model_path = "./saved"autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")f = safe_open(os.path.join(quantized_model_path, "model.safetensors"), framework="pt")- self.assertIn("model.decoder.layers.8.self_attn.k_proj.act_scale", f.keys())+ self.assertIn("model.decoder.layers.8.self_attn.k_proj.input_scale", f.keys())self.assertIn("model.decoder.layers.8.self_attn.k_proj.weight_scale", f.keys())- self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.act_scale").shape, torch.Size([1, 1]))+ self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.input_scale").shape, torch.Size([1, 1]))self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.weight").dtype, torch.float8_e4m3fn)shutil.rmtree(quantized_model_path, ignore_errors=True)@@ -246,9 +246,9 @@ class TestAutoRound(unittest.TestCase):autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")f = safe_open(os.path.join(quantized_model_path, "model.safetensors"), framework="pt")- self.assertIn("model.decoder.layers.8.self_attn.k_proj.act_scale", f.keys())+ self.assertIn("model.decoder.layers.8.self_attn.k_proj.input_scale", f.keys())self.assertIn("model.decoder.layers.8.self_attn.k_proj.weight_scale", f.keys())- self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.act_scale").shape, torch.Size([1, 1]))+ self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.input_scale").shape, torch.Size([1, 1]))self.assertEqual(f.get_tensor("model.decoder.layers.5.self_attn.v_proj.weight").dtype, torch.float8_e4m3fn)shutil.rmtree(quantized_model_path, ignore_errors=True)