PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix bug of get_layer_names_in_block (#861)
Wenhua Cheng25d3044c3977
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex 20e7fc9d9..f56109525 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -270,6 +270,11 @@ class BaseCompressor(object):self.tokenizer = tokenizerself.shared_cache_keys = get_shared_keys(self.model)+ self.to_quant_block_names = to_quant_block_names+ if not hasattr(self, "quant_block_list"):+ all_blocks = get_block_names(model)+ self.quant_block_list = find_matching_blocks(model, all_blocks, self.to_quant_block_names)+if device is not None:logger.warning("`device` is deprecated, please use `device_map` instead")@@ -298,8 +303,6 @@ class BaseCompressor(object):layer_config[name] = {"bits": 16, "act_bits": 16, "data_type": "float", "act_data_type": "float"}self._parse_layer_config(layer_config) # must place after model init- self.to_quant_block_names = to_quant_block_names-# Tuning hyperparametersself.seed = seedset_seed(self.seed)@@ -336,27 +339,6 @@ class BaseCompressor(object):if self.static_kv_dtype is not None:logger.warning("The static kv is experimental and currently has limited support.")- # Model related- self.quantized = False- if isinstance(model, str):- model, tokenizer, low_cpu_mem_usage = llm_load_model(- model, device=device, low_cpu_mem_mode=low_cpu_mem_usage- )- elif tokenizer is None and iters > 0:- raise ValueError("A tokenizer must be set for non-str model input")- self.low_cpu_mem_usage = bool(low_cpu_mem_usage)- if unsupported_meta_device(model):- raise RuntimeError(- "AutoRound does not support parameters on meta device. "- "Please use more GPUs by setting `--device_map 0,1,2,3` or just place the model on CPU."- )- self.model = model.eval()- self.tokenizer = tokenizer- self.shared_cache_keys = get_shared_keys(self.model)- if not hasattr(self, "quant_block_list"):- all_blocks = get_block_names(model)- self.quant_block_list = find_matching_blocks(model, all_blocks, self.to_quant_block_names)-self.scale_dtype = convert_dtype_str2torch(scale_dtype)self._set_amp_dtype()self.cache_device = torch.device("cpu") if self.low_gpu_mem_usage else self.devicediff --git a/auto_round/utils.py b/auto_round/utils.pyindex 099a92a5f..11eb4c343 100644--- a/auto_round/utils.py+++ b/auto_round/utils.py@@ -777,7 +777,7 @@ def get_layer_names_in_block(class_names = []for n, m in model.named_modules():if type(m) in supported_types or (class_names is not None and m.__class__.__name__ in class_names):- m.tmp_name = n+ m.bk_tmp_name = nlayers_in_block = []if bool(quant_block_list):all_blocks = quant_block_list@@ -787,8 +787,10 @@ def get_layer_names_in_block(for block_name in block_names:block = get_module(model, block_name)for n, m in block.named_modules():- if hasattr(m, "tmp_name"):- layers_in_block.append(m.tmp_name)+ if hasattr(m, "bk_tmp_name"):+ layers_in_block.append(m.bk_tmp_name)+ delattr(m, "bk_tmp_name")+return layers_in_block