Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsu52 committed Oct 25, 2023
1 parent ef60e81 commit 2134fc8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
6 changes: 3 additions & 3 deletions nncf/openvino/pot/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def _create_quantization_config(
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
model_type: Optional[ModelType],
ignored_scope: Optional[IgnoredScope],
advanced_parameters: Optional[AdvancedQuantizationParameters],
model_type: Union[ModelType, None],
ignored_scope: Union[IgnoredScope, None],
advanced_parameters: Union[AdvancedQuantizationParameters, None],
) -> Dict[str, Any]:
"""
Creates a quantization configuration.
Expand Down
6 changes: 3 additions & 3 deletions nncf/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def _create_nncf_config(
preset: Union[QuantizationPreset, None],
target_device: TargetDevice,
subset_size: int,
model_type: Optional[ModelType],
ignored_scope: Optional[IgnoredScope],
advanced_parameters: Optional[AdvancedQuantizationParameters],
model_type: Union[ModelType, None],
ignored_scope: Union[IgnoredScope, None],
advanced_parameters: Union[AdvancedQuantizationParameters, None],
) -> NNCFConfig:
"""
Creates the NNCFConfig for the quantization algorithm.
Expand Down
40 changes: 40 additions & 0 deletions tests/common/quantization/test_minmax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2023 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from nncf.common.quantization.structs import QuantizationMode
from nncf.common.quantization.structs import QuantizationPreset
from nncf.common.quantization.structs import QuantizerGroup
from nncf.parameters import ModelType
from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization


@pytest.mark.parametrize(
"preset,model_type,activation_mode,weights_mode",
[
(None, None, QuantizationMode.SYMMETRIC, QuantizationMode.SYMMETRIC),
(QuantizationPreset.PERFORMANCE, None, QuantizationMode.SYMMETRIC, QuantizationMode.SYMMETRIC),
(QuantizationPreset.MIXED, None, QuantizationMode.ASYMMETRIC, QuantizationMode.SYMMETRIC),
(None, ModelType.TRANSFORMER, QuantizationMode.ASYMMETRIC, QuantizationMode.SYMMETRIC),
(QuantizationPreset.PERFORMANCE, ModelType.TRANSFORMER, QuantizationMode.SYMMETRIC, QuantizationMode.SYMMETRIC),
(QuantizationPreset.MIXED, ModelType.TRANSFORMER, QuantizationMode.ASYMMETRIC, QuantizationMode.SYMMETRIC),
],
)
def test_quantization_preset(preset, model_type, activation_mode, weights_mode):
minmax = MinMaxQuantization(preset=preset, model_type=model_type)

global_quantizer_constraints = getattr(minmax, "_global_quantizer_constraints")
assert (
global_quantizer_constraints[QuantizerGroup.ACTIVATIONS].qconf_attr_vs_constraint_dict["mode"]
== activation_mode
)
assert global_quantizer_constraints[QuantizerGroup.WEIGHTS].qconf_attr_vs_constraint_dict["mode"] == weights_mode
25 changes: 24 additions & 1 deletion tests/torch/ptq/test_ptq_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ def ignored_scopes_data(self, request):
@pytest.mark.parametrize(
"params",
(
{
"preset": None,
"target_device": TargetDevice.ANY,
"subset_size": 1,
"model_type": ModelType.TRANSFORMER,
"ignored_scope": IgnoredScope(),
"advanced_parameters": AdvancedQuantizationParameters(),
},
{
"preset": None,
"target_device": TargetDevice.ANY,
"subset_size": 1,
"model_type": None,
"ignored_scope": IgnoredScope(),
"advanced_parameters": AdvancedQuantizationParameters(),
},
{
"preset": QuantizationPreset.MIXED,
"target_device": TargetDevice.ANY,
Expand Down Expand Up @@ -234,7 +250,14 @@ def test_create_nncf_config(params):
assert config["compression"]["overflow_fix"] == params["advanced_parameters"].overflow_fix.value
assert config["compression"]["quantize_outputs"] == params["advanced_parameters"].quantize_outputs

assert config["compression"]["preset"] == params["preset"].value
preset = params["preset"]
if params["preset"] is None:
if params["model_type"] == ModelType.TRANSFORMER:
preset = QuantizationPreset.MIXED
else:
preset = QuantizationPreset.PERFORMANCE

assert config["compression"]["preset"] == preset.value

range_config = config["compression"]["initializer"]["range"]
if isinstance(range_config, dict):
Expand Down

0 comments on commit 2134fc8

Please sign in to comment.