Provides tensor-related APIs for creating and modifying tensor information. These APIs are not thread-safe.
File to include: <mindspore/tensor.h>
Library: libmindspore_lite_ndk.so
System capability: SystemCapability.Ai.MindSpore
Since: 9
Related module: MindSpore
| Name | typedef Keyword | Description |
|---|---|---|
| void * | OH_AI_TensorHandle | Handle to a tensor object. |
| void * | OH_AI_AllocatorHandle | Handle to a memory allocator object. |
OH_AI_API OH_AI_TensorHandle 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)
Description
Creates a tensor object. This API must be used together with OH_AI_TensorDestroy().
Since: 9
Parameters
| Name | Description |
|---|---|
| const char *name | Tensor name. The string length is subject to the system limit. |
| OH_AI_DataType type | Data type of the tensor. |
| const int64_t *shape | Dimension array of the tensor. |
| size_t shape_num | Length of the tensor dimension array. |
| const void *data | Pointer to the data. |
| size_t data_len | Data length. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_TensorHandle | Handle to the tensor object. |
OH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor)
Description
Destroys a tensor object.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle *tensor | Double pointer to the tensor handle. |
OH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor)
Description
Deep copies a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Tensor object handle. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_TensorHandle | Pointer to the new tensor object handle. |
OH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name)
Description
Sets the name of the tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| const char *name | Tensor name. The string length is subject to the system limit. |
OH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor)
Description
Gets the name of the tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Tensor object handle. |
Return
| Type | Description |
|---|---|
| OH_AI_API const char * | Name of the tensor. |
OH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type)
Description
Sets the data type of a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| OH_AI_DataType type | Data type. For details, see OH_AI_DataType. |
OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor)
Description
Obtains the tensor data type.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_DataType | Data type of the tensor. |
OH_AI_API void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num)
Description
Sets the shape of a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| const int64_t *shape | Shape array. |
| size_t shape_num | Length of the tensor shape array. |
OH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num)
Description
Obtains the shape of a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Tensor object handle. |
| size_t *shape_num | Output parameter. The length of the shape array is written to this variable. |
Return
| Type | Description |
|---|---|
| OH_AI_API const int64_t * | Shape array. |
OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format)
Description
Sets the layout of the tensor data.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| OH_AI_Format format | Layout of the tensor data. |
OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor)
Description
Obtains the data layout of a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_Format | Data layout of the tensor data. |
OH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data)
Description
Sets the tensor data.
Since: 9
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| void *data | Pointer to the data. |
OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor)
Description
Obtains the pointer to the tensor data.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Tensor object handle. |
Return
| Type | Description |
|---|---|
| OH_AI_API const void * | Pointer to the tensor data. |
OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor)
Description
Obtains the mutable tensor data pointer. If the data is empty, memory is allocated.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API void * | Pointer to the tensor data. |
OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor)
Description
Obtains the number of elements in a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API int64_t | Number of elements in the tensor. |
OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor)
Description
Gets the byte count of the data in a tensor.
Since: 9
Parameters
| Name | Description |
|---|---|
| const OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API size_t | Byte count of the tensor data. |
OH_AI_API OH_AI_Status OH_AI_TensorSetUserData(OH_AI_TensorHandle tensor, void *data, size_t data_size)
Description
Sets the tensor to user-managed data. This API is commonly used to reuse user data as model input, which reduces one data copy.
Note: This data is external to the tensor. It is not released when the tensor is destroyed; the caller is responsible for releasing it. In addition, the caller must ensure that the data remains valid while the tensor is in use.
Since: 10
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
| void *data | Pointer to the start address of the user data. |
| size_t data_size | Length of the user data. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_Status | Result code. Returns OH_AI_STATUS_SUCCESS if the operation is successful; returns a specific error code otherwise. |
OH_AI_API OH_AI_AllocatorHandle OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor)
Description
Obtains the allocator. This API provides a method for obtaining the allocator of a tensor.
Since: 12
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Handle to the tensor object. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_AllocatorHandle | Handle to the allocator. |
OH_AI_API OH_AI_Status OH_AI_TensorSetAllocator(OH_AI_TensorHandle tensor, OH_AI_AllocatorHandle allocator)
Description
Sets the memory allocator. This API provides a method for setting the memory allocator, and the tensor memory will be allocated by this allocator.
Since: 12
Parameters
| Name | Description |
|---|---|
| OH_AI_TensorHandle tensor | Tensor object handle. |
| OH_AI_AllocatorHandle allocator | Memory allocator object handle. |
Return
| Type | Description |
|---|---|
| OH_AI_API OH_AI_Status | Result code. Returns OH_AI_STATUS_SUCCESS if the operation is successful; otherwise, returns a specific error code. |