Dynamic AIPP (Single Dynamic AIPP Input)

API Call Sequence

In the dynamic AIPP scenario, the model inference process is similar to the Model Management process, including initialization and deinitialization, runtime resource allocation and release, 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 parameters related to dynamic AIPP.

    When building a model, you need to call the API provided by pyacl to set the AIPP configuration required for model inference. The AIPP mode supported by the model has been configured during model building (using the insert_op_conf parameter of the ATC tool). For details about the ATC parameters, see ATC.

    After model building, the AIPP inputs are added to the generated OM model. During model inference, the AIPP input values are provided.

    Assume that the AIPP configuration of input a is dynamic. In the generated .om model, input b is added to describe the AIPP configuration of input a. During model execution, prepare the input data structure of a by referring to Preparing Input/Output Data Structure for Model Execution, and prepare the input data structure of b and set the input data of b by referring to the following content.

  • Before model inference
    • Prepare the data structure of the dynamic AIPP inputs.
      1. Before allocating memory for the dynamic AIPP input, call the acl.mdl.get_input_index_by_name API to obtain the index that identifies the input in the model based on the input name (fixed at ascend_dynamic_aipp_data).
      2. Call acl.mdl.get_input_size_by_index to obtain the input memory size based on the index.
      3. Pass the size obtained in 2 to the acl.rt.malloc call to allocate memory.

        Do not initialize the buffer manually; otherwise, the service will be unavailable. After the API calls described in 2, the system automatically initializes the buffer.

      4. Call the acl.create_data_buffer API to create data of the aclDataBuffer type, which is used to store the memory address and memory size of the dynamic AIPP input data.
      5. Call the acl.mdl.create_dataset API to create data of the aclmdlDataset type, and call the acl.mdl.add_dataset_buffer API to add data of the aclDataBuffer type to the data of the aclmdlDataset type.
    • Set dynamic AIPP parameters.
      Figure 1 API Call Sequence
      1. Call the acl.mdl.get_input_index_by_name API to obtain the index that identifies the input in the model based on the input name (fixed at ascend_dynamic_aipp_data).
      2. Set dynamic AIPP parameters.
        1. Call the acl.mdl.create_aipp API to create the aclmdlAIPP type.
        2. Call the operation API of the aclmdlAIPP data type to set dynamic AIPP parameters as required.
        3. In the dynamic AIPP scenario, the acl.mdl.set_aipp_src_image_size API (for setting the width and height of the source image) must be called.
        4. Call the acl.mdl.set_input_aipp API to set the dynamic AIPP data for model inference.
        5. Call the acl.mdl.destroy_aipp API to destroy the aclmdlAIPP type in a timely manner.

Sample Code

Following the API calls, add exception handling branches and specify log printing of error and information levels. 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import acl
# ......

ACL_YUV420SP_U8 = 1

# 1. Load the model and set dynamic AIPP parameters.
# ......

# 2. Create data of type aclmdlDataset to describe the input data input_dataset and output data output_dataset of the model.
# ......

# 3. Customize a function for setting dynamic AIPP parameters.
def model_set_dynamic_aipp():
    # 3.1 Obtain the index of the dynamic AIPP input.
    index, ret = acl.mdl.get_input_index_by_name(model_desc, "ascend_dynamic_aipp_data")

    # 3.2 Set dynamic AIPP parameters.
    batch_number = 1
    aipp_dynamic_set = acl.mdl.create_aipp(batch_number)
    ret = acl.mdl.set_aipp_src_image_size(aipp_dynamic_set, 256, 224)
    ret = acl.mdl.set_aipp_input_format(aipp_dynamic_set, ACL_YUV420SP_U8)
    ret = acl.mdl.set_aipp_csc_params(aipp_dynamic_set, 1, 256, 443, 0, 256, -86, -178, 256, 0, 350, 0, 0, 0, 0, 128, 128)
    ret = acl.mdl.set_aipp_rbuv_swap_switch(aipp_dynamic_set, 0)
    ret = acl.mdl.set_aipp_dtc_pixel_mean(aipp_dynamic_set, 0, 0, 0, 0, 0)
    ret = acl.mdl.set_aipp_dtc_pixel_min(aipp_dynamic_set, 0, 0, 0, 0, 0)
    ret = acl.mdl.set_aipp_pixel_var_reci(aipp_dynamic_set, 1, 1, 1, 1, 0)
    ret = acl.mdl.set_aipp_crop_params(aipp_dynamic_set, 1, 0, 0, 224, 224, 0)
    ret = acl.mdl.set_input_aipp(model_id, input_dataset, index, aipp_dynamic_set)
    ret = acl.mdl.destroy_aipp(aipp_dynamic_set)	
    # ......

# 4. Customize a function and execute the model.
def model_execute(index):
    # 4.1 Call the custom function to set dynamic AIPP parameters.
    ret = model_set_dynamic_aipp()
    # 4.2 Execute the model. model_id indicates the ID of a successfully loaded model. input_ and output_ indicate the input and output of the model.
    ret = acl.mdl.execute(model_id, input_dataset, output_dataset)
    # ......

# 5. Process the model inference result.
# ......