PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
enhance model_free functionality with strict parameter validation (#2520)
Xin He95eccea11714
diff --git a/auto_round/autoround.py b/auto_round/autoround.pyindex 472706173..ee58e2fb1 100644--- a/auto_round/autoround.py+++ b/auto_round/autoround.py@@ -309,10 +309,12 @@ def _discover_alg_config_fields(config_cls: type) -> frozenset:return frozenset(fields)-def _filter_supported_entry_kwargs(kwargs, *, context="AutoRound"):+def _filter_supported_entry_kwargs(kwargs, *, context="AutoRound", strict=False):supported = {key: value for key, value in kwargs.items() if key in _ENTRY_KWARG_OWNERS}unknown = sorted(set(kwargs) - set(_ENTRY_KWARG_OWNERS))if unknown:+ if strict:+ raise TypeError(f"{context} received unsupported runtime kwargs with model_free=True: {', '.join(unknown)}")logger.warning_once("%s received unsupported runtime kwargs %s. They will be ignored.",context,@@ -321,9 +323,9 @@ def _filter_supported_entry_kwargs(kwargs, *, context="AutoRound"):return supported-def _split_entry_kwargs(kwargs, *, context="AutoRound"):+def _split_entry_kwargs(kwargs, *, context="AutoRound", strict=False):buckets = {"route": {}, "compressor": {}, "base": {}, "mllm": {}, "diffusion": {}}- for key, value in _filter_supported_entry_kwargs(kwargs, context=context).items():+ for key, value in _filter_supported_entry_kwargs(kwargs, context=context, strict=strict).items():buckets[_ENTRY_KWARG_OWNERS[key]][key] = valuereturn buckets@@ -336,7 +338,7 @@ def _owning_algorithm_names(field_name: str) -> list[str]:return [cls.__name__ for _, cls in _iter_registered_alg_configs() if field_name in _discover_alg_config_fields(cls)]-def _normalize_alg_configs(alg_configs, direct_kwargs=None):+def _normalize_alg_configs(alg_configs, direct_kwargs=None, *, strict=False):from auto_round.algorithms.config_resolver import split_quantization_configsfrom auto_round.algorithms.quantization.config import QuantizationConfigfrom auto_round.algorithms.quantization.rtn.config import RTNConfig@@ -477,6 +479,8 @@ def _normalize_alg_configs(alg_configs, direct_kwargs=None):# ignored algorithm-specific error after selecting RTN.if key == "iters" and any(isinstance(config, RTNConfig) for config in configs):continue+ if strict:+ raise TypeError(f"Unsupported parameter '{key}' for the selected algorithm with model_free=True.")owners = _owning_algorithm_names(key)owner = "/".join(owners) if owners else "the selected algorithm"logger.error(@@ -507,8 +511,8 @@ def _normalize_alg_configs(alg_configs, direct_kwargs=None):return [normalize_algorithm_config(config) for config in configs]-def _prepare_entry_kwargs(alg_configs, direct_kwargs):- configs = _normalize_alg_configs(alg_configs, direct_kwargs)+def _prepare_entry_kwargs(alg_configs, direct_kwargs, *, strict=False):+ configs = _normalize_alg_configs(alg_configs, direct_kwargs, strict=strict)runtime_kwargs = {key: value for key, value in direct_kwargs.items() if key in _ENTRY_KWARG_OWNERS}return configs, runtime_kwargs@@ -573,7 +577,7 @@ class _CompressorBuilder:# TODO wenhuach if key in kwargs could override scheme and alg_config, we should pop and override,# e.g. gradient_accumulate_stepdevice_map = normalize_default_device_map(device_map)- split_kwargs = _split_entry_kwargs(kwargs)+ split_kwargs = _split_entry_kwargs(kwargs, strict=bool(kwargs.get("model_free", False)))route_kwargs = dict(split_kwargs["route"])compressor_kwargs = dict(split_kwargs["compressor"])base_kwargs = dict(split_kwargs["base"])@@ -764,6 +768,7 @@ class AutoRound:**kwargs,) -> BaseCompressor:direct_kwargs = dict(kwargs)+ strict_model_free = bool(direct_kwargs.get("model_free", False))legacy_device = direct_kwargs.pop("device", None)if legacy_device is not None:logger.warning_once("`device` is deprecated, please use `device_map` instead")@@ -779,7 +784,7 @@ class AutoRound:direct_kwargs["bits"] = bitsscheme, direct_kwargs = resolve_entry_scheme(scheme, schemes, direct_kwargs)- configs, runtime_kwargs = _prepare_entry_kwargs(alg_configs, direct_kwargs)+ configs, runtime_kwargs = _prepare_entry_kwargs(alg_configs, direct_kwargs, strict=strict_model_free)runtime_kwargs["batch_size"] = batch_sizereturn _CompressorBuilder(diff --git a/auto_round/compressors/model_free.py b/auto_round/compressors/model_free.pyindex e8b88c1b1..2462b1d57 100644--- a/auto_round/compressors/model_free.py+++ b/auto_round/compressors/model_free.py@@ -1887,6 +1887,7 @@ class ModelFreeCompressor(_ModelFreeCompressorCore):from dataclasses import fields as dc_fieldsfallback_kwargs = dict(kwargs)+ self._force_model_free = bool(kwargs.get("model_free", False))# Collect per-field scheme overrides forwarded from AutoRound# (e.g. bits=4, sym=False passed as individual kwargs).@@ -1975,6 +1976,12 @@ class ModelFreeCompressor(_ModelFreeCompressorCore):def _fallback_to_base_compressor(self, save_format: str | None = None):from auto_round.autoround import AutoRound+ if self._force_model_free:+ requested_format = save_format or self.format+ raise ValueError(+ "model_free=True does not support the requested operation "+ f"(format: '{requested_format}'); refusing to fall back to regular AutoRound."+ )logger.info("Model-free mode cannot export this request directly; ""constructing the regular AutoRound compressor (requested format: '%s').",@@ -2158,6 +2165,10 @@ class ModelFreeCompressor(_ModelFreeCompressorCore):accepted_formats = {"llm_compressor", "auto_round", "auto_round:auto_gptq"}if format not in accepted_formats:+ if self._force_model_free:+ raise ValueError(+ f"model_free=True does not support format '{format}' " f"for AutoScheme family '{family}'."+ )logger.warning("Format '%s' is incompatible with model-free + AutoScheme (family=%s); ""fallback before running AutoScheme scoring.",@@ -2216,6 +2227,10 @@ class ModelFreeCompressor(_ModelFreeCompressorCore):# BF16 default with NVFP4_E5M3 layer_config overrides._accepted_formats = {"fake", "llm_compressor", "auto_round", "auto_round:auto_gptq"}if format not in _accepted_formats:+ if self._force_model_free:+ raise ValueError(+ f"model_free=True does not support format '{format}' " f"for scheme '{self.scheme_input}'."+ )logger.warning(f"Format '{format}' is not supported by model-free mode for scheme '{self.scheme_input}'; "f"falling back to the regular AutoRound flow."diff --git a/auto_round/utils/model.py b/auto_round/utils/model.pyindex 559224db6..c805fb714 100644--- a/auto_round/utils/model.py+++ b/auto_round/utils/model.py@@ -3194,10 +3194,12 @@ def is_model_free_route(has_static_attention_quantization = any(kwargs.get(key) is not None for key in ("static_kv_dtype", "static_attention_dtype"))- if has_static_attention_quantization:- return Falseif explicit:+ if has_static_attention_quantization:+ raise ValueError("model_free=True does not support static KV or attention quantization.")return True+ if has_static_attention_quantization:+ return False# Only auto-route when format is auto_round (or not specified).fmt = kwargs.get("format", "auto_round")if fmt is None:diff --git a/test/unit/test_cpu/quantization/test_model_free.py b/test/unit/test_cpu/quantization/test_model_free.pyindex 01a849172..2a3777534 100644--- a/test/unit/test_cpu/quantization/test_model_free.py+++ b/test/unit/test_cpu/quantization/test_model_free.py@@ -200,6 +200,24 @@ def test_model_free_entry_forwards_explicit_format(monkeypatch):assert ar._fallback_init_kwargs.get("format") == "auto_round:llm_compressor"+def test_explicit_model_free_rejects_unknown_runtime_kwargs():+ with pytest.raises(TypeError, match="Unsupported parameter 'unsupported_option'.*model_free=True"):+ AutoRound("unused-model-path", model_free=True, unsupported_option=True)++[email protected]("dtype_key", ["static_kv_dtype", "static_attention_dtype"])+def test_explicit_model_free_rejects_static_attention_quantization(dtype_key):+ with pytest.raises(ValueError, match="model_free=True.*static"):+ AutoRound("unused-model-path", scheme="W4A16", model_free=True, **{dtype_key: "fp8"})+++def test_explicit_model_free_rejects_unsupported_export_without_fallback():+ compressor = AutoRound("unused-model-path", model_free=True, scheme="W4A16")++ with pytest.raises(ValueError, match="model_free=True does not support format 'gguf:q4_k_m'"):+ compressor.quantize_and_save("unused-output", format="gguf:q4_k_m")++def test_fallback_construction_sees_save_format_when_no_construction_format(monkeypatch):"""The fallback exists to serve a quantize_and_save(format=...) call.diff --git a/test/unit/test_cpu/utils/test_model_free_utils.py b/test/unit/test_cpu/utils/test_model_free_utils.pyindex 571829b61..be474c713 100644--- a/test/unit/test_cpu/utils/test_model_free_utils.py+++ b/test/unit/test_cpu/utils/test_model_free_utils.py@@ -1281,7 +1281,11 @@ def test_model_free_route_accepts_mxfp_scheme_case_insensitively(scheme):@pytest.mark.parametrize("explicit", [False, True])def test_model_free_route_rejects_static_attention_quantization(dtype_key, explicit):kwargs = {dtype_key: "fp8", "model_free": explicit}- assert not is_model_free_route("model", "W4A16", 0, True, kwargs)+ if explicit:+ with pytest.raises(ValueError, match="model_free=True.*static"):+ is_model_free_route("model", "W4A16", 0, True, kwargs)+ else:+ assert not is_model_free_route("model", "W4A16", 0, True, kwargs)# ===========================================================================