1 /**
2 * Copyright 2021 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 "include/c_api/tensor_c.h"
17 #include "include/api/status.h"
18 #include "include/ms_tensor.h"
19 #include "src/cxx_api/tensor/tensor_impl.h"
20 #include "src/runtime/inner_allocator.h"
21
OH_AI_TensorCreate(const char * name,OH_AI_DataType type,const int64_t * shape,size_t shape_num,const void * data,size_t data_len)22 OH_AI_TensorHandle OH_AI_TensorCreate(const char *name, OH_AI_DataType type, const int64_t *shape, size_t shape_num,
23 const void *data, size_t data_len) {
24 if (name == nullptr || shape == nullptr) {
25 MS_LOG(ERROR) << "param is nullptr.";
26 return nullptr;
27 }
28 std::vector<int32_t> vec_shape(shape_num);
29 for (size_t i = 0; i < shape_num; i++) {
30 vec_shape[i] = shape[i];
31 }
32 auto lite_tensor =
33 mindspore::lite::Tensor::CreateTensor(name, static_cast<mindspore::TypeId>(type), vec_shape, data, data_len);
34 auto impl = new (std::nothrow) mindspore::MSTensor::Impl(lite_tensor);
35 if (impl == nullptr || impl->lite_tensor() == nullptr) {
36 MS_LOG(ERROR) << "Failed to allocate tensor impl.";
37 return nullptr;
38 }
39 impl->set_from_session(false);
40 return impl;
41 }
42
OH_AI_TensorDestroy(OH_AI_TensorHandle * tensor)43 void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor) {
44 if (tensor != nullptr && *tensor != nullptr) {
45 auto impl = static_cast<mindspore::MSTensor::Impl *>(*tensor);
46 delete impl;
47 *tensor = nullptr;
48 }
49 }
50
OH_AI_TensorClone(OH_AI_TensorHandle tensor)51 OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor) {
52 if (tensor == nullptr) {
53 MS_LOG(ERROR) << "param is nullptr.";
54 return nullptr;
55 }
56 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
57 auto lite_tensor = static_cast<mindspore::lite::Tensor *>(impl->lite_tensor());
58 auto clone = mindspore::lite::Tensor::CopyTensor(*lite_tensor, true, lite_tensor->allocator());
59 if (clone == nullptr) {
60 MS_LOG(ERROR) << "Failed to allocate tensor.";
61 return nullptr;
62 }
63 auto clone_impl = new (std::nothrow) mindspore::MSTensor::Impl(clone);
64 if (clone_impl == nullptr) {
65 delete clone;
66 MS_LOG(ERROR) << "Failed to allocate tensor impl.";
67 return nullptr;
68 }
69 clone_impl->set_from_session(false);
70 return clone_impl;
71 }
72
OH_AI_TensorSetName(OH_AI_TensorHandle tensor,const char * name)73 void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name) {
74 if (tensor == nullptr || name == nullptr) {
75 MS_LOG(ERROR) << "param is nullptr.";
76 return;
77 }
78 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
79 impl->SetName(name);
80 }
81
OH_AI_TensorGetName(const OH_AI_TensorHandle tensor)82 const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor) {
83 if (tensor == nullptr) {
84 MS_LOG(ERROR) << "param is nullptr.";
85 return nullptr;
86 }
87 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
88 return impl->Name().c_str();
89 }
90
OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor,OH_AI_DataType type)91 void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type) {
92 if (tensor == nullptr) {
93 MS_LOG(ERROR) << "param is nullptr.";
94 return;
95 }
96 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
97 impl->SetDataType(static_cast<mindspore::DataType>(type));
98 }
99
OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor)100 OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor) {
101 if (tensor == nullptr) {
102 MS_LOG(ERROR) << "param is nullptr.";
103 return OH_AI_DATATYPE_UNKNOWN;
104 }
105 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
106 auto dtype = impl->DataType();
107 return static_cast<OH_AI_DataType>(dtype);
108 }
109
OH_AI_TensorSetShape(OH_AI_TensorHandle tensor,const int64_t * shape,size_t shape_num)110 void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num) {
111 if (tensor == nullptr || shape == nullptr) {
112 MS_LOG(ERROR) << "param is nullptr.";
113 return;
114 }
115 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
116 std::vector<int64_t> vec_shape(shape_num);
117 for (size_t i = 0; i < shape_num; i++) {
118 vec_shape[i] = shape[i];
119 }
120 impl->SetShape(vec_shape);
121 }
122
OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor,size_t * shape_num)123 const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num) {
124 if (tensor == nullptr) {
125 MS_LOG(ERROR) << "param is nullptr.";
126 return nullptr;
127 }
128 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
129 *shape_num = impl->Shape().size();
130 return impl->Shape().data();
131 }
132
OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor,OH_AI_Format format)133 void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format) {
134 if (tensor == nullptr) {
135 MS_LOG(ERROR) << "param is nullptr.";
136 return;
137 }
138 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
139 return impl->SetFormat(static_cast<mindspore::Format>(format));
140 }
141
OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor)142 OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor) {
143 if (tensor == nullptr) {
144 MS_LOG(ERROR) << "param is nullptr.";
145 return OH_AI_FORMAT_NHWC;
146 }
147 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
148 return static_cast<OH_AI_Format>(impl->format());
149 }
150
OH_AI_TensorSetData(OH_AI_TensorHandle tensor,void * data)151 void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data) {
152 if (tensor == nullptr) {
153 MS_LOG(ERROR) << "param is nullptr.";
154 return;
155 }
156 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
157 return impl->SetData(data);
158 }
159
OH_AI_TensorGetData(const OH_AI_TensorHandle tensor)160 const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor) {
161 if (tensor == nullptr) {
162 MS_LOG(ERROR) << "param is nullptr.";
163 return nullptr;
164 }
165 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
166 return impl->Data().get();
167 }
168
OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor)169 void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor) {
170 if (tensor == nullptr) {
171 MS_LOG(ERROR) << "param is nullptr.";
172 return nullptr;
173 }
174 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
175 return impl->MutableData();
176 }
177
OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor)178 int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor) {
179 if (tensor == nullptr) {
180 MS_LOG(ERROR) << "param is nullptr.";
181 return 0;
182 }
183 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
184 return impl->ElementNum();
185 }
186
OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor)187 size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor) {
188 if (tensor == nullptr) {
189 MS_LOG(ERROR) << "param is nullptr.";
190 return 0;
191 }
192 auto impl = static_cast<mindspore::MSTensor::Impl *>(tensor);
193 return impl->DataSize();
194 }
195