PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
add support for w4a16_mixed (#1326)
Heng Guo7cff6af5e97e
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex 9ea366e09..57e771478 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -303,6 +303,10 @@ class BaseCompressor(object):self.layer_config = layer_config+ self.supported_types = SUPPORTED_LAYER_TYPES+ self.inner_supported_types = INNER_SUPPORTED_LAYER_TYPES+ self.quant_lm_head = kwargs.pop("quant_lm_head", False)+# should be set after loading model and set layer_config, cause some special scheme need these.# Preserve the original, unparsed scheme for later use in auto scheme generation# within `configure_layer_config` (which may need the raw value instead of `self.scheme`).@@ -338,7 +342,6 @@ class BaseCompressor(object):if envs.AR_USE_MODELSCOPE:platform = "model_scope"self.platform = platform- self.quant_lm_head = kwargs.pop("quant_lm_head", False)self.ignore_layers = kwargs.pop("ignore_layers", "")self.supported_types = SUPPORTED_LAYER_TYPES@@ -634,7 +637,15 @@ class BaseCompressor(object):scheme = scheme.strip("'\" ")res = schemescheme = scheme.upper()- self.layer_config = _handle_special_schemes(scheme, self.layer_config, self.model)+ self.layer_config = _handle_special_schemes(+ scheme,+ self.layer_config,+ self.model,+ supported_types=self.supported_types,+ inner_supported_types=self.inner_supported_types,+ quant_lm_head=self.quant_lm_head,+ mllm=getattr(self, "mllm", False),+ )scheme = asdict(preset_name_to_scheme(scheme))scheme_keys = [f.name for f in fields(QuantizationScheme)]for key in scheme_keys:diff --git a/auto_round/formats.py b/auto_round/formats.pyindex 5d53d0f9c..fe7aee47b 100644--- a/auto_round/formats.py+++ b/auto_round/formats.py@@ -459,7 +459,7 @@ class LLMCompressorFormat(OutputFormat):@OutputFormat.register("auto_gptq", "gptqmodel")class AutoGPTQFormat(OutputFormat):- support_schemes = ["W4A16", "W2A16", "W3A16", "W8A16", "BF16", "W2A16G64", "W2A16G32"]+ support_schemes = ["W4A16", "W2A16", "W3A16", "W8A16", "BF16", "W2A16G64", "W2A16G32", "W4A16_MIXED"]format_name = "auto_gptq"def check_and_reset_format(self, ar):@@ -935,6 +935,7 @@ class GGUFFormat(OutputFormat):class AutoRoundFormat(OutputFormat):support_schemes = ["W4A16",+ "W4A16_MIXED","W2A16","W3A16","W8A16",diff --git a/auto_round/schemes.py b/auto_round/schemes.pyindex 93110a833..54824270e 100644--- a/auto_round/schemes.py+++ b/auto_round/schemes.py@@ -18,6 +18,8 @@ from typing import Optional, Unionimport torch+from auto_round.logger import logger+__all__ = ["QuantizationScheme", "get_gguf_scheme", "preset_name_to_scheme"]@@ -285,6 +287,7 @@ PRESET_SCHEMES = {"W2A16G32": W2A16G32,"FP8_STATIC": FP8_STATIC,"BF16": BF16,+ "W4A16_MIXED": W4A16,}from auto_round.export.export_to_gguf.config import GGUF_CONFIG@@ -296,7 +299,15 @@ for key, val in GGUF_CONFIG.items():PRESET_SCHEMES[key.upper()] = QuantizationScheme.from_dict(value)-def _handle_special_schemes(scheme_name: str, layer_config: dict, model: torch.nn.Module) -> dict:+def _handle_special_schemes(+ scheme_name: str,+ layer_config: dict,+ model: torch.nn.Module,+ supported_types=None,+ inner_supported_types=None,+ quant_lm_head=False,+ mllm=False,+) -> dict:"""handle special schemes, like GGUF:Q2_K_MIXED.Provide some special auto_round recipes.@@ -311,6 +322,31 @@ def _handle_special_schemes(scheme_name: str, layer_config: dict, model: torch.nlayer_config[n] = "GGUF:Q8_0"elif isinstance(m, torch.nn.Linear) and ("expert" not in n or "shared_experts" in n) and n != "lm_head":layer_config[n] = "GGUF:Q4_K_S"+ if scheme_name.lower() == "w4a16_mixed":+ logger.warning("W4A16_MIXED is experimental and the recipe may change in the future.")+ from auto_round.utils import get_lm_head_name++ lm_head_name = get_lm_head_name(model)+ if supported_types is None:+ from auto_round.utils import SUPPORTED_DTYPES++ supported_types = SUPPORTED_DTYPES+ if inner_supported_types is None:+ from auto_round.utils import INNER_SUPPORTED_LAYER_TYPES++ inner_supported_types = INNER_SUPPORTED_LAYER_TYPES+ for n, m in model.named_modules():+ if n in layer_config:+ continue+ if type(m) in supported_types or type(m) in inner_supported_types:+ if "expert" in n and "shared" not in n:+ layer_config[n] = {"bits": 4}+ elif n != lm_head_name and mllm:+ layer_config[n] = {"bits": 16}+ elif n != lm_head_name:+ layer_config[n] = {"bits": 8}+ elif n == lm_head_name and quant_lm_head:+ layer_config[n] = {"bits": 8}return layer_configdiff --git a/test/test_cpu/schemes/test_scheme.py b/test/test_cpu/schemes/test_scheme.pyindex f4f0e716b..e2b0c15c3 100644--- a/test/test_cpu/schemes/test_scheme.py+++ b/test/test_cpu/schemes/test_scheme.py@@ -41,6 +41,48 @@ class TestAutoRound:assert ar.bits == 2ar.quantize()+ def test_w4a16_mixed(self, tiny_qwen_moe_model_path, dataloader):++ layer_config = {+ "model.layers.0.self_attn.k_proj": {"bits": 16},+ }+ ar = AutoRound(+ tiny_qwen_moe_model_path,+ scheme="W4A16_MIXED",+ nsamples=1,+ iters=0,+ seqlen=2,+ dataset=dataloader,+ low_cpu_mem_usage=False,+ layer_config=layer_config,+ )+ ar.quantize()+ assert ar.bits == 4+ assert ar.model.model.layers[0].self_attn.q_proj.bits == 8+ assert ar.model.model.layers[0].self_attn.k_proj.bits == 16+ assert ar.model.model.layers[0].mlp.experts[0].up_proj.bits == 4+ assert ar.model.model.layers[0].mlp.shared_expert.gate_proj.bits == 8++ shutil.rmtree(self.save_folder, ignore_errors=True)++ def test_w4a16_mixed_mllm(self, tiny_qwen_2_5_vl_model_path, dataloader):++ ar = AutoRound(+ tiny_qwen_2_5_vl_model_path,+ scheme="W4A16_MIXED",+ nsamples=1,+ batch_size=1,+ iters=0,+ seqlen=2,+ dataset=dataloader,+ low_cpu_mem_usage=False,+ )+ ar.quantize()+ assert ar.bits == 4+ assert ar.model.language_model.layers[0].self_attn.q_proj.bits == 16+ assert ar.model.visual.blocks[0].attn.qkv.bits == 16+ shutil.rmtree(self.save_folder, ignore_errors=True)+def test_mxfp4(self, tiny_opt_model_path, dataloader):ar = AutoRound(tiny_opt_model_path, scheme="MXFP4", nsamples=1, iters=1, seqlen=2, dataset=dataloader)assert ar.bits == 4