Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / CheckForCycle_test.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 import unittest
18
19 from extensions.middle.CheckForCycle import CheckForCycle
20 from mo.utils.error import Error
21 from mo.utils.unittest.graph import build_graph
22
23 nodes_attributes = {'node_1': {'type': 'Identity', 'value': None, 'kind': 'op'},
24                     'node_1_data': {'value': None, 'kind': 'data', 'data_type': None},
25                     'node_2': {'type': 'Identity', 'value': None, 'kind': 'op'},
26                     'concat': {'type': 'Concat', 'value': None, 'kind': 'op'},
27                     'node_3': {'type': 'Identity', 'value': None, 'kind': 'op'},
28                     'node_3_data': {'value': None, 'kind': 'data', 'data_type': None},
29                     # Placeholders
30                     'placeholder_1': {'shape': None, 'type': 'Input', 'kind': 'op', 'op': 'Placeholder'},
31                     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
32                     'placeholder_2': {'shape': None, 'type': 'Input', 'kind': 'op', 'op': 'Placeholder'},
33                     'pl_1': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
34                     'pl_1_data': {'value': None, 'kind': 'data', 'data_type': None},
35                     'pl_2': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
36                     'pl_2_data': {'value': None, 'kind': 'data', 'data_type': None},
37                     'placeholder_2_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
38                     # ScaleShift layer
39                     'scaleshift_1': {'type': 'ScaleShift', 'kind': 'op', 'op': 'ScaleShift'},
40                     'scaleshift_1_w': {'value': None, 'shape': None, 'kind': 'data'},
41                     'scaleshift_1_b': {'value': None, 'shape': None, 'kind': 'data'},
42                     'scaleshift_1_data': {'value': None, 'shape': None, 'kind': 'data'},
43                     # Mul op
44                     'mul_1': {'type': None, 'kind': 'op', 'op': 'Mul'},
45                     'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
46                     'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
47                     'op_output': {'kind': 'op', 'op': 'OpOutput', 'infer': lambda x: None}
48                     }
49
50
51 class CycleTest(unittest.TestCase):
52     def test_check_for_cycle1(self):
53         # cyclic case
54         graph = build_graph(nodes_attributes,
55                             [('node_1', 'node_1_data'),
56                              ('node_1_data', 'node_3'),
57                              ('node_3', 'node_3_data'),
58                              ('node_3_data', 'node_1')],
59                             nodes_with_edges_only=True)
60         with self.assertRaisesRegex(Error, 'Graph contains a cycle. Can not proceed.*'):
61             CheckForCycle().find_and_replace_pattern(graph)
62
63     def test_check_for_cycle2(self):
64         # acyclic case
65         graph = build_graph(nodes_attributes,
66                             [('node_1', 'node_1_data'),
67                              ('node_1_data', 'node_3'),
68                              ('node_3', 'node_3_data'),
69                              ('node_3_data', 'mul_1'),
70                              ('mul_1_w', 'mul_1'),
71                              ('mul_1', 'mul_1_data')
72                              ],
73                             nodes_with_edges_only=True)
74         try:
75             CheckForCycle().find_and_replace_pattern(graph)
76         except Error:
77             self.fail("Unexpected Error raised")