• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Dtypes and utilities"""
16from __future__ import absolute_import
17
18from mindspore.common.dtype import (int8, int16, int32, int64, uint8, uint16, uint32, uint64,
19                                    float16, float32, float64, bool_)
20
21# original numpy has int->int64, float->float64, uint->uint64 mapping. we map
22# them to 32 bit, since 64 bit calculation is not supported from mindspore
23# backend for now.
24
25inf = float('inf')
26PINF = float('inf')
27NINF = float('-inf')
28nan = float('nan')
29# all three of inf, PINF, and NINF are defined in the original numpy, and as we aim for
30# consistency same thing is done here
31pi = 3.141592653589793
32
33int_ = int32
34uint = uint32
35float_ = float32
36
37numeric_types = [
38    'int_',
39    'int8',
40    'int16',
41    'int32',
42    'int64',
43    'uint8',
44    'uint16',
45    'uint32',
46    'uint64',
47    'float_',
48    'float16',
49    'float32',
50    'float64',
51    'bool_']
52
53dtype_tuple = (
54    int_,
55    int8,
56    int16,
57    int32,
58    int64,
59    uint,
60    uint8,
61    uint16,
62    uint32,
63    uint64,
64    float_,
65    float16,
66    float32,
67    float64,
68    bool_)
69
70dtype_map = {
71    'int': int_,
72    'int8': int8,
73    'int16': int16,
74    'int32': int32,
75    'int64': int64,
76    'uint': uint,
77    'uint8': uint8,
78    'uint16': uint16,
79    'uint32': uint32,
80    'uint64': uint64,
81    'float': float_,
82    'float16': float16,
83    'float32': float32,
84    'float64': float64,
85    'bool': bool_
86}
87
88all_types = [
89    'np.int_',
90    'np.int8',
91    'np.int16',
92    'np.int32',
93    'np.int64',
94    'np.uint',
95    'np.uint8',
96    'np.uint16',
97    'np.uint32',
98    'np.uint64',
99    'np.float_',
100    'np.float16',
101    'np.float32',
102    'np.float64',
103    'np.bool_']
104
105promotion_rule = {
106    (uint8, uint16): uint16,
107    (uint8, uint32): uint32,
108    (uint8, uint64): uint64,
109    (uint16, uint32): uint32,
110    (uint16, uint64): uint64,
111    (uint32, uint64): uint64,
112    (uint8, int8): int16,
113    (uint8, int16): int16,
114    (uint8, int32): int32,
115    (uint8, int64): int64,
116    (uint16, int8): int32,
117    (uint16, int16): int32,
118    (uint16, int32): int32,
119    (uint16, int64): int64,
120    (uint32, int8): int64,
121    (uint32, int16): int64,
122    (uint32, int32): int64,
123    (uint32, int64): int64,
124    (uint64, int8): float64,
125    (uint64, int16): float64,
126    (uint64, int32): float64,
127    (uint64, int64): float64,
128    (uint8, float16): float16,
129    (uint8, float32): float32,
130    (uint8, float64): float64,
131    (uint16, float16): float16,
132    (uint16, float32): float32,
133    (uint16, float64): float32,
134    (uint32, float16): float16,
135    (uint32, float32): float32,
136    (uint32, float64): float64,
137    (uint64, float16): float16,
138    (uint64, float32): float32,
139    (uint64, float64): float64,
140    (int8, int16): int16,
141    (int8, int32): int32,
142    (int8, int64): int64,
143    (int16, int32): int32,
144    (int16, int64): int64,
145    (int32, int64): int64,
146    (int8, float16): float16,
147    (int8, float32): float32,
148    (int8, float64): float64,
149    (int16, float16): float16,
150    (int16, float32): float32,
151    (int16, float64): float64,
152    (int32, float16): float16,
153    (int32, float32): float32,
154    (int32, float64): float64,
155    (int64, float16): float16,
156    (int64, float32): float32,
157    (int64, float64): float64,
158    (float16, float32): float32,
159    (float16, float64): float64,
160    (float32, float64): float64,
161    (bool_, uint8): uint8,
162    (bool_, uint16): uint16,
163    (bool_, uint32): uint32,
164    (bool_, uint64): uint64,
165    (bool_, int8): int8,
166    (bool_, int16): int16,
167    (bool_, int32): int32,
168    (bool_, int64): int64,
169    (bool_, float16): float16,
170    (bool_, float32): float32,
171    (bool_, float64): float64,
172}
173
174rule_for_trigonometric = {float16: float16,
175                          float32: float32,
176                          float64: float64,
177                          int8: float16,
178                          int16: float32,
179                          int32: float32,
180                          int64: float32,
181                          uint8: float16,
182                          uint16: float32,
183                          uint32: float32,
184                          uint64: float32,
185                          bool_: float16}
186