forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Result rename operation (openvinotoolkit#4242)
* Added result rename operation * Optimize imports * Added ResultRename to package_BOM * ResultRename moved to the end of back phase, code refactoring * Revert incorrect changes * Optimize imports * Added comments and optimized imports.
- Loading branch information
Showing
8 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Copyright (C) 2018-2021 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. | ||
""" | ||
|
||
from mo.back.replacement import BackReplacementPattern | ||
from mo.graph.graph import Graph | ||
|
||
|
||
class ResultRename(BackReplacementPattern): | ||
# This transformation sets the Result operation name equal to the incoming tensor name. | ||
# For some frameworks like kaldi and onnx this may result in appearance of nodes with identical names, | ||
# which can lead to errors in other transformations. | ||
# So ResultRename should be launched at the end of back phase. | ||
enabled = False | ||
|
||
def find_and_replace_pattern(self, graph: Graph): | ||
for node in graph.get_op_nodes(type='Result'): | ||
if node.in_ports(): | ||
prev_node_out_port = node.in_port(0).get_connection().get_source() | ||
tensor_names = prev_node_out_port.get_tensor_names() | ||
if tensor_names: | ||
result_name = tensor_names[0] | ||
else: | ||
result_name = prev_node_out_port.node.soft_get('name', prev_node_out_port.node.id) + \ | ||
'/sink_port_' + str(prev_node_out_port.idx) | ||
node['name'] = result_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Copyright (C) 2018-2021 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 unittest | ||
|
||
from extensions.back.ResultRename import ResultRename | ||
from mo.graph.graph import Node | ||
from mo.utils.ir_engine.compare_graphs import compare_graphs | ||
from mo.utils.unittest.graph import build_graph, regular_op, result | ||
|
||
nodes = { | ||
**regular_op('Op1', {'type': 'Op1', 'kind': 'op', 'op': 'Op1'}), | ||
**result('result'), | ||
'Op1_data': {'kind': 'data', 'fw_tensor_debug_info': [('Op1', 0, 'Op1_tensor')]} | ||
} | ||
|
||
|
||
class ResultRenameTest(unittest.TestCase): | ||
def test_case1(self): | ||
graph = build_graph(nodes, [('Op1', 'Op1_data'), ('Op1_data', 'result')]) | ||
graph_ref = build_graph(nodes, [('Op1', 'Op1_data'), ('Op1_data', 'result')]) | ||
res_node = Node(graph_ref, 'result') | ||
res_node['name'] = 'Op1_tensor' | ||
|
||
ResultRename().find_and_replace_pattern(graph) | ||
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True) | ||
self.assertTrue(flag, resp) | ||
|
||
def test_case2(self): | ||
graph = build_graph(nodes, []) | ||
graph_ref = build_graph(nodes, []) | ||
|
||
ResultRename().find_and_replace_pattern(graph) | ||
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True) | ||
self.assertTrue(flag, resp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters