Clearing of CustomReplacementRegistry.registry in convert_model() (#15893) (#16347)

* Clearing of CustomReplacementRegistry.registry.

* Added test.
This commit is contained in:
Anastasiia Pnevskaia 2023-03-17 10:17:16 +01:00 committed by GitHub
parent 7e0c8f8c40
commit 11f1841fce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,15 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from openvino.tools.mo.front.tf.replacement import FrontReplacementFromConfigFileGeneral
from openvino.tools.mo.graph.graph import Graph
from openvino.tools.mo.utils.error import Error
class ConfigBasedTestReplacement(FrontReplacementFromConfigFileGeneral):
replacement_id = 'ConfigBasedTestReplacement'
run_not_recursively = True
def transform_graph(self, graph: Graph, replacement_descriptions):
sigmoid_nodes = graph.get_op_nodes(op='Sigmoid')
assert len(sigmoid_nodes) > 0, "Error while applying ConfigBasedTestReplacement."

View File

@ -2,12 +2,14 @@
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import os
import pytest
from openvino.runtime import Model, Layout, PartialShape, Shape, layout_helpers, Type, Dimension
from openvino.tools.mo.convert import InputCutInfo, LayoutMap
from common.mo_convert_test_class import CommonMOConvertTest
from common.tf_layer_test_class import save_to_pb
from openvino.runtime import Model, Layout, PartialShape, Shape, layout_helpers, Type, Dimension
class TestComplexParams(CommonMOConvertTest):
def create_tf_model(self, tmp_dir):
@ -63,6 +65,24 @@ class TestComplexParams(CommonMOConvertTest):
# save model to .pb and return path to the model
return save_to_pb(tf_net, tmp_dir)
def create_tf_model_no_sigmoid(self, tmp_dir):
#
# Create Tensorflow model without Sigmoid nodes
#
import tensorflow as tf
tf.compat.v1.reset_default_graph()
with tf.compat.v1.Session() as sess:
inp = tf.compat.v1.placeholder(tf.float32, [1, 3, 2, 2], 'Input')
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
# save model to .pb and return path to the model
return save_to_pb(tf_net, tmp_dir)
def create_tf_param_res_model(self, tmp_dir):
#
# Create Tensorflow model with following pattern:
@ -204,3 +224,25 @@ class TestComplexParams(CommonMOConvertTest):
test_params.update({'input_model': tf_net_path})
ref_params.update({'input_model': tf_net_path})
self._test(temp_dir, test_params, ref_params)
@pytest.mark.nightly
@pytest.mark.precommit
def test_mo_convert_clearing_transformation_registry(self, ie_device, precision, ir_version,
temp_dir, use_new_frontend, use_old_api):
tf_net_path = self.create_tf_model_single_input_output(temp_dir)
from openvino.tools.mo import convert_model
config_path = os.path.join(os.path.dirname(__file__), "test_transform_config/test_config.json")
test_config_based_transform = os.path.join(os.path.dirname(__file__), "test_legacy_exts/test_config_transform/")
# apply config based transformation on model
_ = convert_model(input_model=tf_net_path, transformations_config=config_path,
extensions=test_config_based_transform)
# convert another model which would fail if custom transform from config_path applied
tf_net_path = self.create_tf_model_no_sigmoid(temp_dir)
_ = convert_model(input_model=tf_net_path, extensions=test_config_based_transform)
# check that CustomReplacementRegistry.registry is cleared
from openvino.tools.mo.front.common.custom_replacement_registry import CustomReplacementRegistry
assert len(CustomReplacementRegistry.registry) == 0

View File

@ -0,0 +1,9 @@
[
{
"custom_attributes": {
"masks_node_prefix_name": "node_name"
},
"id": "ConfigBasedTestReplacement",
"match_kind": "general"
}
]

View File

@ -7,6 +7,7 @@ from enum import Enum
import networkx as nx
from openvino.tools.mo.front.common.custom_replacement_registry import CustomReplacementRegistry
from openvino.tools.mo.graph.graph import Graph
from openvino.tools.mo.middle.passes.eliminate import shape_inference
from openvino.tools.mo.middle.pattern_match import for_graph_and_each_sub_graph_recursively
@ -337,4 +338,5 @@ def apply_replacements(graph: Graph, replacements_type: list):
def clear_registered_classes_dict():
CustomReplacementRegistry.registry = {}
_registered_classes_dict.clear()