Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / FusePermutesSequence.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16 import numpy as np
17
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
24
25
26 class FusePermutesSequence(MiddleReplacementPattern):
27     """
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
30     """
31
32     enabled = True
33     graph_condition = [lambda graph: graph.graph['fw'] != 'caffe']
34
35     def run_after(self):
36         return [ConvertLayoutDependentOperations]
37
38     def find_and_replace_pattern(self, graph: Graph):
39         for node in list(graph.nodes()):
40             if node not in graph.nodes():
41                 continue
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
46                 node = permute_node
47                 while True:
48                     next_ops = get_next_operation(node)
49                     if len(next_ops) != 1:
50                         break
51
52                     next_op = next_ops[0]
53                     if next_op.has_valid('type') and next_op.type == 'Permute':
54                         list_of_permutes.append(next_op)
55                         node = next_op
56                     else:
57                         break
58
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)]
64
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)
68                 else:
69                     if len(list_of_permutes) < 2:
70                         continue
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)
74
75                 graph.remove_edge(last_data_node.in_node().id, last_data_node.id)
76
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)