fix: reject amax assignment on dynamic quantizers#1909
Conversation
📝 WalkthroughWalkthroughTensorQuantizer now blocks ChangesTensorQuantizer Behavior Fixes
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
modelopt/torch/quantization/nn/modules/tensor_quantizer.py (1)
345-355: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the
getattrordering dependency.The fix is correct —
getattr(self, "_dynamic", False)is necessary because__init__callsself.amax = amax(line 201-202) before_dynamicis set byset_from_attribute_config(line 205). This is exactly the kind of non-obvious constraint that should be captured with a short inline comment, otherwise a future refactor could replace it withself._dynamic(matching the getter at line 342) and reintroduce anAttributeErroronTensorQuantizer(amax=...)construction.As per coding guidelines, "Comment cautiously... Use comments only for non-obvious intent or constraints that remain unclear from the code."
💡 Suggested inline comment
`@amax.setter` def amax(self, value): assert value is not None, "amax cannot be set to None." + # _dynamic is not yet set when __init__ assigns `amax` before calling + # set_from_attribute_config, so default to False via getattr here. assert not getattr(self, "_dynamic", False), ( "Dynamic quantization does not have fixed amax; amax cannot be set." )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modelopt/torch/quantization/nn/modules/tensor_quantizer.py` around lines 345 - 355, Add a short inline comment in TensorQuantizer.amax setter near the getattr(self, "_dynamic", False) check to document the initialization-order dependency: __init__ sets amax before _dynamic is assigned, so using getattr avoids AttributeError during construction. Keep the comment focused on this non-obvious constraint and reference the amax setter and _dynamic initialization path so future refactors don’t replace it with a direct self._dynamic access.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@modelopt/torch/quantization/nn/modules/tensor_quantizer.py`:
- Around line 345-355: Add a short inline comment in TensorQuantizer.amax setter
near the getattr(self, "_dynamic", False) check to document the
initialization-order dependency: __init__ sets amax before _dynamic is assigned,
so using getattr avoids AttributeError during construction. Keep the comment
focused on this non-obvious constraint and reference the amax setter and
_dynamic initialization path so future refactors don’t replace it with a direct
self._dynamic access.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 45413200-1064-4069-9a60-9ff6af558626
📒 Files selected for processing (2)
modelopt/torch/quantization/nn/modules/tensor_quantizer.pytests/unit/torch/quantization/test_tensor_quantizer_cpu.py
Setting amax on a dynamic TensorQuantizer silently stored a value that dynamic quantization never uses; fail fast instead. getattr (not self._dynamic) because __init__ assigns self.amax = amax before set_from_attribute_config creates the _dynamic attribute. Also adds a regression test pinning the disabled extra_repr detail output (dead code until NVIDIA#1879 made it reachable). Signed-off-by: arham766 <arhamislam766@yahoo.com>
400db03 to
9a22d32
Compare
What does this PR do?
Type of change: Bug fix
Overview: Setting
amaxon a dynamicTensorQuantizersilently stored a value dynamic quantization never uses; the setter now fails fast.getattr(self, "_dynamic", False)rather thanself._dynamicbecause__init__assignsself.amax = amaxbeforeset_from_attribute_configcreates the_dynamicattribute — a bare attribute access would break theamax=constructor argument (all 28 assignment sites audited).Scope note: this PR originally also fixed the
extra_reprdead code in the disabled branch (built a detail string, returned the literal"disabled"). #1879 has since made that branch reachable onmain, so that part is now upstream; this PR keeps a regression test pinning the disabledextra_reprdetail output and retains only the amax-setter fix. Rebased accordingly.Testing
tests/unit/torch/quantization/test_tensor_quantizer_cpu.py:test_dynamic_amax_set_rejected(fails without the fix) andtest_disabled_extra_repr(pins the now-live disabled branch). Full file: 28 passed.Before your PR is "Ready for review"
Additional Information
Issue: #1902
Summary by CodeRabbit
amaxwhen quantization is configured as dynamic, raising a clear assertion error instead.extra_repr()output (including pre-quant scale) and verifying thatamaxupdates are rejected in dynamic mode.