Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / back / kaldi_remove_memory_output.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 from mo.back.replacement import BackReplacementPattern
18 from mo.graph.graph import Graph
19
20
21 class KaldiRemoveMemoryOutputBackReplacementPattern(BackReplacementPattern):
22     enabled = False
23
24     @staticmethod
25     def pattern():
26         return dict(
27             nodes=[
28                 ('memory_node', dict(op='Memory')),
29                 ('data_node', dict(kind='data')),
30                 ('op_output', dict(op='OpOutput'))
31             ],
32             edges=[
33                 ('memory_node', 'data_node'),
34                 ('data_node', 'op_output')
35             ]
36         )
37
38     @staticmethod
39     def replace_pattern(graph: Graph, match: dict):
40         """
41         Need to find the pattern: Memory -> Data -> OpOutput
42
43         It is needed to make Memory nodes appear in IR,
44         but they are output nodes by default and we remove the OpOutput node after each output memory.
45
46         DO NOT use graph clean up after it
47         otherwise Memory nodes would be removed as they are not on the path from input to output
48
49         Parameters
50         ----------
51         graph : Graph
52            Graph with loaded model.
53         match : dict
54            Patterns which were found in graph structure.
55         """
56         memory = match['memory_node']
57         data = match['data_node']
58
59         graph.remove_edge(memory.id, data.id)
60         graph.remove_node(data.id)