PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Fix missing extra_config export for unsupported ignore_layers like `mlp.gate` (#1660)
Liang Lvd2a62b89ecff
diff --git a/auto_round/compressors/utils.py b/auto_round/compressors/utils.pyindex 58f64f683..5d9b97567 100644--- a/auto_round/compressors/utils.py+++ b/auto_round/compressors/utils.py@@ -347,9 +347,11 @@ def set_layer_config(extra_scheme_keys = ("scale_dtype",)scheme_keys = tuple(f.name for f in fields(QuantizationScheme)) + ("scale_dtype",)layer_config = copy.deepcopy(layer_config) or {}+ ignore_layer_patterns = set()if ignore_layers:ignore_layers = ignore_layers.replace(" ", "").split(",")ignore_layers = [name + "." if name[-1].isdigit() else name for name in ignore_layers]+ ignore_layer_patterns = set(ignore_layers)# 1. ignore_layers -> force 16for name in get_fp_layer_names(model, ignore_layers):@@ -434,11 +436,16 @@ def set_layer_config(if name in all_module_names:m = get_module(model, name)if len(list(m.children())) == 0 and type(m) not in supported_types:- layer_config.pop(name)- logger.warning(- f"'{name}' exists in the model but is not a supported quantization target "- f"in the current scheme, ignoring its setting in `layer_config`"- )+ val = layer_config.pop(name)+ if name in ignore_layer_patterns:+ # Keep unsupported ignore_layers entries so export can serialize+ # them into regex-based extra_config for loaders like vLLM INC.+ regex_config[name] = val+ else:+ logger.warning(+ f"'{name}' exists in the model but is not a supported quantization target "+ f"in the current scheme, ignoring its setting in `layer_config`"+ )continueregex = re.compile(to_standard_regex(name))diff --git a/test/test_cpu/utils/test_set_layer_config.py b/test/test_cpu/utils/test_set_layer_config.pyindex 4ed85bc94..91a2222e7 100644--- a/test/test_cpu/utils/test_set_layer_config.py+++ b/test/test_cpu/utils/test_set_layer_config.py@@ -56,6 +56,30 @@ class _SimpleModel(nn.Module):return self.lm_head(x)+class _Router(nn.Module):+ """Unsupported leaf module used to emulate MoE routers like mlp.gate."""++ def __init__(self, size: int = 32):+ super().__init__()+ self.weight = nn.Parameter(torch.randn(size, size))+++class _MoeBlock(nn.Module):+ def __init__(self, size: int = 32):+ super().__init__()+ self.gate = _Router(size)+ self.up_proj = nn.Linear(size, size)+++class _UnsupportedGateModel(nn.Module):+ def __init__(self, size: int = 32):+ super().__init__()+ self.mlp = _MoeBlock(size)++ def forward(self, x): # pragma: no cover+ return self.mlp.up_proj(x)++# Shared helpers_SUPPORTED_TYPES = (nn.Linear,)_INNER_SUPPORTED_TYPES = ()@@ -87,6 +111,11 @@ def model():return _SimpleModel()+def unsupported_gate_model():+ return _UnsupportedGateModel()++# ---------------------------------------------------------------------------# Tests# ---------------------------------------------------------------------------@@ -131,3 +160,15 @@ class TestUnmatchedLayerConfigWarns:# The warning should mention the bad namewarning_messages = " ".join(str(call) for call in mock_warn.call_args_list)assert "nonexistent_layer" in warning_messages+++class TestIgnoreLayersRegexFallback:+ """Unsupported ignore_layers should be preserved for export as regex extra_config."""++ def test_unsupported_ignore_layer_is_kept_in_regex_config(self, unsupported_gate_model):+ layer_config, _, regex_config = _call_set_layer_config(unsupported_gate_model, ignore_layers="mlp.gate")++ assert "mlp.gate" not in layer_config+ assert "mlp.gate" in regex_config+ assert regex_config["mlp.gate"]["bits"] == 16+ assert regex_config["mlp.gate"]["data_type"] == "float"