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

Name typedef Keyword Description
OH_AI_TensorHandleArray OH_AI_TensorHandleArray Defines the tensor array structure, which is used to store the tensor array pointer and tensor array length.
OH_AI_ShapeInfo OH_AI_ShapeInfo Maximum number of shapes. The maximum value reserved is 32, and the maximum number currently supported is 8.
OH_AI_CallBackParam OH_AI_CallBackParam Defines the operator information passed in a callback.
void * OH_AI_ModelHandle Defines the pointer to a model object.
void * OH_AI_TrainCfgHandle Defines the pointer to a training configuration object.

Functions

Name typedef Keyword Description
typedef bool (*OH_AI_KernelCallBack)(const OH_AI_TensorHandleArray inputs, const OH_AI_TensorHandleArray outputs,const OH_AI_CallBackParam kernel_Info) OH_AI_KernelCallBack 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.
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

Name Description
const OH_AI_TensorHandleArray inputs Tensor array structure corresponding to the model input.
const OH_AI_TensorHandleArray outputs Tensor array structure corresponding to the model output.
const OH_AI_CallBackParam kernel_Info Information about the current operator.

Returns

Type Description
bool true 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

Type Description
OH_AI_API OH_AI_ModelHandle Pointer 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

Name Description
OH_AI_ModelHandle *model Pointer 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const void *model_data Address of the loaded model data in the memory.
size_t data_size Length of the model data.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_context Model runtime context, which is specified by OH_AI_ContextHandle.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const char *model_path Path of the model file. The string length is subject to the file system.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_context Model runtime context, which is specified by OH_AI_ContextHandle.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const OH_AI_TensorHandleArray inputs Tensor array structure corresponding to the model input.
OH_AI_ShapeInfo *shape_infos Input 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_num Length of the shape information array.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const OH_AI_TensorHandleArray inputs Tensor array structure corresponding to the model input.
OH_AI_TensorHandleArray *outputs Pointer to the tensor array structure corresponding to the model output.
const OH_AI_KernelCallBack before Callback function executed before model inference.
const OH_AI_KernelCallBack after Callback function executed after model inference.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
const OH_AI_ModelHandle model Pointer to the model object.

Returns

Type Description
OH_AI_API OH_AI_TensorHandleArray Tensor 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

Name Description
const OH_AI_ModelHandle model Pointer to the model object.

Returns

Type Description
OH_AI_API OH_AI_TensorHandleArray Tensor 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

Name Description
const OH_AI_ModelHandle model Pointer to the model object.
const char *tensor_name Tensor name. The string length is subject to system restrictions.

Returns

Type Description
OH_AI_API OH_AI_TensorHandle Pointer 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

Name Description
const OH_AI_ModelHandle model Pointer to the model object.
const char *tensor_name Tensor name. The string length is subject to system restrictions.

Returns

Type Description
OH_AI_API OH_AI_TensorHandle Pointer 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

Type Description
OH_AI_API OH_AI_TrainCfgHandle Pointer 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

Name Description
OH_AI_TrainCfgHandle *train_cfg Pointer 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

Name Description
OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.
size_t *num Number of loss functions.

Returns

Type Description
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

Name Description
OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.
const char **loss_name List of loss functions.
size_t num Number 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

Name Description
OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.

Returns

Type Description
OH_AI_API OH_AI_OptimizationLevel Optimization 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

Name Description
OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.
OH_AI_OptimizationLevel level Optimization 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const void *model_data Pointer to the buffer that stores the model file to be read.
size_t data_size Buffer size.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_context Model runtime context, which is specified by OH_AI_ContextHandle.
const OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const char *model_path Path of the model file. The string length is subject to the file system.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const OH_AI_ContextHandle model_context Model runtime context, which is specified by OH_AI_ContextHandle.
const OH_AI_TrainCfgHandle train_cfg Pointer to the training configuration object.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const OH_AI_KernelCallBack before Callback function executed before model inference.
const OH_AI_KernelCallBack after Callback function executed after model inference.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
float learning_rate Learning rate.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.

Returns

Type Description
OH_AI_API float Learning 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.

Returns

Type Description
OH_AI_API OH_AI_TensorHandleArray All 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const OH_AI_TensorHandleArray new_weights Weight tensors to be updated.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.

Returns

Type Description
OH_AI_API bool Whether 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
bool train Whether the training mode is used. The value true indicates that the training mode is used, and the value false indicates the opposite.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
int virtual_batch_multiplier Virtual batch multiplier. If the value is less than 1, the virtual batch is disabled. The length is subject to system restrictions.
float lr Learning rate. The default value is -1.0f.
float momentum Momentum. The default value is -1.0f.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const char *model_file Path of the exported model file. The string length is subject to the file system.
OH_AI_QuantizationType quantization_type Quantization type.
bool export_inference_only Whether to export an inference model. The value true means to export an inference model, and the value false means the opposite.
char **output_tensor_name Output tensor of the exported model. This parameter is left blank by default, which indicates full export.
size_t num Number of output tensors.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
void *model_data Pointer to the buffer that stores the exported model file.
size_t *data_size Buffer size.
OH_AI_QuantizationType quantization_type Quantization type.
bool export_inference_only Whether to export an inference model. The value true means to export an inference model, and the value false means the opposite.
char **output_tensor_name Output tensor of the exported model. This parameter is left blank by default, which indicates full export.
size_t num Number of output tensors.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
OH_AI_ModelType model_type Model file type, which is specified by OH_AI_ModelType.
const char *weight_file Path of the exported weight file. The string length is subject to the file system.
bool is_inference Whether to export inference models. Currently, this parameter can only be set to true.
bool enable_fp16 Whether 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_name Name of the weight tensor with a variable shape.
size_t num Number of weight tensors with a variable shape.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const char *config_path Configuration file path. The string length is subject to the file system.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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

Name Description
OH_AI_ModelHandle model Pointer to the model object.
const OH_AI_TensorHandleArray inputs Tensor array structure corresponding to the model input.
OH_AI_TensorHandleArray *outputs Pointer to the tensor array structure corresponding to the model output.
const char *config Model configuration file. The string length is subject to the file system.
const OH_AI_KernelCallBack before Callback function executed before model inference.
const OH_AI_KernelCallBack after Callback function executed after model inference.

Returns

Type Description
OH_AI_API OH_AI_Status Status 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.