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 17 /** 18 * @addtogroup MindSpore 19 * @{ 20 * 21 * @brief Provides APIs related to MindSpore Lite model inference. 22 * 23 * @Syscap SystemCapability.Ai.MindSpore 24 * @since 9 25 */ 26 27 /** 28 * @file tensor.h 29 * 30 * @brief Provides APIs for creating and modifying tensor information. 31 * 32 * @since 9 33 */ 34 35 #ifndef MINDSPORE_INCLUDE_C_API_TENSOE_C_H 36 #define MINDSPORE_INCLUDE_C_API_TENSOE_C_H 37 38 #include <stddef.h> 39 #include "mindspore/types.h" 40 #include "mindspore/data_type.h" 41 #include "mindspore/format.h" 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 /** 48 * @brief Defines the handle of a tensor object. 49 * 50 * @since 9 51 */ 52 typedef void *OH_AI_TensorHandle; 53 54 /** 55 * \brief Creates a tensor object. 56 * 57 * \param name Tensor name. 58 * \param type Tensor data type. 59 * \param shape Tensor dimension array. 60 * \param shape_num Length of the tensor dimension array. 61 * \param data Data pointer. 62 * \param data_len Data length. 63 * 64 * \return Handle of the tensor object. 65 * 66 * @since 9 67 */ 68 OH_AI_API OH_AI_TensorHandle OH_AI_TensorCreate(const char *name, OH_AI_DataType type, const int64_t *shape, 69 size_t shape_num, const void *data, size_t data_len); 70 71 /** 72 * \brief Destroys a tensor object. 73 * 74 * \param tensor Level-2 pointer to the tensor handle. 75 * 76 * @since 9 77 */ 78 OH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor); 79 80 /** 81 * \brief Clones a tensor. 82 * 83 * \param tensor Pointer to the tensor to clone. 84 * 85 * \return Handle of the new tensor object. 86 * 87 * @since 9 88 */ 89 OH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor); 90 91 /** 92 * \brief Sets the name of a tensor. 93 * 94 * \param tensor Handle of the tensor object. 95 * \param name Tensor name. 96 * 97 * @since 9 98 */ 99 OH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name); 100 101 /** 102 * \brief Obtains the name of a tensor. 103 * 104 * \param tensor Handle of the tensor object. 105 * 106 * \return Tensor name. 107 * 108 * @since 9 109 */ 110 OH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor); 111 112 /** 113 * \brief Sets the data type of a tensor. 114 * 115 * \param tensor Handle of the tensor object. 116 * \param type Data type. For details, see {@link OH_AI_DataType}. 117 * 118 * @since 9 119 */ 120 OH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type); 121 122 /** 123 * \brief Obtains the data type of a tensor. 124 * 125 * \param tensor Handle of the tensor object. 126 * 127 * \return Data type of the tensor. 128 * 129 * @since 9 130 */ 131 OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor); 132 133 /** 134 * \brief Sets the shape of a tensor. 135 * 136 * \param tensor Handle of the tensor object. 137 * \param shape Tensor shape array. 138 * \param shape_num Length of the tensor shape array. 139 * 140 * @since 9 141 */ 142 OH_AI_API void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num); 143 144 /** 145 * \brief Obtains the shape of a tensor. 146 * 147 * \param tensor Handle of the tensor object. 148 * \param shape_num Length of the tensor shape array. 149 * 150 * \return Tensor shape array. 151 * 152 * @since 9 153 */ 154 OH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num); 155 156 /** 157 * \brief Sets the tensor data format. 158 * 159 * \param tensor Handle of the tensor object. 160 * \param format Tensor data format. 161 * 162 * @since 9 163 */ 164 OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format); 165 166 /** 167 * \brief Obtains the tensor data format. 168 * 169 * \param tensor Handle of the tensor object. 170 * 171 * \return Tensor data format. 172 * 173 * @since 9 174 */ 175 OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor); 176 177 /** 178 * \brief Sets the tensor data. 179 * 180 * \param tensor Handle of the tensor object. 181 * \param data Data pointer. 182 * 183 * @since 9 184 */ 185 OH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data); 186 187 /** 188 * \brief Obtains the pointer to tensor data. 189 * 190 * \param tensor Handle of the tensor object. 191 * 192 * \return Pointer to tensor data. 193 * 194 * @since 9 195 */ 196 OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor); 197 198 /** 199 * \brief Obtains the pointer to variable tensor data. If the data is empty, memory will be allocated. 200 * 201 * \param tensor Handle of the tensor object. 202 * 203 * \return Pointer to variable tensor data. 204 * 205 * @since 9 206 */ 207 OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor); 208 209 /** 210 * \brief Obtains the number of tensor elements. 211 * 212 * \param tensor Handle of the tensor object. 213 * 214 * \return Number of tensor elements. 215 * 216 * @since 9 217 */ 218 OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor); 219 220 /** 221 * \brief Obtains the number of bytes of the tensor data. 222 * 223 * \param tensor Handle of the tensor object. 224 * 225 * \return Number of bytes of the tensor data. 226 * 227 * @since 9 228 */ 229 OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor); 230 231 #ifdef __cplusplus 232 } 233 #endif 234 235 /** @} */ 236 #endif // MINDSPORE_INCLUDE_C_API_TENSOE_C_H 237