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 #ifndef MINDSPORE_INCLUDE_C_API_TENSOE_C_H 17 #define MINDSPORE_INCLUDE_C_API_TENSOE_C_H 18 19 #include <stddef.h> 20 #include "include/c_api/status_c.h" 21 #include "include/c_api/types_c.h" 22 #include "include/c_api/data_type_c.h" 23 #include "include/c_api/format_c.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 typedef void *OH_AI_TensorHandle; 30 31 /// \brief Create a tensor object. 32 /// 33 /// \param[in] name The name of the tensor. 34 /// \param[in] type The data type of the tensor. 35 /// \param[in] shape The shape of the tensor. 36 /// \param[in] shape_num The num of the shape. 37 /// \param[in] data The data pointer that points to allocated memory. 38 /// \param[in] data_len The length of the memory, in bytes. 39 /// 40 /// \return Tensor object handle. 41 OH_AI_API OH_AI_TensorHandle OH_AI_TensorCreate(const char *name, OH_AI_DataType type, const int64_t *shape, size_t shape_num, 42 const void *data, size_t data_len); 43 44 /// \brief Destroy the tensor object. 45 /// 46 /// \param[in] tensor Tensor object handle address. 47 OH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor); 48 49 /// \brief Obtain a deep copy of the tensor. 50 /// 51 /// \param[in] tensor Tensor object handle. 52 /// 53 /// \return Tensor object handle. 54 OH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor); 55 56 /// \brief Set the name for the tensor. 57 /// 58 /// \param[in] tensor Tensor object handle. 59 /// \param[in] name The name of the tensor. 60 OH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name); 61 62 /// \brief Obtain the name of the tensor. 63 /// 64 /// \param[in] tensor Tensor object handle. 65 /// 66 /// \return The name of the tensor. 67 OH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor); 68 69 /// \brief Set the data type for the tensor. 70 /// 71 /// \param[in] tensor Tensor object handle. 72 /// \param[in] type The data type of the tensor. 73 OH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type); 74 75 /// \brief Obtain the data type of the tensor. 76 /// 77 /// \param[in] tensor Tensor object handle. 78 /// 79 /// \return The date type of the tensor. 80 OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor); 81 82 /// \brief Set the shape for the tensor. 83 /// 84 /// \param[in] tensor Tensor object handle. 85 /// \param[in] shape The shape array. 86 /// \param[in] shape_num Dimension of shape. 87 OH_AI_API void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num); 88 89 /// \brief Obtain the shape of the tensor. 90 /// 91 /// \param[in] tensor Tensor object handle. 92 /// \param[out] shape_num Dimension of shape. 93 /// 94 /// \return The shape array of the tensor. 95 OH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num); 96 97 /// \brief Set the format for the tensor. 98 /// 99 /// \param[in] tensor Tensor object handle. 100 /// \param[in] format The format of the tensor. 101 OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format); 102 103 /// \brief Obtain the format of the tensor. 104 /// 105 /// \param[in] tensor Tensor object handle. 106 /// 107 /// \return The format of the tensor. 108 OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor); 109 110 /// \brief Obtain the data for the tensor. 111 /// 112 /// \param[in] tensor Tensor object handle. 113 /// \param[in] data A pointer to the data of the tensor. 114 OH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data); 115 116 /// \brief Set the data for the tensor with user-allocated data buffer. 117 /// The main purpose of this interface is providing a way of using memory already allocated by user as the Model's 118 /// input, but not which allocated inside the Model object. It can reduce one copy. 119 /// Note: The tensor won't free the data provided by invoker. Invoker has the responsibility to free it. And this 120 /// free action should not be preformed before destruction of the tensor. 121 /// 122 /// \param[in] tensor Tensor object handle. 123 /// \param[in] data A pointer to the user data buffer. 124 /// \param[in] data the byte size of the user data buffer. 125 /// 126 /// \return OH_AI_STATUS_SUCCESS if success, or detail error code if failed. 127 OH_AI_API OH_AI_Status OH_AI_TensorSetUserData(OH_AI_TensorHandle tensor, void *data, size_t data_size); 128 129 /// \brief Obtain the data pointer of the tensor. 130 /// 131 /// \param[in] tensor Tensor object handle. 132 /// 133 /// \return The data pointer of the tensor. 134 OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor); 135 136 /// \brief Obtain the mutable data pointer of the tensor. If the internal data is empty, it will allocate memory. 137 /// 138 /// \param[in] tensor Tensor object handle. 139 /// 140 /// \return The data pointer of the tensor. 141 OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor); 142 143 /// \brief Obtain the element number of the tensor. 144 /// 145 /// \param[in] tensor Tensor object handle. 146 /// 147 /// \return The element number of the tensor. 148 OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor); 149 150 /// \brief Obtain the data size fo the tensor. 151 /// 152 /// \param[in] tensor Tensor object handle. 153 /// 154 /// \return The data size of the tensor. 155 OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor); 156 157 /// \brief Obtain allocator for the tensor. 158 /// 159 /// \param[in] tensor Tensor object handle. 160 /// 161 /// \return The pointer of allocator. 162 OH_AI_API void *OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor); 163 164 /// \brief Set allocator for the tensor. 165 /// 166 /// \param[in] tensor Tensor object handle. 167 /// \param[in] allocator A pointer to the allocator. 168 /// 169 /// \return OH_AI_STATUS_SUCCESS if success, or detail error code if failed. 170 OH_AI_API OH_AI_Status OH_AI_TensorSetAllocator(OH_AI_TensorHandle tensor, void *allocator); 171 172 #ifdef __cplusplus 173 } 174 #endif 175 #endif // MINDSPORE_INCLUDE_C_API_TENSOE_C_H 176