From f72b18fda0d84d26046791bb9b37e4ccd441b27c Mon Sep 17 00:00:00 2001 From: Linxiushen <1787979356@qq.com> Date: Sun, 20 Sep 2026 18:03:13 +0800 Subject: [PATCH] fix: quant_analyse crashes on per-channel/group weight scales PTQ.save() gates a scale-outlier report behind quantization.quant_analyse and tests each scale with `if act_scales_data > 1.5:` / `if weight_scales_data > 1.5:`. That only works for 0-dim scalars. Per-channel and group-wise quantization keep one scale per channel/group, so the tensor holds more than one element and Python calls Tensor.__bool__(), which raises: RuntimeError: Boolean value of Tensor with more than one value is ambiguous This is reachable from the shipped configs: configs/qwen3/ptq/int8_dynamic/*.yaml all set weight: "per-channel", and AbsMaxChannelWiseWeightObserver computes max(dim=quant_axis()) without keepdim, i.e. a 1-D per-channel tensor. Use torch.any() so both 0-dim and multi-element scales work, and report .max().item() so the message stays short instead of printing a whole per-channel tensor. per-tensor behaviour is unchanged (still warns above 1.5, silent below). --- angelslim/compressor/quant/ptq.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/angelslim/compressor/quant/ptq.py b/angelslim/compressor/quant/ptq.py index d4377d17..ee698e60 100644 --- a/angelslim/compressor/quant/ptq.py +++ b/angelslim/compressor/quant/ptq.py @@ -194,20 +194,27 @@ def save(self, save_path: str): and self.quant_model.quant_config.quant_analyse ): # scale analyse + # Scales are not always 0-dim: per-channel / group-wise quantization + # keeps one scale per channel or group, so these tensors usually hold + # more than one element (the int8_dynamic configs default to + # `weight: "per-channel"`). `if tensor > 1.5` then raises + # "Boolean value of Tensor with more than one value is ambiguous". + # torch.any() handles both shapes, and .max() keeps the message short + # instead of printing a whole per-channel tensor. for k in self.quant_model.act_scales_dict.keys(): act_scales_data = self.quant_model.act_scales_dict[k].data - if act_scales_data > 1.5: + if torch.any(act_scales_data > 1.5): print_info( f"[AngelSlim Warning] Act_scales {k}: " - f"The weight is too high:{act_scales_data}. " + f"The weight is too high:{act_scales_data.max().item()}. " f"It is recommended to clip it to 1.5 " ) for k in self.quant_model.weight_scales_dict.keys(): weight_scales_data = self.quant_model.weight_scales_dict[k].data - if weight_scales_data > 1.5: + if torch.any(weight_scales_data > 1.5): print_info( f"[AngelSlim Warning] Weight_scales {k}: " - f"The weight is too high:{weight_scales_data}. " + f"The weight is too high:{weight_scales_data.max().item()}. " f"It is recommended to clip it to 1.5 " )