fix: quant_analyse crashes on per-channel/group weight scales - #384
Open
Linxiushen wants to merge 1 commit into
Open
Linxiushen wants to merge 1 commit into
Linxiushen wants to merge 1 commit into
Conversation
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).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PTQ.save()gates a scale-outlier report behindquantization.quant_analyseand tests each scale like this (angelslim/compressor/quant/ptq.py, lines 199 and 207):if tensor > 1.5works only when the scale is a 0-dim scalar. Per-channel and group-wise quantization keep one scale per channel/group, so the tensor holds more than one element, Python falls back toTensor.__bool__(), and it raises:This is reachable from the shipped configs
configs/qwen3/ptq/int8_dynamic/*.yamlall setweight: "per-channel"(checkedqwen3-0_6b,qwen3-1_7b,qwen3-32b)AbsMaxChannelWiseWeightObserver._cal_min_maxcomputesnew_inp.abs().max(dim=self.quant_axis())withoutkeepdim, i.e. a 1-D per-channel tensorquant_analyseis wired into both the int8 and fp8 branches (quant/core/config.py), so it is meant to be a general switch, not per-tensor-onlySo enabling
quant_analyseon the default int8_dynamic recipe crashes at save time, after calibration has already run.Reproduction
The block below is extracted verbatim from
ptq.pyatmain(ee8ddb2) and executed against a per-channel scale tensor shaped exactly like the observer's output:With the patch applied, the same block reports normally:
Minimal standalone check of the root cause:
Fix
Use
torch.any(), which handles 0-dim and multi-element tensors alike, and print.max().item()so the warning stays readable instead of dumping a whole per-channel tensor.Behaviour across scale shapes
max=2.0)max=5.0)max=9.9)max=2.0)per-tensor behaviour is unchanged in both directions — that was the main thing I wanted to be sure of.
Notes
torchis already imported at the top ofptq.py(line 19), no new dependencyPTQ.save()through a real calibration run. Happy to adjust if you'd like the warning formatted differently, e.g. also printing the offending channel index.Investigated and fixed with AI assistance (Claude); I reviewed the change and the verification above myself.