Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
l-bat committed Apr 24, 2024
1 parent b628adc commit 8c6a339
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions nncf/torch/quantization/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ def __init__(self, scale: torch.Tensor, result_dtype: torch.dtype = None):
"""
super().__init__()
self.register_buffer("_scale", scale)
self.result_dtype = result_dtype

def forward(self, x):
zero_point = torch.zeros_like(self._scale)
Expand Down
23 changes: 10 additions & 13 deletions tests/openvino/native/quantization/test_weights_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_gather_in_4_bit_if_all_layers_with_data(metric):
sensitivity_metric=metric,
dataset=dataset,
)
int4_reference_node_names = ["gather_2_data", "gather_2_data/zero_point"]
int4_reference_node_names = ["gather_2_data"]
nodes_map = {op.get_friendly_name(): op for op in compressed_model.get_ordered_ops()}
for node_name in int4_reference_node_names:
node = nodes_map[node_name]
Expand Down Expand Up @@ -304,17 +304,13 @@ def test_gather_can_be_8_bit_if_all_layers_without_data():
def test_conv_in_8_bit_if_mode_8bit(mode):
model = WeightsModel().ov_model
compressed_model = compress_weights(model, mode=mode)
int8_reference_node_names = [
"conv_weights_0",
"conv_weights_0/zero_point",
"conv_weights_1",
"conv_weights_1/zero_point",
]
int8_reference_node_names = ["conv_weights_0", "conv_weights_1"]
nodes_map = {op.get_friendly_name(): op for op in compressed_model.get_ordered_ops()}
dtype = ov.Type.u8 if mode == CompressWeightsMode.INT8_ASYM else ov.Type.i8
for node_name in int8_reference_node_names:
node = nodes_map[node_name]
assert node.get_type_name() == "Constant"
assert node.get_element_type() == ov.Type.u8
assert node.get_element_type() == dtype


@pytest.mark.parametrize("all_layers", (True, False))
Expand All @@ -337,9 +333,9 @@ def test_conv_in_8_bit_if_mode_4bit(all_layers):
]:
assert ov.Type.u8 == op.get_element_type()
elif op.get_friendly_name() in ["weights_1", "weights_1/zero_point"]:
assert ov.Type.u4 == op.get_element_type()
assert ov.Type.i4 == op.get_element_type()
elif op.get_friendly_name() in ["weights_0", "weights_0/zero_point"]:
dtype = ov.Type.u4 if all_layers else ov.Type.u8
dtype = ov.Type.i4 if all_layers else ov.Type.u8
assert dtype == op.get_element_type()


Expand All @@ -352,7 +348,7 @@ def test_gather_can_be_4_bit_if_all_layers_without_data():
group_size=1,
all_layers=True,
)
int4_reference_node_names = ["gather_2_data", "gather_2_data/zero_point"]
int4_reference_node_names = ["gather_2_data"]
nodes_map = {op.get_friendly_name(): op for op in compressed_model.get_ordered_ops()}
for node_name in int4_reference_node_names:
node = nodes_map[node_name]
Expand All @@ -378,7 +374,7 @@ def test_gather_in_8_bit_if_not_all_layers(metric):
for node_name in int8_reference_node_names:
node = nodes_map[node_name]
assert node.get_type_name() == "Constant"
assert node.get_element_type() == ov.Type.i8
assert node.get_element_type() == ov.Type.u8


MAX_BASELINE_SCORE = 1 / np.finfo(np.float32).eps
Expand Down Expand Up @@ -425,9 +421,10 @@ def test_data_based_criterion(mode, ref_scores, ref_act_scores, mocker):
def test_quantize_Gather_with_multiple_reduction_axes_in_8bit(mode):
model = GatherWithTwoReductionAxes().ov_model
compressed_model = compress_weights(model, mode=mode)
dtype = ov.Type.u8 if mode == CompressWeightsMode.INT8_ASYM else ov.Type.i8
for op in compressed_model.get_ordered_ops():
if op.get_type_name() == "Constant" and op.get_friendly_name() == "gather_1_data":
assert op.get_element_type() == ov.Type.u8
assert op.get_element_type() == dtype


@pytest.mark.parametrize("mode", (CompressWeightsMode.INT4_SYM, CompressWeightsMode.INT4_ASYM))
Expand Down
6 changes: 3 additions & 3 deletions tests/torch/ptq/test_weights_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from nncf import SensitivityMetric
from nncf.quantization import compress_weights
from nncf.torch import wrap_model
from nncf.torch.quantization.layers import WeightsDecompressor
from nncf.torch.quantization.layers import SymmetricWeightsDecompressor

DATA_BASED_SENSITIVITY_METRICS = (
SensitivityMetric.HESSIAN_INPUT_ACTIVATION,
Expand Down Expand Up @@ -132,11 +132,11 @@ def test_compress_weights_functional_model():

input_ids = torch.randint(0, 10, [1, 3, 300, 300])
wrapped_model = wrap_model(model, example_input=input_ids, trace_parameters=True)
compressed_model = compress_weights(wrapped_model)
compressed_model = compress_weights(wrapped_model, mode=CompressWeightsMode.INT8_SYM)

n_compressed_weights = 0
for layer in compressed_model.nncf.external_op.values():
if isinstance(layer, WeightsDecompressor):
if isinstance(layer, SymmetricWeightsDecompressor):
n_compressed_weights += 1
assert n_compressed_weights == 4

Expand Down

0 comments on commit 8c6a339

Please sign in to comment.