Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / pipeline / common_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
17 import unittest
18
19 from generator import generator, generate
20
21 from mo.graph.graph import Node
22 from mo.pipeline.common import determined_sort, get_fw_tensor_debug_info, get_sorted_outputs
23 from mo.utils.unittest.graph import build_graph_with_edge_attrs
24
25
26 @generator
27 class TestTopologicalSort(unittest.TestCase):
28     @generate(
29         [('A', 'Ad', {'out': 0}),
30          ('Ad', 'B', {'in': 0}),
31          ('B', 'Bd', {'out': 0}),
32          ('Bd', 'C', {'in': 0}),
33          ('C', 'Cd', {'out': 0}),
34          ('Cd', 'D', {'in': 0}),
35          ('D', 'Dd', {'out': 0}),
36          ('Dd', 'E', {'in': 0}),
37          ('E', 'Ed', {'out': 0}),
38          ('Ed', 'I', {'in': 0}),
39          ('Cd', 'F', {'in': 0}),
40          ('F', 'Fd', {'out': 0}),
41          ('Fd', 'G', {'in': 0}),
42          ('G', 'Gd', {'out': 0}),
43          ('Gd', 'I', {'in': 1}),
44          ('Cd', 'H', {'in': 0}),
45          ('H', 'Hd', {'out': 0}),
46          ('Hd', 'I', {'in': 2}),
47          ('I', 'Id', {'out': 0}),
48          ('Id', 'J', {'in': 0}),
49          ('J', 'Jd', {'out': 0}),
50          ('Jd', 'K', {'in': 0}),
51          ('K', 'Kd', {'out': 0})],
52
53         [('A', 'Ad', {'out': 0}),
54          ('Ad', 'B', {'in': 0}),
55          ('B', 'Bd', {'out': 0}),
56          ('Bd', 'C', {'in': 0}),
57          ('C', 'Cd', {'out': 0}),
58          ('Cd', 'D', {'in': 0}),
59          ('D', 'Dd', {'out': 0}),
60          ('Dd', 'E', {'in': 0}),
61          ('E', 'Ed', {'out': 0}),
62          ('Ed', 'I', {'in': 0}),
63          ('Cd', 'F', {'in': 0}),
64          ('F', 'Fd', {'out': 0}),
65          ('Fd', 'G', {'in': 0}),
66          ('G', 'Gd', {'out': 0}),
67          ('Gd', 'I', {'in': 1}),
68          ('Cd', 'H', {'in': 0}),
69          ('H', 'Hd', {'out': 0}),
70          ('Hd', 'I', {'in': 2}),
71          ('I', 'Id', {'out': 0}),
72          ('Id', 'J', {'in': 0}),
73          ('J', 'Jd', {'out': 0}),
74          ('Jd', 'K', {'in': 0}),
75          ('K', 'Kd', {'out': 0}),
76          ('Ad', 'E', {'in': 1}),
77          ('Bd', 'K', {'in': 1}),
78          ('Hd', 'J', {'in': 1})],
79
80         [('A', 'Ad', {'out': 0}),
81          ('Ad', 'B', {'in': 0}),
82          ('B', 'Bd', {'out': 0}),
83          ('Bd', 'C', {'in': 0}),
84          ('C', 'Cd', {'out': 0}),
85          ('Cd', 'D', {'in': 0}),
86          ('D', 'Dd', {'out': 0}),
87          ('Dd', 'E', {'in': 0}),
88          ('E', 'Ed', {'out': 0}),
89          ('Ed', 'I', {'in': 0}),
90          ('Cd', 'F', {'in': 0}),
91          ('F', 'Fd', {'out': 0}),
92          ('Fd', 'G', {'in': 0}),
93          ('G', 'Gd', {'out': 0}),
94          ('Gd', 'I', {'in': 1}),
95          ('Cd', 'H', {'in': 0}),
96          ('H', 'Hd', {'out': 0}),
97          ('Hd', 'I', {'in': 2}),
98          ('I', 'Id', {'out': 0}),
99          ('Id', 'J', {'in': 0}),
100          ('J', 'Jd', {'out': 0}),
101          ('Jd', 'K', {'in': 0}),
102          ('K', 'Kd', {'out': 0}),
103          ('Ad', 'E', {'in': 1}),
104          ('Bd', 'K', {'in': 1}),
105          ('Hd', 'J', {'in': 1}),
106          ('Dd', 'F', {'in': 1}),
107          ('Fd', 'H', {'in': 1}),
108          ('Gd', 'H', {'in': 0})]
109     )
110     def test_determined_topological_sort(self, edges):
111         nodes = {'A': {'type': 'Identity', 'kind': 'op'},
112                  'B': {'type': 'Identity', 'kind': 'op'},
113                  'C': {'type': 'Identity', 'kind': 'op'},
114                  'D': {'type': 'Identity', 'kind': 'op'},
115                  'E': {'type': 'Identity', 'kind': 'op'},
116                  'F': {'type': 'Identity', 'kind': 'op'},
117                  'G': {'type': 'Identity', 'kind': 'op'},
118                  'H': {'type': 'Identity', 'kind': 'op'},
119                  'I': {'type': 'Identity', 'kind': 'op'},
120                  'J': {'type': 'Identity', 'kind': 'op'},
121                  'K': {'type': 'Identity', 'kind': 'op'},
122                  'Ad': {'value': None, 'kind': 'data'},
123                  'Bd': {'value': None, 'kind': 'data'},
124                  'Cd': {'value': None, 'kind': 'data'},
125                  'Dd': {'value': None, 'kind': 'data'},
126                  'Ed': {'value': None, 'kind': 'data'},
127                  'Fd': {'value': None, 'kind': 'data'},
128                  'Gd': {'value': None, 'kind': 'data'},
129                  'Hd': {'value': None, 'kind': 'data'},
130                  'Id': {'value': None, 'kind': 'data'},
131                  'Jd': {'value': None, 'kind': 'data'},
132                  'Kd': {'value': None, 'kind': 'data'},
133                  }
134
135         graph = build_graph_with_edge_attrs(nodes, edges)
136         outputs = [Node(graph, 'Kd')]
137         for i in range(100):
138             op_order, data_order = determined_sort(outputs)
139             self.assertListEqual(op_order, ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'])
140             self.assertListEqual(data_order, ['Ad', 'Bd', 'Cd', 'Dd', 'Ed', 'Fd', 'Gd', 'Hd', 'Id', 'Jd', 'Kd'])
141
142
143 class TestGetFWTensorName(unittest.TestCase):
144     def test_get_fw_tensor_debug_info(self):
145         nodes = {
146             'A': {'type': 'Identity', 'kind': 'op'},
147             'B': {'type': 'Identity', 'kind': 'op'},
148             'C': {'type': 'Identity', 'kind': 'op'},
149             'Ad': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('A', 0)]},
150             'Bd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('B', 0)]},
151             'Cd': {'value': None, 'kind': 'data'},
152         }
153         edges = [
154             ('A', 'Ad', {'out': 0}),
155             ('Ad', 'B', {'in': 0}),
156             ('B', 'Bd', {'out': 0}),
157             ('Bd', 'C', {'in': 0}),
158             ('C', 'Cd', {'out': 0})
159         ]
160         graph = build_graph_with_edge_attrs(nodes, edges)
161         fw_debug_info = get_fw_tensor_debug_info(Node(graph, 'Cd'))
162         self.assertEqual(len(fw_debug_info), 1)
163         self.assertEqual(fw_debug_info[0], ('B', 0))
164
165
166 class TestOutputSort(unittest.TestCase):
167     def test_get_sorted_outputs(self):
168         nodes = {'A': {'type': 'Identity', 'kind': 'op'},
169                  'B': {'type': 'Identity', 'kind': 'op'},
170                  'C': {'type': 'Identity', 'kind': 'op'},
171                  'D': {'type': 'Identity', 'kind': 'op'},
172                  'E': {'type': 'Identity', 'kind': 'op'},
173                  'F': {'type': 'Identity', 'kind': 'op'},
174                  'G': {'type': 'Identity', 'kind': 'op'},
175                  'H': {'type': 'Identity', 'kind': 'op'},
176                  'Ad': {'value': None, 'kind': 'data'},
177                  'Bd': {'value': None, 'kind': 'data'},
178                  'Cd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('C', 0)]},
179                  'Dd': {'value': None, 'kind': 'data'},
180                  'Ed': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('E', 0)]},
181                  'Fd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('F', 0)]},
182                  'Gd': {'value': None, 'kind': 'data'},
183                  'Hd': {'value': None, 'kind': 'data'}
184                  }
185         edges = [
186             ('A', 'Ad', {'out': 0}),
187             ('Ad', 'B', {'in': 0}),
188             ('B', 'Bd', {'out': 0}),
189             ('Bd', 'C', {'in': 0}),
190             ('C', 'Cd', {'out': 0}),
191             ('Cd', 'D', {'in': 0}),
192             ('D', 'Dd', {'out': 0}),
193             ('Dd', 'E', {'in': 0}),
194             ('E', 'Ed', {'out': 0}),
195             ('Cd', 'F', {'in': 0}),
196             ('F', 'Fd', {'out': 0}),
197             ('Fd', 'G', {'in': 0}),
198             ('G', 'Gd', {'out': 0}),
199             ('Cd', 'H', {'in': 0}),
200             ('H', 'Hd', {'out': 0})
201         ]
202         graph = build_graph_with_edge_attrs(nodes, edges)
203         self.assertListEqual([node.id for node in get_sorted_outputs(graph)], ['Hd', 'Ed', 'Gd'])
204
205     def test_get_sorted_outputs_fine_situation(self):
206         nodes = {'A': {'type': 'Identity', 'kind': 'op'},
207                  'B': {'type': 'Identity', 'kind': 'op'},
208                  'C': {'type': 'Identity', 'kind': 'op'},
209                  'D': {'type': 'Identity', 'kind': 'op'},
210                  'E': {'type': 'Identity', 'kind': 'op'},
211                  'F': {'type': 'Identity', 'kind': 'op'},
212                  'G': {'type': 'Identity', 'kind': 'op'},
213                  'H': {'type': 'Identity', 'kind': 'op'},
214                  'Ad': {'value': None, 'kind': 'data'},
215                  'Bd': {'value': None, 'kind': 'data'},
216                  'Cd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('C', 0)]},
217                  'Dd': {'value': None, 'kind': 'data'},
218                  'Ed': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('E', 0)]},
219                  'Fd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('F', 0)]},
220                  'Gd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('G', 0)]},
221                  'Hd': {'value': None, 'kind': 'data', 'fw_tensor_debug_info': [('H', 0)]}
222                  }
223         edges = [
224             ('A', 'Ad', {'out': 0}),
225             ('Ad', 'B', {'in': 0}),
226             ('B', 'Bd', {'out': 0}),
227             ('Bd', 'C', {'in': 0}),
228             ('C', 'Cd', {'out': 0}),
229             ('Cd', 'D', {'in': 0}),
230             ('D', 'Dd', {'out': 0}),
231             ('Dd', 'E', {'in': 0}),
232             ('E', 'Ed', {'out': 0}),
233             ('Cd', 'F', {'in': 0}),
234             ('F', 'Fd', {'out': 0}),
235             ('Fd', 'G', {'in': 0}),
236             ('G', 'Gd', {'out': 0}),
237             ('Cd', 'H', {'in': 0}),
238             ('H', 'Hd', {'out': 0})
239         ]
240         graph = build_graph_with_edge_attrs(nodes, edges)
241         self.assertListEqual([node.id for node in get_sorted_outputs(graph)], ['Ed', 'Gd', 'Hd'])