For the API call sequence, see API Call Sequence.
For an operator not supported by Ascend AI Processors, develop a custom operator by referring to Operator Developer Guide and then execute the single-operator as follows.
This sample uses the Add operator as an example. You need to construct the description file (.json file, describing the input and output tensors and operator attributes) of the operator. You also need to use the ATC tool to compile the operator description file into a single-operator model file (.om file), and call the AscendCL APIs to load the .om file and execute the operator. For details about how to use ATC, see ATC Instructions.
Example description file of the Add operator:
[
{
"op": "Add",
"input_desc": [
{
"format": "ND",
"shape": [8, 16],
"type": "int32"
},
{
"format": "ND",
"shape": [8, 16],
"type": "int32"
}
],
"output_desc": [
{
"format": "ND",
"shape": [8, 16],
"type": "int32"
}
]
}
]
The following is a code snippet of key steps only, which is not ready to be built or run. Following the API calls, add exception handling branches and specify log printing of error and information levels. The complete code is available in the acl_execute_add sample.
//1. Initialize AscendCL.
aclRet = aclInit(nullptr);
//2. Allocate runtime resources. (The default context and stream are used. When the default stream is used as the argument of another API, a null pointer can be passed.)
aclRet = aclrtSetDevice(0);
Obtain the run mode of the software stack. Different run modes lead to different API call sequences (for example, whether data transfer is required).
aclrtRunMode runMode;
bool g_isDevice = false;
aclError aclRet = aclrtGetRunMode(&runMode);
g_isDevice = (runMode == ACL_DEVICE);
//3. Load the single-operator model file (.om file).
//This directory is relative to the directory of the executable file. For example, if the executable file is stored in the out directory, the directory is out/op_models.
aclRet = aclopSetModelDir("op_models");
//4. Execute the operator.
//opType indicates the operator type name, for example, Add.
//numInputs indicates the number of operator inputs. For example, the Add operator has two inputs.
//inputDesc indicates the array of the operator input tensor descriptions, describing the format, shape, and data type of each input.
//inputs indicates the input tensor data of the operator.
//numOutputs indicates the number of operator outputs. For example, the Add operator has one output.
//outputDesc indicates the array of the operator output tensor descriptions, describing the format, shape, and data type of each output.
//outputs indicates the output tensor data of the operator.
//attr indicates the operator attributes. If the operator does not have attributes, call aclopCreateAttr to create data of the aclopAttr type.
//stream is used to maintain the execution sequence of some asynchronous operations.
aclopExecuteV2(opType, numInputs, inputDesc, inputs,
numOutputs, outputDesc, outputs, attr, nullptr);
//Process the output data after the operator is executed, for example, printing the data to the screen or writing the data to a file. You can implement this function as required.
// ......
//Block the app until all tasks in the specified stream are complete.
aclrtSynchronizeStream(nullptr);
//5. Release runtime resources. (By default, the context and stream resources are automatically released using the aclrtResetDevice call.)
aclRet = aclrtResetDevice(0);
//6. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....