Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / range_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
19 import numpy as np
20
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
25
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'},
31                     }
32 edges = [('start', 'range'), ('limit', 'range'), ('delta', 'range'), ('range', 'output')]
33
34
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)))},
44                              })
45
46         range_node = Node(graph, 'range')
47
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']
51
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))
55
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())},
62                              })
63
64         range_node = Node(graph, 'range')
65
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']
69
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))
73
74     def test_non_constant_start_range_infer(self):
75         graph = build_graph(nodes_attributes, edges,
76                             {'start': {},
77                              'limit': {'value': np.array([5])},
78                              'delta': {'value': np.array([1])},
79                              'range': {'pb': FakeParam('attr', dict())},
80                              })
81
82         range_node = Node(graph, 'range')
83
84         tf_range_infer(range_node)
85         out_value = graph.node['output']['value']
86         self.assertIsNone(out_value)
87
88     def test_non_constant_limit_range_infer(self):
89         graph = build_graph(nodes_attributes, edges,
90                             {'start': {'value': np.array([1])},
91                              'limit': {},
92                              'delta': {'value': np.array([1])},
93                              'range': {'pb': FakeParam('attr', dict())},
94                              })
95
96         range_node = Node(graph, 'range')
97
98         tf_range_infer(range_node)
99         out_value = graph.node['output']['value']
100         self.assertIsNone(out_value)
101
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])},
106                              'delta': {},
107                              'range': {'pb': FakeParam('attr', dict())},
108                              })
109
110         range_node = Node(graph, 'range')
111
112         tf_range_infer(range_node)
113         out_value = graph.node['output']['value']
114         self.assertIsNone(out_value)