Overview
Basic Concepts
- Level-0 APIs
L0 APIs for short, indicating the APIs of the basic kernel. These APIs directly call the kernel of the affinity SoC.
The return type of an L0 API is a tensor type structure, such as aclTensor*, std::tuple<aclTensor*, aclTensor*>, or aclTensorList*. The last parameter is fixed at aclOpExecutor *executor, and the type and name cannot be changed. The following is an example:
1aclTensor* AddNd(aclTensor *x1, aclTensor *x2, aclOpExecutor *executor)
The L0 API namespace is namespace l0op, and the API name is ${op_type}${format}${dtype}, where ${op_type} indicates the operator name, ${format} indicates the operator input/output format, and ${dtype} indicates the operator input/output type. The following is an example:
1 2
l0op::AddNd // The input of the Add operator is computed in ND format. l0op::MatMulNdFp162Fp32 // The input and output are computed in ND format. The input is fp16 and the output is fp32.
- Level-2 APIs
L2 APIs, which are higher-level APIs (also called host APIs). They can call multiple L0 APIs to implement more flexible functions. In addition, they correspond to framework API functions to facilitate framework adaptation and script migration.
The return type of an L2 API is aclnnStatus, which is generally defined as a two-phase API.
1 2
aclnnStatus aclnnXxxGetWorkspaceSize(const aclTensor *src, ..., aclTensor *out, ..., uint64_t *workspaceSize, aclOpExecutor **executor); aclnnStatus aclnnXxx(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream);
- The last two parameters of aclnnXxxGetWorkspaceSize are fixed at (uint64_t *workspaceSize, aclOpExecutor **executor). Their name and type cannot be changed.
- The aclnnXxx API parameter is fixed at (void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream).
aclnnXxxGetWorkspaceSize is the first-phase API, which is used to compute the workspace size required for the API calling. After obtaining the workspace size, allocate memory on the device based on workspaceSize, and call the second-phase API aclnnXxx to perform computation.
- The workspace refers to the temporary memory required by API computation on the Ascend AI Processor except for input and output.
- The second-phase aclnnXxx(...) API cannot be called repeatedly. The following call throws an exception:
1 2 3
aclnnXxxGetWorkspaceSize(...) aclnnXxx(...) aclnnXxx(...)
- Broadcast relationship
Broadcasting describes how an operator treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is "broadcast" across the larger array so that they have compatible shapes. For more information about the broadcast technology, see the NumPy official website.
Overall Description
This chapter describes the basic framework APIs, macros, classes, and basic kernel function APIs (Level-0 APIs) involved in single-operator API execution. It describes the function prototype, function usage, parameters, constraints, and examples to help you quickly customize neural network (NN) operators and fused operators (generally prefixed with aclnn) or modify built-in CANN operators, in order to support various AI services.
When calling single-operator APIs, you need to include the dependent header files as required. The paths of the header files are as follows:
- The header files of framework capability APIs are stored in the ${INSTALL_DIR}/include directory. For details, see Table 1. For details about the common classes and macro definitions provided by the framework, see Table 2 and Table 3.
- The header files of basic kernel function APIs are stored in the ${INSTALL_DIR}/include directory. For details, see Table 4.
Replace ${INSTALL_DIR} with the actual CANN component directory. If the Ascend-CANN-Toolkit package is installed as the root user, the CANN component directory is /usr/local/Ascend/ascend-toolkit/latest.
Macro |
Description |
Header File |
|---|---|---|
Packs all API input parameters on the host in L2_DFX_PHASE_1. |
aclnn/opdev/op_dfx.h |
|
Packs all API output parameters on the host in L2_DFX_PHASE_1. |
||
Must be used in the L0 API on the host to print the API and input parameters in the L0 API. |
||
Must be called at the beginning of the first-phase API to print the API and input parameters in the first phase. |
||
Must be called at the beginning of the second-phase API for API printing. |
||
Must be used at the beginning of the L0 API to register the L0 operator. |
||
Packs operator attribute parameters in ADD_TO_LAUNCHER_LIST_AICORE. |
aclnn/opdev/op_arg_def.h |
|
Holds the place for an empty input or output in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Packs the operator input aclTensor in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Packs the operator running options in ADD_TO_LAUNCHER_LIST_AICORE, for example, whether to enable HF32. |
||
Packs the operator output aclTensor in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Sets the aclTensor for storing the output shape for the third type of operators in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Packs the precision mode specified by the operator in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Packs the workspace parameter explicitly specified by the operator in ADD_TO_LAUNCHER_LIST_AICORE. |
||
Creates a UniqueExcutor object, which is the factory class of aclOpExecutor. |
aclnn/opdev/make_op_executor.h |
|
Runs the infershape function of a specified operator to infer the output shape. |
||
Creates an execution task for an AI Core operator and adds the task to the aclOpExecutor execution queue. This API is executed in the second phase. |
||
Packs the character type attributes of an AI CPU operator. It is a vector of string type. |
aclnn/opdev/aicpu/aicpu_task.h |
|
Creates an execution task for an AI CPU operator and adds the task to the aclOpExecutor execution queue. This API is executed in the second phase. |
Class |
Description |
Header File |
||||||
|---|---|---|---|---|---|---|---|---|
aclOpExecutor |
Indicates the operator executor, which records the context structure of the API running information on the host, such as the computational graph during the execution of an L2 API, launch subtask of an L0 operator, and workspace address and size. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see op_executor. |
aclnn/opdev/op_executor.h |
||||||
aclTensor |
Indicates a tensor object, including the shape, data type, format, and address of the tensor. The data can be stored on the host or device. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
aclnn/opdev/common_type.h |
||||||
aclScalar |
Indicates a scalar object. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclTensorList |
Indicates a list object consisting of a group of aclTensor types. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclScalarList |
Indicates a list object consisting of a group of aclScalar types. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclBoolArray |
Indicates an array object of the Boolean type. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclIntArray |
Indicates an array object of the int64_t type. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclFloatArray |
Indicates an array object of the fp32 type. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclFp16Array |
Indicates an array object of the fp16 type. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
aclBf16Array |
Indicates an array object of the bf16 type. The data is generally stored on the host. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see common_types. |
|||||||
SmallVector |
This class uses the internal memory pool to implement the vector container. The basic functions of this class are the same as those of the std::vector container in the C++ standard library. Not every capacity expansion requires memory allocation so that the performance is not affected. The member variables defined by this class are private variables and can be ignored. For details about the defined member functions, see small_vector.
|
aclnn/opdev/small_vector.h |
||||||
OpExecMode |
Indicates the enumeration class for the operator run mode. For details, see OpExecMode. |
aclnn/opdev/op_def.h |
||||||
OpImplMode |
Indicates the enumeration class for the operator precision modes. For details, see OpImplMode. |
API |
Description |
Header File |
|---|---|---|
Converts an input tensor to the specified data type. |
aclnn_kernels/cast.h |
|
Converts a non-contiguous tensor to a contiguous tensor. |
aclnn_kernels/contiguous.h |
|
Moves a contiguous tensor to a continuous or non-contiguous tensor. |
||
Pads dimensions of an input tensor based on paddings. The padding value is 0. |
aclnn_kernels/pad.h |
|
Reshapes the input tensor x. |
aclnn_kernels/reshape.h |
|
Obtains tensor slices. |
aclnn_kernels/slice.h |
|
Transposes the shape of input tensor x according to the permutation of dimensions (perm) and outputs the result. |
aclnn_kernels/transpose.h |
|
Converts the format of an input tensor to the specified dstPrimaryFormat. |
aclnn_kernels/transdata.h |
|
Converts the format of an input tensor to the specified dstPrimaryFormat. This API is similar to TransData. |
||
Reformats the input tensor x to the destination format without changing the dimensions. |
||
Checks whether the input pointer is null. |
aclnn_kernels/common/op_error_check.h |