From 8ec693f5f5cb6c209801fde241e53ca258fb6c14 Mon Sep 17 00:00:00 2001 From: dlyakhov Date: Tue, 2 Apr 2024 11:37:17 +0200 Subject: [PATCH] Tests adjusted --- ...TwoConvTestModel_overflow_fix_disable.json | 30 ++++++++++++------- .../TwoConvTestModel_overflow_fix_enable.json | 30 ++++++++++++------- ...stModel_overflow_fix_first_layer_only.json | 30 ++++++++++++------- tests/torch/ptq/helpers.py | 13 -------- tests/torch/ptq/test_graphs.py | 11 +++++-- 5 files changed, 65 insertions(+), 49 deletions(-) diff --git a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_disable.json b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_disable.json index 47fad5258e0..436b2a09a35 100644 --- a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_disable.json +++ b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_disable.json @@ -4,14 +4,14 @@ [ [ [ - -1.0 + -1 ] ] ], [ [ [ - -1.0 + -1 ] ] ] @@ -20,14 +20,14 @@ [ [ [ - 1.0 + 1 ] ] ], [ [ [ - 1.0 + 1 ] ] ] @@ -38,7 +38,7 @@ [ [ [ - -1.0 + -1 ] ] ] @@ -47,18 +47,26 @@ [ [ [ - 1.0 + 1 ] ] ] ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[/nncf_model_input_0|OUTPUT]": { - "input_low": 0.0, - "input_high": 0.9800970554351807 + "input_low": [ + 0 + ], + "input_high": [ + 0.9800970554351807 + ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[TwoConvTestModel/Sequential[features]/Sequential[0]/NNCFConv2d[0]/conv2d_0|OUTPUT]": { - "input_low": -3.8243322372436523, - "input_high": 3.794454574584961 + "input_low": [ + -3.8243322372436523 + ], + "input_high": [ + 3.794454574584961 + ] } -} \ No newline at end of file +} diff --git a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_enable.json b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_enable.json index 2253a59ba08..615b21d1c68 100644 --- a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_enable.json +++ b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_enable.json @@ -4,14 +4,14 @@ [ [ [ - -2.0 + -2 ] ] ], [ [ [ - -2.0 + -2 ] ] ] @@ -20,14 +20,14 @@ [ [ [ - 2.0 + 2 ] ] ], [ [ [ - 2.0 + 2 ] ] ] @@ -38,7 +38,7 @@ [ [ [ - -2.0 + -2 ] ] ] @@ -47,18 +47,26 @@ [ [ [ - 2.0 + 2 ] ] ] ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[/nncf_model_input_0|OUTPUT]": { - "input_low": 0.0, - "input_high": 0.9800970554351807 + "input_low": [ + 0 + ], + "input_high": [ + 0.9800970554351807 + ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[TwoConvTestModel/Sequential[features]/Sequential[0]/NNCFConv2d[0]/conv2d_0|OUTPUT]": { - "input_low": -3.8243322372436523, - "input_high": 3.794454574584961 + "input_low": [ + -3.8243322372436523 + ], + "input_high": [ + 3.794454574584961 + ] } -} \ No newline at end of file +} diff --git a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_first_layer_only.json b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_first_layer_only.json index c004fb4ba61..9400c5e3195 100644 --- a/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_first_layer_only.json +++ b/tests/torch/data/reference_scales/TwoConvTestModel_overflow_fix_first_layer_only.json @@ -4,14 +4,14 @@ [ [ [ - -2.0 + -2 ] ] ], [ [ [ - -2.0 + -2 ] ] ] @@ -20,14 +20,14 @@ [ [ [ - 2.0 + 2 ] ] ], [ [ [ - 2.0 + 2 ] ] ] @@ -38,7 +38,7 @@ [ [ [ - -1.0 + -1 ] ] ] @@ -47,18 +47,26 @@ [ [ [ - 1.0 + 1 ] ] ] ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[/nncf_model_input_0|OUTPUT]": { - "input_low": 0.0, - "input_high": 0.9800970554351807 + "input_low": [ + 0 + ], + "input_high": [ + 0.9800970554351807 + ] }, "TwoConvTestModel/NNCFNetworkInterface[_nncf]/ModuleDict[external_quantizers]/SymmetricQuantizer[TwoConvTestModel/Sequential[features]/Sequential[0]/NNCFConv2d[0]/conv2d_0|OUTPUT]": { - "input_low": -3.8243322372436523, - "input_high": 3.794454574584961 + "input_low": [ + -3.8243322372436523 + ], + "input_high": [ + 3.794454574584961 + ] } -} \ No newline at end of file +} diff --git a/tests/torch/ptq/helpers.py b/tests/torch/ptq/helpers.py index 54fdfa70710..4cf763f033c 100644 --- a/tests/torch/ptq/helpers.py +++ b/tests/torch/ptq/helpers.py @@ -21,7 +21,6 @@ from nncf.torch.graph.operator_metatypes import PTModuleLinearMetatype from nncf.torch.graph.operator_metatypes import PTSumMetatype from nncf.torch.model_creation import create_nncf_network -from nncf.torch.tensor_statistics.statistics import PTMinMaxTensorStatistic from tests.post_training.test_templates.models import NNCFGraphToTest from tests.post_training.test_templates.models import NNCFGraphToTestDepthwiseConv from tests.post_training.test_templates.models import NNCFGraphToTestSumAggregation @@ -85,15 +84,3 @@ def get_nncf_network(model: torch.nn.Module, input_shape: Optional[List[int]] = config=nncf_config, ) return nncf_network - - -def mock_collect_statistics(mocker): - _ = mocker.patch( - "nncf.common.tensor_statistics.aggregator.StatisticsAggregator.collect_statistics", return_value=None - ) - min_, max_ = 0.0, 1.0 - min_, max_ = torch.tensor(min_), torch.tensor(max_) - _ = mocker.patch( - "nncf.experimental.common.tensor_statistics.collectors.TensorCollector.get_statistics", - return_value=PTMinMaxTensorStatistic(min_values=min_, max_values=max_), - ) diff --git a/tests/torch/ptq/test_graphs.py b/tests/torch/ptq/test_graphs.py index d441b5b04e1..c45f253eba1 100644 --- a/tests/torch/ptq/test_graphs.py +++ b/tests/torch/ptq/test_graphs.py @@ -13,6 +13,7 @@ from pathlib import Path import pytest +import torch from nncf.parameters import TargetDevice from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters @@ -23,7 +24,6 @@ from tests.post_training.test_templates.helpers import get_static_dataset from tests.torch import test_models from tests.torch.ptq.helpers import get_nncf_network -from tests.torch.ptq.helpers import mock_collect_statistics from tests.torch.quantization.test_algo_quantization import SharedLayersModel from tests.torch.test_compressed_graph import ModelDesc from tests.torch.test_compressed_graph import check_graph @@ -94,15 +94,20 @@ def get_model_name(description): ("desc", "quantization_parameters"), TEST_MODELS_DESC, ids=[get_model_name(m) for m in TEST_MODELS_DESC] ) def test_min_max_classification_quantized_graphs(desc: ModelDesc, quantization_parameters, graph_dir, mocker): - mock_collect_statistics(mocker) model = desc.model_builder() nncf_network = get_nncf_network(model, desc.input_sample_sizes) quantization_parameters["advanced_parameters"] = AdvancedQuantizationParameters(disable_bias_correction=True) + quantization_parameters["subset_size"] = 1 quantization_algorithm = PostTrainingQuantization(**quantization_parameters) + def transform_fn(input_) -> torch.Tensor: + return torch.tensor(input_[0]) + quantized_model = quantization_algorithm.apply( - nncf_network, nncf_network.nncf.get_graph(), dataset=get_static_dataset(desc.input_sample_sizes, None, None) + nncf_network, + nncf_network.nncf.get_graph(), + dataset=get_static_dataset(desc.input_sample_sizes, transform_fn, None), ) check_graph(quantized_model.nncf.get_graph(), desc.dot_filename(), graph_dir)