A similar workflow, as described in Developing a Basic Inference App, can be followed for inference with larger batch sizes.
However, there are some slight differences worth mentioning in batched inference:
uint32_t batchSize = 8;
uint32_t deviceNum = 1;
uint32_t deviceId = 0;
//Obtain the size of the first input of the model.
uint32_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc, 0);
//Obtain the buffer size per batch.
uint32_t singleBuffSize = modelInputSize / batchSize;
//Define a variable to accumulate the input data to the batch size (8).
uint32_t cnt = 0;
//Define a variable to describe the offset for loading each file to memory.
uint32_t pos = 0;
void* p_batchDst = NULL;
std::vector<std::string>inferFile_vec;
for (int i = 0; i < files.size(); ++i)
{
//Allocate device memory every eight input files (batch size = 8).
if (cnt % batchSize == 0)
{
pos = 0;
inferFile_vec.clear();
//Allocate device memory.
ret = aclrtMalloc(&p_batchDst, modelInputSize, ACL_MEM_MALLOC_NORMAL_ONLY);
}
//TODO: Load a file from a directory and calculate the file size fileSize.
//Allocate memory to store file data based on the file size.
ret = aclrtMallocHost(&p_imgBuf, fileSize);
//Transfer data to the device memory.
ret = aclrtMemcpy((uint8_t *)p_batchDst + pos, fileSize, p_imgBuf, fileSize, ACL_MEMCPY_HOST_TO_DEVICE);
pos += fileSize;
//Free unused memory in a timely manner.
aclrtFreeHost(p_imgBuf);
//Save the ith file to the vector and increase the value of cnt by 1.
inferFile_vec.push_back(files[i]);
cnt++;
//Feed batch-size data (of eight files) to the model for inference.
if (cnt % batchSize == 0)
{
//TODO: Create data of type aclmdlDataset and aclDataBuffer to describe the input and output data of the model.
//TODO: Call aclmdlExecute to start model inference.
//TODO: Call aclrtFree to free device memory after the inference is complete.
}
}
//Feed the remainder data that is less than the batch size to the model for inference.
if (cnt % batchSize != 0)
{
//TODO: Create data of type aclmdlDataset and aclDataBuffer to describe the input and output data of the model.
//TODO: Call aclmdlExecute to start model inference.
//TODO: Call aclrtFree to free device memory after the inference is complete.
}