17777dab0Sopenharmony_ci/** 27777dab0Sopenharmony_ci * Copyright 2021 Huawei Technologies Co., Ltd 37777dab0Sopenharmony_ci * 47777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 57777dab0Sopenharmony_ci * you may not use this file except in compliance with the License. 67777dab0Sopenharmony_ci * You may obtain a copy of the License at 77777dab0Sopenharmony_ci * 87777dab0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 97777dab0Sopenharmony_ci * 107777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 117777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 127777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137777dab0Sopenharmony_ci * See the License for the specific language governing permissions and 147777dab0Sopenharmony_ci * limitations under the License. 157777dab0Sopenharmony_ci */ 167777dab0Sopenharmony_ci 177777dab0Sopenharmony_ci/** 187777dab0Sopenharmony_ci * @addtogroup MindSpore 197777dab0Sopenharmony_ci * @{ 207777dab0Sopenharmony_ci * 217777dab0Sopenharmony_ci * @brief 提供MindSpore Lite的模型推理相关接口。 227777dab0Sopenharmony_ci * 237777dab0Sopenharmony_ci * @Syscap SystemCapability.Ai.MindSpore 247777dab0Sopenharmony_ci * @since 9 257777dab0Sopenharmony_ci */ 267777dab0Sopenharmony_ci 277777dab0Sopenharmony_ci/** 287777dab0Sopenharmony_ci * @file tensor.h 297777dab0Sopenharmony_ci * @kit MindSporeLiteKit 307777dab0Sopenharmony_ci * @brief 提供了张量相关的接口,可用于创建和修改张量信息。 317777dab0Sopenharmony_ci * 327777dab0Sopenharmony_ci * @library libmindspore_lite_ndk.so 337777dab0Sopenharmony_ci * @since 9 347777dab0Sopenharmony_ci */ 357777dab0Sopenharmony_ci#ifndef MINDSPORE_INCLUDE_C_API_TENSOE_C_H 367777dab0Sopenharmony_ci#define MINDSPORE_INCLUDE_C_API_TENSOE_C_H 377777dab0Sopenharmony_ci 387777dab0Sopenharmony_ci#include <stddef.h> 397777dab0Sopenharmony_ci#include "mindspore/status.h" 407777dab0Sopenharmony_ci#include "mindspore/types.h" 417777dab0Sopenharmony_ci#include "mindspore/data_type.h" 427777dab0Sopenharmony_ci#include "mindspore/format.h" 437777dab0Sopenharmony_ci 447777dab0Sopenharmony_ci#ifdef __cplusplus 457777dab0Sopenharmony_ciextern "C" { 467777dab0Sopenharmony_ci#endif 477777dab0Sopenharmony_ci 487777dab0Sopenharmony_citypedef void *OH_AI_TensorHandle; 497777dab0Sopenharmony_ci 507777dab0Sopenharmony_ci/** 517777dab0Sopenharmony_ci * @brief tensor allocator handle. 527777dab0Sopenharmony_ci * 537777dab0Sopenharmony_ci * @since 12 547777dab0Sopenharmony_ci */ 557777dab0Sopenharmony_citypedef void *OH_AI_AllocatorHandle; 567777dab0Sopenharmony_ci 577777dab0Sopenharmony_ci/** 587777dab0Sopenharmony_ci * @brief Create a tensor object. 597777dab0Sopenharmony_ci * @param name The name of the tensor. 607777dab0Sopenharmony_ci * @param type The data type of the tensor. 617777dab0Sopenharmony_ci * @param shape The shape of the tensor. 627777dab0Sopenharmony_ci * @param shape_num The num of the shape. 637777dab0Sopenharmony_ci * @param data The data pointer that points to allocated memory. 647777dab0Sopenharmony_ci * @param data_len The length of the memory, in bytes. 657777dab0Sopenharmony_ci * @return Tensor object handle. 667777dab0Sopenharmony_ci * @since 9 677777dab0Sopenharmony_ci */ 687777dab0Sopenharmony_ciOH_AI_API OH_AI_TensorHandle OH_AI_TensorCreate(const char *name, OH_AI_DataType type, const int64_t *shape, 697777dab0Sopenharmony_ci size_t shape_num, const void *data, size_t data_len); 707777dab0Sopenharmony_ci 717777dab0Sopenharmony_ci/** 727777dab0Sopenharmony_ci * @brief Destroy the tensor object. 737777dab0Sopenharmony_ci * @param tensor Tensor object handle address. 747777dab0Sopenharmony_ci * @since 9 757777dab0Sopenharmony_ci */ 767777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor); 777777dab0Sopenharmony_ci 787777dab0Sopenharmony_ci/** 797777dab0Sopenharmony_ci * @brief Obtain a deep copy of the tensor. 807777dab0Sopenharmony_ci * @param tensor Tensor object handle. 817777dab0Sopenharmony_ci * @return Tensor object handle. 827777dab0Sopenharmony_ci * @since 9 837777dab0Sopenharmony_ci */ 847777dab0Sopenharmony_ciOH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor); 857777dab0Sopenharmony_ci 867777dab0Sopenharmony_ci/** 877777dab0Sopenharmony_ci * @brief Set the name for the tensor. 887777dab0Sopenharmony_ci * @param tensor Tensor object handle. 897777dab0Sopenharmony_ci * @param name The name of the tensor. 907777dab0Sopenharmony_ci * @since 9 917777dab0Sopenharmony_ci */ 927777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name); 937777dab0Sopenharmony_ci 947777dab0Sopenharmony_ci/** 957777dab0Sopenharmony_ci * @brief Obtain the name of the tensor. 967777dab0Sopenharmony_ci * @param tensor Tensor object handle. 977777dab0Sopenharmony_ci * @return The name of the tensor. 987777dab0Sopenharmony_ci * @since 9 997777dab0Sopenharmony_ci */ 1007777dab0Sopenharmony_ciOH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor); 1017777dab0Sopenharmony_ci 1027777dab0Sopenharmony_ci/** 1037777dab0Sopenharmony_ci * @brief Set the data type for the tensor. 1047777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1057777dab0Sopenharmony_ci * @param type The data type of the tensor. 1067777dab0Sopenharmony_ci * @since 9 1077777dab0Sopenharmony_ci */ 1087777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type); 1097777dab0Sopenharmony_ci 1107777dab0Sopenharmony_ci/** 1117777dab0Sopenharmony_ci * @brief Obtain the data type of the tensor. 1127777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1137777dab0Sopenharmony_ci * @return The date type of the tensor. 1147777dab0Sopenharmony_ci * @since 9 1157777dab0Sopenharmony_ci */ 1167777dab0Sopenharmony_ciOH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor); 1177777dab0Sopenharmony_ci 1187777dab0Sopenharmony_ci/** 1197777dab0Sopenharmony_ci * @brief Set the shape for the tensor. 1207777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1217777dab0Sopenharmony_ci * @param shape The shape array. 1227777dab0Sopenharmony_ci * @param shape_num Dimension of shape. 1237777dab0Sopenharmony_ci * @since 9 1247777dab0Sopenharmony_ci */ 1257777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num); 1267777dab0Sopenharmony_ci 1277777dab0Sopenharmony_ci/** 1287777dab0Sopenharmony_ci * @brief Obtain the shape of the tensor. 1297777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1307777dab0Sopenharmony_ci * @param shape_num Dimension of shape. 1317777dab0Sopenharmony_ci * @return The shape array of the tensor. 1327777dab0Sopenharmony_ci * @since 9 1337777dab0Sopenharmony_ci */ 1347777dab0Sopenharmony_ciOH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num); 1357777dab0Sopenharmony_ci 1367777dab0Sopenharmony_ci/** 1377777dab0Sopenharmony_ci * @brief Set the format for the tensor. 1387777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1397777dab0Sopenharmony_ci * @param format The format of the tensor. 1407777dab0Sopenharmony_ci * @since 9 1417777dab0Sopenharmony_ci */ 1427777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format); 1437777dab0Sopenharmony_ci 1447777dab0Sopenharmony_ci/** 1457777dab0Sopenharmony_ci * @brief Obtain the format of the tensor. 1467777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1477777dab0Sopenharmony_ci * @return The format of the tensor. 1487777dab0Sopenharmony_ci * @since 9 1497777dab0Sopenharmony_ci */ 1507777dab0Sopenharmony_ciOH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor); 1517777dab0Sopenharmony_ci 1527777dab0Sopenharmony_ci/** 1537777dab0Sopenharmony_ci * @brief Obtain the data for the tensor. 1547777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1557777dab0Sopenharmony_ci * @param data A pointer to the data of the tensor. 1567777dab0Sopenharmony_ci * @since 9 1577777dab0Sopenharmony_ci */ 1587777dab0Sopenharmony_ciOH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data); 1597777dab0Sopenharmony_ci 1607777dab0Sopenharmony_ci/** 1617777dab0Sopenharmony_ci * @brief Obtain the data pointer of the tensor. 1627777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1637777dab0Sopenharmony_ci * @return The data pointer of the tensor. 1647777dab0Sopenharmony_ci * @since 9 1657777dab0Sopenharmony_ci */ 1667777dab0Sopenharmony_ciOH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor); 1677777dab0Sopenharmony_ci 1687777dab0Sopenharmony_ci/** 1697777dab0Sopenharmony_ci * @brief Obtain the mutable data pointer of the tensor. If the internal data is empty, it will allocate memory. 1707777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1717777dab0Sopenharmony_ci * @return The data pointer of the tensor. 1727777dab0Sopenharmony_ci * @since 9 1737777dab0Sopenharmony_ci */ 1747777dab0Sopenharmony_ciOH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor); 1757777dab0Sopenharmony_ci 1767777dab0Sopenharmony_ci/** 1777777dab0Sopenharmony_ci * @brief Obtain the element number of the tensor. 1787777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1797777dab0Sopenharmony_ci * @return The element number of the tensor. 1807777dab0Sopenharmony_ci * @since 9 1817777dab0Sopenharmony_ci */ 1827777dab0Sopenharmony_ciOH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor); 1837777dab0Sopenharmony_ci 1847777dab0Sopenharmony_ci/** 1857777dab0Sopenharmony_ci * @brief Obtain the data size fo the tensor. 1867777dab0Sopenharmony_ci * @param tensor Tensor object handle. 1877777dab0Sopenharmony_ci * @return The data size of the tensor. 1887777dab0Sopenharmony_ci * @since 9 1897777dab0Sopenharmony_ci */ 1907777dab0Sopenharmony_ciOH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor); 1917777dab0Sopenharmony_ci 1927777dab0Sopenharmony_ci/** 1937777dab0Sopenharmony_ci * @brief Set the data for the tensor with user-allocated data buffer. 1947777dab0Sopenharmony_ci * 1957777dab0Sopenharmony_ci * The main purpose of this interface is providing a way of using memory already allocated by user as the Model's 1967777dab0Sopenharmony_ci * input, but not which allocated inside the Model object. It can reduce one copy. \n 1977777dab0Sopenharmony_ci * Note: The tensor won't free the data provided by invoker. Invoker has the responsibility to free it. And this 1987777dab0Sopenharmony_ci * free action should not be preformed before destruction of the tensor. \n 1997777dab0Sopenharmony_ci * 2007777dab0Sopenharmony_ci * @param tensor Tensor object handle. 2017777dab0Sopenharmony_ci * @param data A pointer to the user data buffer. 2027777dab0Sopenharmony_ci * @param data the byte size of the user data buffer. 2037777dab0Sopenharmony_ci * @return OH_AI_STATUS_SUCCESS if success, or detail error code if failed. 2047777dab0Sopenharmony_ci * @since 10 2057777dab0Sopenharmony_ci */ 2067777dab0Sopenharmony_ciOH_AI_API OH_AI_Status OH_AI_TensorSetUserData(OH_AI_TensorHandle tensor, void *data, size_t data_size); 2077777dab0Sopenharmony_ci 2087777dab0Sopenharmony_ci/** 2097777dab0Sopenharmony_ci * @brief Get allocator for the tensor. 2107777dab0Sopenharmony_ci * 2117777dab0Sopenharmony_ci * The main purpose of this interface is providing a way of getting memory allocator of the tensor. 2127777dab0Sopenharmony_ci * 2137777dab0Sopenharmony_ci * @param tensor Tensor object handle. 2147777dab0Sopenharmony_ci * @return handle of the tensor's allocator. 2157777dab0Sopenharmony_ci * @since 12 2167777dab0Sopenharmony_ci */ 2177777dab0Sopenharmony_ciOH_AI_API OH_AI_AllocatorHandle OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor); 2187777dab0Sopenharmony_ci 2197777dab0Sopenharmony_ci/** 2207777dab0Sopenharmony_ci * @brief Set allocator to the tensor. 2217777dab0Sopenharmony_ci * 2227777dab0Sopenharmony_ci * The main purpose of this interface is providing a way of setting memory allocator, so tensor's memory will be 2237777dab0Sopenharmony_ci * allocated by this allocator. 2247777dab0Sopenharmony_ci * 2257777dab0Sopenharmony_ci * @param tensor Tensor object handle. 2267777dab0Sopenharmony_ci * @param allocator A allocator handle. 2277777dab0Sopenharmony_ci * @return OH_AI_STATUS_SUCCESS if success, or detail error code if failed. 2287777dab0Sopenharmony_ci * @since 12 2297777dab0Sopenharmony_ci */ 2307777dab0Sopenharmony_ciOH_AI_API OH_AI_Status OH_AI_TensorSetAllocator(OH_AI_TensorHandle tensor, OH_AI_AllocatorHandle allocator); 2317777dab0Sopenharmony_ci 2327777dab0Sopenharmony_ci#ifdef __cplusplus 2337777dab0Sopenharmony_ci} 2347777dab0Sopenharmony_ci#endif 2357777dab0Sopenharmony_ci#endif // MINDSPORE_INCLUDE_C_API_TENSOE_C_H 236