Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modelopt/torch/quantization/nn/modules/tensor_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ def amax(self):
@amax.setter
def amax(self, value):
assert value is not None, "amax cannot be set to None."
# getattr (not self._dynamic): __init__ assigns self.amax = amax before
# set_from_attribute_config creates the _dynamic attribute, so a bare
# attribute access would break the amax= constructor argument.
assert not getattr(self, "_dynamic", False), (
"Dynamic quantization does not have fixed amax; amax cannot be set."
)

if not isinstance(value, torch.Tensor):
value = torch.tensor(value)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/torch/quantization/test_tensor_quantizer_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,35 @@

"""Tests of tensor quantizer."""

import pytest
import torch
from _test_utils.torch.quantization.tensor_quantizer_common import (
BlockQuantTester,
SequentialQuantizerTester,
TensorQuantizerTester,
)

from modelopt.torch.quantization.config import QuantizerAttributeConfig
from modelopt.torch.quantization.nn import TensorQuantizer


class TestTensorQuantizerCPU(TensorQuantizerTester):
device = "cpu"

def test_disabled_extra_repr(self):
quantizer = TensorQuantizer(QuantizerAttributeConfig(enable=False)).to(self.device)
assert quantizer.extra_repr() == "disabled"

quantizer.pre_quant_scale = torch.tensor([0.5, 2.0]).to(self.device)
extra_repr = quantizer.extra_repr()
assert extra_repr.startswith("disabled")
assert "pre_quant_scale=" in extra_repr

def test_dynamic_amax_set_rejected(self):
quantizer = TensorQuantizer(QuantizerAttributeConfig(type="dynamic")).to(self.device)
with pytest.raises(AssertionError, match="Dynamic quantization"):
quantizer.amax = 1.0


class TestBlockQuantCPU(BlockQuantTester):
device = "cpu"
Expand Down