Dynamic AIPP (Single Dynamic AIPP Input)

This section describes the key APIs and sample code for model inference with a single dynamic AIPP input.

API Call Sequence

The model inference workflow in the dynamic AIPP scenario is similar to that in the scenario described in Model Inference. Both workflows include AscendCL 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 parameters related to dynamic AIPP.

    When building a model, you need to configure the dynamic AIPP mode by setting the insert_op_conf parameter of the ATC tool. For details about ATC parameters, see --insert_op_conf in ATC Instructions.

    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. For details about how to prepare the data structure of input a for model execution, see Preparing Input/Output Data Structure for Model Execution. For details about how to prepare the data structure of input b and set the data of input b, see the following description.

  • Before model inference
    • Prepare the data structure of the dynamic AIPP inputs.
      1. Before allocating buffer for a dynamic AIPP input, call aclmdlGetAippDataSize to obtain the buffer size.

        In earlier versions, the method of obtaining the buffer size by calling aclmdlGetInputSizeByIndex is still supported. However, the obtained buffer size may be 0 when the batch size is not fixed. In this case, you need to estimate the buffer size. To obtain the buffer size by calling aclmdlGetInputSizeByIndex, you need to call aclmdlGetInputIndexByName to obtain the index that identifies the input in the model based on the input name (fixed to the ACL_DYNAMIC_AIPP_NAME macro, indicating ascend_dynamic_aipp_data) in advance.

      2. Pass the size obtained in 1 to the aclrtMalloc call to allocate buffer.

        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.

      3. Call aclCreateDataBuffer to create data of type aclDataBuffer to describe the buffer address and buffer size of the dynamic AIPP input.
      4. Call aclmdlCreateDataset to create data of type aclmdlDataset, and call aclmdlAddDatasetBuffer to add data of type aclDataBuffer to data of type aclmdlDataset.
    • Set dynamic AIPP parameters.
      Figure 1 API call sequence
      1. Call aclmdlGetInputIndexByName to obtain the index of an input using the input name (fixed to ACL_DYNAMIC_AIPP_NAME).
      2. Set dynamic AIPP parameters.
        1. Call aclmdlCreateAIPP to create data of type aclmdlAIPP.
        2. Call the APIs provided in aclmdlAIPP to set dynamic AIPP attributes as required.
        3. In dynamic AIPP scenarios, aclmdlSetAIPPSrcImageSize must be called to set the source image size.
        4. Call aclmdlSetInputAIPP to set the dynamic AIPP attributes for model inference.
        5. Call aclmdlDestroyAIPP to destroy data of type aclmdlAIPP in a timely manner.

Sample Code

This section focuses on the code logic of model inference. For details about how to initialize and deinitialize AscendCL, see Initializing AscendCL. For details about how to allocate and deallocate runtime resources, see Runtime Resource Allocation and Deallocation.

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 be built or run.

 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
42
43
44
45
46
47
48
49
50
51
//1. Initialize AscendCL.

//2. Allocate runtime resources.

//3. Load a model and then set dynamic AIPP attributes.
// ......

//4. Prepare the model description modelDesc_, and the model inputs input_ and model outputs output_.

//5. Call the user-defined function to set the dynamic AIPP attributes.
int  ModelSetDynamicAIPP()
{
    //3.1 Obtain the index of the dynamic AIPP input.
    size_t index;
    //modelDesc_ is of type aclmdlCreateDesc for the model description, which is obtained based on the ID of the model that is successfully loaded in step 1.
    aclError ret = aclmdlGetInputIndexByName(modelDesc_, ACL_DYNAMIC_AIPP_NAME, &index);

    //3.2 Set dynamic AIPP attributes.
    uint64_t batchNumber = 1;
    aclmdlAIPP *aippDynamicSet = aclmdlCreateAIPP(batchNumber);
    ret = aclmdlSetAIPPSrcImageSize(aippDynamicSet, 256, 224);
    ret = aclmdlSetAIPPInputFormat(aippDynamicSet, ACL_YUV420SP_U8);
    ret = aclmdlSetAIPPCscParams(aippDynamicSet, 1, 256, 443, 0, 256, -86, -178, 256, 0, 350, 0, 0, 0, 0, 128, 128);
    ret = aclmdlSetAIPPRbuvSwapSwitch(aippDynamicSet, 0);
    ret = aclmdlSetAIPPDtcPixelMean(aippDynamicSet, 0, 0, 0, 0, 0);
    ret = aclmdlSetAIPPDtcPixelMin(aippDynamicSet, 0, 0, 0, 0, 0);
    ret = aclmdlSetAIPPPixelVarReci(aippDynamicSet, 1.0, 1.0, 1.0, 1.0, 0);
    ret = aclmdlSetAIPPCropParams(aippDynamicSet, 1, 2, 2, 224, 224, 0);
    ret = aclmdlSetInputAIPP(modelId_, input_, index, aippDynamicSet);
    ret = aclmdlDestroyAIPP(aippDynamicSet);
		
    // ......
}
//6. Customize a function to execute the model.
int ModelExecute(int index)
{
        aclError ret;
        //4.1 Call the user-defined function to set the dynamic AIPP attributes.
	ret = ModelSetDynamicAIPP();
        //4.2 Execute the model. modelId_ indicates the ID of a successfully loaded model, input_ indicates the model inputs, and output_ indicates the model outputs.
        ret = aclmdlExecute(modelId_, input_, output_);
        // ......
}

//7. Process the model inference result.

//8. Deallocate runtime resources.

//9. Deinitialize AscendCL.

// ......