aclmdlGetOutputSizeByIndex

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

Atlas 200I/500 A2 inference product

Atlas inference product

Atlas training product

Function Usage

Obtains the size (in bytes) of an output based on the model description.

Prototype

1
size_t aclmdlGetOutputSizeByIndex(aclmdlDesc *modelDesc, size_t index)

Parameters

Parameter

Input/Output

Description

modelDesc

Input

Pointer to data of the aclmdlDesc type

Call aclmdlCreateDesc to create data of the aclmdlDesc type in advance.

index

Input

Index of the output to obtain, indexed from 0

Returns

If dynamic batch/image size is enabled, the output size of the maximum profile is returned. In other cases, the size of the specified output is returned. The unit is byte.

Restrictions

If the size obtained from this call is 0, the possible cause is that the range of the output shape is unknown. Currently, the following two processing methods are available:

  • Method 1: The system internally allocates output memory of the corresponding index to save the memory. However, after the memory data is used, free it in a timely manner. In addition, memory copy is involved in allocation, which may cause performance loss. This method can be used only when the aclmdlExecute or aclmdlExecuteV2 inference API is used.

    When calling aclCreateDataBuffer to create the aclDataBuffer type for storing output data of the corresponding index, you can pass nullptr to the data parameter and set size to 0 to create an empty aclDataBuffer type. During model execution, the system automatically calculates and allocates the index output memory. This method saves memory. However, you need to free the memory and reset the aclDataBuffer after using the data. In addition, memory copy is involved when the system allocates memory, which may cause performance loss.

    The sample code for freeing the memory and resetting the aclDataBuffer is as follows:
    1
    2
    3
    4
    aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(output, 0); // Obtain the corresponding data buffer based on the index.
    void *data = aclGetDataBufferAddr(dataBuffer);  // Obtain the device pointer to data.
    aclrtFree(data ); // Free the device memory.
    aclUpdateDataBuffer(dataBuffer, nullptr, 0); // Reset the content in the data buffer for next inference.
    
  • Method 2: Estimate the size of the output memory and allocate it. You can manage it by yourself. However, the memory size may be insufficient or exceed the threshold. If the memory size is insufficient, the system reports an error. If it exceeds the threshold, a waste is caused.
    You need to estimate a large output memory as needed. During model execution, the system checks whether the specified output memory size meets the requirement. If no, an error message is returned, with the size of the desired output memory displayed. You can use either of the following methods to view the error message:
    • Obtain the application log files and view errors at the ERROR level.
    • Call aclGetRecentErrMsg in your application.