• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# model.h
2
3<!--Kit: MindSpore Lite Kit-->
4<!--Subsystem: AI-->
5<!--Owner: @zhuguodong8-->
6<!--Designer: @zhuguodong8; @jjfeing-->
7<!--Tester: @principal87-->
8<!--Adviser: @ge-yafang-->
9
10## Overview
11
12Provides model-related APIs for model creation and inference. These APIs are non-thread-safe.
13
14**File to include**: <mindspore/model.h>
15
16**Library**: libmindspore_lite_ndk.so
17
18**System capability**: SystemCapability.Ai.MindSpore
19
20**Since**: 9
21
22**Related module**: [MindSpore](capi-mindspore.md)
23
24## Summary
25
26### Structs
27
28| Name                                                                  | Description                             |
29|----------------------------------------------------------------------|---------------------------------|
30| [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) | Defines the tensor array structure, which is used to store the tensor array pointer and tensor array length.     |
31| [OH_AI_ShapeInfo](capi-mindspore-oh-ai-shapeinfo.md)                 | Maximum number of shapes. The maximum value reserved is **32**, and the maximum number currently supported is **8**.|
32| [OH_AI_CallBackParam](capi-mindspore-oh-ai-callbackparam.md)         | Defines the operator information passed in a callback.                  |
33| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md)                                                | Defines the pointer to a model object.                               |
34| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md)                                             | Defines the pointer to a training configuration object.                               |
35
36### Functions
37
38| Name| typedef Keyword| Description                                                                                                                                                                                                                                                 |
39| -- | -- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
40| [typedef bool (\*OH_AI_KernelCallBack)(const OH_AI_TensorHandleArray inputs, const OH_AI_TensorHandleArray outputs,const OH_AI_CallBackParam kernel_Info)](#oh_ai_kernelcallback) | OH_AI_KernelCallBack | Defines the pointer to a callback.<br> This pointer is used to set the two callback functions in [OH_AI_ModelPredict](capi-model-h.md#oh_ai_modelpredict).<br> 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.<br> You can use the callback functions to monitor the operator execution status, for example, operator execution time and the operator correctness.                                 |
41| [OH_AI_API OH_AI_ModelHandle OH_AI_ModelCreate(void)](#oh_ai_modelcreate) | - | Creates a model object.                                                                                                                                                                                                                                          |
42| [OH_AI_API void OH_AI_ModelDestroy(OH_AI_ModelHandle *model)](#oh_ai_modeldestroy) | - | Destroys a model object.                                                                                                                                                                                                                                          |
43| [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)](#oh_ai_modelbuild) | - | Loads and builds a MindSpore model from the memory buffer.<br> Note that the same [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) object can only be passed to [OH_AI_ModelBuild](capi-model-h.md#oh_ai_modelbuild) or [OH_AI_ModelBuildFromFile](capi-model-h.md#oh_ai_modelbuildfromfile) once. If you call this function multiple times, make sure that you create multiple [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) objects accordingly.|
44| [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)](#oh_ai_modelbuildfromfile) | - | Loads and builds a MindSpore model from a model file.<br> Note that the same [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) object can only be passed to [OH_AI_ModelBuild](capi-model-h.md#oh_ai_modelbuild) or [OH_AI_ModelBuildFromFile](capi-model-h.md#oh_ai_modelbuildfromfile) once. If you call this function multiple times, make sure that you create multiple [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) objects accordingly.    |
45| [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)](#oh_ai_modelresize) | - | Adjusts the input tensor shapes of a built model.                                                                                                                                                                                                                                      |
46| [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)](#oh_ai_modelpredict) | - | Performs model inference.                                                                                                                                                                                                                                            |
47| [OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetInputs(const OH_AI_ModelHandle model)](#oh_ai_modelgetinputs) | - | Obtains the input tensor array structure of a model.                                                                                                                                                                                                                                    |
48| [OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetOutputs(const OH_AI_ModelHandle model)](#oh_ai_modelgetoutputs) | - | Obtains the output tensor array structure of a model.                                                                                                                                                                                                                                    |
49| [OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetInputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)](#oh_ai_modelgetinputbytensorname) | - | Obtains the input tensor of a model by tensor name.                                                                                                                                                                                                                                    |
50| [OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetOutputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)](#oh_ai_modelgetoutputbytensorname) | - | Obtains the output tensor of a model by tensor name.                                                                                                                                                                                                                                    |
51| [OH_AI_API OH_AI_TrainCfgHandle OH_AI_TrainCfgCreate()](#oh_ai_traincfgcreate) | - | Creates the pointer to the training configuration object. This API is used only for on-device training.                                                                                                                                                                                                                                |
52| [OH_AI_API void OH_AI_TrainCfgDestroy(OH_AI_TrainCfgHandle *train_cfg)](#oh_ai_traincfgdestroy) | - | Destroys the pointer to the training configuration object. This API is used only for on-device training.                                                                                                                                                                                                                                |
53| [OH_AI_API char **OH_AI_TrainCfgGetLossName(OH_AI_TrainCfgHandle train_cfg, size_t *num)](#oh_ai_traincfggetlossname) | - | Obtains the list of loss functions, which are used only for on-device training.                                                                                                                                                                                                                               |
54| [OH_AI_API void OH_AI_TrainCfgSetLossName(OH_AI_TrainCfgHandle train_cfg, const char **loss_name, size_t num)](#oh_ai_traincfgsetlossname) | - | Sets the list of loss functions, which are used only for on-device training.                                                                                                                                                                                                                               |
55| [OH_AI_API OH_AI_OptimizationLevel OH_AI_TrainCfgGetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg)](#oh_ai_traincfggetoptimizationlevel) | - | Obtains the optimization level of the training configuration object. This API is used only for on-device training.                                                                                                                                                                                                                               |
56| [OH_AI_API void OH_AI_TrainCfgSetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg, OH_AI_OptimizationLevel level)](#oh_ai_traincfgsetoptimizationlevel) | - | Sets the optimization level of the training configuration object. This API is used only for on-device training.                                                                                                                                                                                                                               |
57| [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)](#oh_ai_trainmodelbuild) | - | 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.                                                                                                                                                                                                        |
58| [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)](#oh_ai_trainmodelbuildfromfile) | - | 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.                                                                                                                                                                                                        |
59| [OH_AI_API OH_AI_Status OH_AI_RunStep(OH_AI_ModelHandle model, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)](#oh_ai_runstep) | - | Defines a single-step training model. This API is used only for on-device training.                                                                                                                                                                                                                                    |
60| [OH_AI_API OH_AI_Status OH_AI_ModelSetLearningRate(OH_AI_ModelHandle model, float learning_rate)](#oh_ai_modelsetlearningrate) | - | Sets the learning rate for model training. This API is used only for on-device training.                                                                                                                                                                                                                                  |
61| [OH_AI_API float OH_AI_ModelGetLearningRate(OH_AI_ModelHandle model)](#oh_ai_modelgetlearningrate) | - | Obtains the learning rate for model training. This API is used only for on-device training.                                                                                                                                                                                                                                  |
62| [OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetWeights(OH_AI_ModelHandle model)](#oh_ai_modelgetweights) | - | Obtains all weight tensors of a model. This API is used only for on-device training.                                                                                                                                                                                                                          |
63| [OH_AI_API OH_AI_Status OH_AI_ModelUpdateWeights(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray new_weights)](#oh_ai_modelupdateweights) | - | Updates the weight tensors of a model. This API is used only for on-device training.                                                                                                                                                                                                                           |
64| [OH_AI_API bool OH_AI_ModelGetTrainMode(OH_AI_ModelHandle model)](#oh_ai_modelgettrainmode) | - | Obtains the training mode.                                                                                                                                                                                                                                            |
65| [OH_AI_API OH_AI_Status OH_AI_ModelSetTrainMode(OH_AI_ModelHandle model, bool train)](#oh_ai_modelsettrainmode) | - | Sets the training mode. This API is used only for on-device training.                                                                                                                                                                                                                                    |
66| [OH_AI_API OH_AI_Status OH_AI_ModelSetupVirtualBatch(OH_AI_ModelHandle model, int virtual_batch_multiplier, float lr,float momentum)](#oh_ai_modelsetupvirtualbatch) | - | Sets the virtual batch for training. This API is used only for on-device training.                                                                                                                                                                                                                             |
67| [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)](#oh_ai_exportmodel) | - | Exports a training model. This API is used only for on-device training.                                                                                                                                                                                                                                    |
68| [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)](#oh_ai_exportmodelbuffer) | - | Exports the memory cache of the training model. This API is used only for on-device training.                                                                                                                                                                                                                                |
69| [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)](#oh_ai_exportweightscollaboratewithmicro) | - | Exports the weight file of the training model for micro inference. This API is used only for on-device training.                                                                                                                                                                                                                        |
70| [OH_AI_API OH_AI_Status OH_AI_ModelLoadConfig(OH_AI_ModelHandle model, const char *config_path)](#oh_ai_modelloadconfig) | - | Loads the model configuration file.|
71
72## Function Description
73
74### OH_AI_KernelCallBack()
75
76```
77typedef bool (*OH_AI_KernelCallBack)(const OH_AI_TensorHandleArray inputs, const OH_AI_TensorHandleArray outputs,const OH_AI_CallBackParam kernel_Info)
78```
79
80**Description**
81
82Defines the pointer to a callback.<br>This pointer is used to set the two callback functions in [OH_AI_ModelPredict](capi-model-h.md#oh_ai_modelpredict).<br>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.<br>You can use the callback functions to monitor the operator execution status, for example, operator execution time and the operator correctness.
83
84**Since**: 9
85
86
87**Parameters**
88
89| Name                                                                               | Description     |
90|------------------------------------------------------------------------------------|---------|
91| const [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) inputs  | Tensor array structure corresponding to the model input.|
92| const [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) outputs | Tensor array structure corresponding to the model output.       |
93| const [OH_AI_CallBackParam](capi-mindspore-oh-ai-callbackparam.md) kernel_Info   | Information about the current operator.       |
94
95**Returns**
96
97| Type  | Description|
98|------|----|
99| bool | **true** if the operation is successful; **false** otherwise.|
100
101### OH_AI_ModelCreate()
102
103```
104OH_AI_API OH_AI_ModelHandle OH_AI_ModelCreate(void)
105```
106
107**Description**
108
109Creates a model object.
110
111**Since**: 9
112
113**Returns**
114
115| Type                             | Description|
116|---------------------------------| -- |
117| OH_AI_API [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) | Pointer to the model object.|
118
119### OH_AI_ModelDestroy()
120
121```
122OH_AI_API void OH_AI_ModelDestroy(OH_AI_ModelHandle *model)
123```
124
125**Description**
126
127Destroys a model object.
128
129**Since**: 9
130
131
132**Parameters**
133
134| Name| Description|
135| -- | -- |
136| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) *model | Pointer to the model object.|
137
138### OH_AI_ModelBuild()
139
140```
141OH_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)
142```
143
144**Description**
145
146Loads and builds a MindSpore model from the memory buffer.<br>Note that the same [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) object can only be passed to [OH_AI_ModelBuild](capi-model-h.md#oh_ai_modelbuild) or [OH_AI_ModelBuildFromFile](capi-model-h.md#oh_ai_modelbuildfromfile) once. If you call this function multiple times, make sure that you create multiple [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) objects accordingly.
147
148**Since**: 9
149
150
151**Parameters**
152
153| Name| Description                                                                           |
154| -- |-------------------------------------------------------------------------------|
155| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.                                                                      |
156| const void *model_data | Address of the loaded model data in the memory.                                                              |
157| size_t data_size | Length of the model data.                                                                     |
158| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).                |
159| const [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) model_context | Model runtime context, which is specified by [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md).|
160
161**Returns**
162
163| Type| Description                                                                                                |
164| -- |----------------------------------------------------------------------------------------------------|
165| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
166
167### OH_AI_ModelBuildFromFile()
168
169```
170OH_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)
171```
172
173**Description**
174
175Loads and builds a MindSpore model from a model file.<br>Note that the same [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) object can only be passed to [OH_AI_ModelBuild](capi-model-h.md#oh_ai_modelbuild) or [OH_AI_ModelBuildFromFile](capi-model-h.md#oh_ai_modelbuildfromfile) once. If you call this function multiple times, make sure that you create multiple [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) objects accordingly.
176
177**Since**: 9
178
179
180**Parameters**
181
182| Name| Description|
183| -- | -- |
184| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
185| const char *model_path | Path of the model file.|
186| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
187| const [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) model_context | Model runtime context, which is specified by [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md).|
188
189**Returns**
190
191| Type| Description|
192| -- | -- |
193| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
194
195### OH_AI_ModelResize()
196
197```
198OH_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)
199```
200
201**Description**
202
203Adjusts the input tensor shapes of a built model.
204
205**Since**: 9
206
207
208**Parameters**
209
210| Name| Description|
211| -- | -- |
212| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
213| [const OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) inputs | Tensor array structure corresponding to the model input.|
214| [OH_AI_ShapeInfo](capi-mindspore-oh-ai-shapeinfo.md) *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.|
215| size_t shape_info_num | Length of the shape information array.|
216
217**Returns**
218
219| Type| Description|
220| -- | -- |
221| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
222
223### OH_AI_ModelPredict()
224
225```
226OH_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)
227```
228
229**Description**
230
231Performs model inference.
232
233**Since**: 9
234
235
236**Parameters**
237
238| Name| Description|
239| -- | -- |
240| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
241| [const OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) inputs | Tensor array structure corresponding to the model input.|
242| [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) *outputs | Pointer to the tensor array structure corresponding to the model output.|
243| [const OH_AI_KernelCallBack](capi-model-h.md#oh_ai_kernelcallback) before | Callback function executed before model inference.|
244| [const OH_AI_KernelCallBack](capi-model-h.md#oh_ai_kernelcallback) after | Callback function executed after model inference.|
245
246**Returns**
247
248| Type| Description|
249| -- | -- |
250| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
251
252### OH_AI_ModelGetInputs()
253
254```
255OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetInputs(const OH_AI_ModelHandle model)
256```
257
258**Description**
259
260Obtains the input tensor array structure of a model.
261
262**Since**: 9
263
264
265**Parameters**
266
267| Name| Description|
268| -- | -- |
269| const [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
270
271**Returns**
272
273| Type                                   | Description|
274|---------------------------------------| -- |
275| OH_AI_API [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) | Tensor array structure corresponding to the model input.|
276
277### OH_AI_ModelGetOutputs()
278
279```
280OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetOutputs(const OH_AI_ModelHandle model)
281```
282
283**Description**
284
285Obtains the output tensor array structure of a model.
286
287**Since**: 9
288
289
290**Parameters**
291
292| Name| Description|
293| -- | -- |
294| const [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
295
296**Returns**
297
298| Type| Description|
299| -- | -- |
300| OH_AI_API [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) | Tensor array structure corresponding to the model output.|
301
302### OH_AI_ModelGetInputByTensorName()
303
304```
305OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetInputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)
306```
307
308**Description**
309
310Obtains the input tensor of a model by tensor name.
311
312**Since**: 9
313
314
315**Parameters**
316
317| Name| Description|
318| -- | -- |
319| const [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
320| const char *tensor_name | Tensor name.|
321
322**Returns**
323
324| Type                              | Description|
325|----------------------------------| -- |
326| OH_AI_API [OH_AI_TensorHandle](capi-mindspore-oh-ai-tensorHandle.md) | Pointer to the input tensor indicated by **tensor_name**. If the tensor does not exist, **null** will be returned.|
327
328### OH_AI_ModelGetOutputByTensorName()
329
330```
331OH_AI_API OH_AI_TensorHandle OH_AI_ModelGetOutputByTensorName(const OH_AI_ModelHandle model, const char *tensor_name)
332```
333
334**Description**
335
336Obtains the output tensor of a model by tensor name.
337
338**Since**: 9
339
340
341**Parameters**
342
343| Name| Description|
344| -- | -- |
345| const [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
346| const char *tensor_name | Tensor name.|
347
348**Returns**
349
350| Type| Description|
351| -- | -- |
352| OH_AI_API [OH_AI_TensorHandle](capi-mindspore-oh-ai-tensorHandle.md) | Pointer to the input tensor indicated by **tensor_name**. If the tensor does not exist, **null** will be returned.|
353
354### OH_AI_TrainCfgCreate()
355
356```
357OH_AI_API OH_AI_TrainCfgHandle OH_AI_TrainCfgCreate()
358```
359
360**Description**
361
362Creates the pointer to the training configuration object. This API is used only for on-device training.
363
364**Since**: 11
365
366**Returns**
367
368| Type                                | Description|
369|------------------------------------| -- |
370| OH_AI_API [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) | Pointer to the training configuration object.|
371
372### OH_AI_TrainCfgDestroy()
373
374```
375OH_AI_API void OH_AI_TrainCfgDestroy(OH_AI_TrainCfgHandle *train_cfg)
376```
377
378**Description**
379
380Destroys the pointer to the training configuration object. This API is used only for on-device training.
381
382**Since**: 11
383
384
385**Parameters**
386
387| Name                                | Description|
388|-------------------------------------| -- |
389| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) *train_cfg | Pointer to the training configuration object.|
390
391### OH_AI_TrainCfgGetLossName()
392
393```
394OH_AI_API char **OH_AI_TrainCfgGetLossName(OH_AI_TrainCfgHandle train_cfg, size_t *num)
395```
396
397**Description**
398
399Obtains the list of loss functions, which are used only for on-device training.
400
401**Since**: 11
402
403
404**Parameters**
405
406| Name| Description|
407| -- | -- |
408| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
409| size_t *num | Number of loss functions.|
410
411**Returns**
412
413| Type| Description|
414| -- | -- |
415| OH_AI_API char ** | List of loss functions.|
416
417### OH_AI_TrainCfgSetLossName()
418
419```
420OH_AI_API void OH_AI_TrainCfgSetLossName(OH_AI_TrainCfgHandle train_cfg, const char **loss_name, size_t num)
421```
422
423**Description**
424
425Sets the list of loss functions, which are used only for on-device training.
426
427**Since**: 11
428
429
430**Parameters**
431
432| Name| Description|
433| -- | -- |
434| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
435| const char **loss_name | List of loss functions.|
436| size_t num | Number of loss functions.|
437
438### OH_AI_TrainCfgGetOptimizationLevel()
439
440```
441OH_AI_API OH_AI_OptimizationLevel OH_AI_TrainCfgGetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg)
442```
443
444**Description**
445
446Obtains the optimization level of the training configuration object. This API is used only for on-device training.
447
448**Since**: 11
449
450
451**Parameters**
452
453| Name| Description|
454| -- | -- |
455| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
456
457**Returns**
458
459| Type                                                                          | Description|
460|------------------------------------------------------------------------------| -- |
461| OH_AI_API [OH_AI_OptimizationLevel](capi-types-h.md#oh_ai_optimizationlevel) | Optimization level.|
462
463### OH_AI_TrainCfgSetOptimizationLevel()
464
465```
466OH_AI_API void OH_AI_TrainCfgSetOptimizationLevel(OH_AI_TrainCfgHandle train_cfg, OH_AI_OptimizationLevel level)
467```
468
469**Description**
470
471Sets the optimization level of the training configuration object. This API is used only for on-device training.
472
473**Since**: 11
474
475
476**Parameters**
477
478| Name| Description|
479| -- | -- |
480| [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
481| [OH_AI_OptimizationLevel](capi-types-h.md#oh_ai_optimizationlevel) level | Optimization level.|
482
483### OH_AI_TrainModelBuild()
484
485```
486OH_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)
487```
488
489**Description**
490
491Loads 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.
492
493**Since**: 11
494
495
496**Parameters**
497
498| Name| Description|
499| -- | -- |
500| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
501| const void *model_data | Pointer to the buffer that stores the model file to be read.|
502| size_t data_size | Buffer size.|
503| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
504| const [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) model_context | Model runtime context, which is specified by [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md).|
505| const [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
506
507**Returns**
508
509| Type| Description|
510| -- | -- |
511| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
512
513### OH_AI_TrainModelBuildFromFile()
514
515```
516OH_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)
517```
518
519**Description**
520
521Loads 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.
522
523**Since**: 11
524
525
526**Parameters**
527
528| Name| Description|
529| -- | -- |
530| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
531| const char *model_path | Path of the model file.|
532| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
533| const [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md) model_context | Model runtime context, which is specified by [OH_AI_ContextHandle](capi-mindspore-oh-ai-contexthandle.md).|
534| const [OH_AI_TrainCfgHandle](capi-mindspore-oh-ai-traincfghandle.md) train_cfg | Pointer to the training configuration object.|
535
536**Returns**
537
538| Type| Description|
539| -- | -- |
540| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
541
542### OH_AI_RunStep()
543
544```
545OH_AI_API OH_AI_Status OH_AI_RunStep(OH_AI_ModelHandle model, const OH_AI_KernelCallBack before,const OH_AI_KernelCallBack after)
546```
547
548**Description**
549
550Defines a single-step training model. This API is used only for on-device training.
551
552**Since**: 11
553
554
555**Parameters**
556
557| Name| Description|
558| -- | -- |
559| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
560| [const OH_AI_KernelCallBack](capi-model-h.md#oh_ai_kernelcallback) before | Callback function executed before model inference.|
561| [const OH_AI_KernelCallBack](capi-model-h.md#oh_ai_kernelcallback) after | Callback function executed after model inference.|
562
563**Returns**
564
565| Type| Description|
566| -- | -- |
567| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
568
569### OH_AI_ModelSetLearningRate()
570
571```
572OH_AI_API OH_AI_Status OH_AI_ModelSetLearningRate(OH_AI_ModelHandle model, float learning_rate)
573```
574
575**Description**
576
577Sets the learning rate for model training. This API is used only for on-device training.
578
579**Since**: 11
580
581
582**Parameters**
583
584| Name| Description|
585| -- | -- |
586| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
587| float learning_rate | Learning rate.|
588
589**Returns**
590
591| Type| Description|
592| -- | -- |
593| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
594
595### OH_AI_ModelGetLearningRate()
596
597```
598OH_AI_API float OH_AI_ModelGetLearningRate(OH_AI_ModelHandle model)
599```
600
601**Description**
602
603Obtains the learning rate for model training. This API is used only for on-device training.
604
605**Since**: 11
606
607
608**Parameters**
609
610| Name| Description|
611| -- | -- |
612| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
613
614**Returns**
615
616| Type| Description|
617| -- | -- |
618| OH_AI_API float | Learning rate. If no optimizer is set, the value is **0.0**.|
619
620### OH_AI_ModelGetWeights()
621
622```
623OH_AI_API OH_AI_TensorHandleArray OH_AI_ModelGetWeights(OH_AI_ModelHandle model)
624```
625
626**Description**
627
628Obtains all weight tensors of a model. This API is used only for on-device training.
629
630**Since**: 11
631
632
633**Parameters**
634
635| Name| Description|
636| -- | -- |
637| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
638
639**Returns**
640
641| Type| Description|
642| -- | -- |
643| OH_AI_API [OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) | All weight tensors of the model.|
644
645### OH_AI_ModelUpdateWeights()
646
647```
648OH_AI_API OH_AI_Status OH_AI_ModelUpdateWeights(OH_AI_ModelHandle model, const OH_AI_TensorHandleArray new_weights)
649```
650
651**Description**
652
653Updates the weight tensors of a model. This API is used only for on-device training.
654
655**Since**: 11
656
657
658**Parameters**
659
660| Name| Description|
661| -- | -- |
662| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
663| [const OH_AI_TensorHandleArray](capi-mindspore-oh-ai-tensorhandlearray.md) new_weights | Weight tensors to be updated.|
664
665**Returns**
666
667| Type| Description|
668| -- | -- |
669| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
670
671### OH_AI_ModelGetTrainMode()
672
673```
674OH_AI_API bool OH_AI_ModelGetTrainMode(OH_AI_ModelHandle model)
675```
676
677**Description**
678
679Obtains the training mode.
680
681**Since**: 11
682
683
684**Parameters**
685
686| Name| Description|
687| -- | -- |
688| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
689
690**Returns**
691
692| Type| Description|
693| -- | -- |
694| OH_AI_API bool | Whether the training mode is used.|
695
696### OH_AI_ModelSetTrainMode()
697
698```
699OH_AI_API OH_AI_Status OH_AI_ModelSetTrainMode(OH_AI_ModelHandle model, bool train)
700```
701
702**Description**
703
704Sets the training mode. This API is used only for on-device training.
705
706**Since**: 11
707
708
709**Parameters**
710
711| Name| Description|
712| -- | -- |
713| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
714| bool train | Whether the training mode is used.|
715
716**Returns**
717
718| Type| Description|
719| -- | -- |
720| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
721
722### OH_AI_ModelSetupVirtualBatch()
723
724```
725OH_AI_API OH_AI_Status OH_AI_ModelSetupVirtualBatch(OH_AI_ModelHandle model, int virtual_batch_multiplier, float lr,float momentum)
726```
727
728**Description**
729
730Sets the virtual batch for training. This API is used only for on-device training.
731
732**Since**: 11
733
734
735**Parameters**
736
737| Name| Description|
738| -- | -- |
739| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
740| int virtual_batch_multiplier | Virtual batch multiplier. If the value is less than **1**, the virtual batch is disabled.|
741| float lr | Learning rate. The default value is **-1.0f**.|
742| float momentum | Momentum. The default value is **-1.0f**.|
743
744**Returns**
745
746| Type| Description|
747| -- | -- |
748| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
749
750### OH_AI_ExportModel()
751
752```
753OH_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)
754```
755
756**Description**
757
758Exports a training model. This API is used only for on-device training.
759
760**Since**: 11
761
762
763**Parameters**
764
765| Name                                                                               | Description|
766|------------------------------------------------------------------------------------| -- |
767| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model                     | Pointer to the model object.|
768| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type                      | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
769| const char *model_file                                                             | Path of the exported model file.|
770| [OH_AI_QuantizationType](capi-types-h.md#oh_ai_quantizationtype) quantization_type | Quantization type.|
771| bool export_inference_only                                                         | Whether to export an inference model.|
772| char **output_tensor_name                                                          | Output tensor of the exported model. This parameter is left blank by default, which indicates full export.|
773| size_t num                                                                         | Number of output tensors.|
774
775**Returns**
776
777| Type| Description|
778| -- | -- |
779| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
780
781### OH_AI_ExportModelBuffer()
782
783```
784OH_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)
785```
786
787**Description**
788
789Exports the memory cache of the training model. This API is used only for on-device training.
790
791**Since**: 11
792
793
794**Parameters**
795
796| Name| Description|
797| -- | -- |
798| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
799| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
800| void *model_data | Pointer to the buffer that stores the exported model file.|
801| size_t *data_size | Buffer size.|
802| [OH_AI_QuantizationType](capi-types-h.md#oh_ai_quantizationtype) quantization_type | Quantization type.|
803| bool export_inference_only | Whether to export an inference model.|
804| char **output_tensor_name | Output tensor of the exported model. This parameter is left blank by default, which indicates full export.|
805| size_t num | Number of output tensors.|
806
807**Returns**
808
809| Type| Description|
810| -- | -- |
811| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
812
813### OH_AI_ExportWeightsCollaborateWithMicro()
814
815```
816OH_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)
817```
818
819**Description**
820
821Exports the weight file of the training model for micro inference. This API is used only for on-device training.
822
823**Since**: 11
824
825
826**Parameters**
827
828| Name| Description|
829| -- | -- |
830| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
831| [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype) model_type | Model file type, which is specified by [OH_AI_ModelType](capi-types-h.md#oh_ai_modeltype).|
832| const char *weight_file | Path of the exported weight file.|
833| bool is_inference | Whether to export inference models. Currently, this parameter can only be set to **true**.|
834| bool enable_fp16 | Whether to save floating-point weights in float16 format.|
835| char **changeable_weights_name | Name of the weight tensor with a variable shape.|
836| size_t num | Number of weight tensors with a variable shape.|
837
838**Returns**
839
840| Type| Description|
841| -- | -- |
842| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
843
844### OH_AI_ModelLoadConfig()
845
846```
847OH_AI_API OH_AI_Status OH_AI_ModelLoadConfig(OH_AI_ModelHandle model, const char *config_path);
848```
849
850**Description**
851
852Loads the model configuration file.
853
854**Since**: 20
855
856**Parameters**
857
858| Name                                                      | Description          |
859| ------------------------------------------------------------ | -------------- |
860| [OH_AI_ModelHandle](capi-mindspore-oh-ai-modelhandle.md) model | Pointer to the model object.|
861| const char *config_path                                      | Configuration file path.|
862
863**Returns**
864
865| Type                                                   | Description                                                        |
866| ------------------------------------------------------- | ------------------------------------------------------------ |
867| OH_AI_API [OH_AI_Status](capi-status-h.md#oh_ai_status) | Status code enumerated by [OH_AI_Status](capi-status-h.md#oh_ai_status). The value **OH_AI_STATUS_SUCCESS** indicates that the operation is successful. If the operation fails, an error code is returned.|
868