openharmony 鸿蒙 capi-tensor-h

2026-08-25 浏览 (1)

tensor.h

Overview

Provides tensor-related APIs, which can be used to create tensors and modify tensor information. These APIs are non-thread-safe.

File to include: <mindspore/tensor.h>

Library: libmindspore_lite_ndk.so

System capability: SystemCapability.Ai.MindSpore

Since: 9

Related module: MindSpore

Summary

Structs

Nametypedef KeywordDescription
void *OH_AI_TensorHandleDefines the handle of a tensor object.
void *OH_AI_AllocatorHandleDefines the handle of the memory allocator.

Functions

NameDescription
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)Clones 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)Obtains the tensor name.
OH_AI_API void OH_AI_TensorSetDataType(OH_AI_TensorHandle tensor, OH_AI_DataType type)Sets the data type of a tensor.
OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor)Obtains 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)Obtains the tensor shape.
OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format)Sets the tensor data format.
OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor)Obtains the tensor data format.
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)Obtains the pointer to tensor data.
OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor)Obtains the pointer to variable tensor data. If the data is empty, memory will be allocated.
OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor)Obtains the number of tensor elements.
OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor)Obtains the number of bytes 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 as the user data. This function allows you to reuse user data as the model input, which helps to reduce data copy by one time.
> NOTE
The user data is type of external data for the tensor and is not automatically released when the tensor is destroyed. The caller needs to release the data separately. In addition, the caller must ensure that the user data is valid during use of the tensor.
OH_AI_API OH_AI_AllocatorHandle OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor)Obtains a memory allocator. The allocator is responsible for allocating memory for tensors.
OH_AI_API OH_AI_Status OH_AI_TensorSetAllocator(OH_AI_TensorHandle tensor, OH_AI_AllocatorHandle allocator)Sets the memory allocator. The allocator is responsible for allocating memory for tensors.

Function Description

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.

Since: 9

Parameters

NameDescription
const char *nameTensor name. The string length is subject to system restrictions.
OH_AI_DataType typeTensor data type.
const int64_t *shapeTensor dimension array.
size_t shape_numLength of the tensor dimension array.
const void *dataData pointer.
size_t data_lenData length.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandleHandle of the tensor object.

OH_AI_TensorDestroy()

OH_AI_API void OH_AI_TensorDestroy(OH_AI_TensorHandle *tensor)

Description

Destroys a tensor object.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle *tensorLevel-2 pointer to the tensor handle.

OH_AI_TensorClone()

OH_AI_API OH_AI_TensorHandle OH_AI_TensorClone(OH_AI_TensorHandle tensor)

Description

Clones a tensor.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandleHandle of the new tensor object.

OH_AI_TensorSetName()

OH_AI_API void OH_AI_TensorSetName(OH_AI_TensorHandle tensor, const char *name)

Description

Sets the tensor name.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
const char *nameTensor name. The string length is subject to system restrictions.

OH_AI_TensorGetName()

OH_AI_API const char *OH_AI_TensorGetName(const OH_AI_TensorHandle tensor)

Description

Obtains the tensor name.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API const char *Tensor name.

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

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
OH_AI_DataType typeData type. For details, see OH_AI_DataType.

OH_AI_TensorGetDataType()

OH_AI_API OH_AI_DataType OH_AI_TensorGetDataType(const OH_AI_TensorHandle tensor)

Description

Obtains the tensor type.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API OH_AI_DataTypeTensor data type.

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 tensor shape.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
const int64_t *shapeShape array.
size_t shape_numLength of the tensor shape array.

OH_AI_TensorGetShape()

OH_AI_API const int64_t *OH_AI_TensorGetShape(const OH_AI_TensorHandle tensor, size_t *shape_num)

Description

Obtains the tensor shape.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.
size_t *shape_numLength of the tensor shape array.

Returns

TypeDescription
OH_AI_API const int64_t *Shape array.

OH_AI_TensorSetFormat()

OH_AI_API void OH_AI_TensorSetFormat(OH_AI_TensorHandle tensor, OH_AI_Format format)

Description

Sets the tensor data format.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
OH_AI_Format formatTensor data format.

OH_AI_TensorGetFormat()

OH_AI_API OH_AI_Format OH_AI_TensorGetFormat(const OH_AI_TensorHandle tensor)

Description

Obtains the tensor data format.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API OH_AI_FormatTensor data format.

OH_AI_TensorSetData()

OH_AI_API void OH_AI_TensorSetData(OH_AI_TensorHandle tensor, void *data)

Description

Sets the tensor data.

Since: 9

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
void *dataData pointer.

OH_AI_TensorGetData()

OH_AI_API const void *OH_AI_TensorGetData(const OH_AI_TensorHandle tensor)

Description

Obtains the pointer to tensor data.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API const void *Pointer to tensor data.

OH_AI_TensorGetMutableData()

OH_AI_API void *OH_AI_TensorGetMutableData(const OH_AI_TensorHandle tensor)

Description

Obtains the pointer to variable tensor data. If the data is empty, memory will be allocated.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API void *Pointer to tensor data.

OH_AI_TensorGetElementNum()

OH_AI_API int64_t OH_AI_TensorGetElementNum(const OH_AI_TensorHandle tensor)

Description

Obtains the number of tensor elements.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API int64_tNumber of tensor elements.

OH_AI_TensorGetDataSize()

OH_AI_API size_t OH_AI_TensorGetDataSize(const OH_AI_TensorHandle tensor)

Description

Obtains the number of bytes of the tensor data.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API size_tNumber of bytes of the tensor data.

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 as the user data. This function allows you to reuse user data as the model input, which helps to reduce data copy by one time.
> NOTE
The user data is type of external data for the tensor and is not automatically released when the tensor is destroyed. The caller needs to release the data separately. In addition, the caller must ensure that the user data is valid during use of the tensor.

Since: 10

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
void *dataStart address of user data.
size_t data_sizeLength of the user data length.

Returns

TypeDescription
OH_AI_API OH_AI_StatusExecution status code. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_TensorGetAllocator()

OH_AI_API OH_AI_AllocatorHandle OH_AI_TensorGetAllocator(OH_AI_TensorHandle tensor)

Description

Obtains a memory allocator. The allocator is responsible for allocating memory for tensors.

Since: 12

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.

Returns

TypeDescription
OH_AI_API OH_AI_AllocatorHandleHandle of the memory allocator.

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. The allocator is responsible for allocating memory for tensors.

Since: 12

Parameters

NameDescription
OH_AI_TensorHandle tensorHandle of the tensor object.
OH_AI_AllocatorHandle allocatorHandle of the memory allocator.

Returns

TypeDescription
OH_AI_API OH_AI_StatusExecution status code. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-format-h

openharmony 鸿蒙 capi-context-h

openharmony 鸿蒙 capi-mindspore-oh-ai-callbackparam

openharmony 鸿蒙 capi-mindspore-oh-ai-shapeinfo

openharmony 鸿蒙 capi-mindspore-oh-ai-contexthandle

openharmony 鸿蒙 capi-mindspore-oh-ai-traincfghandle

openharmony 鸿蒙 capi-mindspore-oh-ai-allocatorhandle

openharmony 鸿蒙 js-apis-mindSporeLite

openharmony 鸿蒙 capi-mindspore-oh-ai-tensorHandle

openharmony 鸿蒙 Readme-EN

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/6c54sttU