Dynamic Batch (Setting Batch Profiles)

Principles

If the dynamic batch size feature is involved during model inference, call the AscendCL 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).

For details about the ATC parameter, see ATC Instructions.

  • For the same model, only one of the aclmdlSetDynamicBatchSize, aclmdlSetDynamicHWSize, and aclmdlSetInputDynamicDims APIs can be called.
  • For output buffer allocation for model inference, you can allocate buffer based on the runtime dimensions. To be accurate, you can call aclmdlGetOutputSizeByIndex to obtain the required buffer size before allocating the buffer.
  • If dynamic AIPP and dynamic batch size are both enabled:
    • The batchSize parameter passed to the aclmdlCreateAIPP call must be set to the allowed maximum profile.
    • For data nodes that require dynamic AIPP, the corresponding input buffer must be allocated based on the maximum batch size profile.

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

//1. Load the model and then set the runtime batch size.
// ......

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

//3. Customize a function to set the runtime batch size.
int  ModelSetDynamicInfo()
{
        size_t index;
       //3.1 Obtain the indexes the inputs with dynamic batch size. The input name is fixed to ACL_DYNAMIC_TENSOR_NAME.
        aclError ret = aclmdlGetInputIndexByName(modelDesc_, ACL_DYNAMIC_TENSOR_NAME, &index);
        //3.2 Set the batch size.
        //modelId_ indicates the ID of the model that is successfully loaded, input_ indicates data of the aclmdlDataset type, index indicates the input index of the dynamic batch input, and batchSize indicates the batch size (8 for example).
        uint64_t batchSize = 8;
        ret = aclmdlSetDynamicBatchSize(modelId_, input_, index, batchSize);
        // ......
}

//4. Customize a function to execute the model.
int ModelExecute(int index)
{
        aclError ret;
        //4.1 Call the user-defined function to set the runtime batch size.
	ret = ModelSetDynamicInfo();
       //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_);
        // ......
}
//5. Process the model inference result.
// TODO