Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / RemoveIdentity.py
1 """
2  Copyright (c) 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
17 from mo.graph.graph import Graph
18 from mo.middle.passes.eliminate import remove_op_node_with_data_node
19 from mo.middle.replacement import MiddleReplacementPattern
20
21
22 class RemoveIdentity(MiddleReplacementPattern):
23     enabled = True
24
25     def run_after(self):
26         from extensions.middle.InputCut import MiddleInputCut
27         return [MiddleInputCut]
28
29     def run_before(self):
30         from extensions.middle.pass_separator import MiddleStart
31         return [MiddleStart]
32
33     def pattern(self):
34         return dict(
35             nodes=[('op', dict(kind='op', identity=True))],
36             edges=[]
37         )
38
39     def replace_pattern(self, graph: Graph, match: dict):
40         remove_op_node_with_data_node(graph, match['op'])
41
42
43 class RemoveDropout(MiddleReplacementPattern):
44     enabled = True
45
46     def run_after(self):
47         from extensions.middle.pass_separator import PreMiddleStart
48         return [PreMiddleStart]
49
50     def run_before(self):
51         from extensions.middle.pass_separator import MiddleStart
52         return [MiddleStart]
53
54     def pattern(self):
55         return dict(
56             nodes=[('op', dict(op='Dropout'))],
57             edges=[]
58         )
59
60     def replace_pattern(self, graph: Graph, match: dict):
61         remove_op_node_with_data_node(graph, match['op'])
62
63
64 class RemoveNodesWithZeroPhase(MiddleReplacementPattern):
65     enabled = True
66     force_clean_up = True
67
68     def run_after(self):
69         from extensions.middle.pass_separator import PreMiddleStart
70         return [PreMiddleStart]
71
72     def run_before(self):
73         from extensions.middle.pass_separator import MiddleStart
74         return [MiddleStart]
75
76     def pattern(self):
77         return dict(
78             nodes=[('op', dict(kind='op', phase=0))],
79             edges=[]
80         )
81
82     def replace_pattern(self, graph: Graph, match: dict):
83         remove_op_node_with_data_node(graph, match['op'])