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.
21 from mo.front.common.partial_infer.range import tf_range_infer
22 from mo.graph.graph import Node
23 from mo.utils.unittest.extractors import FakeParam
24 from mo.utils.unittest.graph import build_graph
26 nodes_attributes = {'start': {'kind': 'data'},
27 'limit': {'kind': 'data'},
28 'delta': {'kind': 'data'},
29 'range': {'kind': 'op'},
30 'output': {'value': None, 'shape': None, 'kind': 'data'},
32 edges = [('start', 'range'), ('limit', 'range'), ('delta', 'range'), ('range', 'output')]
35 class TestRangePartialInfer(unittest.TestCase):
36 def test_int32_specific_data_type_range_infer(self):
37 # import tensorflow to use TF data types
38 import tensorflow as tf
39 graph = build_graph(nodes_attributes, edges,
40 {'start': {'value': np.array([1])},
41 'limit': {'value': np.array([5])},
42 'delta': {'value': np.array([1])},
43 'range': {'pb': FakeParam('attr', dict(type=FakeParam('type', tf.int32)))},
46 range_node = Node(graph, 'range')
48 tf_range_infer(range_node)
49 exp_value = np.array([1, 2, 3, 4], dtype=np.int32)
50 out_value = graph.node['output']['value']
52 self.assertTrue(exp_value.dtype == out_value.dtype)
53 self.assertTrue(np.array_equal(exp_value.shape, out_value.shape))
54 self.assertTrue(np.array_equal(exp_value, out_value))
56 def test_automatic_data_type_range_infer(self):
57 graph = build_graph(nodes_attributes, edges,
58 {'start': {'value': np.array([2], dtype=np.float32)},
59 'limit': {'value': np.array([5])},
60 'delta': {'value': np.array([1])},
61 'range': {'pb': FakeParam('attr', dict())},
64 range_node = Node(graph, 'range')
66 tf_range_infer(range_node)
67 exp_value = np.array([2.0, 3.0, 4.0], dtype=np.float32)
68 out_value = graph.node['output']['value']
70 self.assertTrue(exp_value.dtype == out_value.dtype)
71 self.assertTrue(np.array_equal(exp_value.shape, out_value.shape))
72 self.assertTrue(np.array_equal(exp_value, out_value))
74 def test_non_constant_start_range_infer(self):
75 graph = build_graph(nodes_attributes, edges,
77 'limit': {'value': np.array([5])},
78 'delta': {'value': np.array([1])},
79 'range': {'pb': FakeParam('attr', dict())},
82 range_node = Node(graph, 'range')
84 tf_range_infer(range_node)
85 out_value = graph.node['output']['value']
86 self.assertIsNone(out_value)
88 def test_non_constant_limit_range_infer(self):
89 graph = build_graph(nodes_attributes, edges,
90 {'start': {'value': np.array([1])},
92 'delta': {'value': np.array([1])},
93 'range': {'pb': FakeParam('attr', dict())},
96 range_node = Node(graph, 'range')
98 tf_range_infer(range_node)
99 out_value = graph.node['output']['value']
100 self.assertIsNone(out_value)
102 def test_non_constant_delta_range_infer(self):
103 graph = build_graph(nodes_attributes, edges,
104 {'start': {'value': np.array([1])},
105 'limit': {'value': np.array([10])},
107 'range': {'pb': FakeParam('attr', dict())},
110 range_node = Node(graph, 'range')
112 tf_range_infer(range_node)
113 out_value = graph.node['output']['value']
114 self.assertIsNone(out_value)