Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / range.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 import logging as log
17 import numpy as np
18 from mo.front.tf.extractors.utils import tf_dtype_extractor
19
20
21 def tf_range_infer(node):
22     start = node.in_node(0)
23     limit = node.in_node(1)
24     delta = node.in_node(2)
25     output = node.out_node()
26
27     if not start.has_valid('value') or not limit.has_valid('value') or not delta.has_valid('value'):
28         log.error("Range operation is supported with constant inputs only")
29         return
30     if 'type' in node.pb.attr:
31         result_data_type = tf_dtype_extractor(node.pb.attr["type"].type)
32     else:
33         result_data_type = start.value.dtype
34     output.value = np.arange(start.value, limit.value, delta.value, dtype=result_data_type)
35     output.shape = np.array(output.value.shape, dtype=np.int64)
36
37     # Some notes on the automatic result data type infer. The tf.range does is differently than np.arange. Numpy
38     # by default creates array with elements of type int64 and float64, but TF does not widen data types and keep them
39     # int32 and float32.
40     # Compare:
41
42     # >>> tf.range(1, 5, 0.5)
43     # <tf.Tensor 'range_1:0' shape = (8,) dtype = float32>
44     # >>> tf.range(1, 5, 2)
45     # <tf.Tensor 'range_2:0' shape = (2,) dtype = int32>
46
47     # >>> np.array([0.5], dtype=np.float32)
48     # array([0.5], dtype=float32)
49     # >>> np.arange(np.array([1], dtype=np.int32), np.array([5], dtype=np.int32), np.array([2], dtype=np.int32)).dtype
50     # dtype('int64')
51     # >>> np.arange(np.array([1], dtype=np.int32), np.array([5], dtype=np.int32), np.array([0.5], dtype=np.float32)).dtype
52     # dtype('float64')