2 Copyright (c) 2018-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 from extensions.middle.ConvertLayoutDependentOperations import ConvertLayoutDependentOperations
19 from mo.graph.graph import Node, Graph
20 from mo.middle.passes.eliminate import merge_data_nodes, graph_clean_up_tf
21 from mo.middle.passes.fusing.helpers import get_next_operation
22 from mo.middle.replacement import MiddleReplacementPattern
23 from mo.utils.error import Error
26 class FusePermutesSequence(MiddleReplacementPattern):
28 This pass finds sequence of Permute operations and merge them to single Permute operation
29 In case if resulting Permutation do nothing, we just remove it
33 graph_condition = [lambda graph: graph.graph['fw'] != 'caffe']
36 return [ConvertLayoutDependentOperations]
38 def find_and_replace_pattern(self, graph: Graph):
39 for node in list(graph.nodes()):
40 if node not in graph.nodes():
42 permute_node = Node(graph, node)
43 if permute_node.has_valid('type') and permute_node.type == 'Permute':
44 list_of_permutes = [permute_node]
45 # Get sequence of permutations
48 next_ops = get_next_operation(node)
49 if len(next_ops) != 1:
53 if next_op.has_valid('type') and next_op.type == 'Permute':
54 list_of_permutes.append(next_op)
59 final_permutation = np.array([x for x in range(len(list_of_permutes[0].order))], dtype=np.int64)
60 for permute in list_of_permutes:
61 if not permute.has_valid('order'):
62 raise Error("Permute node {} has wrong attribute order = None".format(permute.name))
63 final_permutation = final_permutation[np.array(permute.order, dtype=np.int64)]
65 if np.array_equal(final_permutation, [x for x in range(len(list_of_permutes[0].order))]):
66 first_data_node, last_data_node = list_of_permutes[0].in_node(), list_of_permutes[-1].out_node()
67 graph.remove_edge(first_data_node.id, list_of_permutes[0].id)
69 if len(list_of_permutes) < 2:
71 first_data_node, last_data_node = list_of_permutes[0].out_node(), list_of_permutes[-1].out_node()
72 list_of_permutes[0].order = final_permutation
73 graph.remove_edge(first_data_node.id, first_data_node.out_node().id)
75 graph.remove_edge(last_data_node.in_node().id, last_data_node.id)
77 merge_data_nodes(graph, first_data_node, last_data_node)
78 graph.remove_node(last_data_node.id)
79 graph_clean_up_tf(graph)