Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-churkin committed Sep 18, 2023
1 parent 1584b82 commit c0ce9ae
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 49 deletions.
8 changes: 3 additions & 5 deletions nncf/experimental/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.scopes import IgnoredScope
from nncf.torch.dynamic_graph.context import no_nncf_trace
from nncf.torch.dynamic_graph.io_handling import replicate_same_tensors
Expand Down Expand Up @@ -105,7 +105,7 @@ def quantize_impl(

nncf_network = create_nncf_network(model.eval(), calibration_dataset)

quantization_algorithm = PostTrainingQuantization(
quantization_pipeline = PostTrainingQuantization(
preset=preset,
target_device=target_device,
subset_size=subset_size,
Expand All @@ -115,9 +115,7 @@ def quantize_impl(
advanced_parameters=advanced_parameters,
)

quantized_model = quantization_algorithm.apply(
nncf_network, nncf_network.nncf.get_graph(), dataset=calibration_dataset
)
quantized_model = quantization_pipeline.run(nncf_network, calibration_dataset)

quantized_model.nncf.disable_dynamic_graph_building()

Expand Down
7 changes: 3 additions & 4 deletions nncf/onnx/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.quantization.telemetry_extractors import CompressionStartedWithQuantizeApi
from nncf.scopes import IgnoredScope
from nncf.telemetry import tracked_function
Expand Down Expand Up @@ -56,7 +56,7 @@ def quantize_impl(
advanced_parameters.weights_quantization_params.per_channel = False
advanced_parameters.activations_quantization_params.per_channel = False

quantization_algorithm = PostTrainingQuantization(
quantization_pipeline = PostTrainingQuantization(
preset=preset,
target_device=target_device,
subset_size=subset_size,
Expand All @@ -66,7 +66,6 @@ def quantize_impl(
advanced_parameters=advanced_parameters,
)

graph = GraphConverter.create_nncf_graph(model)
quantized_model = quantization_algorithm.apply(model, graph, dataset=calibration_dataset)
quantized_model = quantization_pipeline.run(model, calibration_dataset)

return quantized_model
2 changes: 1 addition & 1 deletion nncf/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from nncf.quantization.advanced_parameters import AdvancedAccuracyRestorerParameters
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.algorithms.accuracy_control.evaluator import MetricResults
from nncf.quantization.pipelines.hyperparameter_tuner.pipeline import HyperparameterTuner
from nncf.quantization.pipelines.hyperparameter_tuner.param_grid import get_quantization_param_grids
from nncf.quantization.pipelines.hyperparameter_tuner.pipeline import HyperparameterTuner
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.scopes import IgnoredScope

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import pytest

from nncf.quantization.algorithms.hyperparameter_tuner.algorithm import apply_combination
from nncf.quantization.algorithms.hyperparameter_tuner.algorithm import create_combinations
from nncf.quantization.algorithms.hyperparameter_tuner.algorithm import find_best_combination
from nncf.quantization.pipelines.hyperparameter_tuner.pipeline import apply_combination
from nncf.quantization.pipelines.hyperparameter_tuner.pipeline import create_combinations
from nncf.quantization.pipelines.hyperparameter_tuner.pipeline import find_best_combination

CombinationKey = Tuple[int, ...]
Combination = Dict[str, Any]
Expand Down
8 changes: 3 additions & 5 deletions tests/onnx/quantization/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from nncf.onnx.graph.onnx_graph import ONNXGraph
from nncf.onnx.statistics.statistics import ONNXMinMaxTensorStatistic
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.fake_quantize import FakeQuantizeParameters
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from tests.onnx.common import get_random_generator
from tests.onnx.opset_converter import convert_opset_version
from tests.shared.nx_graph import check_nx_graph
Expand Down Expand Up @@ -95,7 +95,6 @@ def min_max_quantize_model(
) -> onnx.ModelProto:
if convert_model_opset:
original_model = convert_opset_version(original_model)
graph = GraphConverter.create_nncf_graph(original_model)
dataset = get_random_dataset_for_test(original_model, dataset_has_batch_size)
quantization_params = {} if quantization_params is None else quantization_params

Expand All @@ -105,7 +104,7 @@ def min_max_quantize_model(

post_training_quantization = PostTrainingQuantization(subset_size=1, **quantization_params)

quantized_model = post_training_quantization.apply(original_model, graph, dataset=dataset)
quantized_model = post_training_quantization.run(original_model, dataset)
return quantized_model


Expand All @@ -117,11 +116,10 @@ def ptq_quantize_model(
) -> onnx.ModelProto:
if convert_model_opset:
original_model = convert_opset_version(original_model)
graph = GraphConverter.create_nncf_graph(original_model)
dataset = get_random_dataset_for_test(original_model, dataset_has_batch_size)
quantization_params = {} if quantization_params is None else quantization_params
post_training_quantization = PostTrainingQuantization(subset_size=1, **quantization_params)
quantized_model = post_training_quantization.apply(original_model, graph, dataset=dataset)
quantized_model = post_training_quantization.run(original_model, dataset)
return quantized_model


Expand Down
6 changes: 3 additions & 3 deletions tests/onnx/quantization/test_ptq_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from nncf.onnx.statistics.collectors import ONNXMinMaxStatisticCollector
from nncf.parameters import TargetDevice
from nncf.quantization.algorithms.min_max.onnx_backend import ONNXMinMaxAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.scopes import IgnoredScope
from tests.common.quantization.metatypes import Conv2dTestMetatype
from tests.common.quantization.metatypes import LinearTestMetatype
Expand All @@ -49,8 +49,8 @@ def get_ignored_patterns(device: TargetDevice = TargetDevice.ANY) -> GraphPatter

@pytest.mark.parametrize("target_device", TargetDevice)
def test_target_device(target_device):
algo = PostTrainingQuantization(target_device=target_device)
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization(target_device=target_device)
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = ONNXMinMaxAlgoBackend()
assert min_max_algo._target_device == target_device

Expand Down
6 changes: 3 additions & 3 deletions tests/openvino/native/quantization/test_ptq_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from nncf.openvino.graph.transformations.commands import OVTargetPoint
from nncf.parameters import TargetDevice
from nncf.quantization.algorithms.min_max.openvino_backend import OVMinMaxAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.scopes import IgnoredScope
from tests.common.quantization.metatypes import Conv2dTestMetatype
from tests.common.quantization.metatypes import LinearTestMetatype
Expand All @@ -49,8 +49,8 @@ def get_ignored_patterns(device: TargetDevice = TargetDevice.ANY) -> GraphPatter
# pylint: disable=protected-access
@pytest.mark.parametrize("target_device", [TargetDevice.CPU, TargetDevice.GPU, TargetDevice.VPU])
def test_target_device(target_device):
algo = PostTrainingQuantization(target_device=target_device)
min_max_algo = algo.algorithms[0]
pipelines = PostTrainingQuantization(target_device=target_device)
min_max_algo = pipelines.pipeline_steps[-1][0]
min_max_algo._backend_entity = OVMinMaxAlgoBackend()
assert min_max_algo._target_device.value == HW_CONFIG_TYPE_TARGET_DEVICE_MAP[target_device.value]

Expand Down
2 changes: 1 addition & 1 deletion tests/post_training/test_templates/test_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from nncf.quantization.advanced_parameters import OverflowFix
from nncf.quantization.algorithms.bias_correction.algorithm import BiasCorrection
from nncf.quantization.algorithms.bias_correction.backend import BiasCorrectionAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from tests.post_training.test_templates.helpers import ConvTestModel
from tests.post_training.test_templates.helpers import MultipleConvTestModel
from tests.post_training.test_templates.helpers import SplittedModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from nncf.quantization.advanced_parameters import OverflowFix
from nncf.quantization.algorithms.fast_bias_correction.algorithm import FastBiasCorrection
from nncf.quantization.algorithms.fast_bias_correction.backend import FastBiasCorrectionAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from tests.post_training.test_templates.helpers import ConvBNTestModel
from tests.post_training.test_templates.helpers import ConvTestModel
from tests.post_training.test_templates.helpers import get_static_dataset
Expand Down
18 changes: 9 additions & 9 deletions tests/post_training/test_templates/test_ptq_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import OverflowFix
from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.passes import transform_to_inference_graph
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.quantization.range_estimator import RangeEstimatorParametersSet
from nncf.scopes import IgnoredScope
from tests.common.quantization.metatypes import Conv2dTestMetatype
Expand Down Expand Up @@ -131,12 +131,12 @@ def metatypes_mapping(self):
"range_estimator_params", [RangeEstimatorParametersSet.MINMAX, RangeEstimatorParametersSet.MEAN_MINMAX, None]
)
def test_range_estimator_per_tensor(self, test_params, range_estimator_params):
algo = PostTrainingQuantization(
pipeline = PostTrainingQuantization(
advanced_parameters=AdvancedQuantizationParameters(
activations_range_estimator_params=range_estimator_params
)
)
min_max_algo = algo.algorithms[0]
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
assert min_max_algo._range_estimator_params[QuantizerGroup.ACTIVATIONS] == range_estimator_params

Expand All @@ -161,10 +161,10 @@ def test_range_estimator_per_tensor(self, test_params, range_estimator_params):

@pytest.mark.parametrize("quantize_outputs", [False, True])
def test_quantize_outputs(self, test_params, quantize_outputs):
algo = PostTrainingQuantization(
pipeline = PostTrainingQuantization(
advanced_parameters=AdvancedQuantizationParameters(quantize_outputs=quantize_outputs)
)
min_max_algo = algo.algorithms[0]
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()

nncf_graph = test_params["test_quantize_outputs"]["nncf_graph"]
Expand All @@ -189,8 +189,8 @@ def test_quantize_outputs(self, test_params, quantize_outputs):

def test_ignored_scopes(self, test_params, ignored_scopes_data):
ignored_scope, act_num_ref, weight_num_ref = ignored_scopes_data
algo = PostTrainingQuantization(ignored_scope=ignored_scope)
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization(ignored_scope=ignored_scope)
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
assert min_max_algo._ignored_scope == ignored_scope

Expand All @@ -215,8 +215,8 @@ def test_ignored_scopes(self, test_params, ignored_scopes_data):

@pytest.mark.parametrize("model_type", [ModelType.TRANSFORMER])
def test_model_type_pass(self, test_params, model_type):
algo = PostTrainingQuantization(preset=QuantizationPreset.MIXED, model_type=model_type)
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization(preset=QuantizationPreset.MIXED, model_type=model_type)
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()

nncf_graph = test_params["test_model_type_pass"]["nncf_graph"]
Expand Down
18 changes: 9 additions & 9 deletions tests/post_training/test_templates/test_quantizer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import QuantizationParameters
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.passes import transform_to_inference_graph
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.quantization.range_estimator import RangeEstimatorParametersSet
from tests.post_training.test_templates.models import NNCFGraphToTest
from tests.post_training.test_templates.models import NNCFGraphToTestDepthwiseConv
Expand Down Expand Up @@ -81,8 +81,8 @@ def statistic_collector_parameters(self, request) -> TestGetStatisticsCollectorP
pass

def test_default_quantizer_config(self, single_conv_nncf_graph):
algo = PostTrainingQuantization()
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization()
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
nncf_graph = single_conv_nncf_graph.nncf_graph
inference_nncf_graph = transform_to_inference_graph(
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_quantizer_config_from_ptq_params_for_CPU(
signed_activations,
single_conv_nncf_graph,
):
algo = PostTrainingQuantization(
pipeline = PostTrainingQuantization(
preset=preset,
advanced_parameters=AdvancedQuantizationParameters(
activations_quantization_params=QuantizationParameters(
Expand All @@ -138,7 +138,7 @@ def test_quantizer_config_from_ptq_params_for_CPU(
),
),
)
min_max_algo = algo.algorithms[0]
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
nncf_graph = single_conv_nncf_graph.nncf_graph
inference_nncf_graph = transform_to_inference_graph(
Expand Down Expand Up @@ -179,8 +179,8 @@ def test_quantizer_config_from_ptq_params_for_CPU(
assert quantization_point.qconfig.signedness_to_force == signed_activations

def test_depthwise_conv_default_quantizer_config(self, depthwise_conv_nncf_graph):
algo = PostTrainingQuantization()
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization()
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
nncf_graph = depthwise_conv_nncf_graph.nncf_graph
inference_nncf_graph = transform_to_inference_graph(
Expand Down Expand Up @@ -223,12 +223,12 @@ def test_get_stat_collector(
statistic_collector_parameters: TestGetStatisticsCollectorParameters,
):
params = statistic_collector_parameters
algo = PostTrainingQuantization(
pipeline = PostTrainingQuantization(
advanced_parameters=AdvancedQuantizationParameters(
activations_range_estimator_params=range_estimator_params
)
)
min_max_algo = algo.algorithms[0]
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = self.get_algo_backend()
q_config = QuantizerConfig(num_bits=8, mode=q_config_mode, per_channel=q_config_per_channel)

Expand Down
2 changes: 1 addition & 1 deletion tests/post_training/test_templates/test_smooth_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from nncf.parameters import ModelType
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import OverflowFix
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.algorithms.smooth_quant.algorithm import SmoothQuant
from nncf.quantization.algorithms.smooth_quant.backend import SmoothQuantAlgoBackend
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from tests.post_training.test_templates.helpers import LinearMultiShapeModel
from tests.post_training.test_templates.helpers import NonZeroLinearModel
from tests.post_training.test_templates.helpers import get_static_dataset
Expand Down
2 changes: 1 addition & 1 deletion tests/torch/ptq/test_fq_params_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import OverflowFix
from nncf.quantization.algorithms.min_max.algorithm import MinMaxQuantization
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.torch.model_creation import create_nncf_network
from nncf.torch.nncf_network import NNCFNetwork
from nncf.torch.quantization.layers import QUANTIZATION_MODULES
Expand Down
6 changes: 3 additions & 3 deletions tests/torch/ptq/test_ptq_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from nncf.quantization.advanced_parameters import QuantizationMode
from nncf.quantization.advanced_parameters import QuantizationParameters
from nncf.quantization.algorithms.min_max.torch_backend import PTMinMaxAlgoBackend
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import PostTrainingQuantization
from nncf.quantization.range_estimator import RangeEstimatorParametersSet
from nncf.scopes import IgnoredScope
from nncf.torch.graph.graph import PTTargetPoint
Expand Down Expand Up @@ -94,8 +94,8 @@ def forward(self, x):

@pytest.mark.parametrize("target_device", TargetDevice)
def test_target_device(target_device):
algo = PostTrainingQuantization(target_device=target_device)
min_max_algo = algo.algorithms[0]
pipeline = PostTrainingQuantization(target_device=target_device)
min_max_algo = pipeline.pipeline_steps[-1][0]
min_max_algo._backend_entity = PTMinMaxAlgoBackend()
assert min_max_algo._target_device == target_device

Expand Down

0 comments on commit c0ce9ae

Please sign in to comment.