Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / assign_elimination.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
17 import logging as log
18
19 import networkx as nx
20
21 from mo.front.common.replacement import FrontReplacementOp
22 from mo.graph.graph import Node, Graph
23 from mo.utils.error import Error
24
25
26 class AssignElimination(FrontReplacementOp):
27     op = "Assign"
28     enabled = True
29
30     def replace_sub_graph(self, graph: Graph, match: dict):
31         node = match['op']
32         # here we request all data flow output edges (control flow edges will not be listed)
33         out_edges = node.out_edges()
34         if len(out_edges) == 0:
35             graph.remove_node(node.id)
36             log.debug('Assign op was removed {}'.format(node.id))
37         else:
38             raise Error('Data flow edge coming out of Assign node {}'.format(node.id))
39
40
41 class AssignSubElimination(FrontReplacementOp):
42     op = "AssignSub"
43     enabled = True
44
45     def replace_sub_graph(self, graph: Graph, match: dict):
46         node = match['op']
47         # here we request all data flow output edges (control flow edges will not be listed)
48         out_edges = node.out_edges()
49         if len(out_edges) == 0:
50             graph.remove_node(node.id)
51             log.debug('AssignSub op was removed {}'.format(node.id))
52         else:
53             raise Error('Data flow edge coming out of AssignSub node {}'.format(node.id))
54
55
56 class AssignAddElimination(FrontReplacementOp):
57     op = "AssignAdd"
58     enabled = True
59
60     def replace_sub_graph(self, graph: Graph, match: dict):
61         node = match['op']
62         # here we request all data flow output edges (control flow edges will not be listed)
63         out_edges = node.out_edges()
64         if len(out_edges) == 0:
65             graph.remove_node(node.id)
66             log.debug('AssignAdd op was removed {}'.format(node.id))
67         else:
68             raise Error('Data flow edge coming out of AssignAdd node {}'.format(node.id))
69
70
71 class AssertElimination(FrontReplacementOp):
72     op = "Assert"
73     enabled = True
74
75     def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
76         node = match['op']
77         # here we request all data flow output edges (control flow edges will not be listed)
78         out_edges = node.out_edges()
79         if len(out_edges) == 0:
80             graph.remove_node(node.id)
81             log.debug('Assert op was removed {}'.format(node.id))
82         else:
83             raise Error('Data flow edge coming out of Assert node {}'.format(node.id))