Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

fix wrong match of ignore_layers and use warning instead of error for mismatch (#1553)

Xin Hed8ee85c8de9e
diff --git a/auto_round/compressors/utils.py b/auto_round/compressors/utils.py
index bda959e37..fdba49a88 100644
--- a/auto_round/compressors/utils.py
+++ b/auto_round/compressors/utils.py
@@ -28,7 +28,7 @@ from torch.amp import autocast
from auto_round.export.export_to_gguf.config import GGML_QUANT_SIZES, GGUF_CONFIG, GGUF_INNER_CONFIG, QK_K, ModelType
from auto_round.logger import logger
from auto_round.schemes import QuantizationScheme, get_gguf_scheme, preset_name_to_scheme
-from auto_round.utils import check_to_quantized
+from auto_round.utils import check_to_quantized, to_standard_regex
class BackendDataType(str, Enum):
@@ -341,6 +341,9 @@ 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 {}
+ if ignore_layers:
+ ignore_layers = ignore_layers.replace(" ", "").split(",")
+ ignore_layers = [name + "." if name[-1].isdigit() else name for name in ignore_layers]
# 1. ignore_layers -> force 16
for name in get_fp_layer_names(model, ignore_layers):
@@ -429,17 +432,20 @@ def set_layer_config(
logger.debug(f"{name} is not supported in current scheme, ignoring its setting in `layer_config`")
continue
- regex = re.compile(name)
+ regex = re.compile(to_standard_regex(name))
matched = [ln for ln in all_supported_layer_names if regex.search(ln)]
safetensor_only_matched = [ln for ln in safetensor_only_names if regex.search(ln)]
# skip it for mtp layers not loaded in transformers
if not matched and not safetensor_only_matched:
- raise ValueError(f"Invalid '{name}' in layer_config, no match found.")
+ # type(mlp.gate) is Qwen3VLMoeTextTopKRouter instead of Linear
+ logger.warning_once(
+ f"Layer name or regex '{name}' in layer_config does not match any supported layers. "
+ + "Please check for typos or update the regex pattern, ignore it for now"
+ )
val = layer_config.pop(name)
regex_config[name] = val # keep regex config
for match in matched:
layer_config[match] = val
- # regex_config = None if len(regex_config)==0 else regex_config
# 7. lm_head
lm_head_name = get_lm_head_name(model)
@@ -962,7 +968,7 @@ def get_fp_layer_names(model: torch.nn.Module, ignore_layers: str):
if not ignore_layers:
return []
- ignore_layers = ignore_layers.replace(" ", "").split(",")
+
all_layer_names = []
for n, m in model.named_modules():
if type(m) in SUPPORTED_LAYER_TYPES:
@@ -975,8 +981,6 @@ def get_fp_layer_names(model: torch.nn.Module, ignore_layers: str):
if fp_layer in all_layer_names:
not_to_quantized_layers.append(fp_layer)
continue
- if fp_layer[-1].isdigit():
- fp_layer = fp_layer + "." ##tricky setting
for name in all_layer_names:
if fp_layer in name:
not_to_quantized_layers.append(name)
diff --git a/test/test_cpu/core/test_autoround.py b/test/test_cpu/core/test_autoround.py
index 382f3ab51..0ed63dda4 100644
--- a/test/test_cpu/core/test_autoround.py
+++ b/test/test_cpu/core/test_autoround.py
@@ -548,26 +548,6 @@ class TestAutoRound:
print(res)
shutil.rmtree(self.save_folder, ignore_errors=True)
- def test_fallback_layers_regex_exception(self, tiny_opt_model_path, dataloader):
- model_name = tiny_opt_model_path
- bits, group_size, sym = 4, 128, True
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", trust_remote_code=True)
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
- layer_config = {"model.decoder.layers.12.self_attn.k_proj": {"bits": 16}}
- with pytest.raises(ValueError):
- autoround = AutoRound(
- model,
- tokenizer=tokenizer,
- bits=bits,
- group_size=group_size,
- sym=sym,
- iters=2,
- seqlen=2,
- dataset=dataloader,
- layer_config=layer_config,
- )
- autoround.quantize()
-
def test_dequant_fp8_weight(self):
from auto_round.utils.model import _dequant_fp8_linear_weight
@@ -655,28 +635,6 @@ class TestAutoRound:
):
raise ValueError("mixed bits is not correct")
- def test_invalid_layer_config(self, tiny_opt_model_path):
- with pytest.raises(ValueError):
- layer_config = {"model.decoder.layers.2.self_attnx": {"bits": 2}}
- ar = AutoRound(
- tiny_opt_model_path,
- scheme="W3A16",
- nsamples=1,
- iters=1,
- layer_config=layer_config,
- )
- ar.quantize()
- with pytest.raises(ValueError):
- layer_config = {"model.decoder.layers.2.self_attn": {"bit": 2}} # should be bits
- ar = AutoRound(
- tiny_opt_model_path,
- scheme="W3A16",
- nsamples=1,
- iters=1,
- layer_config=layer_config,
- )
- ar.quantize()
-
def test_quant_lm_head(self, tiny_untied_qwen_model_path):
model_name = tiny_untied_qwen_model_path
ar = AutoRound(model_name, quant_lm_head=True, iters=0, seqlen=8, nsamples=1, disable_opt_rtn=True)
diff --git a/test/test_cpu/utils/test_set_layer_config.py b/test/test_cpu/utils/test_set_layer_config.py
new file mode 100644
index 000000000..4ed85bc94
--- /dev/null
+++ b/test/test_cpu/utils/test_set_layer_config.py
@@ -0,0 +1,133 @@
+# Copyright (c) 2026 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.
+
+from unittest.mock import patch
+
+import pytest
+import torch
+import torch.nn as nn
+
+from auto_round.compressors.utils import set_layer_config
+
+# ---------------------------------------------------------------------------
+# Minimal test models
+# ---------------------------------------------------------------------------
+
+
+class _Block(nn.Module):
+ """A tiny two-linear block, used to build multi-layer test models."""
+
+ def __init__(self, size: int = 32):
+ super().__init__()
+ self.fc1 = nn.Linear(size, size)
+ self.fc2 = nn.Linear(size, size)
+
+
+class _SimpleModel(nn.Module):
+ """Model with 12 numbered blocks + lm_head.
+
+ Named modules that matter for these tests (all ``nn.Linear`` leaves):
+ layers.0.fc1, layers.0.fc2,
+ layers.1.fc1, layers.1.fc2,
+ ...
+ layers.11.fc1, layers.11.fc2,
+ lm_head
+ """
+
+ def __init__(self, num_layers: int = 12, size: int = 32):
+ super().__init__()
+ self.layers = nn.ModuleList([_Block(size) for _ in range(num_layers)])
+ self.lm_head = nn.Linear(size, size * 2, bias=False)
+
+ def forward(self, x): # pragma: no cover
+ for block in self.layers:
+ x = block.fc1(x) + block.fc2(x)
+ return self.lm_head(x)
+
+
+# Shared helpers
+_SUPPORTED_TYPES = (nn.Linear,)
+_INNER_SUPPORTED_TYPES = ()
+_DEFAULT_SCHEME = "W4A16"
+_SCALE_DTYPE = torch.float16
+
+
+def _call_set_layer_config(model, layer_config=None, ignore_layers="", quant_lm_head=False):
+ """Thin wrapper so tests don't repeat all positional args."""
+ return set_layer_config(
+ model=model,
+ layer_config=layer_config or {},
+ default_scheme=_DEFAULT_SCHEME,
+ default_scale_dtype=_SCALE_DTYPE,
+ supported_types=_SUPPORTED_TYPES,
+ inner_supported_types=_INNER_SUPPORTED_TYPES,
+ ignore_layers=ignore_layers,
+ quant_lm_head=quant_lm_head,
+ )
+
+
+def model_12layers():
+ return _SimpleModel(num_layers=12)
+
+
+def model():
+ return _SimpleModel()
+
+
+# ---------------------------------------------------------------------------
+# Tests
+# ---------------------------------------------------------------------------
+
+
+class TestIgnoreLayersDigitSuffix:
+ """A digit-ending name should match only its own subtree, not higher-numbered ones."""
+
+ def test_digit_ending_name_does_not_bleed_into_higher_indices(self, model_12layers):
+ """'layers.1' should ignore layers.1.* but NOT layers.10.* or layers.11.*"""
+ layer_config, _, _ = _call_set_layer_config(model_12layers, ignore_layers="layers.1")
+
+ # layers.1 subtree must be fp16
+ assert "layers.1.fc1" in layer_config
+ assert layer_config["layers.1.fc1"]["bits"] == 16
+ assert "layers.1.fc2" in layer_config
+ assert layer_config["layers.1.fc2"]["bits"] == 16
+
+ # layers.10 and layers.11 must NOT be force-fp16 by this setting
+ # (they are absent from ignore-forced entries; their bits should be 4 from default)
+ assert layer_config.get("layers.10.fc1", {}).get("bits", 4) != 16
+ assert layer_config.get("layers.11.fc1", {}).get("bits", 4) != 16
+
+ def test_block_level_name_ignores_entire_block(self, model_12layers):
+ """'layers.0' (digit-ending) should force-fp16 both fc1 and fc2 under layers.0."""
+ layer_config, _, _ = _call_set_layer_config(model_12layers, ignore_layers="layers.0")
+ assert layer_config["layers.0.fc1"]["bits"] == 16
+ assert layer_config["layers.0.fc2"]["bits"] == 16
+
+
+class TestUnmatchedLayerConfigWarns:
+ """An unrecognised key in layer_config must trigger a warning, not ValueError."""
+
+ def test_unknown_name_emits_warning_not_valueerror(self, model):
+ """A layer name that matches nothing should warn and not raise."""
+ from auto_round.logger import logger
+
+ bad_layer_config = {"nonexistent_layer": {"bits": 4}}
+ with patch.object(logger, "warning") as mock_warn:
+ layer_config, _, _ = _call_set_layer_config(model, layer_config=bad_layer_config)
+
+ # The warning should mention the bad name
+ warning_messages = " ".join(str(call) for call in mock_warn.call_args_list)
+ assert "nonexistent_layer" in warning_messages