Sample Code

Principles

After the model is successfully loaded and the input and output data structures are prepared, the input data needs to be loaded to the corresponding buffer of the input data structure for model execution.

After model execution is complete, free up memory and destroy allocations (including the input data, aclmdlDesc type, aclmdlDataset type, and aclDataBuffer type) in a timely manner to avoid buffer exceptions. Data of type aclDataBuffer is used to describe the buffer address and buffer size of an input or output. A model may have multiple inputs or outputs. Call aclDestroyDataBuffer to destroy data of type aclDataBuffer and call aclrtFree to free up memory for each input and output separately.

Sample Code

This sample processes the outputs of the image classification model by printing the class indexes of the top 5 confidence values of each image. You can customize your own output processing logic.

You can view the complete code in Sample Overview.

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. Obtain the model description based on the model ID.
//modelDesc_ is of type aclmdlDesc.
modelDesc_ = aclmdlCreateDesc();
ret = aclmdlGetDesc(modelDesc_, modelId_);

//2. Prepare the input data structure for model inference.
//(1) Allocate input buffer.
size_t modelInputSize;
void *modelInputBuffer = nullptr;
//The model in this sample code has only one input, which is naturally indexed 0. If the model has multiple inputs, call the aclmdlGetNumInputs API to obtain the actual input count.
modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, 0);
aclRet = aclrtMalloc(&modelInputBuffer, modelInputSize, ACL_MEM_MALLOC_NORMAL_ONLY);

//(2) Prepare the input data structure of the model.
//Create data of the aclmdlDataset type to describe the inputs for model inference. input_ is of the aclmdlDataset type.
input_ = aclmdlCreateDataset();
aclDataBuffer *inputData = aclCreateDataBuffer(modelInputBuffer, modelInputSize);
ret = aclmdlAddDatasetBuffer(input_, inputData);

//3. Prepare the output data structure for model inference.
//(1) Create data of the aclmdlDataset type to describe the outputs of model inference. output_ is of the aclmdlDataset type.
output_ = aclmdlCreateDataset();

//(2) Obtain the number of model outputs.
size_t outputSize = aclmdlGetNumOutputs(modelDesc_);

//(3) Allocate buffer for each output with a for loop and add each output to aclmdlDataset.
for (size_t i = 0; i < outputSize; ++i) {
    size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i);
    void *outputBuffer = nullptr;
    aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY);
    aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size);   
    ret = aclmdlAddDatasetBuffer(output_, outputData);
    }

//4. Run the model.
string testFile[] = {
        "../data/dog1_1024_683.bin",
        "../data/dog2_1024_683.bin"
    };

for (size_t index = 0; index < sizeof(testFile) / sizeof(testFile[0]); ++index) {
    //4.1 Define the ReadBinFile function, and call the functions in the C++ standard library std::ifstream to read the image files to obtain the buffer size (inputBuffSize) and buffer address (inputBuff) of the image files.
    void *inputBuff = nullptr;
    uint32_t inputBuffSize = 0;
    auto ret = Utils::ReadBinFile(fileName, inputBuff, inputBuffSize);
    
    //4.2. Prepare the input data for model inference.
    //Call aclrtGetRunMode to obtain the run mode of the software stack before you allocate runtime resources.
    //If the run mode is ACL_DEVICE, the value of g_isDevice is True, which indicates that the software stack runs on the device side. Therefore, image data transfer is not required. You need to transfer data on the device. Otherwise, the memory copy API needs to be called to transfer data to the device.
    if (!g_isDevice) {
        // if app is running in host, need copy data from host to device
        //modelInputBuffer and modelInputSize indicate the buffer address and buffer size of the input data for model inference, respectively. The buffer is allocated when the input/output data structure is prepared.
        aclError aclRet = aclrtMemcpy(modelInputBuffer, modelInputSize, inputBuff, inputBuffSize, ACL_MEMCPY_HOST_TO_DEVICE);
        (void)aclrtFreeHost(inputBuff);
    } else { // app is running in device
        aclError aclRet = aclrtMemcpy(modelInputBuffer, modelInputSize, inputBuff, inputBuffSize, ACL_MEMCPY_DEVICE_TO_DEVICE);
        (void)aclrtFree(inputBuff);
    }

    //4.3 Run model inference.
    //modelId_ indicates the model ID. After the model is successfully loaded, the model ID is returned.
    //input_ and output_ indicate the input data and output data of model inference, respectively. They are defined when the input and output data structures of model inference are prepared.
    aclError ret = aclmdlExecute(modelId_, input_, output_)
        

    //Process the model inference output and print the class indexes corresponding to the top 5 confidence values.
    //output_ indicates the output of model execution.
    for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) {
    //Obtain the buffer address and buffer size of each output.
        aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i);
        void* data = aclGetDataBufferAddr(dataBuffer);

        size_t len = aclGetDataBufferSizeV2(dataBuffer);

        //Cast the buffered data to the float type.
        float *outData = NULL;
        outData = reinterpret_cast<float*>(data);
        
        //Print the class indexes of top 5 confidence values.
        map<float, int, greater<float> > resultMap;
        for (int j = 0; j < len / sizeof(float); ++j) {
            resultMap[*outData] = j;
            outData++;
        }
        int cnt = 0;
        for (auto it = resultMap.begin(); it != resultMap.end(); ++it) {
            // print top 5
            if (++cnt > 5) {
                break;
            }

            INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first);
    }
}

//5. Destroy the input and output allocations for model inference.
//Destroy the input data structures and free the buffer.
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) {
        aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(input_, i);
        (void)aclDestroyDataBuffer(dataBuffer);
}
(void)aclmdlDestroyDataset(input_);
input_ = nullptr;
aclrtFree(modelInputBuffer);

//Destroy the output data structures and free the buffer.
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) {
    aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i);
    void* data = aclGetDataBufferAddr(dataBuffer);
    (void)aclrtFree(data);
    (void)aclDestroyDataBuffer(dataBuffer);
}

(void)aclmdlDestroyDataset(output_);
output_ = nullptr;