openharmony 鸿蒙 capi-model-h

2026-08-25 浏览 (1)

model.h

Overview

Provides model-related APIs for model creation and inference. These APIs are non-thread-safe.

File to include: <mindspore/model.h>

Library: libmindspore_lite_ndk.so

System capability: SystemCapability.Ai.MindSpore

Since: 9

Related module: MindSpore

Summary

Structs

Nametypedef KeywordDescription
OH_AI_TensorHandleArrayOH_AI_TensorHandleArrayDefines the tensor array structure, which is used to store the tensor array pointer and tensor array length.
OH_AI_ShapeInfoOH_AI_ShapeInfoMaximum number of shapes. The maximum value reserved is 32, and the maximum number currently supported is 8.
OH_AI_CallBackParamOH_AI_CallBackParamDefines the operator information passed in a callback.
void *OH_AI_ModelHandleDefines the pointer to a model object.
void *OH_AI_TrainCfgHandleDefines the pointer to a training configuration object.

Functions

Nametypedef KeywordDescription
typedef bool (*OH_AI_KernelCallBack)(const OH_AI_TensorHandleArray inputs, const OH_AI_TensorHandleArray outputs,const OH_AI_CallBackParam kernel_Info)OH_AI_KernelCallBackDefines the pointer to a callback.
This pointer is used to set the two callback functions in OH_AI_ModelPredict.
Each callback function must contain three parameters, where inputs and outputs indicate the input and output tensors of the operator, and kernel_Info indicates information about the current operator.
You can use the callback functions to monitor the operator execution status, for example, operator execution time and the operator correctness.
OH_AI_API OH_AI_ModelHandle OH_AI_ModelCreate(void)-Creates a model object.
OH_AI_API void OH_AI_ModelDestroy(OH_AI_ModelHandle *model)-Destroys a model object.
OH_AI_API OH_AI_Status OH_AI_ModelBuild(OH_AI_ModelHandle model, const void *model_data, size_t data_size,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context)-Loads and builds a MindSpore Lite model from the memory buffer.
Note that the same OH_AI_ContextHandle object can only be passed to OH_AI_ModelBuild or OH_AI_ModelBuildFromFile once. If you call this function multiple times, make sure that you create multiple OH_AI_ContextHandle objects accordingly.
OH_AI_API OH_AI_Status OH_AI_ModelBuildFromFile(OH_AI_ModelHandle model, const char *model_path,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context)-Loads and builds a MindSpore Lite model from a model file.
Note that the same OH_AI_ContextHandle object can only be passed to OH_AI_ModelBuild or OH_AI_ModelBuildFromFile once. If you call this function multiple times, make sure that you create multiple OH_AI_ContextHandle objects accordingly.
OH_AI_API OH_AI_Status OH_AI_ModelResize(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs,OH_AI_ShapeInfo *shape_infos, size_t shape_info_num)-Adjusts the input tensor shapes of a built model.
OH_AI_API OH_AI_Status OH_AI_ModelPredict(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs,OH_AI_TensorHandleArray *outputs, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)-Performs model inference.
OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetInputs(const OH_AI_ModelHandle model)-Obtains the input tensor array structure of a model.
OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetOutputs(const OH_AI_ModelHandle model)-Obtains the output tensor array structure of a model.
OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetInputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)-Obtains the input tensor of a model by tensor name.
OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetOutputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)-Obtains the output tensor of a model by tensor name.
OH_AI_API OH_AI_TrainCfgHandle OH_AI_TrainCfgCreate()-Creates the pointer to the training configuration object. This API is used only for on-device training.
OH_AI_API void OH_AI_TrainCfgDestroy(OH_AI_TrainCfgHandle *train_cfg)-Destroys the pointer to the training configuration object. This API is used only for on-device training.
OH_AI_API char **OH_AI_TrainCfgGetLossName(OH_AI_TrainCfgHandle train_cfg, size_t *num)-Obtains the list of loss functions, which are used only for on-device training.
OH_AI_API void OH_AI_TrainCfgSetLossName(OH_AI_TrainCfgHandle train_cfg, const char **loss_name, size_t num)-Sets the list of loss functions, which are used only for on-device training.
OH_AI_API OH_AI_OptimizationLevel OH_AI_TrainCfgGetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg)-Obtains the optimization level of the training configuration object. This API is used only for on-device training.
OH_AI_API void OH_AI_TrainCfgSetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg, OH_AI_OptimizationLevel level)-Sets the optimization level of the training configuration object. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_TrainModelBuild(OH_AI_ModelHandle model, const void *model_data, size_t data_size,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context,const OH_AI_TrainCfgHandle train_cfg)-Loads a training model from the memory buffer and compiles the model to a state ready for running on the device. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_TrainModelBuildFromFile(OH_AI_ModelHandle model, const char *model_path,OH_AI_ModelType model_type,const OH_AI_ContextHandle model_context,const OH_AI_TrainCfgHandle train_cfg)-Loads the training model from the specified path and compiles the model to a state ready for running on the device. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_RunStep(OH_AI_ModelHandle model, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)-Defines a single-step training model. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ModelSetLearningRate(OH_AI_ModelHandle model, float learning_rate)-Sets the learning rate for model training. This API is used only for on-device training.
OH_AI_API float OH_AI_ModelGetLearningRate(OH_AI_ModelHandle model)-Obtains the learning rate for model training. This API is used only for on-device training.
OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetWeights(OH_AI_ModelHandle model)-Obtains all weight tensors of a model. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ModelUpdateWeights(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray new_weights)-Updates the weight tensors of a model. This API is used only for on-device training.
OH_AI_API bool OH_AI_ModelGetTrainMode(OH_AI_ModelHandle model)-Obtains the training mode.
OH_AI_API OH_AI_Status OH_AI_ModelSetTrainMode(OH_AI_ModelHandle model, bool train)-Sets the training mode. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ModelSetupVirtualBatch(OH_AI_ModelHandle model, int virtual_batch_multiplier, float lr,float momentum)-Sets the virtual batch for training. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ExportModel(OH_AI_ModelHandle model, OH_AI_ModelType model_type, const char *model_file,OH_AI_QuantizationType quantization_type, bool export_inference_only,char **output_tensor_name, size_t num)-Exports a training model. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ExportModelBuffer(OH_AI_ModelHandle model, OH_AI_ModelType model_type, void *model_data,size_t *data_size, OH_AI_QuantizationType quantization_type,bool export_inference_only, char **output_tensor_name, size_t num)-Exports the memory cache of the training model. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ExportWeightsCollaborateWithMicro(OH_AI_ModelHandle model, OH_AI_ModelType model_type,const char *weight_file, bool is_inference,bool enable_fp16, char **changeable_weights_name,size_t num)-Exports the weight file of the training model for micro inference. This API is used only for on-device training.
OH_AI_API OH_AI_Status OH_AI_ModelLoadConfig(OH_AI_ModelHandle model, const char *config_path)-Loads the model configuration file.
OH_AI_API OH_AI_Status OH_AI_ModelPredictWithConfig(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs, OH_AI_TensorHandleArray *outputs, const char *config, const OH_AI_KernelCallBack before, const OH_AI_KernelCallBack after)-Performs model inference. Different inference parameters can be set for each inference.

Function Description

OH_AI_KernelCallBack()

typedef bool (*OH_AI_KernelCallBack)(const OH_AI_TensorHandleArray inputs, const OH_AI_TensorHandleArray outputs,const OH_AI_CallBackParam kernel_Info)

Description

Defines the pointer to a callback.
This pointer is used to set the two callback functions in OH_AI_ModelPredict.
Each callback function must contain three parameters, where inputs and outputs indicate the input and output tensors of the operator, and kernel_Info indicates information about the current operator.
You can use the callback functions to monitor the operator execution status, for example, operator execution time and the operator correctness.

Since: 9

Parameters

NameDescription
const OH_AI_TensorHandleArray inputsTensor array structure corresponding to the model input.
const OH_AI_TensorHandleArray outputsTensor array structure corresponding to the model output.
const OH_AI_CallBackParam kernel_InfoInformation about the current operator.

Returns

TypeDescription
booltrue if the operation is successful; false otherwise.

OH_AI_ModelCreate()

OH_AI_API OH_AI_ModelHandle OH_AI_ModelCreate(void)

Description

Creates a model object.

Since: 9

Returns

TypeDescription
OH_AI_API OH_AI_ModelHandlePointer to the model object.

OH_AI_ModelDestroy()

OH_AI_API void OH_AI_ModelDestroy(OH_AI_ModelHandle *model)

Description

Destroys a model object.

Since: 9

Parameters

NameDescription
OH_AI_ModelHandle *modelPointer to the model object.

OH_AI_ModelBuild()

OH_AI_API OH_AI_Status OH_AI_ModelBuild(OH_AI_ModelHandle model, const void *model_data, size_t data_size,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context)

Description

Loads and builds a MindSpore Lite model from the memory buffer.
Note that the same OH_AI_ContextHandle object can only be passed to OH_AI_ModelBuild or OH_AI_ModelBuildFromFile once. If you call this function multiple times, make sure that you create multiple OH_AI_ContextHandle objects accordingly.

Since: 9

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const void *model_dataAddress of the loaded model data in the memory.
size_t data_sizeLength of the model data.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_contextModel runtime context, which is specified by OH_AI_ContextHandle.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelBuildFromFile()

OH_AI_API OH_AI_Status OH_AI_ModelBuildFromFile(OH_AI_ModelHandle model, const char *model_path,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context)

Description

Loads and builds a MindSpore Lite model from a model file.
Note that the same OH_AI_ContextHandle object can only be passed to OH_AI_ModelBuild or OH_AI_ModelBuildFromFile once. If you call this function multiple times, make sure that you create multiple OH_AI_ContextHandle objects accordingly.

Since: 9

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const char *model_pathPath of the model file. The string length is subject to the file system.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_contextModel runtime context, which is specified by OH_AI_ContextHandle.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelResize()

OH_AI_API OH_AI_Status OH_AI_ModelResize(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs,OH_AI_ShapeInfo *shape_infos, size_t shape_info_num)

Description

Adjusts the input tensor shapes of a built model.

Since: 9

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const OH_AI_TensorHandleArray inputsTensor array structure corresponding to the model input.
OH_AI_ShapeInfo *shape_infosInput shape information array, which consists of tensor shapes arranged in the model input sequence. The model adjusts the tensor shapes in sequence.
size_t shape_info_numLength of the shape information array.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelPredict()

OH_AI_API OH_AI_Status OH_AI_ModelPredict(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs,OH_AI_TensorHandleArray *outputs, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)

Description

Performs model inference.

Since: 9

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const OH_AI_TensorHandleArray inputsTensor array structure corresponding to the model input.
OH_AI_TensorHandleArray *outputsPointer to the tensor array structure corresponding to the model output.
const OH_AI_KernelCallBack beforeCallback function executed before model inference.
const OH_AI_KernelCallBack afterCallback function executed after model inference.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelGetInputs()

OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetInputs(const OH_AI_ModelHandle model)

Description

Obtains the input tensor array structure of a model.

Since: 9

Parameters

NameDescription
const OH_AI_ModelHandle modelPointer to the model object.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandleArrayTensor array structure corresponding to the model input.

OH_AI_ModelGetOutputs()

OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetOutputs(const OH_AI_ModelHandle model)

Description

Obtains the output tensor array structure of a model.

Since: 9

Parameters

NameDescription
const OH_AI_ModelHandle modelPointer to the model object.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandleArrayTensor array structure corresponding to the model output.

OH_AI_ModelGetInputByTensorName()

OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetInputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)

Description

Obtains the input tensor of a model by tensor name.

Since: 9

Parameters

NameDescription
const OH_AI_ModelHandle modelPointer to the model object.
const char *tensor_nameTensor name. The string length is subject to system restrictions.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandlePointer to the input tensor indicated by tensor_name. If the tensor does not exist, null will be returned.

OH_AI_ModelGetOutputByTensorName()

OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetOutputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)

Description

Obtains the output tensor of a model by tensor name.

Since: 9

Parameters

NameDescription
const OH_AI_ModelHandle modelPointer to the model object.
const char *tensor_nameTensor name. The string length is subject to system restrictions.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandlePointer to the output tensor indicated by tensor_name. If the tensor does not exist, null will be returned.

OH_AI_TrainCfgCreate()

OH_AI_API OH_AI_TrainCfgHandle OH_AI_TrainCfgCreate()

Description

Creates the pointer to the training configuration object. This API is used only for on-device training.

Since: 11

Returns

TypeDescription
OH_AI_API OH_AI_TrainCfgHandlePointer to the training configuration object.

OH_AI_TrainCfgDestroy()

OH_AI_API void OH_AI_TrainCfgDestroy(OH_AI_TrainCfgHandle *train_cfg)

Description

Destroys the pointer to the training configuration object. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_TrainCfgHandle *train_cfgPointer to the training configuration object.

OH_AI_TrainCfgGetLossName()

OH_AI_API char **OH_AI_TrainCfgGetLossName(OH_AI_TrainCfgHandle train_cfg, size_t *num)

Description

Obtains the list of loss functions, which are used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.
size_t *numNumber of loss functions.

Returns

TypeDescription
OH_AI_API char **List of loss functions.

OH_AI_TrainCfgSetLossName()

OH_AI_API void OH_AI_TrainCfgSetLossName(OH_AI_TrainCfgHandle train_cfg, const char **loss_name, size_t num)

Description

Sets the list of loss functions, which are used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.
const char **loss_nameList of loss functions.
size_t numNumber of loss functions.

OH_AI_TrainCfgGetOptimizationLevel()

OH_AI_API OH_AI_OptimizationLevel OH_AI_TrainCfgGetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg)

Description

Obtains the optimization level of the training configuration object. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.

Returns

TypeDescription
OH_AI_API OH_AI_OptimizationLevelOptimization level.

OH_AI_TrainCfgSetOptimizationLevel()

OH_AI_API void OH_AI_TrainCfgSetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg, OH_AI_OptimizationLevel level)

Description

Sets the optimization level of the training configuration object. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.
OH_AI_OptimizationLevel levelOptimization level.

OH_AI_TrainModelBuild()

OH_AI_API OH_AI_Status OH_AI_TrainModelBuild(OH_AI_ModelHandle model, const void *model_data, size_t data_size,OH_AI_ModelType model_type, const OH_AI_ContextHandle model_context,const OH_AI_TrainCfgHandle train_cfg)

Description

Loads a training model from the memory buffer and compiles the model to a state ready for running on the device. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const void *model_dataPointer to the buffer that stores the model file to be read.
size_t data_sizeBuffer size.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_contextModel runtime context, which is specified by OH_AI_ContextHandle.
const OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_TrainModelBuildFromFile()

OH_AI_API OH_AI_Status OH_AI_TrainModelBuildFromFile(OH_AI_ModelHandle model, const char *model_path,OH_AI_ModelType model_type,const OH_AI_ContextHandle model_context,const OH_AI_TrainCfgHandle train_cfg)

Description

Loads the training model from the specified path and compiles the model to a state ready for running on the device. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const char *model_pathPath of the model file. The string length is subject to the file system.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_contextModel runtime context, which is specified by OH_AI_ContextHandle.
const OH_AI_TrainCfgHandle train_cfgPointer to the training configuration object.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_RunStep()

OH_AI_API OH_AI_Status OH_AI_RunStep(OH_AI_ModelHandle model, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)

Description

Defines a single-step training model. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const OH_AI_KernelCallBack beforeCallback function executed before model inference.
const OH_AI_KernelCallBack afterCallback function executed after model inference.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelSetLearningRate()

OH_AI_API OH_AI_Status OH_AI_ModelSetLearningRate(OH_AI_ModelHandle model, float learning_rate)

Description

Sets the learning rate for model training. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
float learning_rateLearning rate.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelGetLearningRate()

OH_AI_API float OH_AI_ModelGetLearningRate(OH_AI_ModelHandle model)

Description

Obtains the learning rate for model training. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.

Returns

TypeDescription
OH_AI_API floatLearning rate. If no optimizer is set, the value is 0.0.

OH_AI_ModelGetWeights()

OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetWeights(OH_AI_ModelHandle model)

Description

Obtains all weight tensors of a model. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.

Returns

TypeDescription
OH_AI_API OH_AI_TensorHandleArrayAll weight tensors of the model.

OH_AI_ModelUpdateWeights()

OH_AI_API OH_AI_Status OH_AI_ModelUpdateWeights(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray new_weights)

Description

Updates the weight tensors of a model. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const OH_AI_TensorHandleArray new_weightsWeight tensors to be updated.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelGetTrainMode()

OH_AI_API bool OH_AI_ModelGetTrainMode(OH_AI_ModelHandle model)

Description

Obtains the training mode.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.

Returns

TypeDescription
OH_AI_API boolWhether the training mode is used. The value true indicates that the training mode is used, and the value false indicates the opposite.

OH_AI_ModelSetTrainMode()

OH_AI_API OH_AI_Status OH_AI_ModelSetTrainMode(OH_AI_ModelHandle model, bool train)

Description

Sets the training mode. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
bool trainWhether the training mode is used. The value true indicates that the training mode is used, and the value false indicates the opposite.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelSetupVirtualBatch()

OH_AI_API OH_AI_Status OH_AI_ModelSetupVirtualBatch(OH_AI_ModelHandle model, int virtual_batch_multiplier, float lr,float momentum)

Description

Sets the virtual batch for training. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
int virtual_batch_multiplierVirtual batch multiplier. If the value is less than 1, the virtual batch is disabled. The length is subject to system restrictions.
float lrLearning rate. The default value is -1.0f.
float momentumMomentum. The default value is -1.0f.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ExportModel()

OH_AI_API OH_AI_Status OH_AI_ExportModel(OH_AI_ModelHandle model, OH_AI_ModelType model_type, const char *model_file,OH_AI_QuantizationType quantization_type, bool export_inference_only,char **output_tensor_name, size_t num)

Description

Exports a training model. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const char *model_filePath of the exported model file. The string length is subject to the file system.
OH_AI_QuantizationType quantization_typeQuantization type.
bool export_inference_onlyWhether to export an inference model. The value true means to export an inference model, and the value false means the opposite.
char **output_tensor_nameOutput tensor of the exported model. This parameter is left blank by default, which indicates full export.
size_t numNumber of output tensors.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ExportModelBuffer()

OH_AI_API OH_AI_Status OH_AI_ExportModelBuffer(OH_AI_ModelHandle model, OH_AI_ModelType model_type, void *model_data,size_t *data_size, OH_AI_QuantizationType quantization_type,bool export_inference_only, char **output_tensor_name, size_t num)

Description

Exports the memory cache of the training model. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
void *model_dataPointer to the buffer that stores the exported model file.
size_t *data_sizeBuffer size.
OH_AI_QuantizationType quantization_typeQuantization type.
bool export_inference_onlyWhether to export an inference model. The value true means to export an inference model, and the value false means the opposite.
char **output_tensor_nameOutput tensor of the exported model. This parameter is left blank by default, which indicates full export.
size_t numNumber of output tensors.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ExportWeightsCollaborateWithMicro()

OH_AI_API OH_AI_Status OH_AI_ExportWeightsCollaborateWithMicro(OH_AI_ModelHandle model, OH_AI_ModelType model_type,const char *weight_file, bool is_inference,bool enable_fp16, char **changeable_weights_name,size_t num)

Description

Exports the weight file of the training model for micro inference. This API is used only for on-device training.

Since: 11

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
OH_AI_ModelType model_typeModel file type, which is specified by OH_AI_ModelType.
const char *weight_filePath of the exported weight file. The string length is subject to the file system.
bool is_inferenceWhether to export inference models. Currently, this parameter can only be set to true.
bool enable_fp16Whether to save floating-point weights in float16 format. The value true means to save floating-point weights in float16 format, and the value false means the opposite.
char **changeable_weights_nameName of the weight tensor with a variable shape.
size_t numNumber of weight tensors with a variable shape.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelLoadConfig()

OH_AI_API OH_AI_Status OH_AI_ModelLoadConfig(OH_AI_ModelHandle model, const char *config_path);

Description

Loads the model configuration file.

Since: 20

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const char *config_pathConfiguration file path. The string length is subject to the file system.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. The value OH_AI_STATUS_SUCCESS indicates that the operation is successful. If the operation fails, an error code is returned.

OH_AI_ModelPredictWithConfig()

OH_AI_API OH_AI_Status OH_AI_ModelPredictWithConfig(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray inputs, OH_AI_TensorHandleArray *outputs, const char *config, const OH_AI_KernelCallBack before, const OH_AI_KernelCallBack after)

Description

Performs model inference. Different inference parameters can be set for each inference.

Since: 23

Parameters

NameDescription
OH_AI_ModelHandle modelPointer to the model object.
const OH_AI_TensorHandleArray inputsTensor array structure corresponding to the model input.
OH_AI_TensorHandleArray *outputsPointer to the tensor array structure corresponding to the model output.
const char *configModel configuration file. The string length is subject to the file system.
const OH_AI_KernelCallBack beforeCallback function executed before model inference.
const OH_AI_KernelCallBack afterCallback function executed after model inference.

Returns

TypeDescription
OH_AI_API OH_AI_StatusStatus code enumerated by OH_AI_Status. 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-tensor-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

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