Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / TensorIterator_utils.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 from mo.middle.replacement import MiddleReplacementPattern
17
18 next_ops = ['NextIteration', 'TensorArrayWriteV3']
19
20
21 class DeleteSelect(MiddleReplacementPattern):
22     enabled = True
23     graph_condition = [lambda graph: graph.graph['is_cyclic']]
24
25     def run_after(self):
26         from extensions.middle.AddIsCyclicAttribute import AddIsCyclicAttribute
27         return [AddIsCyclicAttribute]
28
29     def run_before(self):
30         return []
31
32     @staticmethod
33     def pattern():
34         return dict(
35             nodes=[
36                 ('Select', dict(kind='op', op='Select')),
37                 ('Select_data', dict(kind='data')),
38
39                 ('next_op', dict(kind='op')),
40             ],
41             edges=[
42                 ('Select', 'Select_data'),
43                 ('Select_data', 'next_op'),
44             ],
45         )
46
47     @staticmethod
48     def replace_pattern(graph, match: dict):
49         if match['next_op']['op'] not in next_ops:
50             return
51         select = match['Select']
52         assert len(select.in_nodes()) == 3
53
54         default = select.in_node(1)
55         input = select.in_node(2)
56
57         edge_attrs = graph.get_edge_data(match['Select_data'].id, match['next_op'].id)
58         graph.add_edges_from([(input.id, match['next_op'].id, edge_attrs[0])])
59
60         graph.remove_edge(input.id, select.id)
61
62         safe_nodes = ['next_op']
63         nodes_for_remove = []
64         for node in match.keys():
65             if node not in safe_nodes:
66                 nodes_for_remove.append(match[node].id)
67         graph.remove_nodes_from(nodes_for_remove)