Skip to content

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

fix: CLI ignore --scheme when bits/act_bits are unset (#2019)

Heng Guoc70e99f63048
diff --git a/auto_round/algorithms/quantization/sign_round/config.py b/auto_round/algorithms/quantization/sign_round/config.py
index a3af1799c..442cf2775 100644
--- a/auto_round/algorithms/quantization/sign_round/config.py
+++ b/auto_round/algorithms/quantization/sign_round/config.py
@@ -79,16 +79,10 @@ class SignRoundConfig(QuantizationConfig):
logger.warning("`iters` must be non-negative, reset it to 200")
self.iters = 200
- if not lr:
- # TODO need to check 4 bits lr setting for auto-round-best, 3bits only validate on small models
- if self.iters >= 1000 and self.bits is not None and self.bits <= 3:
- self.lr = 2.0 / self.iters
- logger.info("set the lr to 2.0/iters for better accuracy")
- else:
- self.lr = 1.0 / self.iters
- else:
- self.lr = lr
- self.minmax_lr = minmax_lr or self.lr
+ # lr/minmax_lr depend on `bits`, which may still be unresolved here
+ # (e.g. only `scheme=` was given) -- finalize_scheme() fills them in.
+ self.lr = lr
+ self.minmax_lr = minmax_lr
self.lr_scheduler = lr_scheduler
self.nblocks = nblocks
@@ -108,6 +102,17 @@ class SignRoundConfig(QuantizationConfig):
self.optimizer = optimizer
self.enable_adam = enable_adam
+ def finalize_scheme(self) -> None:
+ """Resolve lr/minmax_lr once `bits` is known (low-bit schemes use a higher lr)."""
+ if self.lr is None:
+ # TODO need to check 4 bits lr setting for auto-round-best, 3bits only validate on small models
+ if self.iters >= 1000 and self.bits is not None and self.bits <= 3:
+ self.lr = 2.0 / self.iters
+ logger.info("set the lr to 2.0/iters for better accuracy")
+ else:
+ self.lr = 1.0 / self.iters
+ self.minmax_lr = self.minmax_lr or self.lr
+
def check_configs(self) -> None:
"""Checks if the configurations are valid.
diff --git a/auto_round/compressors/entry.py b/auto_round/compressors/entry.py
index 380f1dc4f..7abf12767 100644
--- a/auto_round/compressors/entry.py
+++ b/auto_round/compressors/entry.py
@@ -251,13 +251,18 @@ def _build_model_type_ctor_kwargs(model, base_kwargs, mllm_kwargs, diffusion_kwa
def _select_rtn_compressor_base_cls(quant_config: RTNConfig, scheme, format, base_kwargs) -> type:
enable_imatrix = False
- resolved_attrs = {}
disable_opt_rtn = getattr(quant_config, "disable_opt_rtn", False)
- # If disable_opt_rtn was not explicitly set and scheme is W8A16/W8A8,
- # auto-disable optimization to improve efficiency.
+ # Preview resolved scheme attrs once (authoritative resolution happens later).
+ resolved_attrs = _preview_resolved_attrs(quant_config, scheme)
+
+ # Auto-disable rtn optimization for W8A16/W8A8-equivalent resolved schemes,
+ # unless the user already set disable_opt_rtn explicitly.
if getattr(quant_config, "orig_disable_opt_rtn", None) is None:
- if isinstance(scheme, str) and scheme.upper() in ["W8A16", "W8A8"]:
+ bits = resolved_attrs.get("bits", getattr(quant_config, "bits", None))
+ act_bits = resolved_attrs.get("act_bits", getattr(quant_config, "act_bits", None))
+ data_type = resolved_attrs.get("data_type", getattr(quant_config, "data_type", None))
+ if bits is not None and bits >= 8 and act_bits is not None and act_bits >= 8 and data_type == "int":
logger.warning("`disable_opt_rtn` is turned on for W8A16/W8A8 quantization to improve efficiency.")
disable_opt_rtn = True
quant_config.disable_opt_rtn = True
@@ -267,9 +272,6 @@ def _select_rtn_compressor_base_cls(quant_config: RTNConfig, scheme, format, bas
if has_gguf_k:
enable_imatrix = True
else:
- # Resolve scheme attrs for routing. SchemeMixin will do the authoritative
- # resolution later; this preview only chooses the compressor class.
- resolved_attrs = _preview_resolved_attrs(quant_config, scheme)
sym = resolved_attrs.get("sym", getattr(quant_config, "sym", None))
data_type = resolved_attrs.get("data_type", getattr(quant_config, "data_type", "") or "")
bits = resolved_attrs.get("bits", getattr(quant_config, "bits", None))
@@ -280,7 +282,6 @@ def _select_rtn_compressor_base_cls(quant_config: RTNConfig, scheme, format, bas
elif is_weight_scheme(scheme):
enable_imatrix = True
- resolved_attrs = resolved_attrs if not disable_opt_rtn else _preview_resolved_attrs(quant_config, scheme)
act_bits = resolved_attrs.get("act_bits", getattr(quant_config, "act_bits", None))
act_data_type = resolved_attrs.get("act_data_type", getattr(quant_config, "act_data_type", None))
act_dynamic = resolved_attrs.get("act_dynamic", getattr(quant_config, "act_dynamic", None))
diff --git a/test/test_cpu/core/test_pipeline_fail_fast.py b/test/test_cpu/core/test_pipeline_fail_fast.py
index cc6779028..84b68ca10 100644
--- a/test/test_cpu/core/test_pipeline_fail_fast.py
+++ b/test/test_cpu/core/test_pipeline_fail_fast.py
@@ -15,7 +15,9 @@ from auto_round.algorithms.quantization.rtn.quantizer import RTNQuantizer
from auto_round.compressors.base import collect_user_scheme_overrides
from auto_round.compressors.data_driven import DataDrivenCompressor
from auto_round.compressors.entry import AutoRound as NewAutoRound
+from auto_round.compressors.entry import _select_rtn_compressor_base_cls
from auto_round.logger import logger
+from auto_round.schemes import QuantizationScheme
class PartialSharedConfig(RTNConfig):
@@ -206,3 +208,53 @@ def test_user_scheme_overrides_reject_explicit_conflicts():
collect_user_scheme_overrides([AWQConfig(bits=8), RTNConfig(bits=4)])
with pytest.raises(ValueError, match="Conflicting shared scheme field 'bits'"):
resolve_shared_config_values([AWQConfig(bits=8), RTNConfig(bits=4)])
+
+
+# ===========================================================================
+# Scheme-dependent config heuristics must see resolved values, not just
+# whatever (often None) bits/lr the config was constructed with directly.
+# ===========================================================================
+
+
+ "scheme, expect_disable_opt_rtn",
+ [
+ ("W8A16", True),
+ # "INT8" (bits=8, act_bits=8, data_type=int) is W8A8-equivalent but was
+ # previously missed because routing only matched the literal strings
+ # "W8A16"/"W8A8", not schemes reaching the same resolved values.
+ ("INT8", True),
+ ("W4A16", False),
+ ({"bits": 8, "act_bits": 8, "data_type": "int", "sym": True}, True),
+ ],
+)
+def test_rtn_routing_disable_opt_rtn_from_resolved_scheme(scheme, expect_disable_opt_rtn):
+ config = RTNConfig()
+ _select_rtn_compressor_base_cls(config, scheme, "auto_round", {})
+ assert config.disable_opt_rtn is expect_disable_opt_rtn
+
+
+def test_rtn_routing_respects_explicit_enable_opt_rtn():
+ """An explicit user choice must not be clobbered by the W8A16/W8A8 heuristic."""
+ config = RTNConfig(enable_opt_rtn=True)
+ _select_rtn_compressor_base_cls(config, "W8A16", "auto_round", {})
+ assert config.disable_opt_rtn is False
+
+
[email protected]("bits, expected_lr", [(3, 2.0 / 1000), (4, 1.0 / 1000)])
+def test_sign_round_finalize_scheme_lr_heuristic(bits, expected_lr):
+ """The low-bit lr bump must apply once `bits` is resolved via the scheme,
+ even though it was unset (None) at construction time (e.g. `scheme=` alone,
+ no explicit `bits=`)."""
+ config = SignRoundConfig(iters=1000)
+ config.scheme = QuantizationScheme(bits=bits, act_bits=16, data_type="int")
+ config.finalize_scheme()
+ assert config.lr == expected_lr
+
+
+def test_sign_round_finalize_scheme_respects_explicit_lr():
+ config = SignRoundConfig(iters=1000, lr=0.01, minmax_lr=0.05)
+ config.scheme = QuantizationScheme(bits=2, act_bits=16, data_type="int")
+ config.finalize_scheme()
+ assert config.lr == 0.01
+ assert config.minmax_lr == 0.05