1 /**
2 * Copyright 2022 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
17 #include "include/c_api/ms/value.h"
18 #include <memory>
19 #include "c_api/src/helper.h"
20 #include "c_api/src/common.h"
21 #include "ir/tensor.h"
22 #include "c_api/src/utils.h"
23
MSNewValueInt64(ResMgrHandle res_mgr,const int64_t v)24 ValueHandle MSNewValueInt64(ResMgrHandle res_mgr, const int64_t v) {
25 if (res_mgr == nullptr) {
26 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
27 return nullptr;
28 }
29 auto value = std::make_shared<Int64ImmImpl>(v);
30 return GetRawPtr(res_mgr, value);
31 }
32
MSNewValueFloat32(ResMgrHandle res_mgr,const float v)33 ValueHandle MSNewValueFloat32(ResMgrHandle res_mgr, const float v) {
34 if (res_mgr == nullptr) {
35 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
36 return nullptr;
37 }
38 auto value = std::make_shared<Float32ImmImpl>(v);
39 return GetRawPtr(res_mgr, value);
40 }
41
MSNewValueBool(ResMgrHandle res_mgr,const bool v)42 ValueHandle MSNewValueBool(ResMgrHandle res_mgr, const bool v) {
43 if (res_mgr == nullptr) {
44 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
45 return nullptr;
46 }
47 auto value = std::make_shared<BoolImmImpl>(v);
48 return GetRawPtr(res_mgr, value);
49 }
50
MSNewValueType(ResMgrHandle res_mgr,DataTypeC type)51 ValueHandle MSNewValueType(ResMgrHandle res_mgr, DataTypeC type) {
52 if (res_mgr == nullptr) {
53 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
54 return nullptr;
55 }
56 auto type_value = mindspore::TypeIdToType(mindspore::TypeId(type));
57 return GetRawPtr(res_mgr, type_value);
58 }
59
MSNewValueStrings(ResMgrHandle res_mgr,const char * strs[],size_t vec_len)60 ValueHandle MSNewValueStrings(ResMgrHandle res_mgr, const char *strs[], size_t vec_len) {
61 if (res_mgr == nullptr) {
62 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
63 return nullptr;
64 }
65 std::vector<std::string> converted_strs;
66 for (size_t i = 0; i < vec_len; i++) {
67 std::string converted_ele(strs[i]);
68 converted_strs.push_back(converted_ele);
69 }
70 auto value = mindspore::MakeValue(converted_strs);
71 return GetRawPtr(res_mgr, value);
72 }
73
MSNewValueArray(ResMgrHandle res_mgr,void * value,size_t vec_size,DataTypeC data_type)74 ValueHandle MSNewValueArray(ResMgrHandle res_mgr, void *value, size_t vec_size, DataTypeC data_type) {
75 if (res_mgr == nullptr) {
76 MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
77 return nullptr;
78 }
79
80 // Allow empty attribute value
81 if (value == nullptr && vec_size != 0) {
82 MS_LOG(ERROR) << "Input Handle [value] is nullptr.";
83 return nullptr;
84 }
85
86 mindspore::ValuePtr value_ptr;
87 switch (data_type) {
88 case MS_BOOL: {
89 std::vector<bool> vec_value(static_cast<bool *>(value), static_cast<bool *>(value) + vec_size);
90 value_ptr = mindspore::MakeValue(vec_value);
91 break;
92 }
93 case MS_INT32: {
94 std::vector<int32_t> vec_value(static_cast<int32_t *>(value), static_cast<int32_t *>(value) + vec_size);
95 value_ptr = mindspore::MakeValue(vec_value);
96 break;
97 }
98 case MS_INT64: {
99 std::vector<int64_t> vec_value(static_cast<int64_t *>(value), static_cast<int64_t *>(value) + vec_size);
100 value_ptr = mindspore::MakeValue(vec_value);
101 break;
102 }
103 case MS_FLOAT32: {
104 std::vector<float> vec_value(static_cast<float *>(value), static_cast<float *>(value) + vec_size);
105 value_ptr = mindspore::MakeValue(vec_value);
106 break;
107 }
108 default:
109 MS_LOG(ERROR) << "Unrecognized datatype w/ DataTypeC ID: " << data_type << std::endl;
110 return nullptr;
111 }
112 return GetRawPtr(res_mgr, value_ptr);
113 }
114