PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
handle invalid layername in weight_config (#93)
WeiweiZhang18956226180c8
diff --git a/auto_round/autoround.py b/auto_round/autoround.pyindex bff4e7c61..74b3d01c9 100644--- a/auto_round/autoround.py+++ b/auto_round/autoround.py@@ -1317,13 +1317,12 @@ class AutoRound(object):for key in self.weight_config.keys():if key in all_layers_in_block:continue- try:- layer = get_module(self.model, key)- if isinstance(layer, tuple(self.supported_types)) and check_to_quantized(self.weight_config[key]):- layer_names.append(key)- except:+ layer = get_module(self.model, key)+ if layer is None:logger.error(f"could not find layer {key} in the model, exit...")exit()+ if isinstance(layer, tuple(self.supported_types)) and check_to_quantized(self.weight_config[key]):+ layer_names.append(key)return layer_namesdiff --git a/auto_round/export/export_to_autogptq.py b/auto_round/export/export_to_autogptq.pyindex 5fc872cfe..aaf40eef7 100644--- a/auto_round/export/export_to_autogptq.py+++ b/auto_round/export/export_to_autogptq.py@@ -46,6 +46,8 @@ from safetensors.torch import save_file as safe_savefrom auto_round.export.register import register_formatfrom auto_round.utils import check_to_quantized, get_block_names, get_module, logger+from ..utils import convert_dtype_torch2str_hf+@register_format("auto_gptq")def save_quantized_as_autogptq(output_dir, use_triton=True, inplace=True, **kwargs):@@ -224,7 +226,7 @@ def _save_quantized_to_autogptq(safetensors_metadata["minmax_lr"] = str(minmax_lr)safetensors_metadata["enable_minmax_tuning"] = str(enable_minmax_tuning)safetensors_metadata["enable_quanted_input"] = str(enable_quanted_input)- safetensors_metadata["scale_dtype"] = str(scale_dtype)+ safetensors_metadata["scale_dtype"] = convert_dtype_torch2str_hf(scale_dtype)safe_save(state_dict, join(save_dir, model_save_name), safetensors_metadata)else:model_save_name = model_base_name + ".bin"@@ -251,7 +253,7 @@ def _save_quantized_to_autogptq(config_dict["minmax_lr"] = minmax_lrconfig_dict["enable_minmax_tuning"] = enable_minmax_tuningconfig_dict["enable_quanted_input"] = enable_quanted_input- config_dict["scale_dtype"] = str(scale_dtype)+ config_dict["scale_dtype"] = convert_dtype_torch2str_hf(scale_dtype)if modules_in_block_to_quantize is not None:config_dict["modules_in_block_to_quantize"] = modules_in_block_to_quantizediff --git a/auto_round/utils.py b/auto_round/utils.pyindex d7cf32a42..84073d447 100644--- a/auto_round/utils.py+++ b/auto_round/utils.py@@ -292,9 +292,7 @@ def get_module(module, key):"""name_list = key.split(".")for name in name_list:- if hasattr(module, name):- module = getattr(module, name)- module = module+ module = getattr(module, name, None)return module@@ -311,8 +309,6 @@ def set_module(model, key, new_module):for name in name_list[:-1]:if hasattr(module, name):module = getattr(module, name)- else:- module = modulesetattr(module, name_list[-1], new_module)@@ -676,6 +672,32 @@ def convert_dtype_torch2str(dtype):assert False, "Unsupported pytorch dtype {} to str dtype".format(dtype)+def convert_dtype_torch2str_hf(dtype):+ """Converts a PyTorch dtype to its corresponding huggingface string dtype, e.g. torch.float32 -> 'float32'.++ Args:+ dtype: PyTorch dtype or str. The dtype to convert.++ Returns:+ str: The string representation of the dtype.++ Raises:+ AssertionError: If the input str_dtype is unsupported.+ """+ if dtype is None:+ return dtype+ if isinstance(dtype, str):+ if "float" not in dtype and "int" not in dtype:+ dtype = convert_dtype_str2torch(dtype)+ else:+ return dtype+ str_dtype = str(dtype)+ if "." not in str_dtype:+ assert False, "Unsupported pytorch dtype {} to huggingface str dtype".format(dtype)+ str_dtype = str_dtype.split(".")[1]+ return str_dtype++def check_memory_availability(device, inputs, weight, org_seqlen, org_bs):"""Checks the availability of memory on the specified device for processing inputs using a given weight tensor.diff --git a/examples/language-modeling/README.md b/examples/language-modeling/README.mdindex 35e34d54a..79d1cba4a 100644--- a/examples/language-modeling/README.md+++ b/examples/language-modeling/README.md@@ -18,10 +18,12 @@ The transformers version required varies across different types of models. Here,| facebook/opt-6.7b | 4.28/4.30/4.34/4.36 || tiiuae/falcon-7b | 4.28/4.30/4.34/4.36 || mosaicml/mpt-7b | 4.28/4.30/4.34/4.36 |+| mosaicml/mpt-7b-chat | 4.34 || bigscience/bloom-3b | 4.28/4.30/4.34/4.36 |-| facebook/opt-2.7b | 4.28/4.30/4.34/4.36 |+| bigscience/bloom-7b1 | 4.28/4.30/4.34/4.36 || baichuan-inc/Baichuan2-7B-Chat | 4.36 || Qwen/Qwen-7B | 4.28/4.30/4.34/4.36 |+| Qwen/Qwen1.5-7B-Chat | 4.38/4.40 || THUDM/chatglm3-6b | 4.34/4.36 || mistralai/Mistral-7B-v0.1 | 4.34/4.36 || MBZUAI/LaMini-GPT-124M | 4.34/4.36 |@@ -31,6 +33,7 @@ The transformers version required varies across different types of models. Here,| Intel/neural-chat-7b-v3 | 4.34/4.36 || rinna/bilingual-gpt-neox-4b | 4.36 || microsoft/phi-2 | 4.36 |+| google/gemma-7b | 4.38/4.40 |## 2. Prepare Calibration Dataset@@ -119,3 +122,4 @@ If you find SignRound useful for your research, please cite our paper:+diff --git a/examples/language-modeling/main.py b/examples/language-modeling/main.pyindex 828984eb3..c26aeb971 100644--- a/examples/language-modeling/main.py+++ b/examples/language-modeling/main.py@@ -287,14 +287,15 @@ if __name__ == '__main__':export_dir = args.output_dir + "/" + model_name.split('/')[-1] + f"-autoround-w{args.bits}g{args.group_size}"output_dir = args.output_dir + "/" + model_name.split('/')[-1] + f"-autoround-w{args.bits}g{args.group_size}-qdq"deployment_device = args.deployment_device.split(',')+ inplace = True if len(deployment_device) < 2 else Falseif 'gpu' in deployment_device:- autoround.save_quantized(f'{export_dir}-gpu', format="auto_gptq", use_triton=True, inplace=False)+ autoround.save_quantized(f'{export_dir}-gpu', format="auto_gptq", use_triton=True, inplace=inplace)if 'xpu' in deployment_device:- autoround.save_quantized(f'{export_dir}-xpu', format="itrex_xpu", use_triton=True, inplace=False,+ autoround.save_quantized(f'{export_dir}-xpu', format="itrex_xpu", use_triton=True, inplace=inplace,compression_dtype=torch.int8, compression_dim=0, use_optimum_format=False,device="xpu")if "cpu" in deployment_device:- autoround.save_quantized(output_dir=f'{export_dir}-cpu', format='itrex', inplace=False)+ autoround.save_quantized(output_dir=f'{export_dir}-cpu', format='itrex', inplace=inplace)if "fake" in deployment_device:model = model.to("cpu")model.save_pretrained(output_dir)