Before loading and executing a dynamic-shape operator, develop a custom operator and generate the corresponding binary file. For details, see "Special Topics > TIK Custom Operator with Dynamic Shape" in Operator Developer Guide.
The procedure of loading and executing a dynamic-shape operator is as follows:
The operator selector needs to be defined and implemented by the user in advance.
typedef aclError (*aclopCompileFunc)(int numInputs, const aclTensorDesc *const inputDesc[], int numOutputs, const aclTensorDesc *const outputDesc[], const aclopAttr *opAttr, aclopKernelDesc *aclopKernelDesc);
Write a piece of code to implement the selection between tiling policies and the generation of tiling arguments. Use aclopSetKernelArgs to set the tiling arguments and number of times the AI Cores will be kickstarted for concurrent execution.
The aclrtSetDevice call also implicitly creates a default stream. If no stream is created explicitly, the default stream is used. To pass the default stream to any API call, pass NULL directly.
Call aclopUpdateParams to compile the operator and trigger the call logic of the operator selector.
Call aclopExecuteV2 to load and execute the operator.
If no stream is created explicitly and the default stream is used, aclrtDestroyStream does not need to be called.
If no context is created explicitly and the default context is used, aclrtDestroyContext does not need to be called.
Following the API calls in the sample, 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.
For the sample about building and running a dynamic-shape operator (with an operator selector registered), see "Special Topics > TIK Custom Operator with Dynamic Shape > Sample Usage" in Operator Developer Guide.
#include "acl/acl.h"
// ......
//1. Initialize resources.
//This path is relative to the directory of the executable file.
aclError ret = aclInit(NULL);
aclopRegisterCompileFunc("BatchNorm", SelectAclopBatchNorm);
//Build the .o file of the operator kernel in advance and call the user-defined function to load the file to the memory buffer. length indicates the memory size. To load multiple .o files, a separate call is needed for each .o file.
aclopCreateKernel("BatchNorm", "tiling_mode_1__kernel0", "tiling_mode_1__kernel0",
buffer, length, ACL_ENGINE_AICORE, Deallocator);
//-----User-defined function BatchNormTest(n, c, h, w) start-----
//2. Construct the input/output tensors and the input/output tensor descriptions of the BatchNorm operator. Allocate memory for storing the input and output data of the operator.
aclTensorDesc *input_desc[3];
aclTensorDesc *output_desc[1];
input_desc[0] = aclCreateTensorDesc(ACL_FLOAT16, 4, shape_input, ACL_FORMAT_NCHW);
input_desc[1] = aclCreateTensorDesc(ACL_FLOAT16, 1, shape_gamma, ACL_FORMAT_ND);
input_desc[2] = aclCreateTensorDesc(ACL_FLOAT16, 1, shape_beta, ACL_FORMAT_ND);
output_desc[0] = aclCreateTensorDesc(ACL_FLOAT16, 4, shape_out, ACL_FORMAT_NCHW);
for (int i = 0; i < n * c * h * w; ++i) {
input[i] = aclFloatToFloat16(1.0f);
}
for (int i = 0; i < c; ++i) {
gamma[i] = aclFloatToFloat16(0.5f);
beta[i] = aclFloatToFloat16(0.1f);
}
aclrtMalloc(&devInput, size_input, ACL_MEM_MALLOC_NORMAL_ONLY);
aclrtMalloc(&devInput_gamma, size_gamma, ACL_MEM_MALLOC_NORMAL_ONLY);
aclrtMalloc(&devInput_beta, size_beta, ACL_MEM_MALLOC_NORMAL_ONLY);
aclrtMalloc(&devOutput, size_output, ACL_MEM_MALLOC_NORMAL_ONLY);
//3. Transfer the operator input data to the device memory.
aclrtMemcpy(devInput, size_input, input, size_input, ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(devInput_gamma, size_gamma, gamma, size_gamma, ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(devInput_beta, size_beta, beta, size_beta, ACL_MEMCPY_HOST_TO_DEVICE);
//4. Call aclopUpdateParams to compile the operator.
aclopUpdateParams("BatchNorm", 3, input_desc, 1, output_desc, nullptr, ACL_ENGINE_AICORE, ACL_COMPILE_UNREGISTERED, nullptr));
//5. Call aclopExecuteV2 to load and execute the operator.
aclopExecuteV2("BatchNorm", 3, input_desc, inputs, 1, output_desc, outputs, nullptr, stream);
//-----User-defined function BatchNormTest(n, c, h, w) end-----
//6. Obtain the operator output data.
aclrtMemcpy(output, size_output, devOutput, size_output, ACL_MEMCPY_DEVICE_TO_HOST);
//7. Destroy allocations in sequence.
//7.1 Destroy the input and output tensor description.
for (auto desc : input_desc) {
aclDestroyTensorDesc(desc);
}
for (auto desc : output_desc) {
aclDestroyTensorDesc(desc);
}
//7.2 Free unused memory in a timely manner.
delete[]input;
delete[]gamma;
delete[]beta;
delete[]output;
//7.3 Free device memory.
aclrtFree(devInput);
aclrtFree(devInput_gamma);
aclrtFree(devInput_beta));
aclrtFree(devOutput);
//7.4 Destroy the stream and context and reset the device in sequence. Skip this step if no stream or context is created explicitly.
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();