1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
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 #include "backend/kernel_compiler/aicpu/aicpu_util.h"
17 #include "proto/types.pb.h"
18 #include "runtime/mem.h"
19 #include "utils/convert_utils.h"
20
21 namespace mindspore {
22 namespace kernel {
23 static const std::map<int32_t, int32_t> kMsProtoDataTypeMap = {
24 {mindspore::TypeId::kTypeUnknown, mindspore::DataType::MS_UNKNOWN},
25 {mindspore::TypeId::kNumberTypeBool, mindspore::DataType::MS_BOOL},
26 {mindspore::TypeId::kNumberTypeInt, mindspore::DataType::MS_INT32},
27 {mindspore::TypeId::kNumberTypeInt8, mindspore::DataType::MS_INT8},
28 {mindspore::TypeId::kNumberTypeInt16, mindspore::DataType::MS_INT16},
29 {mindspore::TypeId::kNumberTypeInt32, mindspore::DataType::MS_INT32},
30 {mindspore::TypeId::kNumberTypeInt64, mindspore::DataType::MS_INT64},
31 {mindspore::TypeId::kNumberTypeUInt, mindspore::DataType::MS_UINT32},
32 {mindspore::TypeId::kNumberTypeUInt8, mindspore::DataType::MS_UINT8},
33 {mindspore::TypeId::kNumberTypeUInt16, mindspore::DataType::MS_UINT16},
34 {mindspore::TypeId::kNumberTypeUInt32, mindspore::DataType::MS_UINT32},
35 {mindspore::TypeId::kNumberTypeUInt64, mindspore::DataType::MS_UINT64},
36 {mindspore::TypeId::kNumberTypeFloat16, mindspore::DataType::MS_FLOAT16},
37 {mindspore::TypeId::kNumberTypeFloat, mindspore::DataType::MS_FLOAT32},
38 {mindspore::TypeId::kNumberTypeFloat32, mindspore::DataType::MS_FLOAT32},
39 {mindspore::TypeId::kNumberTypeFloat64, mindspore::DataType::MS_FLOAT64},
40 {mindspore::TypeId::kNumberTypeComplex64, mindspore::DataType::MS_COMPLEX64},
41 {mindspore::TypeId::kNumberTypeComplex128, mindspore::DataType::MS_COMPLEX128},
42 };
43
44 static const std::map<int32_t, int32_t> kProtoDataTypeToMsDataTypeMap = {
45 {mindspore::DataType::MS_UNKNOWN, mindspore::TypeId::kTypeUnknown},
46 {mindspore::DataType::MS_BOOL, mindspore::TypeId::kNumberTypeBool},
47 {mindspore::DataType::MS_INT32, mindspore::TypeId::kNumberTypeInt32},
48 {mindspore::DataType::MS_INT8, mindspore::TypeId::kNumberTypeInt8},
49 {mindspore::DataType::MS_INT16, mindspore::TypeId::kNumberTypeInt16},
50 {mindspore::DataType::MS_INT64, mindspore::TypeId::kNumberTypeInt64},
51 {mindspore::DataType::MS_UINT8, mindspore::TypeId::kNumberTypeUInt8},
52 {mindspore::DataType::MS_UINT16, mindspore::TypeId::kNumberTypeUInt16},
53 {mindspore::DataType::MS_UINT32, mindspore::TypeId::kNumberTypeUInt32},
54 {mindspore::DataType::MS_UINT64, mindspore::TypeId::kNumberTypeUInt64},
55 {mindspore::DataType::MS_FLOAT16, mindspore::TypeId::kNumberTypeFloat16},
56 {mindspore::DataType::MS_FLOAT32, mindspore::TypeId::kNumberTypeFloat32},
57 {mindspore::DataType::MS_FLOAT64, mindspore::TypeId::kNumberTypeFloat64},
58 {mindspore::DataType::MS_COMPLEX64, mindspore::TypeId::kNumberTypeComplex64},
59 {mindspore::DataType::MS_COMPLEX128, mindspore::TypeId::kNumberTypeComplex128},
60 };
61
MsTypeToProtoType(TypeId ms_type)62 int AicpuOpUtil::MsTypeToProtoType(TypeId ms_type) {
63 auto iter = kMsProtoDataTypeMap.find(ms_type);
64 if (iter == kMsProtoDataTypeMap.end()) {
65 MS_LOG(ERROR) << "UnSupported ms_type value" << static_cast<int>(ms_type);
66 return -1;
67 }
68 return iter->second;
69 }
70
ProtoTypeToMsType(int proto_type)71 int AicpuOpUtil::ProtoTypeToMsType(int proto_type) {
72 auto iter = kProtoDataTypeToMsDataTypeMap.find(proto_type);
73 if (iter == kProtoDataTypeToMsDataTypeMap.end()) {
74 MS_LOG(ERROR) << "UnSupported proto_type value:" << proto_type;
75 return -1;
76 }
77 return iter->second;
78 }
79 } // namespace kernel
80 } // namespace mindspore
81