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 "mindspore/status.h" 21 #include "mindspore/types.h" 22 #include "mindspore/data_type.h" 23 #include "mindspore/format.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, 42 size_t shape_num, 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 Obtain the data pointer of the tensor. 117 /// 118 /// \param[in] tensor Tensor object handle. 119 /// 120 /// \return The data pointer of the tensor. 121 OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor); 122 123 /// \brief Obtain the mutable data pointer of the tensor. If the internal data is empty, it will allocate memory. 124 /// 125 /// \param[in] tensor Tensor object handle. 126 /// 127 /// \return The data pointer of the tensor. 128 OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor); 129 130 /// \brief Obtain the element number of the tensor. 131 /// 132 /// \param[in] tensor Tensor object handle. 133 /// 134 /// \return The element number of the tensor. 135 OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor); 136 137 /// \brief Obtain the data size fo the tensor. 138 /// 139 /// \param[in] tensor Tensor object handle. 140 /// 141 /// \return The data size of the tensor. 142 OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor); 143 144 /// \brief Set the data for the tensor with user-allocated data buffer. 145 /// The main purpose of this interface is providing a way of using memory already allocated by user as the Model's 146 /// input, but not which allocated inside the Model object. It can reduce one copy. 147 /// Note: The tensor won't free the data provided by invoker. Invoker has the responsibility to free it. And this 148 /// free action should not be preformed before destruction of the tensor. 149 /// 150 /// \param[in] tensor Tensor object handle. 151 /// \param[in] data A pointer to the user data buffer. 152 /// \param[in] data the byte size of the user data buffer. 153 /// 154 /// \return OH_AI_STATUS_SUCCESS if success, or detail error code if failed. 155 OH_AI_API OH_AI_Status OH_AI_TensorSetUserData(OH_AI_TensorHandle tensor, void *data, size_t data_size); 156 #ifdef __cplusplus 157 } 158 #endif 159 #endif // MINDSPORE_INCLUDE_C_API_TENSOE_C_H 160