Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / TensorIteratorInput_test.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 unittest
17
18 import numpy as np
19
20 from extensions.middle.TensorIteratorInput import SmartInputMatcher, SimpleInputMatcher, BackEdgeSimpleInputMatcher
21 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
22
23
24 class SmartInputMatcherTests(unittest.TestCase):
25     def test(self):
26         pattern_matcher = SmartInputMatcher()
27         pattern = pattern_matcher.pattern()
28
29         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
30                                        update_edge_attrs={('range_data', 'TensorArrayScatter', 0): {'in': 1},
31                                                           ('TensorArray_handle', 'TensorArrayScatter', 0): {'in': 0},
32                                                           ('TensorArray_flow', 'TensorArrayScatter', 0): {'in': 3}},
33                                        new_nodes_with_attrs=[('ta_size', {'kind': 'data'}),
34                                                              ('ta_size_op', {'kind': 'op'}),
35                                                              ('value', {'kind': 'data'}),
36                                                              ],
37                                        new_edges_with_attrs=[
38                                            ('ta_size_op', 'ta_size'),
39                                            ('ta_size', 'TensorArray'),
40                                            ('value', 'TensorArrayScatter', {'in':2}),
41                                                              ],
42                                        update_nodes_attributes=[('Enter_data', {'value': np.array([1])}),
43                                                                 ('stack_data', {'value': np.array([0])}),
44                                                                 ('stack_1_data', {'value': np.array([1])}),
45                                                                 ('stack_2_data', {'value': np.array([1])}),
46                                                                 ('start_data', {'value': np.array([0])}),
47                                                                 ('delta_data', {'value': np.array([1])})
48                                                                 ])
49
50         pattern_matcher.find_and_replace_pattern(graph)
51         graph_ref = build_graph_with_attrs(
52             nodes_with_attrs=[('condition_data', {'kind': 'data'}),
53                               ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
54                               ('TensorArrayRead_data', {'kind': 'data'}),
55                               ('condition_data', {'kind': 'data'}),
56                               ('value', {'kind': 'data'}),
57                               ('ta_size', {'kind': 'data'}),
58                               ('ta_size_op', {'kind': 'op'})],
59             edges_with_attrs=[('ta_size', 'TensorIteratorInput', {'in': 0}),
60                               ('condition_data', 'TensorIteratorInput', {'in': 2}),
61                               ('value', 'TensorIteratorInput', {'in': 1}),
62                               ('TensorIteratorInput', 'TensorArrayRead_data'),
63                               ('ta_size_op', 'ta_size')],
64             update_edge_attrs=None,
65             new_nodes_with_attrs=[],
66             new_edges_with_attrs=[],
67             )
68         (flag, resp) = compare_graphs(graph, graph_ref, 'TensorArrayRead_data', check_op_attrs=True)
69         self.assertTrue(flag, resp)
70
71
72 class SimpleInputMatcherTest(unittest.TestCase):
73     def test(self):
74         pattern_matcher = SimpleInputMatcher()
75         pattern = pattern_matcher.pattern()
76
77         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
78                                        update_edge_attrs=None,
79                                        new_nodes_with_attrs=[('in_node', {'kind': 'data'}),
80                                                              ('Enter_data', {'kind': 'data'})],
81                                        new_edges_with_attrs=[('in_node', 'Enter'), ('Enter', 'Enter_data')],
82                                        update_nodes_attributes=[])
83
84         pattern_matcher.find_and_replace_pattern(graph)
85
86         graph_ref = build_graph_with_attrs(
87             nodes_with_attrs=[('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
88                               ('in_node', {'kind': 'data'}),
89                               ('Enter_data', {'kind': 'data'})
90                               ],
91             edges_with_attrs=[('in_node', 'TensorIteratorInput'), ('TensorIteratorInput', 'Enter_data')],
92         )
93         (flag, resp) = compare_graphs(graph, graph_ref, 'Enter_data', check_op_attrs=True)
94         self.assertTrue(flag, resp)
95
96
97 class BackEdgeInputMatcherTest(unittest.TestCase):
98     def test1(self):
99         """
100         Case with constant input to init
101         """
102         pattern_matcher = BackEdgeSimpleInputMatcher()
103         pattern = pattern_matcher.pattern()
104
105         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
106                                        new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
107                                                              ('condition', {'kind': 'data'}),
108                                                              ('init', {'kind': 'data', 'shape': np.array([1,3])}),
109                                                              ],
110                                        new_edges_with_attrs=[('condition', 'BackEdge', {'in': 2}),
111                                                              ('init', 'BackEdge', {'in': 0}),
112                                                              ('cycle_data', 'BackEdge', {'in': 1})],)
113
114         pattern_matcher.find_and_replace_pattern(graph)
115         graph_ref = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
116                                        new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
117                                                              ('condition', {'kind': 'data'}),
118                                                              ('init', {'kind': 'data', 'shape': np.array([1,3])}),
119                                                              ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
120                                                              ('TensorIteratorInput_data', {'kind': 'data', 'shape': np.array([1,3])}),
121                                                              ],
122                                        new_edges_with_attrs=[('TensorIteratorInput_data', 'TensorIteratorInput'),
123                                                              ('TensorIteratorInput', 'init'),
124                                                             ('condition', 'BackEdge', {'in': 2}),
125                                                              ('init', 'BackEdge', {'in': 0}),
126                                                              ('cycle_data', 'BackEdge', {'in': 1})],)
127         (flag, resp) = compare_graphs(graph, graph_ref, 'BackEdge', check_op_attrs=True)
128         self.assertTrue(flag, resp)
129
130     def test2(self):
131         """
132         Case with non-constant input to init.
133         Nothing should happen with graph.
134         """
135         pattern_matcher = BackEdgeSimpleInputMatcher()
136         pattern = pattern_matcher.pattern()
137
138         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
139                                        new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
140                                                              ('condition', {'kind': 'data'}),
141                                                              ('init', {'kind': 'data', 'shape': np.array([1, 3])}),
142                                                              ('Enter', {'kind': 'op', 'op': 'Enter'}),
143                                                              ],
144                                        new_edges_with_attrs=[('Enter', 'init'),
145                                                              ('condition', 'BackEdge', {'in': 2}),
146                                                              ('init', 'BackEdge', {'in': 0}),
147                                                              ('cycle_data', 'BackEdge', {'in': 1})])
148
149         pattern_matcher.find_and_replace_pattern(graph)
150
151         graph_ref = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
152                                        new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
153                                                              ('condition', {'kind': 'data'}),
154                                                              ('init', {'kind': 'data', 'shape': np.array([1, 3])}),
155                                                              ('Enter', {'kind': 'op', 'op': 'Enter'}),
156                                                              ],
157                                        new_edges_with_attrs=[('Enter', 'init'),
158                                                              ('condition', 'BackEdge', {'in': 2}),
159                                                              ('init', 'BackEdge', {'in': 0}),
160                                                              ('cycle_data', 'BackEdge', {'in': 1})], )
161
162         (flag, resp) = compare_graphs(graph, graph_ref, 'BackEdge', check_op_attrs=True)
163         self.assertTrue(flag, resp)