2 Copyright (c) 2018 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 class TestMerge(unittest.TestCase):
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}),
35 ('first', 'merge', {'in': 0}),
36 ('second', 'merge', {'in': 1}),
37 ('merge', 'merge_output', {'out': 0}),
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)
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})])
50 tested_class = Merge(graph=graph, attrs={})
51 node = Node(graph, 'merge')
52 tested_class.merge_infer(node)
54 (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
55 self.assertTrue(flag, resp)
57 def test_merge_infer_complex_case(self):
59 Case as in cycles when in first visit only one input are inferred and in the second -- both.
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,
64 ('second', {'executable': True})])
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,
72 ('merge_output', {'shape': np.array([2, 2]),
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)
79 (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
80 self.assertTrue(flag, resp)
82 # Imitate that inputs nodes now is inferred
83 graph.node['first']['is_partial_inferred'] = True
85 # Run infer second time
86 tested_class = Merge(graph=graph, attrs={})
87 node = Node(graph, 'merge')
88 tested_class.merge_infer(node)
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,
95 ('merge_output', {'shape': np.array([2, 2]),
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)