目录

tensor.h

Overview

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

Summary

[h2]Structs

Name typedef Keyword Description
void * OH_AI_TensorHandle Handle to a tensor object.
void * OH_AI_AllocatorHandle Handle to a memory allocator object.

[h2]Functions

Name Description
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) Creates a tensor object.
OH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor) Destroys a tensor object.
OH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor) Deep copies a tensor.
OH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name) Sets the tensor name.
OH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor) Gets the tensor name.
OH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type) Sets the tensor data type.
OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor) Gets the tensor type.
OH_AI_API void OH_AI_TensorSetShape(OH_AI_TensorHandle tensor, const int64_t *shape, size_t shape_num) Sets the tensor shape.
OH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num) Gets the tensor shape.
OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format) Sets the tensor data layout.
OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor) Gets the tensor data layout.
OH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data) Sets the tensor data.
OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor) Gets the pointer to the tensor data.
OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor) Gets the mutable tensor data pointer. If the data is empty, memory is allocated.
OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor) Gets the number of tensor elements.
OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor) Gets the 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)

Sets the tensor to user-managed data. This API is often used to reuse user data as model input, which reduces one data copy.

Note: This data is external to the tensor and is not released when the tensor is destroyed; the caller is responsible for releasing it. In addition, the caller must ensure that this data remains valid while the tensor is in use.

OH_AI_API OH_AI_AllocatorHandle OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor) Gets the memory allocator. This API mainly provides a method for obtaining the memory allocator of the tensor.
OH_AI_API OH_AI_Status OH_AI_TensorSetAllocator(OH_AI_TensorHandle tensor, OH_AI_AllocatorHandle allocator) Sets the memory allocator. This API mainly provides a method for setting the memory allocator. The tensor memory is allocated by this allocator.

Function Description

[h2]OH_AI_TensorCreate()

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.

[h2]OH_AI_TensorDestroy()

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.

[h2]OH_AI_TensorClone()

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.

[h2]OH_AI_TensorSetName()

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.

[h2]OH_AI_TensorGetName()

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.

[h2]OH_AI_TensorSetDataType()

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.

[h2]OH_AI_TensorGetDataType()

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.

[h2]OH_AI_TensorSetShape()

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.

[h2]OH_AI_TensorGetShape()

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.

[h2]OH_AI_TensorSetFormat()

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.

[h2]OH_AI_TensorGetFormat()

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.

[h2]OH_AI_TensorSetData()

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.

[h2]OH_AI_TensorGetData()

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.

[h2]OH_AI_TensorGetMutableData()

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.

[h2]OH_AI_TensorGetElementNum()

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.

[h2]OH_AI_TensorGetDataSize()

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.

[h2]OH_AI_TensorSetUserData()

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.

[h2]OH_AI_TensorGetAllocator()

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.

[h2]OH_AI_TensorSetAllocator()

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.