Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MinMax algorithm for OpenVINO backend #1444

Merged
merged 33 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Add graph tests
  • Loading branch information
l-bat committed Jan 18, 2023
commit d04fcb840d863000a947baf318208f8a609cb480
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ def _insert_fake_quantize_op(self, transformation: OVQuantizerInsertionCommand)
elif transform_type == TargetType.POST_LAYER_OPERATION:
output = target_node.output(port_id)
target_inputs = output.get_target_inputs()
fq_name = f'{node_name}/fq_output_{port_id}'
fq = ov.opset9.fake_quantize(output, input_low, input_high,
output_low, output_high, levels, name=fq_name)
for inp_node in target_inputs:
next_port_id = inp_node.get_index()
fq_name = f'{node_name}/fq_output_{port_id}_{next_port_id}'
fq = ov.opset9.fake_quantize(output, input_low, input_high,
output_low, output_high, levels, name=fq_name)
inp_node.replace_source_output(fq.output(0))
else:
raise RuntimeError(f'Incorrect target point type {transform_type}')
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def convert_ov_dtype_to_nncf_dtype(ov_dtype: str) -> Dtype:
conversion_map = {
'f16': 'float',
'f32': 'float',
'f64': 'float',
'i4': 'int',
'i8': 'int',
'i32': 'int',
Expand Down
1 change: 1 addition & 0 deletions tests/openvino/native/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from nncf.experimental.openvino_native.graph.nncf_graph_builder import GraphConverter
from tests.common.graph.nx_graph import sort_dot


def compare_nncf_graphs(model: ov.Model, path_ref_graph: str) -> None:
nncf_graph = GraphConverter.create_nncf_graph(model)
nx_graph = nncf_graph.get_graph_for_structure_analysis(extended=True)
Expand Down
57 changes: 57 additions & 0 deletions tests/openvino/native/quantization/test_graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Copyright (c) 2022 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
import openvino.runtime as ov

from nncf.common.quantization.structs import QuantizationPreset

from tests.openvino.conftest import OPENVINO_NATIVE_TEST_ROOT
from tests.openvino.omz_helpers import convert_model
from tests.openvino.omz_helpers import download_model
from tests.openvino.native.common import compare_nncf_graphs
from tests.openvino.native.models import SYNTHETIC_MODELS
from tests.openvino.native.quantization.test_fq_params_calculation import quantize_model

QUANTIZED_REF_GRAPHS_DIR = OPENVINO_NATIVE_TEST_ROOT / 'data' / 'reference_graphs' / 'quantized'


@pytest.mark.skip(reason="Enable after fixing an issue with operation outputs order")
@pytest.mark.parametrize('model_creator_func', SYNTHETIC_MODELS.values())
def test_syntetic_models_fq_placement(model_creator_func):
model = model_creator_func()
quantized_model = quantize_model(model.ov_model, QuantizationPreset.PERFORMANCE)

path_ref_graph = QUANTIZED_REF_GRAPHS_DIR / model.ref_graph_name
compare_nncf_graphs(quantized_model, path_ref_graph)


OMZ_MODELS = [
'mobilenet-v2-pytorch',
'mobilenet-v3-small-1.0-224-tf',
'resnet-18-pytorch',
'ssd_mobilenet_v1_coco',
'yolo-v4-tiny-tf',
]


@pytest.mark.skip(reason="Enable after fixing an issue with operation outputs order")
@pytest.mark.parametrize('model_name', OMZ_MODELS)
def test_omz_models_fq_placement(model_name, tmp_path):
_ = download_model(model_name, tmp_path)
model_path = convert_model(model_name, tmp_path)
model = ov.Core().read_model(model_path)
quantized_model = quantize_model(model, QuantizationPreset.PERFORMANCE)

path_ref_graph = QUANTIZED_REF_GRAPHS_DIR / f'_{model_name}.dot'
compare_nncf_graphs(quantized_model, path_ref_graph)
23 changes: 8 additions & 15 deletions tests/openvino/native/test_nncf_graph_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,21 @@
import pytest
import openvino.runtime as ov

from nncf.experimental.openvino_native.graph.nncf_graph_builder import GraphConverter

from tests.common.graph.nx_graph import compare_nx_graph_with_reference
from tests.openvino.conftest import OPENVINO_TEST_ROOT
from tests.openvino.conftest import OPENVINO_NATIVE_TEST_ROOT
from tests.openvino.omz_helpers import convert_model
from tests.openvino.omz_helpers import download_model
from tests.openvino.native.common import compare_nncf_graphs
from tests.openvino.native.models import SYNTHETIC_MODELS

REFERENCE_GRAPHS_DIR = OPENVINO_TEST_ROOT / 'data' / 'reference_graphs' / 'original_nncf_graph'
REFERENCE_GRAPHS_DIR = OPENVINO_NATIVE_TEST_ROOT / 'data' / 'reference_graphs' / 'original_nncf_graph'


@pytest.mark.skip(reason="Enable after fixing an issue with operation outputs order")
@pytest.mark.parametrize("model_cls_to_test", SYNTHETIC_MODELS.values())
def test_compare_nncf_graph_synthetic_models(model_cls_to_test):
model_to_test = model_cls_to_test()
path_to_dot = REFERENCE_GRAPHS_DIR / 'synthetic' / model_to_test.ref_graph_name

nncf_graph = GraphConverter.create_nncf_graph(model_to_test.ov_model)
nx_graph = nncf_graph.get_graph_for_structure_analysis(extended=True)
compare_nx_graph_with_reference(nx_graph, path_to_dot, check_edge_attrs=True)
path_to_dot = REFERENCE_GRAPHS_DIR / model_to_test.ref_graph_name
compare_nncf_graphs(model_to_test.ov_model, path_to_dot)


OMZ_MODELS = [
Expand All @@ -46,15 +41,13 @@ def test_compare_nncf_graph_synthetic_models(model_cls_to_test):
'yolo-v4-tiny-tf',
]


@pytest.mark.skip(reason="Enable after fixing an issue with operation outputs order")
@pytest.mark.parametrize('model_name', OMZ_MODELS)
def test_compare_nncf_graph_omz_models(tmp_path, model_name):
_ = download_model(model_name, tmp_path)
model_path = convert_model(model_name, tmp_path)
model = ov.Core().read_model(model_path)

nncf_graph = GraphConverter.create_nncf_graph(model)
nx_graph = nncf_graph.get_graph_for_structure_analysis(extended=True)

path_to_dot = REFERENCE_GRAPHS_DIR / 'omz' / f'{model_name}.dot'
compare_nx_graph_with_reference(nx_graph, path_to_dot, check_edge_attrs=True)
path_to_dot = REFERENCE_GRAPHS_DIR / f'{model_name}.dot'
compare_nncf_graphs(model, path_to_dot)