Operator Project Build
After the operator kernel and host implementations are developed, build the operator project to generate a custom OPP runfile (*.run). The build workflow includes:
- Compile the code implementation file *.cpp on the kernel of the Ascend C operator, which can be released in source code or binary mode.
- Source release: The kernel implementation of the operator is not compiled, and the kernel source code file *.cpp of the operator is retained. This method supports online operator build and operator build through ATC model conversion.
- Binary release: Compile the kernel implementation of the operator and generate the JSON file *.json and operator binary file *.o that describe the operator information. If you need to directly call the operator binary, use this compilation mode. For example, call a single operator in Single-Operator API Calling mode, single-operator calling in the PyTorch framework, or operator calling on a dynamic network.
- Compile the code implementation files *.cpp and *.h of the Ascend C operator on the host.
- Compile the prototype definition and shape derivation implementation into the operator prototype definition dynamic library libcust_opsproto_*.so, and generate the external interface op_proto.h of the operator prototype.
- Compile the Tiling implementation into the Tiling dynamic library liboptiling.so.
- The single-operator API call code and header file aclnn_*.h are automatically generated based on the operator prototype definition, and the dynamic library libcust_opapi.so for single-operator API call is generated through compilation.
Procedure
- Complete the project build configuration.
- Modify cacheVariables in CMakePresets.json under the project directory. The content of the CMakePresets.json file is as follows. For details about the parameters to be set, see Table 1. Other parameters are automatically generated during project creation.
{ "version": 1, "cmakeMinimumRequired": { "major": 3, "minor": 19, "patch": 0 }, "configurePresets": [ { "name": "default", "displayName": "Default Config", "description": "Default build using Unix Makefiles generator", "generator": "Unix Makefiles", "binaryDir": "${sourceDir}/build_out", "cacheVariables": { "CMAKE_BUILD_TYPE": { "type": "STRING", "value": "Release" }, "ENABLE_SOURCE_PACKAGE": { "type": "BOOL", "value": "True" }, "ENABLE_BINARY_PACKAGE": { "type": "BOOL", "value": "True" }, "ASCEND_COMPUTE_UNIT": { "type": "STRING", "value": "ascendxxx" }, "ENABLE_TEST": { "type": "BOOL", "value": "True" }, "vendor_name": { "type": "STRING", "value": "customize" }, "ASCEND_PYTHON_EXECUTABLE": { "type": "STRING", "value": "python3" }, "CMAKE_INSTALL_PREFIX": { "type": "PATH", "value": "${sourceDir}/build_out" }, "ENABLE_CROSS_COMPILE": { "type": "BOOL", "value": "False" }, "CMAKE_CROSS_PLATFORM_COMPILER": { "type": "PATH", "value": "/usr/bin/aarch64-linux-gnu-g++" }, "ASCEND_PACK_SHARED_LIBRARY": { "type": "BOOL", "value": "False" } } } ] }Table 1 Parameters to be configured by developers Parameter
Description
Default Value
CMAKE_BUILD_TYPE
Compilation mode options:
- Release: release version, which does not contain debugging information. The final release version is compiled.
- Debug: debug version, which contains debugging information for you to develop and debug.
Release
ENABLE_SOURCE_PACKAGE
Whether to enable source code compilation
True: enabled.
ENABLE_BINARY_PACKAGE
Whether to enable binary compilation
True: enabled.
vendor_name
Indicates the name of the vendor to which the custom operator belongs. You are advised to specify the vendor name to avoid conflicts with OPPs provided by other vendors.
customize
ASCEND_PACK_SHARED_LIBRARY
Whether to enable dynamic library compilation
**False**
- (Optional) Configure environment variables for compilation.
Table 2 Description of environment variables Environment Variable
Configuration Description
CMAKE_CXX_COMPILER_LAUNCHER
Configures the C++ compiler (such as g++) and BiSheng Compiler launcher as ccache, enabling cache compilation, accelerating repeated compilation, and improving build efficiency. Before using this function, you need to install ccache.
The following shows how to perform configuration in the corresponding CMakeLists.txt file.
set(CMAKE_CXX_COMPILER_LAUNCHER <launcher_program>)
<launcher_program> indicates the ccache installation path, for example, /usr/bin/ccache.
set(CMAKE_CXX_COMPILER_LAUNCHER /usr/bin/ccache)
- Modify cacheVariables in CMakePresets.json under the project directory. The content of the CMakePresets.json file is as follows. For details about the parameters to be set, see Table 1. Other parameters are automatically generated during project creation.
- Run the following command in the operator project directory to compile the operator project:
./build.sh
After the project is built successfully, an OPP runfile named custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.
To save build process logs, use the environment variable ASCENDC_BUILD_LOG_DIR to control the storage path. If no error occurs during compilation, the log file name is followed by _success. If an error occurs, the corresponding error information is displayed on the screen, indicating the path and name of the log file, and the log file name is followed by _error.
# To store build logs in /home/build_log/, do as follows and disable log storage by default. export ASCENDC_BUILD_LOG_DIR=/home/build_log/
Cross Compilation of OPP
After the operator code is implemented, if the current platform architecture is consistent with the operating environment, compile the operator package by referring to the previous section. To implement cross compilation of the operator package, perform the following steps:
- Download the cross compilation tool. The following table uses Ubuntu as an example to describe how to download the compilation tool. For other operating systems, replace it with the actual download command.
Table 3 Example command for downloading the cross compilation tool for Ubuntu Current Platform Architecture
Operating Environment Platform Architecture
Commands for Downloading Build Tools
x86_64
aarch64
sudo apt-get install -y g++-aarch64-linux-gnu
aarch64
x86_64
sudo apt-get install g++-x86-64-linux-gnu
- Cross compile the custom operator project and generate the custom operator package.
- Change the value of ENABLE_CROSS_COMPILE in CMakePresets.json to True to enable cross compilation.
"ENABLE_CROSS_COMPILE": { "type": "BOOL", "value": "True" } - Change CMAKE_CROSS_PLATFORM_COMPILER in CMakePresets.json to the path of the cross compilation tool after installation.
"CMAKE_CROSS_PLATFORM_COMPILER": { "type": "PATH", "value": "/usr/bin/aarch64-linux-gnu-g++" } - Run the following command in the operator project directory to cross compile the operator project:
After the project is built successfully, an OPP runfile named custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.
- Change the value of ENABLE_CROSS_COMPILE in CMakePresets.json to True to enable cross compilation.
Supported Customization Options
In an operator project, if you want to add some custom compilation options to the kernel code, you can customize the compilation options by referring to the following method:
Modify the CMakeLists.txt file in the op_kernel directory of the operator project and use add_ops_compile_options to add compilation options as follows:
add_ops_compile_options(OpType COMPUTE_UNIT soc_version1 soc_version2 ... OPTIONS option1 option2 ...)
The following table describes the parameters.
- Compilation options are configured based on the operator type and AI processor model series. That is, different compilation options can be configured for different operator types and AI processor model series.
add_ops_compile_options(AddCustom COMPUTE_UNIT Ascendxxxyy ... OPTIONS -DNEW_MACRO1=xx) add_ops_compile_options(AddCustom COMPUTE_UNIT Ascendxxxyy ... OPTIONS -DNEW_MACRO2=xx) add_ops_compile_options(AddCustom COMPUTE_UNIT Ascendxxxyy ... OPTIONS -DNEW_MACRO3=xx)
- For the same operator type and AI processor model series, configure compilation options for multiple times. The latest configuration is used.
- If there is no conflict between the compilation options that take effect for ALL and the compilation options that take effect for a single-operator, the compilation options take effect at the same time. If there is a conflict, use the compilation options of the single-operator.