PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Fix mixed-precision accuracy regression when AutoScheme runs with CPU offloading and Hadamard rotation enabled (#1753)
Liang Lv65ae39e764a0
diff --git a/auto_round/auto_scheme/delta_loss.py b/auto_round/auto_scheme/delta_loss.pyindex 792c71d10..90449d2db 100644--- a/auto_round/auto_scheme/delta_loss.py+++ b/auto_round/auto_scheme/delta_loss.py@@ -994,7 +994,15 @@ def _gen_layer_config(_model_dir = model_nameif _model_dir is None and hasattr(model, "config"):_model_dir = getattr(model.config, "_name_or_path", None)- offload_context = OffloadManager(enabled=True, mode="clean", model_dir=_model_dir, cache_numel=True)+ offload_mode = "clean"+ offload_kwargs = {"model_dir": _model_dir}+ # Rotation mutates weights in memory before AutoScheme starts. Clean-mode+ # reloads from the original checkpoint and would silently discard those+ # transformed weights during scoring and final restore.+ if getattr(model, "rotation_config", None):+ offload_mode = "offload"+ offload_kwargs = {"offload_dir_prefix": "autoscheme", "retain_saved_entries": True}+ offload_context = OffloadManager(enabled=True, mode=offload_mode, cache_numel=True, **offload_kwargs)target_bits = auto_scheme.avg_bits# HF gates gradient checkpointing on ``self.training`` — it's a no-op in eval mode.diff --git a/auto_round/auto_scheme/utils.py b/auto_round/auto_scheme/utils.pyindex b6b744a5a..8a3ae951d 100644--- a/auto_round/auto_scheme/utils.py+++ b/auto_round/auto_scheme/utils.py@@ -66,8 +66,16 @@ def remove_quant_scheme(model: The model whose layers are to be cleared."""scheme_keys = [f.name for f in fields(QuantizationScheme)] + ["scale_dtype"]+ # `rotation_config` is a QuantizationScheme field but on the model root+ # it carries the active Hadamard rotation state (weights + hooks). Deleting+ # it there would silently corrupt the rotation transform, so we never strip+ # `rotation_config` from the top-level model object.+ root_preserve = {"rotation_config"}for n, m in model.named_modules():+ is_root = n == ""for key in scheme_keys:+ if is_root and key in root_preserve:+ continueif hasattr(m, key):delattr(m, key)diff --git a/auto_round/compressors/utils.py b/auto_round/compressors/utils.pyindex fb6c8a0e6..6b11de9d3 100644--- a/auto_round/compressors/utils.py+++ b/auto_round/compressors/utils.py@@ -418,6 +418,10 @@ def set_layer_config(all_module_names.append(n)# cleanup stale attributesfor key in scheme_keys:+ # `rotation_config` on the root model carries the active+ # Hadamard rotation state (weights + hooks)+ if n == "" and key == "rotation_config":+ continueif hasattr(m, key):delattr(m, key)if type(m) not in supported_types and m.__class__.__name__ not in inner_supported_types:diff --git a/auto_round/utils/offload.py b/auto_round/utils/offload.pyindex 5e2d6aefd..f0546db59 100644--- a/auto_round/utils/offload.py+++ b/auto_round/utils/offload.py@@ -272,6 +272,7 @@ class OffloadManager:model_dir: Optional[str] = None,offload_dir_prefix: str = "ar_offload",cache_numel: bool = False,+ retain_saved_entries: bool = False,):from auto_round import envs@@ -280,6 +281,7 @@ class OffloadManager:self.model_dir = model_dirself.cache_numel = cache_numelself._prefix = offload_dir_prefix+ self.retain_saved_entries = retain_saved_entries# Disk state (offload mode)self._tempdir: Optional[str] = None@@ -500,7 +502,8 @@ class OffloadManager:returnif self.mode == "offload":self._load_from_disk(name, module)- self._remove_saved_entry(name)+ if not self.retain_saved_entries:+ self._remove_saved_entry(name)else:if self.model_dir is None:logger.warning("OffloadManager: model_dir is required for clean mode")