Dynamic Batch/Dynamic Image Size/Dynamic Dimension (Setting Multi-Dimension Profiles)
API Call Sequence
The model inference workflow in the dynamic shape input scenario is similar to that in the scenario described in Model Inference. Both workflows include pyACL initialization and deinitialization, runtime resource allocation and deallocation, model building, model loading, model execution, and model unloading.
This section focuses on the differences between the two scenarios.
- During model building: You need to set the dynamic batch size, dynamic image size, and dynamic dimensions (ND format).
If the dynamic batch size feature is involved during model inference, call the pyACL API to set the batch size. The batch size supported by the model has been configured during model building (by using the dynamic_batch_size parameter of ATC).
If the dynamic image size feature is involved during model inference, call the pyACL API to set the image size. The image size supported by the model has been configured during model building (by using the dynamic_image_size parameter of ATC).
If the dynamic dimensions (ND format) feature is involved during model inference, call the pyACL API to set the dimensions. The dimensions supported by the model have been configured during model building (by using the dynamic_dims parameter of ATC).
After model building, the inputs of dynamic batch size, dynamic image size, and dynamic dimensions are added to the generated OM model. During model inference, the input values are provided.
Assume that the batch size of input a is dynamic. In the generated OM model, input b is added to describe the batch size of input a. To proceed to run your model, prepare the data structure of input a (see Preparing Input/Output Data Structure for Model Execution for details), prepare the data structure of input b, and set the data of input b (see 2 for details).
- Before model inference
- Prepare the data structures of the inputs of dynamic batch size, dynamic image size, and dynamic dimensions.
- Before allocating memory for the inputs of dynamic batch size, dynamic image size, and dynamic dimensions, call acl.mdl.get_input_index_by_name to obtain the indexes of the inputs in the model based on the input name (the input name is fixed to ascend_mbatch_shape_data).
- Pass the input index to the acl.mdl.get_input_size_by_index call to obtain the input buffer size.
- Pass the size obtained in 2.b to the acl.rt.malloc call to allocate memory.
Data in the memory does not need to be set after memory allocation (otherwise, services may be abnormal). After calling the APIs described in 2.b, the system automatically sets the data in the memory.
- Call acl.create_data_buffer to create data of the aclDataBuffer type to store the memory address and memory size of the inputs of dynamic batch size, image size, and dimensions.
- Call acl.mdl.create_dataset to create data of type aclmdlDataset, and call acl.mdl.add_dataset_buffer to add data of type aclDataBuffer to data of type aclmdlDataset.
- Set the dynamic batch size, dynamic image size, and dynamic dimensions.
Figure 1 API call sequence
- Call acl.mdl.get_input_index_by_name to obtain the index of an input using the input name (fixed to ascend_mbatch_shape_data).
- Set the dynamic batch size, dynamic image size, and dynamic dimensions.
- Call acl.mdl.set_dynamic_batch_size to set the dynamic batch size.
The configured batch size must be among the batch size profiles set during model building.
You can call acl.mdl.get_dynamic_batch to obtain the batch size profiles supported by the model.
- Call acl.mdl.set_dynamic_hw_size to set the dynamic image size.
The configured image size must be among the image size profiles set during model building.
You can also call acl.mdl.get_dynamic_hw to obtain the number of image size profiles as well as the image size of each profile supported by the model.
- Call acl.mdl.set_input_dynamic_dims to set the dynamic dimensions.
The configured dimensions must be among the dimension profiles set during model building.
You can also call acl.mdl.get_input_dynamic_dims to obtain the dimension profiles supported by the model.
- Call acl.mdl.set_dynamic_batch_size to set the dynamic batch size.
- Prepare the data structures of the inputs of dynamic batch size, dynamic image size, and dynamic dimensions.
Sample Code for Dynamic Batch
After APIs are called, add an exception handling branch, and record error logs and warning logs. The following is a code snippet of key steps only, which is not ready to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 1. Load the model and set dynamic batch size profiles. # ...... # 2. Create data of type aclmdlDataset to describe the inputs and outputs of the model. # ...... # 3. Customize a function for setting dynamic batch size profiles. def model_set_dynamicinfo(): # 3.1 Obtain the indexes of the input with dynamic batch size. The input name is fixed to ascend_mbatch_shape_data. index, ret = acl.mdl.get_input_index_by_name(model_desc, "ascend_mbatch_shape_data") # 3.2 Set the batch size. model_id indicates the ID of a successfully loaded model, input indicates data of type aclmdlDataset, and index indicates the index of the input with dynamic batch size. batchSize = 8 ret = acl.mdl.set_dynamic_batch_size(model_id_, input, index, batch_size) # ...... # 4. Customize a function and execute the model. def model_execute(index): # 4.1 Call the customized function to set dynamic batch size profiles. ret = model_set_dynamicinfo() # 4.2 Execute the model. model_id indicates the ID of a successfully loaded model. input and output indicate the inputs and outputs of the model. ret = acl.mdl.execute(model_id, input, output) # ...... } # 5. Process the model inference result. # ...... |
Sample Code for Dynamic Image Size
After APIs are called, add an exception handling branch, and record error logs and warning logs. The following is a code snippet of key steps only, which is not ready to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 1. Load the model and set dynamic image size profiles. # ...... # 2. Create data of type aclmdlDataset to describe the inputs and outputs of the model. # ...... # 3. Customize a function for setting dynamic image size profiles. def model_set_dynamicInfo(): # 3.1 Obtain the indexes of the inputs with dynamic image size. The input name is fixed to ascend_mbatch_shape_data. index, ret = acl.mdl.get_input_index_by_name(modelDesc, "ascend_mbatch_shape_data") # 3.2 Set the image size. model_id indicates the ID of a successfully loaded model, input indicates data of type aclmdlDataset, and index indicates the index of the input with dynamic image size. height = 224 width = 224 ret = acl.mdl.set_dynamic_hw_size(model_id, input, index, height, width) # ...... # 4. Customize a function and execute the model. def model_execute(index): # 4.1 Call the customized function to set dynamic image size profiles. ret = model_set_dynamicInfo() # 4.2 Execute the model. model_id indicates the ID of a successfully loaded model. input and output indicate the inputs and outputs of the model. ret = acl.mdl.execute(model_id, input, output) # ...... # 5. Process the model inference result. # ...... |
Sample Code for Dynamic Dimensions (ND Format)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import acl # ...... # 1. Load the model and set dynamic dimension profiles. # ...... # 2. Prepare the model description model_desc, model inputs input_dataset, and model outputs output_dataset. # ...... # 3. Define a function for setting dynamic dimension profiles. def model_set_dynamic_info(): # 3.1 Obtain the indexes of the inputs with dynamic dimensions. The input name is fixed to ascend_mbatch_shape_data. index, ret = acl.mdl.get_input_index_by_name(model_desc, "ascend_mbatch_shape_data") # 3.2 Set the runtime dimensions, including the dimension count (dimCount) and the size of each dimension. model_id indicates the ID of a successfully loaded model, input_dataset indicates data of type aclmdlDataset, and index indicates the index of the dynamic dimension input. current_dims = {'name': '', 'dimCount': 4, 'dims': [8, 3, 224, 224]} ret = acl.mdl.set_input_dynamic_dims(mode_id, input_dataset, index, current_dims) # ...... # 4. Customize a function and execute the model. def ModelExecute(int index): # 4.1 Call the customized function to set dynamic dimension profiles. ret = model_set_dynamic_info() # 4.2 Execute the model. model_id indicates the ID of a successfully loaded model. input_dataset and output_dataset indicates the inputs and outputs of the model. ret = acl.mdl.execute(model_id, input_dataset, output_dataset) # ...... # 5. Process the model inference result. # ...... |