PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Fix nvfp4&fp8 packing ram issue, refine all exporting format ram release for origin layer (#1129)
Yi Liu777126813b2d
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex 64a210d07..327c82b07 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -1417,9 +1417,10 @@ class BaseCompressor(object):weight_global_scale = calculate_gparam(m.weight, self.group_size)setattr(m, "weight_global_scale", weight_global_scale)- modules = list(self.model.modules())- for module in tqdm(modules, desc="Update weight global scale for fuse module"):+ logger.info("Start to update fused layer global scales, it may take some time.")+ for name, module in self.model.named_modules():update_fused_layer_global_scales(module)+ logger.info("Finished updating fused layer global scales.")if not (any("gguf" in fmt for fmt in getattr(self, "formats", [])) or self.super_bits is not None):self._quantize_embedding_layer() # leave to gguf itself to handlediff --git a/auto_round/export/export_to_autogptq/export.py b/auto_round/export/export_to_autogptq/export.pyindex 533143609..1f99d1ff7 100644--- a/auto_round/export/export_to_autogptq/export.py+++ b/auto_round/export/export_to_autogptq/export.py@@ -49,7 +49,12 @@ import transformersfrom tqdm import tqdmimport auto_round.export.export_to_autogptq.qlinear_triton-from auto_round.export.utils import filter_quantization_config, get_autogptq_packing_qlinear, save_model+from auto_round.export.utils import (+ filter_quantization_config,+ get_autogptq_packing_qlinear,+ release_layer_safely,+ save_model,+)from auto_round.schemes import QuantizationSchemeGPTQ_REQUIRED_CONFIG_KEYS = (@@ -157,13 +162,11 @@ def pack_layer(name, model, backend, device=None):bias = layer.bias is not None##bias = True ## if using the above, llama3 lambada RTN will be NAN , TODO why?- new_layer = QuantLinear( ##pylint: disable=E1123+ qlayer = QuantLinear( ##pylint: disable=E1123bits, group_size, in_features, out_features, bias, weight_dtype=layer.weight.dtype)- new_layer.device = orig_device- set_module(model, name, new_layer)- qlayer = new_layer+ qlayer.device = orig_devicescale = layer.scalezero = layer.zp# so far can only pack layer on CPU@@ -171,7 +174,7 @@ def pack_layer(name, model, backend, device=None):##force to float32 to be compatible with torch 2.0if sym and isinstance(zero, torch.Tensor):layer, scale, zero = layer.to("cpu"), scale.to("cpu"), zero.to("cpu")- if isinstance(new_layer, auto_round.export.export_to_autogptq.qlinear_triton.QuantLinear):+ if isinstance(qlayer, auto_round.export.export_to_autogptq.qlinear_triton.QuantLinear):zero = int(zero.flatten()[0])else:layer, scale, zero = layer.to("cpu"), scale.to("cpu"), zero@@ -182,10 +185,9 @@ def pack_layer(name, model, backend, device=None):else:qlayer.pack(layer, scale, zero, None, device)qlayer.to(orig_device)- if hasattr(layer, "weight"):- layer.weight = None- if hasattr(layer, "bias"):- layer.bias = None+ set_module(model, name, qlayer)+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_autogptq(output_dir, inplace=True, backend="auto_gptq:exllamav2", **kwargs):diff --git a/auto_round/export/export_to_autoround/export.py b/auto_round/export/export_to_autoround/export.pyindex a67dc7c21..06b43ab4c 100644--- a/auto_round/export/export_to_autoround/export.py+++ b/auto_round/export/export_to_autoround/export.py@@ -29,7 +29,12 @@ from tqdm import tqdmfrom auto_round.compressors.utils import is_mx_fp, is_nv_fp, is_standard_fpfrom auto_round.export.export_to_autoround.utils import check_neq_config-from auto_round.export.utils import filter_quantization_config, get_autogptq_packing_qlinear, save_model+from auto_round.export.utils import (+ filter_quantization_config,+ get_autogptq_packing_qlinear,+ release_layer_safely,+ save_model,+)from auto_round.logger import loggerfrom auto_round.schemes import QuantizationSchemefrom auto_round.utils import (@@ -250,10 +255,8 @@ def pack_layer(layer_name, model, backend, device=None):)qlayer.to(orig_device)set_module(model, layer_name, qlayer)- if hasattr(layer, "weight"):- layer.weight = None- if hasattr(layer, "bias"):- layer.bias = None+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_autoround(output_dir, inplace=True, backend="auto_round:exllamav2", **kwargs):diff --git a/auto_round/export/export_to_autoround/export_to_fp8.py b/auto_round/export/export_to_autoround/export_to_fp8.pyindex 8b8a618e2..ccd823b12 100644--- a/auto_round/export/export_to_autoround/export_to_fp8.py+++ b/auto_round/export/export_to_autoround/export_to_fp8.py@@ -25,7 +25,7 @@ from tqdm import tqdmfrom auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_padfrom auto_round.export.export_to_autoround.utils import check_neq_config-from auto_round.export.utils import filter_quantization_config, save_model+from auto_round.export.utils import filter_quantization_config, release_layer_safely, save_modelfrom auto_round.logger import loggerfrom auto_round.schemes import QuantizationSchemefrom auto_round.utils import (@@ -141,6 +141,8 @@ def pack_layer(layer_name, model, data_type, device=None):my_linear.to(orig_device)set_module(model, layer_name, my_linear)+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_autoround(output_dir, inplace=True, backend="auto_round", **kwargs):diff --git a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.pyindex 7cbb3266a..cbb45d90e 100644--- a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py+++ b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py@@ -27,7 +27,7 @@ from tqdm import tqdmfrom auto_round.compressors.utils import is_mx_fp, is_nv_fpfrom auto_round.export.export_to_autoround.utils import check_neq_config-from auto_round.export.utils import filter_quantization_config, save_model+from auto_round.export.utils import filter_quantization_config, release_layer_safely, save_modelfrom auto_round.logger import loggerfrom auto_round.schemes import QuantizationSchemefrom auto_round.utils import (@@ -92,7 +92,7 @@ def pack_layer(name, model, backend, device=None):bias = layer.bias is not None##bias = True ## if using the above, llama3 lambada RTN will be NAN , TODO why?- new_layer = QuantLinear( ##pylint: disable=E1123+ qlayer = QuantLinear( ##pylint: disable=E1123bits,group_size,in_features,@@ -105,16 +105,17 @@ def pack_layer(name, model, backend, device=None):act_data_type=act_data_type,)- new_layer.device = orig_device- set_module(model, name, new_layer)- qlayer = new_layer+ qlayer.device = orig_devicescale = layer.scaleglobal_scale = getattr(layer, "weight_global_scale", None)input_global_scale = getattr(layer, "input_global_scale", None)+ ## no zeros to handle, as mxfp/nvfp do not support asym quantization# zero = layer.zpqlayer.pack(layer, scale, global_scale=global_scale, input_global_scale=input_global_scale, device=device)- ## no zeros to handle, as mxfp not support asym quantizationqlayer.to(orig_device)+ set_module(model, name, qlayer)+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_fp(output_dir, inplace=True, **kwargs):diff --git a/auto_round/export/export_to_awq/export.py b/auto_round/export/export_to_awq/export.pyindex b9282af27..0fa922833 100644--- a/auto_round/export/export_to_awq/export.py+++ b/auto_round/export/export_to_awq/export.py@@ -31,7 +31,7 @@ import torch.nn as nnfrom tqdm import tqdmfrom auto_round.export.export_to_awq.utils import WQLinear_GEMM-from auto_round.export.utils import filter_quantization_config, save_model+from auto_round.export.utils import filter_quantization_config, release_layer_safely, save_modelfrom auto_round.logger import loggerfrom auto_round.utils import (SUPPORTED_LAYER_TYPES,@@ -75,10 +75,8 @@ def pack_layer(name, model, backend, device=None):device=device,)set_module(model, name, q_linear)- if hasattr(layer, "weight"):- layer.weight = None- if hasattr(layer, "bias"):- layer.bias = None+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_autoawq(output_dir, inplace=True, **kwargs):diff --git a/auto_round/export/export_to_llmcompressor/export_to_fp.py b/auto_round/export/export_to_llmcompressor/export_to_fp.pyindex 69600c8af..377d10f2c 100644--- a/auto_round/export/export_to_llmcompressor/export_to_fp.py+++ b/auto_round/export/export_to_llmcompressor/export_to_fp.py@@ -27,7 +27,7 @@ from tqdm import tqdmfrom auto_round.compressors.utils import is_mx_fp, is_nv_fpfrom auto_round.export.export_to_autoround.qlinear_fp import QuantLinearfrom auto_round.export.export_to_llmcompressor.utils import generate_ignore_regex_list-from auto_round.export.utils import filter_quantization_config, save_model+from auto_round.export.utils import filter_quantization_config, release_layer_safely, save_modelfrom auto_round.logger import loggerfrom auto_round.utils import (SUPPORTED_LAYER_TYPES,@@ -93,7 +93,7 @@ def pack_layer(name, model, backend, device=None):bias = layer.bias is not None##bias = True ## if using the above, llama3 lambada RTN will be NAN , TODO why?- new_layer = QuantLinear( ##pylint: disable=E1123+ qlayer = QuantLinear( ##pylint: disable=E1123bits,group_size,in_features,@@ -106,15 +106,16 @@ def pack_layer(name, model, backend, device=None):act_data_type=act_data_type,)- new_layer.device = orig_device- set_module(model, name, new_layer)- qlayer = new_layer+ qlayer.device = orig_devicescale = layer.scaleglobal_scale = getattr(layer, "weight_global_scale", None)input_global_scale = getattr(layer, "input_global_scale", None)- # zero = layer.zp # no zeros to handle, as mxfp not support asym quantization+ # zero = layer.zp # no zeros to handle, as mxfp/nvfp do not support asym quantizationqlayer.pack(layer, scale, global_scale=global_scale, input_global_scale=input_global_scale, device=device)qlayer.to(orig_device)+ set_module(model, name, qlayer)+ # Note: release weight and bias explicitly, in case they are referenced elsewhere+ release_layer_safely(layer)def save_quantized_as_fp(output_dir, inplace=True, **kwargs):diff --git a/auto_round/export/utils.py b/auto_round/export/utils.pyindex 519077529..2a1787f7f 100644--- a/auto_round/export/utils.py+++ b/auto_round/export/utils.py@@ -192,3 +192,17 @@ def filter_quantization_config(quantization_config):quantization_config.pop("act_dynamic", None)quantization_config.pop("act_sym", None)quantization_config.pop("act_group_size", None)+++def release_layer_safely(layer: nn.Module):+ """+ Safely releases the weight and bias tensors of a layer to free memory.+ Handles the case where attributes might not exist or are already None.+ """+ for attr in ["weight", "bias"]:+ if hasattr(layer, attr):+ tensor = getattr(layer, attr)+ if tensor is not None:+ # Detach and delete to avoid memory leaks+ tensor.detach_()+ setattr(layer, attr, None)