PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
remove autoawq requirement at packing stage (#249)
n1ck-guo98c465636119
diff --git a/README.md b/README.mdindex fada849a0..8a4170e74 100644--- a/README.md+++ b/README.md@@ -181,7 +181,7 @@ Additionally, symmetric quantization tends to perform poorly at 2-bit precision.**AutoAWQ format**: This format is well-suited for asymmetric 4-bit quantization on CUDA devices and is widely adoptedwithin the community. Asymmetric quantization typically improves accuracy but may reduce inference speed. It features-specialized layer fusion tailored for Llama models. However, it supports only 4-bit asymmetric quantization. Currently, please manually install autoawq via `pip install autoawq` before exporting.+specialized layer fusion tailored for Llama models. However, it supports only 4-bit asymmetric quantization.## Model Inferencediff --git a/auto_round/__main__.py b/auto_round/__main__.pyindex 6a235dfd8..ea24d8b4f 100644--- a/auto_round/__main__.py+++ b/auto_round/__main__.py@@ -314,10 +314,7 @@ def tune(args):inplace = False if len(format_list) > 1 else Truefor format_ in format_list:eval_folder = f'{export_dir}-{format_}'- if 'auto_awq' in format_:- autoround.save_quantized(eval_folder, format=format_, inplace=inplace, model_path=model_name)- else:- autoround.save_quantized(eval_folder, format=format_, inplace=inplace)+ autoround.save_quantized(eval_folder, format=format_, inplace=inplace)def get_library_version(library_name):diff --git a/auto_round/autoround.py b/auto_round/autoround.pyindex b930c1c48..6f84275c7 100644--- a/auto_round/autoround.py+++ b/auto_round/autoround.py@@ -1094,7 +1094,7 @@ class AutoRound(object):Args:output_dir (str, optional): The directory to save the quantized model. Defaults to None.- format (str, optional): The format in which to save the model. Defaults to "auto_gptq".+ format (str, optional): The format in which to save the model. Defaults to "auto_round".inplace (bool, optional): Whether to modify the model in place. Defaults to True.**kwargs: Additional keyword arguments specific to the export format.diff --git a/auto_round/export/export_to_autoround/export.py b/auto_round/export/export_to_autoround/export.pyindex 4bee03570..fd185028a 100644--- a/auto_round/export/export_to_autoround/export.py+++ b/auto_round/export/export_to_autoround/export.py@@ -125,12 +125,8 @@ def dynamic_import_quantLinear_for_packing(backend, bits, group_size, sym):from auto_round_extension.cuda.qlinear_triton import QuantLinearreturn QuantLinearelif "awq" in backend:- try:- from awq.modules.linear import WQLinear_GEMM # pylint: disable=E0401- return WQLinear_GEMM- except:- logger.error("autoawq is required. Please install it by 'pip install autoawq' to support auto_awq format.")- return+ from ..export_to_awq.utils import WQLinear_GEMM+ return WQLinear_GEMMelif "gptq" in backend:return get_autogptq_packing_qlinear(backend, bits, group_size, sym)else:@@ -186,7 +182,7 @@ def pack_layer(name, model, layer_config, backend, pbar):qlayer.pack(layer, scale, zero, None)qlayer.to(device)else:- from awq.utils.utils import clear_memory # pylint: disable=E0401+ from ..export_to_awq.utils import clear_memoryscale, zp = layer_config[name]["scale"].to(torch.float32), layer_config[name]["zp"].to(torch.float32)scale = scale.t().contiguous()zp = zp.t().contiguous()diff --git a/auto_round/export/export_to_awq/export.py b/auto_round/export/export_to_awq/export.pyindex 042d71be7..8ae65bca3 100644--- a/auto_round/export/export_to_awq/export.py+++ b/auto_round/export/export_to_awq/export.py@@ -42,7 +42,7 @@ from typing import Dict, List, Optional, Union@register_format("auto_awq")-def save_quantized_as_autoawq(output_dir, model_path, inplace=True, **kwargs):+def save_quantized_as_autoawq(output_dir, inplace=True, **kwargs):"""Export the model to autogptq format to easily leverage cuda kernel."""model = kwargs["model"]layer_config = kwargs["layer_config"]@@ -67,17 +67,10 @@ def save_quantized_as_autoawq(output_dir, model_path, inplace=True, **kwargs):else:compressed_model = copy.deepcopy(model.to("cpu"))- try:- from awq import AutoAWQForCausalLM # pylint: disable=E0401- from awq.modules.linear import WQLinear_GEMM # pylint: disable=E0401- from awq.utils.utils import clear_memory # pylint: disable=E0401- except:- logger.error("autoawq is required. Please install it by 'pip install autoawq' to support auto_awq format.")+ from .utils import WQLinear_GEMM, clear_memory, get_self_modulesq_linear_module = WQLinear_GEMM- awq_model = AutoAWQForCausalLM.from_pretrained(model_path)- self_modules = awq_model.get_model_layers(compressed_model)- del awq_model # release memory+ self_modules = get_self_modules(compressed_model)for i in range(len(self_modules)):module = self_modules[i]named_linears = get_named_linears(module)diff --git a/auto_round/export/export_to_awq/utils.py b/auto_round/export/export_to_awq/utils.pynew file mode 100644index 000000000..5dcaf0ef0--- /dev/null+++ b/auto_round/export/export_to_awq/utils.py@@ -0,0 +1,386 @@+# Copyright (c) 2024 Intel Corporation+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++# Copyright (c) 2023 MIT HAN Lab++# Permission is hereby granted, free of charge, to any person obtaining a copy+# of this software and associated documentation files (the "Software"), to deal+# in the Software without restriction, including without limitation the rights+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+# copies of the Software, and to permit persons to whom the Software is+# furnished to do so, subject to the following conditions:++# The above copyright notice and this permission notice shall be included in all+# copies or substantial portions of the Software.++# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+# SOFTWARE.++import gc+import torch+import warnings+import torch.nn as nn+from torch.autograd import Function+++def unpack_awq(qweight: torch.Tensor, qzeros: torch.Tensor, bits: int):+ shifts = torch.arange(0, 32, bits, device=qzeros.device)++ # unpacking columnwise+ iweights = torch.bitwise_right_shift(qweight[:, :, None], shifts[None, None, :]).to(+ torch.int8 # smallest dtype available+ )+ iweights = iweights.view(iweights.shape[0], -1)++ # unpacking columnwise+ if qzeros is not None:+ izeros = torch.bitwise_right_shift(qzeros[:, :, None], shifts[None, None, :]).to(+ torch.int8 # smallest dtype available+ )+ izeros = izeros.view(izeros.shape[0], -1)+ else:+ izeros = qzeros++ return iweights, izeros++AWQ_REVERSE_ORDER = [0, 4, 1, 5, 2, 6, 3, 7]++def reverse_awq_order(iweights: torch.Tensor, izeros: torch.Tensor, bits: int):+ reverse_order_tensor = torch.arange(+ iweights.shape[-1],+ dtype=torch.int32,+ device=izeros.device,+ )+ reverse_order_tensor = reverse_order_tensor.view(-1, 32 // bits)+ reverse_order_tensor = reverse_order_tensor[:, AWQ_REVERSE_ORDER]+ reverse_order_tensor = reverse_order_tensor.view(-1)++ if izeros is not None:+ izeros = izeros[:, reverse_order_tensor]+ iweights = iweights[:, reverse_order_tensor]++ return iweights, izeros+++def dequantize_gemm(qweight, qzeros, scales, bits, group_size):+ # Unpack the qweight and qzeros tensors+ iweight, izeros = unpack_awq(qweight, qzeros, bits)+ # Reverse the order of the iweight and izeros tensors+ iweight, izeros = reverse_awq_order(iweight, izeros, bits)++ # overflow checks+ iweight = torch.bitwise_and(iweight, (2**bits) - 1)+ izeros = torch.bitwise_and(izeros, (2**bits) - 1)++ # fp16 weights+ scales = scales.repeat_interleave(group_size, dim=0)+ izeros = izeros.repeat_interleave(group_size, dim=0)+ iweight = (iweight - izeros) * scales++ return iweight+++def get_best_device():+ if torch.backends.mps.is_available():+ return "mps"+ elif torch.cuda.is_available():+ return "cuda:0"+ else:+ return "cpu"+++class WQLinearMMFunction(Function):+ @staticmethod+ # ctx is the first argument to forward+ def forward(+ ctx,+ x,+ qweight,+ qzeros,+ scales,+ w_bit=4,+ group_size=128,+ bias=None,+ out_features=0,+ ):+ # The forward pass can use ctx.+ ctx.save_for_backward(x, qweight, qzeros, scales, bias)+ ctx.out_features = out_features++ out_shape = x.shape[:-1] + (out_features,)+ x = x.to(torch.float16)++ out = dequantize_gemm(qweight, qzeros, scales, w_bit, group_size)+ out = torch.matmul(x, out)++ out = out + bias if bias is not None else out+ out = out.reshape(out_shape)++ # always want 3D tensor if tensor is 2D+ if len(out.shape) == 2:+ out = out.unsqueeze(0)++ return out+++class WQLinear_GEMM(nn.Module):+ def __init__(+ self, w_bit, group_size, in_features, out_features, bias, dev, training=False+ ):+ super().__init__()++ if w_bit not in [4]:+ raise NotImplementedError("Only 4-bit are supported for now.")++ self.in_features = in_features+ self.out_features = out_features+ self.w_bit = w_bit+ self.group_size = group_size if group_size != -1 else in_features+ self.training = training++ # quick sanity check (make sure alignment)+ assert self.in_features % self.group_size == 0+ assert out_features % (32 // self.w_bit) == 0++ self.register_buffer(+ "qweight",+ torch.zeros(+ (in_features, out_features // (32 // self.w_bit)),+ dtype=torch.int32,+ device=dev,+ ),+ )+ self.register_buffer(+ "qzeros",+ torch.zeros(+ (in_features // self.group_size, out_features // (32 // self.w_bit)),+ dtype=torch.int32,+ device=dev,+ ),+ )+ self.register_buffer(+ "scales",+ torch.zeros(+ (in_features // self.group_size, out_features),+ dtype=torch.float16,+ device=dev,+ ),+ )+ if bias:+ self.register_buffer(+ "bias",+ torch.zeros(+ (out_features),+ dtype=torch.float16,+ device=dev,+ ),+ )+ else:+ self.bias = None++ @classmethod+ def from_linear(+ cls, linear, w_bit, group_size, init_only=False, scales=None, zeros=None+ ):+ awq_linear = cls(+ w_bit,+ group_size,+ linear.in_features,+ linear.out_features,+ linear.bias is not None,+ linear.weight.device,+ )+ if init_only: # just prepare for loading sd+ return awq_linear++ # need scales and zeros info for real quantization+ assert scales is not None and zeros is not None+ scale_zeros = zeros * scales++ awq_linear.scales = scales.clone().half()+ if linear.bias is not None:+ awq_linear.bias = linear.bias.clone().half()++ pack_num = 32 // awq_linear.w_bit++ intweight = []+ for idx in range(awq_linear.in_features):+ intweight.append(+ torch.round(+ (linear.weight.data[:, idx] + scale_zeros[idx // group_size])+ / awq_linear.scales[idx // group_size]+ ).to(torch.int)[:, None]+ )+ intweight = torch.cat(intweight, dim=1)+ intweight = intweight.t().contiguous()+ intweight = intweight.to(dtype=torch.int32)++ best_device = get_best_device()++ # Avoid: The operator 'aten::__lshift__.Scalar' is not currently implemented for the MPS device+ if "mps" in best_device:+ intweight = intweight.to("cpu")++ qweight = torch.zeros(+ (intweight.shape[0], intweight.shape[1] // 32 * awq_linear.w_bit),+ dtype=torch.int32,+ device=intweight.device,+ )++ for col in range(intweight.shape[1] // pack_num):+ if awq_linear.w_bit == 4:+ order_map = [0, 2, 4, 6, 1, 3, 5, 7]+ else:+ raise NotImplementedError("Only 4-bit are supported for now.")+ for i in range(pack_num):+ qweight_col = intweight[:, col * pack_num + order_map[i]]+ qweight[:, col] |= qweight_col << (i * awq_linear.w_bit)+ awq_linear.qweight = qweight++ zeros = zeros.to(dtype=torch.int32, device=best_device)++ if "mps" in best_device:+ zeros = zeros.to("cpu")++ qzeros = torch.zeros(+ (zeros.shape[0], zeros.shape[1] // 32 * awq_linear.w_bit),+ dtype=torch.int32,+ device=zeros.device,+ )++ for col in range(zeros.shape[1] // pack_num):+ if awq_linear.w_bit == 4:+ order_map = [0, 2, 4, 6, 1, 3, 5, 7]+ else:+ raise NotImplementedError("Only 4-bit are supported for now.")+ for i in range(pack_num):+ qzero_col = zeros[:, col * pack_num + order_map[i]]+ qzeros[:, col] |= qzero_col << (i * awq_linear.w_bit)+ awq_linear.qzeros = qzeros++ return awq_linear++ def forward(self, x):+ out_shape = x.shape[:-1] + (self.out_features,)++ input_dtype = x.dtype+ if input_dtype != torch.float16:+ x = x.half()++ if self.training:+ out = WQLinearMMFunction.apply(+ x,+ self.qweight,+ self.qzeros,+ self.scales,+ self.w_bit,+ self.group_size,+ self.bias,+ self.out_features,+ )+ else:+ with torch.no_grad():+ out = WQLinearMMFunction.apply(+ x,+ self.qweight,+ self.qzeros,+ self.scales,+ self.w_bit,+ self.group_size,+ self.bias,+ self.out_features,+ )++ if input_dtype != torch.float16:+ out = out.to(dtype=input_dtype)++ return out.reshape(out_shape)++ def extra_repr(self) -> str:+ return (+ "in_features={}, out_features={}, bias={}, w_bit={}, group_size={}".format(+ self.in_features,+ self.out_features,+ self.bias is not None,+ self.w_bit,+ self.group_size,+ )+ )+++def clear_memory(weight=None):+ if weight is not None:+ del weight+ gc.collect()+ torch.cuda.empty_cache()+++MODEL_LAYERS_BY_MODEL_TYPE = {+ 'aquila': 'model.layers',+ 'baichuan': 'model.layers',+ 'bloom': 'transformer.h',+ 'cohere': 'model.layers',+ 'deepseek_v2': 'model.layers',+ 'falcon': 'transformer.h',+ 'gemma': 'model.layers',+ 'gemma2': 'model.layers',+ 'gpt2': 'transformer.h',+ 'gpt_bigcode': 'transformer.h',+ 'gpt_neo': 'transformer.h',+ 'gpt_neox': 'gpt_neox.layers',+ 'gptj': 'transformer.h',+ 'internlm2': 'model.layers',+ 'llama': 'model.layers',+ 'llava': 'language_model.model.layers',+ 'llava_next': 'language_model.model.layers',+ 'minicpm': 'model.layers',+ 'mistral': 'model.layers',+ 'mixtral': 'model.layers',+ 'mpt': 'transformer.blocks',+ 'opt': 'model.decoder.layers',+ 'phi': 'model.layers',+ 'phi3': 'model.layers',+ 'qwen': 'transformer.h',+ 'qwen2': 'model.layers',+ 'roberta': 'roberta.encoder',+ 'stablelm': 'model.layers',+ 'starcoder2': 'model.layers',+ 'xlm-roberta': 'roberta.encoder',+ 'yi': 'model.layers',+}++def get_self_modules(model):+ def _get(model, self_modules):+ module = model+ for m in self_modules.split('.'):+ module = getattr(module, m)+ return module++ model_type = model.config.model_type+ if model_type not in MODEL_LAYERS_BY_MODEL_TYPE:+ for tmp_self_modules in set(MODEL_LAYERS_BY_MODEL_TYPE.values()):+ try:+ return _get(model, tmp_self_modules)+ except:+ continue+ warnings.warn(f"Model type {model_type} may be not supported by auto_awq,"+ " please checkout the auto_awq repository.")+ return None+ self_modules = MODEL_LAYERS_BY_MODEL_TYPE.get(model_type)+ return _get(model, self_modules)diff --git a/examples/language-modeling/main.py b/examples/language-modeling/main.pyindex bbda9171f..49206aea9 100644--- a/examples/language-modeling/main.py+++ b/examples/language-modeling/main.py@@ -344,35 +344,26 @@ if __name__ == '__main__':inplace = False if len(format_list) > 1 else Truefor format_ in format_list:eval_folder = f'{export_dir}-{format_}'- if 'auto_awq' in format_:- autoround.save_quantized(eval_folder, format=format_, inplace=inplace, model_path=model_name)- else:- autoround.save_quantized(eval_folder, format=format_, inplace=inplace)+ autoround.save_quantized(eval_folder, format=format_, inplace=inplace)else:deployment_device = args.deployment_device.split(',')gpu_formats = []for item in deployment_device:- if "gpu" in item or "auto_gptq" in item or "auto_round" in item or "auto_awq" in item:- gpu_formats.append(item)-- if 'gpu' in deployment_device:- if lm_head_layer_name in layer_config.keys() and layer_config[lm_head_layer_name]["data_type"] == "int":- gpu_formats.append("auto_round")- else:- gpu_formats.append("auto_gptq")+ if item in ["gpu", "auto_gptq", "auto_round", "auto_awq"]:+ if item == "gpu":+ if lm_head_layer_name in layer_config.keys() and layer_config[lm_head_layer_name]["data_type"] == "int":+ gpu_formats.append("auto_round")+ else:+ gpu_formats.append("auto_gptq")+ else:+ gpu_formats.append(item)+gpu_formats = list(set(gpu_formats))inplace = True if len(deployment_device) < 2 else Falsefor gpu_format in gpu_formats:- if "round" in gpu_format:- eval_folder = f'{export_dir}-round'- autoround.save_quantized(eval_folder, format=gpu_format, use_triton=False, inplace=inplace)- elif "gptq" in gpu_format:- eval_folder = f'{export_dir}-gpu'- autoround.save_quantized(eval_folder, format=gpu_format, use_triton=False, inplace=inplace)- elif "auto_awq" in gpu_format: # pragma: no cover- eval_folder = f'{export_dir}-awq'- autoround.save_quantized(eval_folder, format=gpu_format, inplace=inplace, model_path=model_name)+ eval_folder = f'{export_dir}-{gpu_format}'+ autoround.save_quantized(eval_folder, format=gpu_format, inplace=inplace)if 'xpu' in deployment_device:autoround.save_quantized(f'{export_dir}-xpu', format="itrex_xpu", use_triton=True, inplace=inplace)@@ -384,7 +375,7 @@ if __name__ == '__main__':tokenizer.save_pretrained(output_dir)if eval_folder is None:eval_folder = output_dir- if not ('gpu' in deployment_device or len(gpu_formats) > 0) or 'fake' not in deployment_device:+ if (not ('gpu' in deployment_device or len(gpu_formats) > 0)) and 'fake' not in deployment_device:print('does not support cpu, xpu model evaluation.')exit() ## does not support cpu,xpu model eval@@ -402,14 +393,13 @@ if __name__ == '__main__':lm_eval_version = Version(lm_eval_version)if lm_eval_version == Version("0.3.0"):use_eval_legacy = True+ from eval_legacy import eval_model_legacy as eval_model+ else:+ use_eval_legacy = False+ from eval_legacy import eval_modelif isinstance(tasks, str):tasks = tasks.split(',')- if not use_eval_legacy:- from eval_legacy import eval_model- else:- from eval_legacy import eval_model_legacy as eval_model-if isinstance(tasks, list):if "mmlu" in tasks:tmp_tasks = tasks@@ -426,34 +416,36 @@ if __name__ == '__main__':use_qdq = Trueif args.format and ('fake' in args.format or 'qdq' in args.format):use_qdq = True- if use_qdq and not args.disable_eval:++ # evaluation+ if not args.disable_eval:if use_eval_legacy:print("Using the legacy lm_eval(0.3.0)")else:print(f"Using the latest {lm_eval_version}")++ if use_qdq and lm_eval_version < Version("0.4.2"):+ excel_name = f"{output_dir}_result.xlsx"+ output_dir += "/"+ print(excel_name, flush=True)+ eval_model(+ model_path=output_dir, tasks=tasks, dtype=dtype, limit=None,+ eval_bs=args.eval_bs, use_accelerate=args.low_gpu_mem_usage,+ device=torch_device, excel_file=excel_name,+ trust_remote_code=not args.disable_trust_remote_code)++ if lm_eval_version >= Version("0.4.2"):+ from eval.evaluation import simple_evaluate- if not args.disable_eval and use_qdq and lm_eval_version < Version("0.4.2"):- excel_name = f"{output_dir}_result.xlsx"- output_dir += "/"- print(excel_name, flush=True)- eval_model(model_path=output_dir, tasks=tasks, dtype=dtype, limit=None,- eval_bs=args.eval_bs, use_accelerate=args.low_gpu_mem_usage,- device=torch_device, excel_file=excel_name,- trust_remote_code=not args.disable_trust_remote_code)-- if not args.disable_eval and lm_eval_version >= Version("0.4.2"):- from eval.evaluation import simple_evaluate-- model_args = f"pretrained={eval_folder}"- model_args = model_args + f",trust_remote_code={not args.disable_trust_remote_code}"- user_model = None- if args.act_bits <= 8:- user_model = model.to(device_str)-- res = simple_evaluate(model="hf", model_args=model_args,- tasks=tasks,- batch_size=args.eval_bs, user_model=user_model)- from lm_eval.utils import make_table+ model_args = f"pretrained={eval_folder}"+ model_args = model_args + f",trust_remote_code={not args.disable_trust_remote_code}"+ user_model = None+ if args.act_bits <= 8:+ user_model = model.to(device_str)- print(make_table(res))+ res = simple_evaluate(model="hf", model_args=model_args,+ tasks=tasks,+ batch_size=args.eval_bs, user_model=user_model)+ from lm_eval.utils import make_table+ print(make_table(res))