2 Copyright (c) 2018-2019 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.select import Select
21 from mo.graph.graph import Node
22 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
25 class TestSelect(unittest.TestCase):
27 ('than', {'value': np.ones((2, 2)), 'kind': 'data', 'executable': True, 'shape': np.array([2, 2])}),
28 ('else', {'value': np.zeros((2, 2)), 'kind': 'data', 'executable': True, 'shape': np.array([2, 2])}),
29 ('condition', {'value': None, 'kind': 'data', 'executable': True}),
30 ('select', {'type': 'Select', 'kind': 'op', 'op': 'Select'}),
31 ('select_output', {'value': None, 'kind': 'data', 'executable': True, 'shape': None}),
34 ('condition', 'select', {'in': 0}),
35 ('than', 'select', {'in': 1}),
36 ('else', 'select', {'in': 2}),
37 ('select', 'select_output', {'out': 0}),
40 def test_select_infer_no_condition(self):
41 graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges)
43 # We should propagate only shapes
44 graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
45 edges_with_attrs=self.edges,
46 update_nodes_attributes=[('select_output', {'shape': np.array([2, 2])})])
48 tested_class = Select(graph=graph, attrs={})
50 node = Node(graph, 'select')
51 tested_class.infer(node)
53 (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
54 self.assertTrue(flag, resp)
56 def test_select_infer_condition_true(self):
57 graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
58 update_nodes_attributes=[('condition', {'value': np.array([True])})])
60 # We should propagate shapes and values
61 graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
62 edges_with_attrs=self.edges,
63 update_nodes_attributes=[('select_output', {'shape': np.array([2, 2]),
64 'value': np.ones((2,2))})])
66 tested_class = Select(graph=graph, attrs={})
68 node = Node(graph, 'select')
69 tested_class.infer(node)
71 (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
72 self.assertTrue(flag, resp)
74 def test_select_infer_condition_false(self):
75 graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
76 update_nodes_attributes=[('condition', {'value': np.array([False])})])
78 # We should propagate shapes and values
79 graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
80 edges_with_attrs=self.edges,
81 update_nodes_attributes=[('select_output', {'shape': np.array([2, 2]),
82 'value': np.zeros((2, 2))})])
84 tested_class = Select(graph=graph, attrs={})
86 node = Node(graph, 'select')
87 tested_class.infer(node)
89 (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
90 self.assertTrue(flag, resp)
92 def test_select_infer_assert_shapes(self):
93 graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
94 update_nodes_attributes=[('else', {'shape': np.array([3,3]), 'value':np.zeros((3,3))})])
96 tested_class = Select(graph=graph, attrs={})
98 node = Node(graph, 'select')
99 with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
100 " \'then\' and \'else\' tensors.\'then\' and \'else\' tensors"
101 " must have the same shape by TensorFlow reference"):
102 tested_class.infer(node)
104 def test_select_infer_assert_condition_bool(self):
105 graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
106 update_nodes_attributes=[('condition', {'value': np.array([3])})])
108 tested_class = Select(graph=graph, attrs={})
110 node = Node(graph, 'select')
111 with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
112 " \'then\' and \'else\' tensors. Value of \'condition\' tensor"
113 " must be boolen by TensorFlow reference"):
114 tested_class.infer(node)