Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / merge_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 import numpy as np
19
20 from extensions.ops.merge import Merge
21 from mo.graph.graph import Node
22 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
23
24
25 class TestMerge(unittest.TestCase):
26     nodes = [
27         ('first', {'value': np.ones((2, 2)), 'kind': 'data', 'executable': True, 'shape': np.array([2, 2]),
28                    'is_partial_inferred': True}),
29         ('second', {'value': np.zeros((2, 2)), 'kind': 'data', 'executable': False, 'shape': np.array([2, 2]),
30                     'is_partial_inferred': True}),
31         ('merge', {'type': 'Merge', 'kind': 'op', 'op': 'Merge'}),
32         ('merge_output', {'value': None, 'kind': 'data', 'executable': True, 'shape': None}),
33     ]
34     edges = [
35         ('first', 'merge', {'in': 0}),
36         ('second', 'merge', {'in': 1}),
37         ('merge', 'merge_output', {'out': 0}),
38     ]
39
40     def test_merge_infer_simple_case_one_executable(self):
41         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges)
42
43         # We should propagate value of the first input since only this input is executable
44         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
45                                            edges_with_attrs=self.edges,
46                                            update_nodes_attributes=[('merge_output', {'shape': np.array([2, 2]),
47                                                                                       'value': np.ones((2,2))}),
48                                                                     ('merge', {'is_not_fully_inferred': False})])
49
50         tested_class = Merge(graph=graph, attrs={})
51         node = Node(graph, 'merge')
52         tested_class.merge_infer(node)
53
54         (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
55         self.assertTrue(flag, resp)
56
57     def test_merge_infer_complex_case(self):
58         """
59         Case as in cycles when in first visit only one input are inferred and in the second -- both.
60         """
61         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
62                                        update_nodes_attributes=[('first', {'is_partial_inferred': False,
63                                                                            'value': None}),
64                                                                 ('second', {'executable': True})])
65
66         # In first visit we should propagate only shapes
67         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
68                                            edges_with_attrs=self.edges,
69                                            update_nodes_attributes=[('second', {'executable': True}),
70                                                                     ('first', {'is_partial_inferred': False,
71                                                                                 'value': None}),
72                                                                     ('merge_output', {'shape': np.array([2, 2]),
73                                                                                       'value': None}),
74                                                                     ('merge', {'is_not_fully_inferred': True})])
75         tested_class = Merge(graph=graph, attrs={})
76         node = Node(graph, 'merge')
77         tested_class.merge_infer(node)
78
79         (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
80         self.assertTrue(flag, resp)
81
82         # Imitate that inputs nodes now is inferred
83         graph.node['first']['is_partial_inferred'] = True
84
85         # Run infer second time
86         tested_class = Merge(graph=graph, attrs={})
87         node = Node(graph, 'merge')
88         tested_class.merge_infer(node)
89
90         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
91                                            edges_with_attrs=self.edges,
92                                            update_nodes_attributes=[('second', {'executable': True}),
93                                                                     ('first', {'is_partial_inferred': True,
94                                                                                'value': None}),
95                                                                     ('merge_output', {'shape': np.array([2, 2]),
96                                                                                       'value': None}),
97                                                                     ('merge', {'is_not_fully_inferred': False})])
98         (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
99         self.assertTrue(flag, resp)