PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix model_warpper issue
Zhang, Weiwei1c93825a00d1f
diff --git a/auto_round/export.py b/auto_round/export.pyindex bb5a552b5..84188f4db 100644--- a/auto_round/export.py+++ b/auto_round/export.py@@ -95,8 +95,7 @@ def compress_model(scale_dtype = v["scale_dtype"]m = get_module(compressed_model, k)fp_weight = m.weight.data- scale = torch.tensor(v["scale"], dtype=torch.float32)-+ scale = torch.tensor(v["scale"], dtype=scale_dtype)zp = None if scheme == "sym" else torch.tensor(v["zp"], dtype=torch.int32)int_weight = quant_weight_w_scale(fp_weight, scale, zp, group_size)int_weight = int_weight.type(torch.int32)@@ -201,3 +200,4 @@ def save_quantized_to_autogptq(model, save_dir: str, bits=4, group_size=128, symquantization_config.model_file_base_name = model_base_namequantization_config.save_pretrained(save_dir)+diff --git a/auto_round/model_wrapper.py b/auto_round/model_wrapper.pyindex 4bddd6942..bc52c3ec0 100644--- a/auto_round/model_wrapper.py+++ b/auto_round/model_wrapper.py@@ -323,13 +323,21 @@ class WeightOnlyLinear(torch.nn.Module):return fp32_weightdef forward(self, input):- weight = self.recover()- device = self.scales.device- if weight.dtype == torch.float16 and device.type == "cpu":- weight = weight.float()- self.bias = self.bias.float() if self.bias is not None else None- input = input.type(weight.dtype)- return F.linear(input, weight, self.bias)+ if not hasattr(self, "weight"):+ weight = self.recover()+ device = self.scales.device+ if weight.dtype == torch.float16 and device.type == "cpu":+ weight = weight.float()+ self.bias = self.bias.float() if self.bias is not None else None+ if True: # keep reusing self.weight due to recover is too slow.+ if not hasattr(self, "weight"):+ self.weight = weight+ input = input.type(self.weight.dtype)+ logger.debug(f"Calculating {self}")+ return F.linear(input, self.weight, self.bias)+ else:+ input = input.type(weight.dtype)+ return F.linear(input, weight, self.bias)def extra_repr(self) -> str:tmp_str = "in_features={}, out_features={}, bits={}, group_size={}, bias={}".format(@@ -343,3 +351,4 @@ class WeightOnlyLinear(torch.nn.Module):tmp_str += ", use_optimum_format=True"return tmp_str+diff --git a/examples/eval/evaluation.py b/examples/eval/evaluation.pyindex 443423844..e1d00ea3d 100644--- a/examples/eval/evaluation.py+++ b/examples/eval/evaluation.py@@ -359,10 +359,11 @@ if __name__ == "__main__":"hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte", "arc_easy","arc_challenge"]model_name = args.model_name.rstrip('/')- excel_name = (args.model_name).split('/')[-1] + ".xlsx"+ excel_name = model_name.split('/')[-1] + ".xlsx"eval_model(output_dir=args.model_name,tasks=test_tasks,eval_bs=args.eval_bs, eval_orig_float=True, limit=None, excel_file=excel_name)print("cost time: ", time.time() - s)+diff --git a/examples/eval/utils.py b/examples/eval/utils.pyindex 783010ea3..18730a334 100644--- a/examples/eval/utils.py+++ b/examples/eval/utils.py@@ -190,9 +190,10 @@ def eval_ppl_same_with_gptq(model, testenc, dev):testenc = testenc.input_idsnsamples = testenc.numel() // model.seqlen-- use_cache = model.config.use_cache- model.config.use_cache = False+ use_cache = None+ if hasattr(model.config, "use_cache"):+ use_cache = model.config.use_cache+ model.config.use_cache = Falsetestenc = testenc.to(dev)nlls = []@@ -209,6 +210,7 @@ def eval_ppl_same_with_gptq(model, testenc, dev):neg_log_likelihood = loss.float() * model.seqlennlls.append(neg_log_likelihood)ppl = torch.exp(torch.stack(nlls).sum() / (nsamples * model.seqlen))-- model.config.use_cache = use_cache+ if hasattr(model.config, "use_cache"):+ model.config.use_cache = use_cachereturn ppl.item()+diff --git a/examples/main.py b/examples/main.pyindex f1a4dd768..f876e2842 100644--- a/examples/main.py+++ b/examples/main.py@@ -89,12 +89,16 @@ if __name__ == '__main__':parser.add_argument("--enable_minmax_tuning", action='store_true',help="whether enable weight minmax tuning")- parser.add_argument("--target_platform", default='cpu',- help="targeted inference acceleration platform, 'cpu' or 'gpu'")+ parser.add_argument("--deployment_device", default='fake', type=str,+ help="targeted inference acceleration platform, 'fake', 'cpu' or 'gpu'")parser.add_argument("--scale_dtype", default='fp32',help="which scale data type to use for quantization, 'fp16', 'fp32' or 'bf16'.")+ # parser.add_argument("--tasks",+ # default=['lambada_openai'],+ # help="lm-eval tasks")+parser.add_argument("--tasks",default=['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',"hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte",@@ -190,7 +194,7 @@ if __name__ == '__main__':seed=args.seed, gradient_accumulate_steps=args.gradient_accumulate_steps, scale_dtype=args.scale_dtype) ##TODO args passmodel, q_config = autoround.quantize()- if args.target_platform == 'cpu':+ if args.deployment_device == 'cpu':output_dir = args.output_dir + "/compressed_" + args.model_name.split('/')[-1] + "/"compressed_model = compress_model(model, q_config)quantize_config = AutoroundQuantConfig(bits=args.bits, sym=args.sym, group_size=args.group_size)