PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
add other non-integer scheme support for awq (#1892)
Weiwei5a3ade8cd56b
diff --git a/auto_round/algorithms/pipeline.py b/auto_round/algorithms/pipeline.pyindex 39141eda9..9cc61d63d 100644--- a/auto_round/algorithms/pipeline.py+++ b/auto_round/algorithms/pipeline.py@@ -22,7 +22,7 @@ Design invariants (see AWQ_REFACTOR_PLAN.md §0.0 and §3.0):- ``QuantizationPipeline`` is the *first-class abstraction*; it is NOT justAWQ's helper.- All block-wise scheduling in ``DataDrivenCompressor`` operates against- ``QuantizationPipeline``, never against a concrete ``AWQQuantizer``.+ ``QuantizationPipeline``, never against a concrete ``AWQTransform``.- Single-algorithm use is expressed as``QuantizationPipeline(preprocessors=[], block_quantizer=q)``, which issemantically identical to the current direct-quantizer path.diff --git a/auto_round/algorithms/quantization/__init__.py b/auto_round/algorithms/quantization/__init__.pyindex 98acde190..51bedbec8 100644--- a/auto_round/algorithms/quantization/__init__.py+++ b/auto_round/algorithms/quantization/__init__.py@@ -37,8 +37,8 @@ def __getattr__(name):from auto_round.algorithms.transforms.awq.config import AWQConfigreturn AWQConfig- if name == "AWQQuantizer":- from auto_round.algorithms.transforms.awq.quantizer import AWQQuantizer+ if name == "AWQTransform":+ from auto_round.algorithms.transforms.awq.base import AWQTransform- return AWQQuantizer+ return AWQTransformraise AttributeError(f"module {__name__!r} has no attribute {name!r}")diff --git a/auto_round/algorithms/quantization/sign_roundv2/quantizer.py b/auto_round/algorithms/quantization/sign_roundv2/quantizer.pyindex 4d272172f..a3575cb52 100644--- a/auto_round/algorithms/quantization/sign_roundv2/quantizer.py+++ b/auto_round/algorithms/quantization/sign_roundv2/quantizer.py@@ -30,13 +30,14 @@ from auto_round.data_type.gguf import (search_gguf_scale_min_asym,search_gguf_scale_min_sym,)-from auto_round.data_type.int import quant_tensor_asym, quant_tensor_sym, search_scales-from auto_round.data_type.mxfp import quant_mx, search_mx_scale-from auto_round.data_type.nvfp import nv_fp4, search_nvfp4_scale+from auto_round.data_type.int import quant_tensor_asym, quant_tensor_symfrom auto_round.data_type.utils import (+ get_optimized_quant_func,+ reshape_imatrix_for_weight,reshape_pad_tensor_by_group_size,revert_tensor_by_pad,round_ste,+ search_optimized_init_scale,)from auto_round.logger import loggerfrom auto_round.utils import check_to_quantized, compile_func, get_reciprocal@@ -89,47 +90,66 @@ def _named_wrapper_block(wrapper_cls, name: str):return wrapped+def _dq_asym_group_size(bits: int) -> int:+ """Group size used by the GGUF asym double-quant path (16 for 2-bit, else 32)."""+ return 16 if bits == 2 else 32++class SignRoundOptimizedWrapperLinear(WrapperLinear):minmax_scale_bound = (0.0, 2.0)def _init_tuning_params_and_quant_func(self):super()._init_tuning_params_and_quant_func()- orig_weight = getattr(self.orig_layer, "get_weight", lambda: self.orig_layer.weight)()- if type(self.orig_layer) == transformers.pytorch_utils.Conv1D:- orig_weight = orig_weight.t()- weight_reshape, _, _ = reshape_pad_tensor_by_group_size(orig_weight.data, self.orig_layer.group_size)- if hasattr(self.orig_layer, "imatrix"):- imatrix = self.orig_layer.imatrix.reshape(1, -1)- imatrix = reshape_pad_tensor_by_group_size(imatrix, self.orig_layer.group_size, val=1e-5)[0].view(1, -1)- imatrix = imatrix.expand(weight_reshape.numel() // imatrix.numel(), -1)- imatrix = imatrix.reshape(weight_reshape.shape).to(orig_weight.device)- else:- imatrix = 1.0-- if self.orig_layer.data_type.startswith("int"):- self.init_scale = search_scales(weight_reshape, self.orig_layer.bits, imatrix)- self.init_scale = torch.where(- self.init_scale < 0,- torch.clamp(self.init_scale, max=-self.q_scale_thresh),- torch.clamp(self.init_scale, min=self.q_scale_thresh),+ layer = self.orig_layer+ data_type = layer.data_type+ weight_reshape = self._prepare_init_scale_weight()+ imatrix = reshape_imatrix_for_weight(getattr(layer, "imatrix", None), weight_reshape, layer.group_size)++ self.init_scale = search_optimized_init_scale(+ weight_reshape, data_type, layer.bits, imatrix, self.q_scale_thresh+ )+ self.weight_quant_func = get_optimized_quant_func(data_type)+ if self.init_scale is None or self.weight_quant_func is None:+ raise ValueError(+ f"SignRound optimized path does not support data_type={data_type!r}; "+ "expected a symmetric int / mx / nv type.")- self.weight_quant_func = quant_tensor_sym- elif self.orig_layer.data_type.startswith("mx"):- self.init_scale = search_mx_scale(weight_reshape, self.orig_layer.bits, imatrix)- self.weight_quant_func = quant_mx- elif self.orig_layer.data_type.startswith("nv"):- self.init_scale = search_nvfp4_scale(weight_reshape, self.orig_layer.bits, imatrix)- self.weight_quant_func = nv_fp4- else:- raise ValueError(f"unsupported SignRound optimized data type: {self.orig_layer.data_type}")- self.data_type = self.orig_layer.data_type- if hasattr(self.orig_layer, "imatrix"):- delattr(self.orig_layer, "imatrix")+ self.data_type = data_type+ if hasattr(layer, "imatrix"):+ del layer.imatrixif self.enable_torch_compile:self.weight_quant_func = compile_func(self.weight_quant_func, self.device)+ def _prepare_init_scale_weight(self) -> torch.Tensor:+ """Return the group-reshaped weight that seeds the init-scale search.++ Reproduces the layout the quant func sees at tuning time: the (optionally+ transposed) FP weight grouped to ``[-1, group_size]``. When AWQ ran in+ ``clip_as_init`` mode and stored a per-group ``awq_clip_max`` on the layer,+ the weight is clamped to that range first so the searched ``init_scale``+ already reflects the AWQ clip (``max_scale`` then tunes a coefficient on+ top of it).+ """+ layer = self.orig_layer+ weight = layer.get_weight() if hasattr(layer, "get_weight") else layer.weight+ if isinstance(layer, transformers.pytorch_utils.Conv1D):+ weight = weight.t()+ weight_reshape, _, _ = reshape_pad_tensor_by_group_size(weight.data, layer.group_size)++ clip_max = getattr(layer, "awq_clip_max", None)+ if clip_max is not None:+ clip_max = clip_max.reshape(-1, 1).to(weight_reshape.device, weight_reshape.dtype)+ if clip_max.shape[0] == weight_reshape.shape[0]:+ weight_reshape = torch.clamp(weight_reshape, -clip_max, clip_max)+ else:+ logger.warning_once(+ "SignRoundV2: ignoring awq_clip_max with shape %s incompatible with "+ "grouped weight shape %s." % (tuple(clip_max.shape), tuple(weight_reshape.shape))+ )+ return weight_reshape+class SignRoundDQWrapperLinear(WrapperLinear):minmax_scale_bound = (0.5, 1.5)@@ -187,7 +207,7 @@ class SignRoundDQWrapperLinear(WrapperLinear):imatrix = getattr(self.orig_layer, "imatrix", None)if self._dq_kind == "asym":- group_size = 16 if bits == 2 else 32+ group_size = _dq_asym_group_size(bits)t, _, _ = reshape_pad_tensor_by_group_size(weight.to(torch.float32), group_size)v_r = vif isinstance(v, torch.Tensor):@@ -261,7 +281,7 @@ class SignRoundDQWrapperLinear(WrapperLinear):bits = self.orig_layer.bitsif self._dq_kind == "asym":- group_size = 16 if bits == 2 else 32+ group_size = _dq_asym_group_size(bits)weight_q = self.weight_quant_func(weight,params["scale"],diff --git a/auto_round/algorithms/registry.py b/auto_round/algorithms/registry.pyindex d3a16e6cb..7fe106b59 100644--- a/auto_round/algorithms/registry.py+++ b/auto_round/algorithms/registry.py@@ -46,7 +46,7 @@ def _ensure_pipeline_members_registered() -> None:"auto_round.algorithms.quantization.sign_round.quantizer","auto_round.algorithms.quantization.sign_roundv2.quantizer","auto_round.algorithms.quantization.adam_round.adam",- "auto_round.algorithms.transforms.awq.quantizer",+ "auto_round.algorithms.transforms.awq.base",):importlib.import_module(module_name)_pipeline_members_registered = Truediff --git a/auto_round/algorithms/transforms/__init__.py b/auto_round/algorithms/transforms/__init__.pyindex 7a28b480a..f7efbfbea 100644--- a/auto_round/algorithms/transforms/__init__.py+++ b/auto_round/algorithms/transforms/__init__.py@@ -90,10 +90,10 @@ def __getattr__(name):from auto_round.algorithms.transforms.awq.config import AWQConfigreturn AWQConfig- if name == "AWQQuantizer":- from auto_round.algorithms.transforms.awq.quantizer import AWQQuantizer+ if name == "AWQTransform":+ from auto_round.algorithms.transforms.awq.base import AWQTransform- return AWQQuantizer+ return AWQTransformraise AttributeError(f"module {__name__!r} has no attribute {name!r}")diff --git a/auto_round/algorithms/transforms/awq/__init__.py b/auto_round/algorithms/transforms/awq/__init__.pyindex 5637a3e0b..9bbfcd907 100644--- a/auto_round/algorithms/transforms/awq/__init__.py+++ b/auto_round/algorithms/transforms/awq/__init__.py@@ -13,6 +13,6 @@# limitations under the License.from auto_round.algorithms.transforms.awq.config import AWQConfig-from auto_round.algorithms.transforms.awq.quantizer import AWQQuantizer+from auto_round.algorithms.transforms.awq.base import AWQTransform-__all__ = ["AWQConfig", "AWQQuantizer"]+__all__ = ["AWQConfig", "AWQTransform"]diff --git a/auto_round/algorithms/transforms/awq/quantizer.py b/auto_round/algorithms/transforms/awq/base.pysimilarity index 52%rename from auto_round/algorithms/transforms/awq/quantizer.pyrename to auto_round/algorithms/transforms/awq/base.pyindex 0908ac78c..84e1abfb2 100644--- a/auto_round/algorithms/transforms/awq/quantizer.py+++ b/auto_round/algorithms/transforms/awq/base.py@@ -29,8 +29,8 @@ Reference implementations:from __future__ import annotations-import gcimport inspect+import refrom contextlib import contextmanagerfrom typing import TYPE_CHECKING, Any@@ -49,9 +49,9 @@ from auto_round.algorithms.transforms.awq.mappings import (check_model_compatibility,resolve_mappings,)+from auto_round.algorithms.transforms.awq.qdq import QDQToolfrom auto_round.algorithms.transforms.base import BaseWeightTransformerfrom auto_round.data_type.utils import (- get_quant_func,reshape_pad_tensor_by_group_size,revert_tensor_by_pad,)@@ -61,9 +61,56 @@ if TYPE_CHECKING:from auto_round.algorithms.pipeline import BlockContext+# Known normalization classes whose ``forward`` computes+# ``output = (1 + weight) * x_norm`` (Gemma-style "unit-offset" RMSNorm) rather+# than the standard ``output = weight * x_norm``. Folding an AWQ smoothing scale+# ``s`` into such a layer requires ``weight <- (1 + weight) / s - 1`` instead of+# ``weight <- weight / s``; using the wrong fold silently breaks AWQ's output+# invariance and severely degrades accuracy (e.g. Qwen3.5, Gemma2/3, Qwen3-Next).+_UNIT_OFFSET_RMSNORM_NAMES = frozenset(+ {+ "GemmaRMSNorm",+ "Gemma2RMSNorm",+ "Gemma3RMSNorm",+ "Gemma3TextRMSNorm",+ "Qwen3_5RMSNorm",+ "Qwen3_5MoeRMSNorm",+ "Qwen3NextRMSNorm",+ }+)++# Detects ``1 + self.weight`` / ``self.weight + 1`` in a norm's forward source.+_UNIT_OFFSET_SRC_RE = re.compile(r"1(\.0)?\s*\+\s*self\.weight|self\.weight(\.float\(\))?\s*\+\s*1")++# Cache the unit-offset decision per norm class to avoid repeated source parsing.+_unit_offset_cache: dict[type, bool] = {}+++def _rmsnorm_has_unit_offset(module: torch.nn.Module) -> bool:+ """Return True if ``module`` applies a Gemma-style ``(1 + weight)`` gain.++ Uses a fast class-name allowlist, falling back to source inspection of the+ module's ``forward`` so newly-added Gemma-style norms are detected without a+ code change. Result is cached per class.+ """+ cls = type(module)+ cached = _unit_offset_cache.get(cls)+ if cached is not None:+ return cached+ result = cls.__name__ in _UNIT_OFFSET_RMSNORM_NAMES+ if not result:+ try:+ src = inspect.getsource(cls.forward)+ result = bool(_UNIT_OFFSET_SRC_RE.search(src))+ except (OSError, TypeError):+ result = False+ _unit_offset_cache[cls] = result+ return result++@register_pipeline_member(AWQConfig)-class AWQQuantizer(BaseWeightTransformer):- """AWQ quantizer: activation-aware weight smoothing pre-processor.+class AWQTransform(BaseWeightTransformer):+ """AWQ transform: activation-aware weight smoothing pre-processor.Inherits :class:`~auto_round.algorithms.transforms.base.BaseWeightTransformer`.It smooths block weights in-place; actual weight compression (RTN /@@ -74,6 +121,24 @@ class AWQQuantizer(BaseWeightTransformer):super().__init__(config)self.duo_scaling: bool | str = config.duo_scalingself.n_grid: int = config.n_grid+ self.smooth_iters: int = getattr(config, "smooth_iters", 1)++ # AWQ weight-clip options (search + hard-clamp after smoothing).+ self.apply_clip: bool = getattr(config, "apply_clip", False)+ self.clip_as_init: bool = getattr(config, "clip_as_init", False)+ self.clip_n_grid: int = getattr(config, "clip_n_grid", 20)+ self.clip_max_shrink: float = getattr(config, "clip_max_shrink", 0.5)+ self.clip_n_sample_token: int = getattr(config, "clip_n_sample_token", 512)++ # Single source of truth for "QDQ a candidate weight under the target+ # block-quantizer scheme", used as AWQ's grid-search / clip loss. AWQ+ # composes this instead of re-implementing block-quantizer dispatch;+ self._qdq_tool = QDQTool(+ bits=config.bits,+ group_size=config.group_size,+ sym=config.sym,+ data_type=config.data_type,+ )self._user_mappings: list[dict] | None = config.mappings@@ -86,6 +151,9 @@ class AWQQuantizer(BaseWeightTransformer):self._activation_stats: dict[str, list] = {}self._parent_args_cache: dict[torch.nn.Module, list[dict]] = {}self._parent_signatures: dict[int, inspect.Signature] = {}+ # Per-mapping balance-layer input features captured for the clip search+ # (keyed by smooth_name). Only populated when ``apply_clip`` is set.+ self._clip_input_feat: dict[str, torch.Tensor] = {}self._finalized: bool = False@@ -125,6 +193,8 @@ class AWQQuantizer(BaseWeightTransformer):prefix = _extract_block_prefix(m.smooth_name)self._block_mappings.setdefault(prefix, []).append(m)+ self._qdq_tool.configure(compressor)+if compressor.compress_context is not None:compressor.compress_context.cache_device = torch.device("cpu")@@ -174,7 +244,12 @@ class AWQQuantizer(BaseWeightTransformer):if not block_mappings:logger.debug("AWQ: no mappings for block '%s', skipping.", block_name)return+ # The compressor sets ``layer_config`` after ``prepare_run``; keep the+ # QDQ service in sync before it is used for the grid-search / clip loss.+ self._qdq_tool.layer_config = self.layer_configself._smooth_block(block_name, block_mappings)+ if self.apply_clip:+ self._clip_block(block_name, block_mappings)modified = []for mapping in block_mappings:modified.extend(mapping.balance_names)@@ -188,6 +263,12 @@ class AWQQuantizer(BaseWeightTransformer):returnfor m in block_mappings:self._activation_stats.pop(m.smooth_name, None)+ self._clip_input_feat.pop(m.smooth_name, None)+ # Drop the transient clip attribute once the block quantizer has+ # consumed it (the persistent copy lives on the model context).+ for bl in m.balance_layers:+ if hasattr(bl, "awq_clip_max"):+ delattr(bl, "awq_clip_max")seen_parents: set[int] = set()for m in block_mappings:pid = id(m.parent)@@ -202,6 +283,7 @@ class AWQQuantizer(BaseWeightTransformer):self._activation_stats.clear()self._parent_args_cache.clear()self._parent_signatures.clear()+ self._clip_input_feat.clear()self._finalized = Truelogger.debug("AWQ: finalize_quantization complete.")@@ -247,7 +329,38 @@ class AWQQuantizer(BaseWeightTransformer):h = module.register_forward_hook(_make_stats_hook(full_name))handles.append(h)- # ── Parent-kwargs hooks ───────────────────────────────────────────────+ # ── Clip input-feature hooks (only when apply_clip is enabled) ────────+ # The balance layers of a mapping share the same input; capture it once+ # per mapping (keyed by smooth_name) for the post-smooth clip search.+ if self.apply_clip:+ for mapping in mappings:+ if not mapping.balance_layers:+ continue+ target_layer = mapping.balance_layers[0]++ def _make_clip_hook(smooth_name: str):++ def hook_fn(mod, args):+ x = args[0] if isinstance(args, tuple) else args+ if x is None or not isinstance(x, torch.Tensor) or x.numel() == 0:+ return+ feat = x.detach().reshape(-1, x.shape[-1])+ # Subsample tokens to bound memory.+ if feat.shape[0] > self.clip_n_sample_token:+ step = max(1, feat.shape[0] // self.clip_n_sample_token)+ feat = feat[::step]+ feat = feat.float().cpu()+ prev = self._clip_input_feat.get(smooth_name)+ if prev is None:+ self._clip_input_feat[smooth_name] = feat+ else:+ self._clip_input_feat[smooth_name] = torch.cat([prev, feat], dim=0)++ return hook_fn++ h = target_layer.register_forward_pre_hook(_make_clip_hook(mapping.smooth_name))+ handles.append(h)+# One forward_pre_hook per unique parent module in the current block.parent_modules_hooked: set[int] = set()for mapping in mappings:@@ -320,33 +433,45 @@ class AWQQuantizer(BaseWeightTransformer):# ── Smoothing (grid search + scale apply) ─────────────────────────────────def _smooth_block(self, block_prefix: str, block_mappings: list) -> None:- """Run grid search and apply AWQ scales for one block."""- for mapping in block_mappings:- if mapping.smooth_name not in self._activation_stats:- logger.warning(- "AWQ: no activation stats for '%s' in block '%s'; skipping.",- mapping.smooth_name,- block_prefix,- )- continue+ """Run grid search and apply AWQ scales for one block.- act_sum, act_count = self._activation_stats[mapping.smooth_name]- if act_count == 0:- logger.warning(- "AWQ: zero activation count for '%s' in block '%s'; skipping.",- mapping.smooth_name,- block_prefix,- )- continue+ When ``smooth_iters > 1`` the grid search + scale apply is repeated.+ Repeating refines the smoothing scale because the mx max_scale search+ and the AWQ alpha (ratio) search influence each other: each extra pass+ re-derives the max_scale from the freshly-smoothed weights and+ re-searches the ratio, accumulating the resulting scales.+ """+ n_passes = max(1, int(self.smooth_iters))+ for smooth_pass in range(n_passes):+ for mapping in block_mappings:+ if mapping.smooth_name not in self._activation_stats:+ logger.warning(+ "AWQ: no activation stats for '%s' in block '%s'; skipping.",+ mapping.smooth_name,+ block_prefix,+ )+ continue++ act_sum, act_count = self._activation_stats[mapping.smooth_name]+ if act_count == 0:+ logger.warning(+ "AWQ: zero activation count for '%s' in block '%s'; skipping.",+ mapping.smooth_name,+ block_prefix,+ )+ continue++ x_mean = (act_sum / act_count).to(torch.float32)+ del act_sum- x_mean = (act_sum / act_count).to(torch.float32)- del act_sum+ best_scales = self._grid_search_scales(mapping, x_mean)+ if best_scales is not None:+ self._apply_scales(mapping, best_scales)- best_scales = self._grid_search_scales(mapping, x_mean)- if best_scales is not None:- self._apply_scales(mapping, best_scales)+ if n_passes > 1:+ logger.debug("AWQ: completed smooth pass %d/%d for block '%s'", smooth_pass + 1, n_passes, block_prefix)- # Release parent kwargs after ALL mappings for this block are processed.+ # Release parent kwargs after ALL passes/mappings for this block are processed.seen_parents: set[int] = set()for mapping in block_mappings:pid = id(mapping.parent)@@ -369,12 +494,22 @@ class AWQQuantizer(BaseWeightTransformer):case _:raise ValueError(f"Unexpected duo_scaling value: {self.duo_scaling!r}")+ @staticmethod+ def _normalize_group_size(group_size: int | None, fallback: int) -> int:+ """Return ``group_size`` if it denotes a real per-group size, else ``fallback``.++ A ``None``, ``0`` or negative ``group_size`` means "no grouping" (per-row),+ which each caller represents with its own sentinel (``-1`` for the quant+ funcs, the row width for weight reshaping).+ """+ return group_size if (group_size is not None and group_size > 0) else fallback+@staticmethoddef _compute_layer_means(layers: list[torch.nn.Module], group_size: int) -> torch.Tensor:"""Per-channel mean of normalised weights across all balance layers."""weight = torch.cat([m.weight.detach().float() for m in layers], dim=0)org_shape = weight.shape- gs = group_size if group_size > 0 else org_shape[1]+ gs = AWQTransform._normalize_group_size(group_size, org_shape[1])weight, _, pad_len = reshape_pad_tensor_by_group_size(weight, gs)w_scale = weight.abs() / (weight.abs().amax(dim=1, keepdim=True) + 1e-6)w_scale = revert_tensor_by_pad(w_scale, orig_shape=org_shape, pad_len=pad_len)@@ -390,7 +525,7 @@ class AWQQuantizer(BaseWeightTransformer):device = mapping.balance_layers[0].weight.devicex_mean = x_mean.to(device)- group_size = self.group_size if (self.group_size is not None and self.group_size > 0) else -1+ group_size = self._normalize_group_size(self._qdq_tool.group_size, -1)if self.duo_scaling is not False:w_mean = self._compute_layer_means(mapping.balance_layers, group_size).to(device)@@ -406,22 +541,11 @@ class AWQQuantizer(BaseWeightTransformer):if not use_parent_forward:orig_weights = orig_state # same reference is fine- # Pre-resolve quant function once to avoid repeated dispatch in loop.- ref_layer = mapping.balance_layers[0]- ref_name = getattr(ref_layer, "global_name", None) or ""- ref_cfg = (self.layer_config or {}).get(ref_name, {})- try:- cached_quant_func, _ = get_quant_func(- ref_cfg.get("data_type", self.data_type),- ref_cfg.get("bits", self.bits),- ref_cfg.get("sym", self.sym),- disable_opt_rtn=True,- group_size=ref_cfg.get("group_size", self.group_size),- iters=0,- )- except Exception as exc:- logger.debug("AWQ: failed to pre-resolve quant function for '%s': %s", ref_name, exc)- cached_quant_func = None+ # Resolve each balance layer's scheme once, then pre-resolve the quant+ # functions for the grid-search loop. ``opt_quant_func`` is non-None only+ # when the SignRoundV2 optimized init-scale path applies for this mapping.+ bl_params = {bl: self._qdq_tool.resolve_params(bl) for bl in mapping.balance_layers}+ cached_quant_func, opt_quant_func = self._qdq_tool.resolve_quant_funcs(bl_params[mapping.balance_layers[0]])best_error = float("inf")best_scales = None@@ -438,13 +562,18 @@ class AWQQuantizer(BaseWeightTransformer):scales_view = scales.view(1, -1).to(device)if use_parent_forward:+ # Quantize each balance layer's smoothed weight and write the+ # de-smoothed result back, so the parent forward below sees the+ # weights the layer would actually compute with.for bl in mapping.balance_layers:- bl.weight.data.copy_(orig_state[bl] * scales_view)- w_qdq = self._quantize_dequantize_weight(bl, bl.weight.data.float(), quant_func=cached_quant_func)- if w_qdq is not None:- bl.weight.data = (w_qdq / scales_view).to(bl.weight.dtype)- else:- bl.weight.data.copy_(orig_state[bl])+ w_qdq = self._qdq_tool.qdq(+ orig_state[bl] * scales_view,+ bl_params[bl],+ quant_func=cached_quant_func,+ opt_quant_func=opt_quant_func,+ imatrix=getattr(bl, "imatrix", None),+ )+ bl.weight.data = (w_qdq / scales_view).to(bl.weight.dtype)int_w_outputs = self._run_parent_samples(mapping.parent, parent_kwargs_list)total_loss = self._compute_loss(fp16_outputs, int_w_outputs)@@ -455,10 +584,13 @@ class AWQQuantizer(BaseWeightTransformer):total_loss = 0.0for bl in mapping.balance_layers:w_orig = orig_weights[bl].to(device)- w_qdq = self._quantize_dequantize_weight(bl, w_orig * scales_view, quant_func=cached_quant_func)- if w_qdq is None:- total_loss = float("inf")- break+ w_qdq = self._qdq_tool.qdq(+ w_orig * scales_view,+ bl_params[bl],+ quant_func=cached_quant_func,+ opt_quant_func=opt_quant_func,+ imatrix=getattr(bl, "imatrix", None),+ )total_loss += (w_orig - w_qdq / scales_view).pow(2).sum().item()if total_loss < best_error:@@ -507,61 +639,194 @@ class AWQQuantizer(BaseWeightTransformer):return float("inf")return (loss / num_elements).item()- def _quantize_dequantize_weight(+ @torch.no_grad()+ def _apply_scales(self, mapping: ResolvedMapping, scales: torch.Tensor) -> None:+ """Apply computed AWQ scales to smooth and balance layers in-place.++ Each balance layer's input channels are multiplied by ``scales`` while+ the upstream smooth layer's output is divided by the same factor, so the+ block's overall function is preserved and quantization difficulty is+ shifted off the balance weights.+ """+ for bl in mapping.balance_layers:+ bl.weight.data.mul_(scales.to(bl.weight.device).view(1, -1))++ self._fold_scales_into_smooth_layer(mapping.smooth_layer, scales)++ # Keep the captured clip input features consistent with the smoothing:+ # the balance-layer input is divided by the smooth scales, so the stored+ # features (used later by the clip search) must be divided too.+ if self.apply_clip:+ feat = self._clip_input_feat.get(mapping.smooth_name)+ if feat is not None:+ feat.div_(scales.detach().to(feat.device).view(1, -1))++ @staticmethod+ @torch.no_grad()+ def _fold_scales_into_smooth_layer(smooth: torch.nn.Module, scales: torch.Tensor) -> None:+ """Divide a smooth layer's output by ``scales`` to offset balance scaling.++ Dispatches on the smooth layer's weight layout:++ * 1-D norm weight with a Gemma-style ``(1 + weight)`` gain: folded as+ ``weight <- (1 + weight) / s - 1`` to preserve output invariance.+ * 1-D standard norm weight: folded as ``weight <- weight / s``.+ * 2-D linear weight: its trailing ``s.numel()`` output rows are divided.++ Any bias is always divided by ``s``.+ """+ s = scales.to(smooth.weight.device)+ weight = smooth.weight.data+ if weight.ndim == 1:+ if _rmsnorm_has_unit_offset(smooth):+ weight.copy_((1.0 + weight) / s - 1.0)+ else:+ weight.div_(s)+ else:+ weight[-s.size(0) :].div_(s.view(-1, 1))++ if getattr(smooth, "bias", None) is not None:+ smooth.bias.data.div_(s)++ # ── Weight clipping (search best per-group clip + hard-clamp) ─────────────++ # Layers whose clipping is skipped: clipping q/k projections hurts the+ # attention score (q·kᵀ) precision, mirroring AutoAWQ's ``avoid_clipping``.+ _AVOID_CLIP_TOKENS = ("q_", "k_", "query", "key", "Wqkv", "wqkv")++ def _should_skip_clip(self, balance_name: str) -> bool:+ local = balance_name.rsplit(".", 1)[-1]+ return any(token in local for token in self._AVOID_CLIP_TOKENS)++ @torch.no_grad()+ def _clip_block(self, block_prefix: str, block_mappings: list) -> None:+ """Search per-group weight clip thresholds for one block.++ Runs after smoothing. The searched per-group clip magnitude is always+ recorded on the model context (and, in ``clip_as_init`` mode, on the+ balance layer) so it is kept for downstream use. Two modes:++ * ``clip_as_init=False`` (default): the clip is hard-clamped in place on+ the (already smoothed) balance-layer weights, so any downstream block+ quantizer (RTN / SignRound / SignRoundV2) re-derives its min/max+ range from the clipped weights.+ * ``clip_as_init=True``: the weights are left untouched and the clip is+ stored on the layer (``awq_clip_max``); the downstream SignRound /+ SignRoundV2 quantizer uses it to *initialize* its tunable weight range+ (capping ``weight_min``/``weight_max`` or clamping before the scale+ search) and then tunes ``min_scale``/``max_scale`` on top.+ """+ clip_store = getattr(self.model_context, "awq_clip_values", None)+ for mapping in block_mappings:+ feat = self._clip_input_feat.get(mapping.smooth_name)+ if feat is None:+ logger.warning(+ "AWQ: no clip input features for '%s' in block '%s'; skipping clip.",+ mapping.smooth_name,+ block_prefix,+ )+ continue+ for bl, name in zip(mapping.balance_layers, mapping.balance_names):+ if self._should_skip_clip(name):+ logger.debug("AWQ: skip clip for '%s' (avoid-clipping layer).", name)+ continue+ max_val = self._compute_best_clip(bl, feat)+ if max_val is None:+ continue+ key = getattr(bl, "global_name", None) or name+ if clip_store is not None:+ clip_store[key] = max_val.detach().to("cpu")+ if self.clip_as_init:+ # Keep the weights intact; hand the clip to the block+ # quantizer as the initialization of its weight range.+ bl.awq_clip_max = max_val.detach()+ else:+ self._apply_clip(bl, max_val)++ @torch.no_grad()+ def _compute_best_clip(self,layer: torch.nn.Module,- weight: torch.Tensor,- quant_func=None,+ input_feat: torch.Tensor,) -> torch.Tensor | None:- """Quantize-dequantize a weight tensor using the layer's config.+ """Search the per-group clip threshold that minimizes output MSE.- Used internally for grid search loss calculation only. Does NOT- modify the layer's stored weights.+ Returns a ``[out_channels, n_group]`` tensor of clip magnitudes, or+ ``None`` if clipping is not applicable to this layer."""- layer_name = getattr(layer, "global_name", None) or ""- config = (self.layer_config or {}).get(layer_name, {})- bits = config.get("bits", self.bits)- group_size = config.get("group_size", self.group_size)- sym = config.get("sym", self.sym)- data_type = config.get("data_type", self.data_type)+ params = self._qdq_tool.resolve_params(layer)+ bits = params["bits"]+ if bits is None or bits >= 16:+ return None+ group_size = params["group_size"]- if quant_func is None:- try:- quant_func, _ = get_quant_func(- data_type,- bits,- sym,- disable_opt_rtn=True,- group_size=group_size,- iters=0,- )- except Exception as exc:- logger.debug("AWQ: failed to resolve quant function for '%s': %s", layer_name, exc)- return None+ device = layer.weight.device+ weight = layer.weight.detach().float()+ out_features, in_features = weight.shape+ gs = self._normalize_group_size(group_size, in_features)+ if in_features % gs != 0:+ logger.warning(+ "AWQ: in_features=%d not divisible by group_size=%d for clip; skipping '%s'.",+ in_features,+ gs,+ getattr(layer, "global_name", "") or "<layer>",+ )+ return None+ n_group = in_features // gs+ # Clip search is a flat per-group weight QDQ: substitute the normalized+ # group size and drop super-block (double-quant) params, which the clip+ # path does not apply.+ clip_params = {**params, "group_size": gs, "super_bits": None, "super_group_size": None}+ quant_func, _ = self._qdq_tool.resolve_quant_funcs(clip_params)if quant_func is None:return None- try:- qdq_weight, _, _ = quant_func(weight, bits=bits, group_size=group_size)- return qdq_weight- except Exception as exc:- logger.debug("AWQ: quantize-dequantize failed for '%s': %s", layer_name, exc)- return None+ feat = input_feat.to(device).reshape(-1, in_features)+ if feat.shape[0] > self.clip_n_sample_token:+ step = max(1, feat.shape[0] // self.clip_n_sample_token)+ feat = feat[::step]+ # [1, n_token, n_group, gs]+ feat = feat.reshape(1, feat.shape[0], n_group, gs)++ # [out_features, 1, n_group, gs]+ w = weight.reshape(out_features, 1, n_group, gs)++ # Batch output channels to bound peak memory.+ oc_batch = 256 if out_features % 256 == 0 else (64 if out_features % 64 == 0 else out_features)+ best_max_val_all = []+ n_steps = max(1, int(self.clip_max_shrink * self.clip_n_grid))++ for i_b in range(0, out_features, oc_batch):+ w_b = w[i_b : i_b + oc_batch]+ org_max_val = w_b.abs().amax(dim=-1, keepdim=True) # [oc_b, 1, n_group, 1]+ best_max_val = org_max_val.clone()+ min_errs = torch.full_like(org_max_val, 1e9)+ org_out = (feat * w_b).sum(dim=-1) # [oc_b, n_token, n_group]++ for i_s in range(n_steps):+ max_val = org_max_val * (1 - i_s / self.clip_n_grid)+ cur_w = torch.clamp(w_b, -max_val, max_val)+ cur_w_flat = cur_w.reshape(cur_w.shape[0], n_group * gs)+ q_w = self._qdq_tool.qdq(cur_w_flat, clip_params, quant_func=quant_func).reshape(cur_w.shape)+ cur_out = (feat * q_w).sum(dim=-1)+ err = (cur_out - org_out).pow(2).mean(dim=1).view(min_errs.shape)+ improved = err < min_errs+ min_errs[improved] = err[improved]+ best_max_val[improved] = max_val[improved]+ del cur_w, q_w, cur_out++ best_max_val_all.append(best_max_val)++ best_max_val = torch.cat(best_max_val_all, dim=0)+ return best_max_val.squeeze(1) # [out_features, n_group, 1]@torch.no_grad()- def _apply_scales(self, mapping: ResolvedMapping, scales: torch.Tensor) -> None:- """Apply computed AWQ scales to smooth and balance layers in-place."""- for bl in mapping.balance_layers:- s = scales.to(bl.weight.device).view(1, -1)- bl.weight.data.mul_(s)-- smooth = mapping.smooth_layer- s = scales.to(smooth.weight.device)- if smooth.weight.ndim == 1:- smooth.weight.data.div_(s)- else:- smooth.weight.data[-s.size(0) :].div_(s.view(-1, 1))-- if hasattr(smooth, "bias") and smooth.bias is not None:- smooth.bias.data.div_(s)+ def _apply_clip(self, layer: torch.nn.Module, max_val: torch.Tensor) -> None:+ """Hard-clamp the layer weight to ``[-max_val, max_val]`` per group."""+ org_dtype = layer.weight.dtype+ max_val = max_val.to(device=layer.weight.device, dtype=org_dtype)+ org_shape = layer.weight.shape+ w = layer.weight.data.reshape(*max_val.shape[:2], -1)+ w = torch.clamp(w, -max_val, max_val)+ layer.weight.data = w.reshape(org_shape).to(org_dtype)diff --git a/auto_round/algorithms/transforms/awq/config.py b/auto_round/algorithms/transforms/awq/config.pyindex c2c580cb4..b5b620735 100644--- a/auto_round/algorithms/transforms/awq/config.py+++ b/auto_round/algorithms/transforms/awq/config.py@@ -13,6 +13,7 @@# limitations under the License.from auto_round.algorithms.quantization.config import QuantizationConfig+from auto_round.logger import loggerclass AWQConfig(QuantizationConfig):@@ -40,9 +41,15 @@ class AWQConfig(QuantizationConfig):nsamples: int = 128,batch_size: int = 8,apply_smooth: bool = True,+ smooth_iters: int = 1,+ apply_clip: bool = False,+ clip_as_init: bool = False,+ clip_n_grid: int = 20,+ clip_max_shrink: float = 0.5,+ clip_n_sample_token: int = 512,mappings: list[dict] | None = None,**kwargs,- ) -> None:+ ):"""Initialize an AWQ configuration.Args:@@ -60,6 +67,43 @@ class AWQConfig(QuantizationConfig):AWQ entry points.apply_smooth: Whether to apply AWQ smoothing before the downstreamblock quantizer.+ smooth_iters: Number of times the per-block smooth (grid search ++ scale apply) is repeated. Repeating refines the smoothing scale+ because the mx max_scale search and the AWQ alpha search affect+ each other; each extra pass re-derives the max_scale from the+ freshly-smoothed weights and re-searches alpha, accumulating the+ resulting scales. ``1`` reproduces the original single-pass AWQ.+ apply_clip: Whether to search and apply AWQ weight clipping after+ smoothing. When True, AWQ searches a per-group clipping+ threshold for each balance layer (minimizing output MSE) and+ hard-clamps the weights to ``[-max_val, max_val]`` in place.+ This is a pure weight transformation, so it composes with any+ downstream block quantizer: a SignRound/SignRoundV2 quantizer+ re-derives its ``weight_min``/``weight_max`` from the clamped+ weights (the AWQ clip becomes the initialization) and then tunes+ ``min_scale``/``max_scale`` on top of it. Set False to keep the+ original smooth-only behavior (the block quantizer performs its+ own clip/min-max tuning).+ clip_as_init: Selects how the searched clip threshold is consumed+ (only relevant when ``apply_clip`` is True). When False+ (default), the weights are hard-clamped in place. When True,+ the weights are left untouched and the per-group clip magnitude+ is instead stored (on the model context and on each balance+ layer) so the downstream block quantizer uses it to *initialize*+ its tunable weight range: SignRound caps its+ ``weight_min``/``weight_max`` to the clip and tunes+ ``min_scale``/``max_scale`` on top, while SignRoundV2 clamps the+ weight before its scale search. This works for both symmetric+ and asymmetric quantization and any data type, analogous to+ ``enable_minmax_tuning``.+ clip_n_grid: Number of shrink steps used when searching the AWQ+ clipping threshold.+ clip_max_shrink: Maximum fraction by which the per-group max value+ may be shrunk during the clip search (e.g. ``0.5`` searches down+ to 50% of the original per-group max).+ clip_n_sample_token: Maximum number of calibration tokens used per+ balance layer when searching the clip threshold (subsampled to+ bound memory).mappings: Optional explicit AWQ smooth/balance mappings. Eachitem should contain ``smooth_layer`` and``balance_layers`` entries. If None, mappings are inferred@@ -78,15 +122,43 @@ class AWQConfig(QuantizationConfig):self.nsamples = nsamplesself.batch_size = batch_sizeself.apply_smooth = apply_smooth+ if smooth_iters is None or smooth_iters < 1:+ raise ValueError(f"`smooth_iters` must be a positive integer, got {smooth_iters!r}")+ self.smooth_iters = smooth_iters+ self.apply_clip = apply_clip+ self.clip_as_init = clip_as_init+ if self.clip_as_init and not self.apply_clip:+ raise ValueError("`clip_as_init=True` requires `apply_clip=True`.")+ if clip_n_grid is None or clip_n_grid < 1:+ raise ValueError(f"`clip_n_grid` must be a positive integer, got {clip_n_grid!r}")+ if not (0.0 < clip_max_shrink < 1.0):+ raise ValueError(f"`clip_max_shrink` must be in (0, 1), got {clip_max_shrink!r}")+ if clip_n_sample_token is None or clip_n_sample_token < 1:+ raise ValueError(f"`clip_n_sample_token` must be a positive integer, got {clip_n_sample_token!r}")+ self.clip_n_grid = clip_n_grid+ self.clip_max_shrink = clip_max_shrink+ self.clip_n_sample_token = clip_n_sample_tokenself.mappings = mappingsself.infer_bs_coeff = 1self.batch_dim = None- # NOTE: enable_quanted_input is NOT set here. It belongs to the- # block_quantizer (RTN/AutoRound), not to AWQ. See §3.7.1.++ def finalize_scheme(self) -> None:+ """Adjust AWQ state that depends on the resolved run scheme."""+ data_type = self.data_type+ is_gguf_double_quant = bool(data_type) and (data_type.endswith("_dq") or data_type.endswith("float_zp"))+ if self.apply_clip and is_gguf_double_quant:+ logger.warning(+ "AWQ weight clipping (apply_clip=True) is not supported for GGUF "+ "double-quant schemes; disabling clipping and proceeding with AWQ "+ "smoothing only."+ )+ self.apply_clip = Falsedef __repr__(self) -> str:return (f"AWQConfig(duo_scaling={self.duo_scaling!r}, n_grid={self.n_grid}, "+ f"smooth_iters={self.smooth_iters}, "+ f"apply_clip={self.apply_clip}, clip_as_init={self.clip_as_init}, "f"bits={self.bits}, group_size={self.group_size}, sym={self.sym}, "f"mappings={'<explicit>' if self.mappings else 'auto'})")diff --git a/auto_round/algorithms/transforms/awq/qdq.py b/auto_round/algorithms/transforms/awq/qdq.pynew file mode 100644index 000000000..fce0b1c40--- /dev/null+++ b/auto_round/algorithms/transforms/awq/qdq.py@@ -0,0 +1,179 @@+# 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.+"""AWQ reference quantize-dequantize (QDQ).++AWQ searches smoothing scales and weight-clip thresholds by minimizing the+error of the *block quantizer's* weight QDQ. :class:`QDQTool` reproduces that+exact weight QDQ for every scheme AutoRound supports -- plain int, MX-FP,+NV-FP etc. -- behind a single+:meth:`QDQTool.qdq` entry point, so the search loss stays in lock-step with what+the downstream block quantizer will actually do and there is no second, parallel+implementation to drift from.++Usage (the same unified QDQ serves both the scale grid-search and clip-search)::++ tool = QDQTool(bits=..., group_size=..., sym=..., data_type=...)+ tool.layer_config = <per-layer overrides> # wired in by AWQTransform+ tool.configure(compressor) # opt_rtn / v2 flags++ params = tool.resolve_params(layer) # per-layer scheme+ qf, opt_qf = tool.resolve_quant_funcs(params) # dispatch once, reuse in loop+ w_qdq = tool.qdq(weight, params, quant_func=qf, opt_quant_func=opt_qf)+"""++from __future__ import annotations++import torch++from auto_round.data_type.utils import (+ compute_optimized_init_scale,+ get_optimized_quant_func,+ get_quant_func,+)+++class QDQTool:+ """Reproduce the block quantizer's weight QDQ for AWQ's search loss.++ The constructor takes the scheme-level *fallback* params (used when a layer+ is absent from ``layer_config``). :meth:`configure` derives the runtime+ quant-func-selection flags from the run's block quantizer; ``layer_config``+ is wired in by the owning ``AWQTransform``.+ """++ def __init__(self, *, bits, group_size, sym, data_type) -> None:+ self.bits = bits+ self.group_size = group_size+ self.sym = sym+ self.data_type = data_type++ self.layer_config: dict | None = None+ self.disable_opt_rtn: bool | None = None+ self.use_v2_scale_search: bool = False++ # ── runtime wiring ────────────────────────────────────────────────────────+ def configure(self, compressor) -> None:+ """Derive QDQ behaviour from the run's block quantizer."""+ block_config = getattr(compressor, "quantize_config", None)+ self.disable_opt_rtn = bool(getattr(block_config, "disable_opt_rtn", False))+ self.use_v2_scale_search = self._block_quantizer_is_signroundv2(compressor)++ @staticmethod+ def _block_quantizer_is_signroundv2(compressor) -> bool:+ """Return ``True`` if the terminal block quantizer is SignRoundV2."""+ block_config = getattr(compressor, "quantize_config", None)+ if block_config is None:+ return False+ from auto_round.algorithms.registry import resolve_pipeline_member++ try:+ return resolve_pipeline_member(block_config).__name__ == "SignRoundV2Quantizer"+ except Exception:+ return False++ # ── per-layer scheme resolution + dispatch ────────────────────────────────+ def _layer_config_for(self, layer: torch.nn.Module) -> dict:+ name = getattr(layer, "global_name", None) or ""+ return (self.layer_config or {}).get(name, {})++ def resolve_params(self, layer: torch.nn.Module) -> dict:+ """Resolve the per-layer weight-quant params (``layer_config`` + fallbacks).++ Single source of every scheme field the QDQ needs, so callers pass the+ returned dict straight to :meth:`resolve_quant_funcs` and :meth:`qdq`.+ ``super_bits`` / ``super_group_size`` are the GGUF double-quant+ super-block params and are ``None`` for non-GGUF schemes.+ """+ cfg = self._layer_config_for(layer)+ return {+ "bits": cfg.get("bits", self.bits),+ "group_size": cfg.get("group_size", self.group_size),+ "sym": cfg.get("sym", self.sym),+ "data_type": cfg.get("data_type", self.data_type),+ "disable_opt_rtn": cfg.get("disable_opt_rtn", self.disable_opt_rtn),+ "super_bits": cfg.get("super_bits", None),+ "super_group_size": cfg.get("super_group_size", None),+ }++ def resolve_quant_funcs(self, params: dict):+ """Dispatch ``(quant_func, opt_quant_func)`` for ``params``.++ ``opt_quant_func`` is non-``None`` only when the SignRoundV2 optimized+ init-scale path applies (it depends solely on ``data_type`` / ``sym``).+ Hoisting this out of a search loop avoids repeated dispatch.+ """+ quant_func, _ = get_quant_func(+ params["data_type"],+ params["bits"],+ params["sym"],+ disable_opt_rtn=params["disable_opt_rtn"],+ group_size=params["group_size"],+ iters=0,+ )+ opt_quant_func = None+ if self.use_v2_scale_search and params["sym"]:+ opt_quant_func = get_optimized_quant_func(params["data_type"])+ return quant_func, opt_quant_func++ # ── the unified QDQ for AWQ search ──────────────────────+ @torch.no_grad()+ def qdq(+ self,+ weight: torch.Tensor,+ params: dict,+ *,+ quant_func=None,+ opt_quant_func=None,+ imatrix: torch.Tensor | None = None,+ ) -> torch.Tensor:+ """Quantize-dequantize ``weight`` under the resolved ``params``.++ The single QDQ entry point for every scheme: ``bits`` / ``group_size`` /+ ``sym`` / ``data_type`` plus the optional GGUF super-block params drive+ the dispatched ``quant_func``; a non-``None`` ``opt_quant_func`` enables+ the SignRoundV2 optimized init-scale path for this weight. Used for+ both the scale grid-search and the clip-search -- reference/loss only,+ it never mutates stored weights.+ """+ if quant_func is None:+ quant_func, opt_quant_func = self.resolve_quant_funcs(params)+ if quant_func is None:+ raise RuntimeError(+ "QDQTool: no quantization function resolved for "+ f"data_type={params['data_type']}, bits={params['bits']}, "+ f"sym={params['sym']}, group_size={params['group_size']}."+ )++ quant_kwargs = {+ "bits": params["bits"],+ "group_size": params["group_size"],+ "data_type": params["data_type"],+ "sym": params["sym"],+ }+ if params.get("super_bits") is not None:+ quant_kwargs["super_bits"] = params["super_bits"]+ if params.get("super_group_size") is not None:+ quant_kwargs["super_group_size"] = params["super_group_size"]++ active_quant_func = quant_func+ if opt_quant_func is not None:+ init_scale = compute_optimized_init_scale(+ weight, params["data_type"], params["bits"], params["group_size"], imatrix=imatrix+ )+ if init_scale is not None:+ quant_kwargs["init_scale"] = init_scale+ active_quant_func = opt_quant_func++ qdq_weight, _, _ = active_quant_func(weight, **quant_kwargs)+ return qdq_weightdiff --git a/auto_round/cli/algorithms.py b/auto_round/cli/algorithms.pyindex d81f21d44..37692d03a 100644--- a/auto_round/cli/algorithms.py+++ b/auto_round/cli/algorithms.py@@ -188,7 +188,7 @@ class AWQ(AlgorithmHandler):def register(self, group) -> None:group.add_argument(- "--duo-scaling",+ "--awq-duo-scaling",dest="duo_scaling",default=True,type=_parse_bool_or_mode,@@ -196,12 +196,27 @@ class AWQ(AlgorithmHandler):help="Use activation+weight duo scaling (true/false/both).",)group.add_argument(- "--n-grid",+ "--awq-n-grid",dest="n_grid",default=20,type=int,help="Number of grid-search points for AWQ scaling ratio.",)+ group.add_argument(+ "--awq-apply-clip",+ dest="awq_apply_clip",+ action="store_true",+ help="Search and hard-clamp per-group AWQ weight clipping after smoothing.",+ )+ group.add_argument(+ "--awq-clip-as-init",+ dest="awq_clip_as_init",+ action="store_true",+ help=(+ "Use the searched AWQ clip to initialize the block quantizer's "+ "weight range instead of hard-clamping (requires --awq-apply-clip)."+ ),+ )def build(self, args, common_kwargs: dict[str, Any]):from auto_round.algorithms.transforms.awq.config import AWQConfig@@ -209,6 +224,8 @@ class AWQ(AlgorithmHandler):return AWQConfig(duo_scaling=getattr(args, "duo_scaling", True),n_grid=getattr(args, "n_grid", 20),+ apply_clip=getattr(args, "awq_apply_clip", False),+ clip_as_init=getattr(args, "awq_clip_as_init", False),**common_kwargs,)diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex 9dcfc2239..c8d932b3d 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -457,6 +457,10 @@ class BaseCompressor(object):if hasattr(config, "scheme"):config.scheme = self.scheme_contextself.quantize_config.check_config()+ for config in self._alg_configs:+ finalize_scheme = getattr(config, "finalize_scheme", None)+ if callable(finalize_scheme):+ finalize_scheme()self.orig_scheme = copy.deepcopy(self.scheme)self.scheme = default_schemediff --git a/auto_round/context/model.py b/auto_round/context/model.pyindex e016d72b9..285f6f5df 100644--- a/auto_round/context/model.py+++ b/auto_round/context/model.py@@ -89,6 +89,12 @@ class ModelContext(BaseContext):self.image_processor = Noneself.pipe = None+ # AWQ weight-clip thresholds kept for downstream block quantizers.+ # Populated by AWQTransform when ``apply_clip`` is enabled; keyed by+ # layer ``global_name`` -> per-group clip magnitude tensor. SignRound /+ # SignRoundV2 use these to initialize their tunable weight range.+ self.awq_clip_values: dict = {}+if envs.AR_USE_MODELSCOPE:platform = "model_scope"self.platform = platformdiff --git a/auto_round/data_type/int.py b/auto_round/data_type/int.pyindex 9c7e47243..f3b3e4930 100644--- a/auto_round/data_type/int.py+++ b/auto_round/data_type/int.py@@ -199,7 +199,14 @@ def quant_tensor_sym(maxq = int(2.0 ** (bits - 1))if init_scale is not None:- scale = init_scale * max_scale.unsqueeze(dim=-1)+ # ``max_scale`` is a per-group tuning coefficient (Tensor) during+ # SignRound optimization, but may be a plain scalar (e.g. 1.0) when the+ # init_scale is reused for a one-shot QDQ such as AWQ's smooth/clip grid+ # search.+ if isinstance(max_scale, torch.Tensor):+ scale = init_scale * max_scale.unsqueeze(dim=-1)+ else:+ scale = init_scale * max_scalescale = scale.to(scale_dtype)scale = torch.where(scale < 0, torch.clamp(scale, max=-q_scale_thresh), torch.clamp(scale, min=q_scale_thresh))int_w = round_ste(tensor / scale + v)diff --git a/auto_round/data_type/utils.py b/auto_round/data_type/utils.pyindex dbfd44425..4bbc35179 100644--- a/auto_round/data_type/utils.py+++ b/auto_round/data_type/utils.py@@ -175,6 +175,140 @@ def get_quant_func()+def _resolve_optimized_dtype_funcs(data_type: str, q_scale_thresh: float = 1e-5):+ """Resolve the SignRound optimized ``(scale_search_fn, quant_func)`` for a data type.++ Single source of truth for the optimized-path dispatch shared by+ ``SignRoundOptimizedWrapperLinear`` and AWQ's internal QDQ:++ * ``scale_search_fn(weight_reshape, bits, imatrix) -> init_scale`` searches the+ data-type-specific per-group init scale (the int variant clamps to+ ``q_scale_thresh`` to avoid a degenerate zero scale).+ * ``quant_func`` is the matching *plain* quant function (``init_scale`` already+ encodes the searched scale, so no opt-rtn / rtn variant is needed).++ Returns ``(None, None)`` for data types without an optimized path+ (asym int, ``*_dq``, or unrelated types).+ """+ dt = str(data_type)+ if dt.endswith("dq"):+ return None, None+ if dt.startswith("int"):+ # The optimized int init-scale search is symmetric-only; asym int uses+ # the standard tensor_min/tensor_max range instead.+ if "asym" in dt:+ return None, None+ from auto_round.data_type.int import quant_tensor_sym, search_scales++ def search_int(weight_reshape, bits, imatrix):+ init_scale = search_scales(weight_reshape, bits, imatrix)+ return torch.where(+ init_scale < 0,+ torch.clamp(init_scale, max=-q_scale_thresh),+ torch.clamp(init_scale, min=q_scale_thresh),+ )++ return search_int, quant_tensor_sym+ if dt.startswith("mx"):+ from auto_round.data_type.mxfp import quant_mx, search_mx_scale++ return search_mx_scale, quant_mx+ if dt.startswith("nv"):+ from auto_round.data_type.nvfp import nv_fp4, search_nvfp4_scale++ return search_nvfp4_scale, nv_fp4+ return None, None+++def search_optimized_init_scale(+ weight_reshape: torch.Tensor,+ data_type: str,+ bits: int,+ imatrix=None,+ q_scale_thresh: float = 1e-5,+):+ """Compute the SignRoundV2 optimized per-group ``init_scale`` for a grouped weight.++ Mirrors ``SignRoundOptimizedWrapperLinear``: dispatches on ``data_type`` so that+ any caller (the optimized wrapper itself or AWQ's internal QDQ used for the+ smooth/clip grid search) seeds the quantizer with the same initial scale.+ Returns ``None`` for data types that do not use the optimized init-scale search+ (asym int, ``*_dq``, or unrelated types).++ Args:+ weight_reshape: Weight reshaped/padded to ``[..., group_size]``.+ data_type: Resolved weight data type (e.g. ``"int_sym"``, ``"mx_fp4"``, ``"nv_fp4"``).+ bits: Weight bit-width.+ imatrix: Per-element importance matrix matching ``weight_reshape`` (or ``None``/scalar).+ q_scale_thresh: Minimum scale magnitude used to clamp the int init_scale.++ Returns:+ The per-group ``init_scale`` tensor, or ``None`` if unsupported.+ """+ search_fn, _ = _resolve_optimized_dtype_funcs(data_type, q_scale_thresh)+ if search_fn is None:+ return None+ if imatrix is None:+ imatrix = 1.0+ return search_fn(weight_reshape, bits, imatrix)+++def get_optimized_quant_func(data_type: str):+ """Return the plain quant function used by the SignRound optimized path.++ The optimized init-scale search always pairs with the *plain* (non opt-rtn /+ non rtn) quant function for the data type, since ``init_scale`` already+ encodes the searched scale. Returns ``None`` for data types without an+ optimized path (asym int, ``*_dq``, or unrelated types).+ """+ _, quant_func = _resolve_optimized_dtype_funcs(data_type)+ return quant_func+++def reshape_imatrix_for_weight(imatrix, weight_reshape: torch.Tensor, group_size):+ """Reshape/pad an importance matrix to match a group-reshaped weight.++ Encapsulates the imatrix grouping logic shared by the SignRound optimized+ wrapper and AWQ's internal QDQ so callers never handle the low-level reshape.+ Returns the scalar ``1.0`` when no imatrix is available (uniform importance).+ """+ if imatrix is None:+ return 1.0+ imatrix = imatrix.reshape(1, -1)+ imatrix = reshape_pad_tensor_by_group_size(imatrix, group_size, val=1e-5)[0].view(1, -1)+ imatrix = imatrix.expand(weight_reshape.numel() // imatrix.numel(), -1)+ return imatrix.reshape(weight_reshape.shape).to(weight_reshape.device)+++def compute_optimized_init_scale(+ weight: torch.Tensor,+ data_type: str,+ bits: int,+ group_size,+ imatrix=None,+ q_scale_thresh: float = 1e-5,+):+ """Compute the SignRound optimized per-group ``init_scale`` for a full weight.++ Group-reshapes ``weight``, prepares the ``imatrix`` layout, and runs the+ data-type-specific scale search. Unlike :func:`search_optimized_init_scale`+ (which expects an already group-reshaped weight), this is the entry point for+ callers holding a full 2-D weight, e.g. AWQ's internal QDQ. Pair it with+ :func:`get_optimized_quant_func` (resolved once) to obtain the matching quant+ function, so the smooth/clip grid-search loss mirrors what+ ``SignRoundOptimizedWrapperLinear`` applies.++ Returns ``None`` for data types without an optimized init-scale path+ (asym int, ``*_dq``, or unrelated types).+ """+ search_fn, _ = _resolve_optimized_dtype_funcs(data_type, q_scale_thresh)+ if search_fn is None:+ return None+ weight_reshape, _, _ = reshape_pad_tensor_by_group_size(weight, group_size)+ imatrix = reshape_imatrix_for_weight(imatrix, weight_reshape, group_size)+ return search_fn(weight_reshape, bits, imatrix)++def round_ste(x: torch.Tensor):"""Straight-Through Estimator for rounding.diff --git a/auto_round/wrapper.py b/auto_round/wrapper.pyindex 0f6c222ea..5a25b25d9 100644--- a/auto_round/wrapper.py+++ b/auto_round/wrapper.py@@ -165,6 +165,17 @@ class WrapperLinear(torch.nn.Module):else:self.weight_min = Noneself.weight_max = None+ # AWQ clip-as-init: cap the tunable weight range to the per-group clip+ # magnitude searched by AWQ (``apply_clip`` with ``clip_as_init=True``).+ # This initializes the range used by quant_tensor_sym/asym, leaving+ # min_scale/max_scale to tune a coefficient on top. Only the standard+ # (non-tuple) group layout maps onto weight_min/weight_max here.+ awq_clip_max = getattr(orig_layer, "awq_clip_max", None)+ if awq_clip_max is not None and self.weight_min is not None and not isinstance(orig_layer.group_size, tuple):+ clip_flat = awq_clip_max.reshape(-1).to(self.weight_max.device, self.weight_max.dtype)+ if clip_flat.numel() == self.weight_max.numel():+ self.weight_max = torch.minimum(self.weight_max, clip_flat)+ self.weight_min = torch.maximum(self.weight_min, -clip_flat)self._init_params("value", p_dtype, weight_reshape.shape, 0, self.enable_round_tuning and self.orig_layer.bits < 16)diff --git a/test/test_cpu/algorithms/test_awq.py b/test/test_cpu/algorithms/test_awq.pyindex e6b55e3a1..b245f61ab 100644--- a/test/test_cpu/algorithms/test_awq.py+++ b/test/test_cpu/algorithms/test_awq.py@@ -110,6 +110,34 @@ class TestAWQNormalLLM:assert qconfig["sym"] == sym+class TestAWQNonIntegerSchemes:+ """Regression: AWQ smoothing must run under non-integer schemes (MX/NV-FP).++ AWQ's grid-search / clip loss reproduces the block quantizer's weight QDQ.+ The reported failure mode was an end-to-end ``algorithm='awq'`` run raising+ under an MXFP/NVFP scheme.+ """++ @pytest.mark.parametrize("scheme", ["MXFP4", "NVFP4"])+ def test_awq_non_integer_scheme_smoke(self, tiny_opt_model_path, scheme):+ ar = AutoRound(+ tiny_opt_model_path,+ scheme=scheme,+ algorithm="awq,signround",+ n_grid=4,+ nsamples=2,+ seqlen=32,+ batch_size=2,+ )+ model, layer_config = ar.quantize()++ assert model is not None+ assert len(layer_config) > 0+ for name, cfg in layer_config.items():+ assert cfg["bits"] == 4, f"Layer {name} expected bits=4, got {cfg['bits']}"+ assert cfg["act_bits"] == 4, f"Layer {name} expected act_bits=4, got {cfg['act_bits']}"++class TestAWQW8A8LLMCompressor:"""AWQ INT W8A8 quantization with llm_compressor export format.@@ -267,28 +295,162 @@ class TestAWQMoE:assert qconfig["group_size"] == 128assert "auto-round" in qconfig["quant_method"]- def test_awq_moe_save_compressed_size(self, tiny_qwen_moe_model_path):- """AWQ MoE W4: quantized safetensors should be smaller than original."""++class TestAWQWeightClip:+ """AWQ weight-clip option (issue #1854).++ Validates the ``apply_clip`` AWQ preprocessing combined with downstream+ block quantizers, covering the two extensibility scenarios:+ - clip + RTN (clip as the weight range, then round-to-nearest)+ - clip + SignRound (clip as initialization, then SignRound tuning)+ """++ @pytest.fixture(autouse=True)+ def _save_dir(self, tmp_path):+ self.save_dir = str(tmp_path / "saved")+ yield+ shutil.rmtree(self.save_dir, ignore_errors=True)++ def test_awq_clip_then_rtn(self, tiny_opt_model_path):+ """AWQ smooth+clip → RTN: produces a valid W4 model that can generate."""+ from auto_round.algorithms.quantization.rtn.config import RTNConfig+ from auto_round.algorithms.transforms.awq.config import AWQConfig+ar = AutoRound(- tiny_qwen_moe_model_path,- scheme="W4A16",- algorithm="awq",+ tiny_opt_model_path,+ alg_configs=[+ AWQConfig(+ bits=4, group_size=128, sym=True, apply_clip=True, n_grid=2, clip_n_grid=4, clip_n_sample_token=64+ ),+ RTNConfig(disable_opt_rtn=True),+ ],nsamples=2,seqlen=32,batch_size=2,)- _, save_path = ar.quantize_and_save(output_dir=self.save_dir, format="auto_round")+ model, layer_config = ar.quantize()- def _safetensors_size(path):- return sum(os.path.getsize(os.path.join(path, f)) for f in os.listdir(path) if f.endswith(".safetensors"))+ assert model is not None+ assert len(layer_config) > 0+ for name, cfg in layer_config.items():+ assert cfg["bits"] == 4, f"Layer {name} expected bits=4, got {cfg['bits']}"- original_size = _safetensors_size(tiny_qwen_moe_model_path)- quantized_size = _safetensors_size(save_path)- assert quantized_size > 0, f"No safetensors files in {save_path}"+ tokenizer = AutoTokenizer.from_pretrained(tiny_opt_model_path)+ output = generate_prompt(model, tokenizer, device="cpu")+ assert len(output) > 0, "Clipped model should produce non-empty output"++ def test_awq_clip_as_init_signround(self, tiny_opt_model_path):+ """clip_as_init: clip is kept on the model context and initializes SignRound's range."""+ from auto_round.algorithms.quantization.sign_round.config import SignRoundConfig+ from auto_round.algorithms.transforms.awq.config import AWQConfig- ratio = quantized_size / original_size- # W4G128 packing: (4-bit weights + scale/zp overhead)- assert 0.35 < ratio < 0.65, (- f"Compression ratio {ratio:.4f} outside expected range (0.35, 0.65): "- f"original={original_size / (1024**2):.1f}MB, quantized={quantized_size / (1024**2):.1f}MB"+ ar = AutoRound(+ tiny_opt_model_path,+ alg_configs=[+ AWQConfig(+ bits=4,+ group_size=128,+ sym=False,+ apply_clip=True,+ clip_as_init=True,+ n_grid=2,+ clip_n_grid=4,+ clip_n_sample_token=64,+ ),+ SignRoundConfig(iters=1),+ ],+ nsamples=2,+ seqlen=32,+ batch_size=2,)+ model, layer_config = ar.quantize()++ assert model is not None+ assert len(layer_config) > 0+ for name, cfg in layer_config.items():+ assert cfg["bits"] == 4, f"Layer {name} expected bits=4, got {cfg['bits']}"++ # The searched clip magnitudes are kept on the model context.+ clip_values = getattr(ar.model_context, "awq_clip_values", {})+ assert len(clip_values) > 0, "clip_as_init should record per-group clip values on the model context"++ tokenizer = AutoTokenizer.from_pretrained(tiny_opt_model_path)+ output = generate_prompt(model, tokenizer, device="cpu")+ assert len(output) > 0, "clip_as_init model should produce non-empty output"+++class TestAWQUseV2ScaleSearch:+ """Unit tests for AWQ's ``use_v2_scale_search`` detection and dispatch.++ The flag is True whenever the terminal block quantizer resolves to+ ``SignRoundV2Quantizer``. The per-data-type init_scale injection (int/mx/nv,+ sym only) is then handled by ``search_optimized_init_scale`` inside the QDQ+ path. Regression guard: the block-quantizer config does not expose+ ``_alg_cls``, so detection must go through the pipeline registry, not an+ ``_alg_cls`` string comparison (which always evaluated False before the fix).+ """++ @staticmethod+ def _make_compressor(block_config):+ import types++ return types.SimpleNamespace(quantize_config=block_config, alg_configs=[block_config])++ @staticmethod+ def _awq_quantizer():+ from auto_round.algorithms.transforms.awq.base import AWQTransform+ from auto_round.algorithms.transforms.awq.config import AWQConfig++ return AWQTransform(AWQConfig())++ @staticmethod+ def _signroundv2_config(data_type=None):+ from auto_round.algorithms.quantization.sign_round.config import SignRoundConfig+ from auto_round.algorithms.registry import normalize_algorithm_config++ cfg = normalize_algorithm_config(SignRoundConfig(iters=2, enable_alg_ext=True))+ assert type(cfg).__name__ == "SignRoundV2Config"+ if data_type is not None:+ cfg.data_type = data_type+ return cfg++ def test_rtn_block_is_not_v2(self):+ """An RTN block quantizer must NOT be detected as V2."""+ from auto_round.algorithms.quantization.rtn.config import RTNConfig++ q = self._awq_quantizer()+ compressor = self._make_compressor(RTNConfig(disable_opt_rtn=True))+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is False++ def test_use_v2_true_for_v2_block(self):+ """Gate is True for any SignRoundV2 block (data-type gating is per-layer)."""+ q = self._awq_quantizer()+ compressor = self._make_compressor(self._signroundv2_config(data_type="mx_fp"))+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is True++ def test_use_v2_false_for_non_v2_block(self):+ """Gate is False when the block is not SignRoundV2, regardless of dtype."""+ from auto_round.algorithms.quantization.sign_round.config import SignRoundConfig++ q = self._awq_quantizer()+ block = SignRoundConfig(iters=2)+ block.data_type = "mx_fp"+ compressor = self._make_compressor(block)+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is False++ def test_init_scale_dispatch_by_data_type(self):+ """``search_optimized_init_scale`` injects only for sym int/mx/nv."""+ import torch++ from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, search_optimized_init_scale++ for dt, gs in (("int_sym", 128), ("mx_fp4", 32)):+ weight = torch.randn(32, 128)+ weight_reshape, _, _ = reshape_pad_tensor_by_group_size(weight, gs)+ init_scale = search_optimized_init_scale(weight_reshape, dt, 4, None)+ assert init_scale is not None, dt+ assert init_scale.shape[0] == weight_reshape.shape[0]++ # asym int and *_dq are not part of the optimized init-scale path.+ assert search_optimized_init_scale(torch.randn(4, 128), "int_asym", 4, None) is None+ assert search_optimized_init_scale(torch.randn(4, 128), "int_sym_dq", 4, None) is Nonediff --git a/test/test_cpu/core/test_pipeline_fail_fast.py b/test/test_cpu/core/test_pipeline_fail_fast.pyindex 09c8d733e..cc6779028 100644--- a/test/test_cpu/core/test_pipeline_fail_fast.py+++ b/test/test_cpu/core/test_pipeline_fail_fast.py@@ -36,7 +36,7 @@ def test_split_awq_plus_rtn():def test_pipeline_preprocessor_only_auto_appends_rtn():pipeline = QuantizationPipeline.from_configs([AWQConfig()])- assert type(pipeline.preprocessors[0]).__name__ == "AWQQuantizer"+ assert type(pipeline.preprocessors[0]).__name__ == "AWQTransform"assert isinstance(pipeline.block_quantizer, RTNQuantizer)diff --git a/test/test_cuda/algorithms/test_awq.py b/test/test_cuda/algorithms/test_awq.pyindex eaca31c79..f664a931b 100644--- a/test/test_cuda/algorithms/test_awq.py+++ b/test/test_cuda/algorithms/test_awq.py@@ -53,6 +53,7 @@ class TestAWQLLM:tiny_opt_model_path,scheme="W4A16",algorithm="awq",+ n_grid=4,nsamples=2,seqlen=32,batch_size=2,@@ -77,6 +78,7 @@ class TestAWQLLM:group_size=group_size,sym=sym,algorithm="awq",+ n_grid=4,nsamples=2,seqlen=32,batch_size=2,@@ -98,6 +100,7 @@ class TestAWQLLM:model_name,scheme="W4A16",algorithm="awq",+ n_grid=4,nsamples=2,seqlen=32,)@@ -158,6 +161,7 @@ class TestAWQMoE:scheme="W4A16",algorithm="awq",nsamples=2,+ n_grid=4,seqlen=32,batch_size=2,)@@ -192,6 +196,7 @@ class TestAWQMoE:tiny_qwen_moe_model_path,scheme="W4A16",algorithm="awq",+ n_grid=4,nsamples=2,seqlen=32,batch_size=2,@@ -249,3 +254,85 @@ class TestAWQEval:batch_size="auto:8",limit=50,)+++# ---------------------------------------------------------------------------+# Section 4: use_v2_scale_search detection and init-scale dispatch+# ---------------------------------------------------------------------------+++class TestAWQUseV2ScaleSearch:+ """Unit tests for AWQ's ``use_v2_scale_search`` detection and dispatch.++ The flag is True whenever the terminal block quantizer resolves to+ ``SignRoundV2Quantizer``. The per-data-type init_scale injection (int/mx/nv,+ sym only) is then handled by ``search_optimized_init_scale`` inside the QDQ+ path. Detection lives on ``QDQTool`` (accessed via ``AWQTransform._qdq_tool``)+ and must go through the pipeline registry, not an ``_alg_cls`` string+ comparison (which always evaluated False before the fix).+ """++ @staticmethod+ def _make_compressor(block_config):+ import types++ return types.SimpleNamespace(quantize_config=block_config, alg_configs=[block_config])++ @staticmethod+ def _awq_quantizer():+ from auto_round.algorithms.transforms.awq.base import AWQTransform+ from auto_round.algorithms.transforms.awq.config import AWQConfig++ return AWQTransform(AWQConfig())++ @staticmethod+ def _signroundv2_config(data_type=None):+ from auto_round.algorithms.quantization.sign_round.config import SignRoundConfig+ from auto_round.algorithms.registry import normalize_algorithm_config++ cfg = normalize_algorithm_config(SignRoundConfig(iters=2, enable_alg_ext=True))+ assert type(cfg).__name__ == "SignRoundV2Config"+ if data_type is not None:+ cfg.data_type = data_type+ return cfg++ def test_rtn_block_is_not_v2(self):+ """An RTN block quantizer must NOT be detected as V2."""+ from auto_round.algorithms.quantization.rtn.config import RTNConfig++ q = self._awq_quantizer()+ compressor = self._make_compressor(RTNConfig(disable_opt_rtn=True))+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is False++ def test_use_v2_true_for_v2_block(self):+ """Gate is True for any SignRoundV2 block (data-type gating is per-layer)."""+ q = self._awq_quantizer()+ compressor = self._make_compressor(self._signroundv2_config(data_type="mx_fp"))+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is True++ def test_use_v2_false_for_non_v2_block(self):+ """Gate is False when the block is not SignRoundV2, regardless of dtype."""+ from auto_round.algorithms.quantization.sign_round.config import SignRoundConfig++ q = self._awq_quantizer()+ block = SignRoundConfig(iters=2)+ block.data_type = "mx_fp"+ compressor = self._make_compressor(block)+ assert q._qdq_tool._block_quantizer_is_signroundv2(compressor) is False++ def test_init_scale_dispatch_by_data_type(self):+ """``search_optimized_init_scale`` injects only for sym int/mx/nv."""+ import torch++ from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, search_optimized_init_scale++ for dt, gs in (("int_sym", 128), ("mx_fp4", 32), ("nv_fp4", 16)):+ weight = torch.randn(32, 128)+ weight_reshape, _, _ = reshape_pad_tensor_by_group_size(weight, gs)+ init_scale = search_optimized_init_scale(weight_reshape, dt, 4, None)+ assert init_scale is not None, dt+ assert init_scale.shape[0] == weight_reshape.shape[0]++ # asym int and *_dq are not part of the optimized init-scale path.+ assert search_optimized_init_scale(torch.randn(4, 128), "int_asym", 4, None) is None+ assert search_optimized_init_scale(torch.randn(4, 128), "int_sym_dq", 4, None) is None