Operator Project Building
After the custom operator project is built, a custom OPP (a .run file) is generated. You do not need to modify the build script.
TensorFlow
- As the HwHiAiUser user (running user), go to the TensorFlow custom operator project directory op/tensorflow and run the following command to create a directory for storing build result files. The directory name can be user-defined.
mkdir build_out
- Set the environment variables.Run the export command to declare environment variables on the current terminal. The environment variables become invalid when the shell terminal is closed.
export ASCEND_TENSOR_COMPLIER_INCLUDE=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/atc/include export SYSTEM_INFO=centos_x86_64
- ASCEND_TENSOR_COMPLIER_INCLUDE specifies the path of the ATC header files. Set it to the actual one.
- SYSTEM_INFO specifies the form name field of the generated OPP runfile after build. Set it as needed.
Assume that the operating system is CentOS and the architecture is AArch64, set this parameter to centos_aarch64. Then, the generated OPP runfile is named custom_opp_centos_aarch64.run.
If the SYSTEM_INFO environment variable is not set, the form name is automatically obtained based on the OS and architecture configuration.
- Go to the build_out directory created in 1 and run the cmake command to generate the build configuration file of your custom operator project:
cd build_out
cmake .. -DCMAKE_CXX_COMPILER=g++
- Build your operator project:
make
After successful build, an OPP runfile custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.
The generated OPP contains all operators in the directory of the current custom operator project.
Caffe
- Add the definition of custom operator to the op/caffe/custom.proto file of the Caffe custom operator project.
The custom.proto file is as follows:
syntax = "proto2"; package domi.caffe; message NetParameter { optional string name = 1; // LayerParameter definition. Retain the default definition. repeated LayerParameter layer = 100; // ID 100 so layers are printed last. } message LayerParameter { optional string name = 1; // Definition for model parsing. Retain the default definition. optional string type = 2; // Definition for model parsing. Retain the default definition. // Add the definition of the custom operator layer to LayerParameter. The ID must be unique in the built-in caffe.proto file and must be less than 5000. // The built-in caffe.proto file is stored in atc/include/proto/caffe.proto in the ATC installation path. optional CustomTestParameter custom_test_param = 1000; } // Add the definition of the custom layer. message CustomTestParameter { optional bool adj_x1 = 1 [default = false]; optional bool adj_x2 = 2 [default = false]; }
- You are advised to keep the parameter type (in bold and italic) unique and not the same as that defined in the built-in caffe.proto file in the atc/include/proto directory.
- The custom.proto file in the sample code contains the definition of the custom Caffe operator. If there are other custom operators, add their definitions to this file.
- Modify the build.sh build script.
Configure environment variables and build settings in the op/caffe/build.sh script in the Caffe custom project.
- ASCEND_TENSOR_COMPLIER_INCLUDE specifies the path of the ATC header files.
- If it is not set, the default path /usr/local/Ascend/atc/include is used.
- If the actual ATC path is not the default path, uncomment this environment variable and change it to the actual path of the ATC header files, for example:
export ASCEND_TENSOR_COMPLIER_INCLUDE=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/atc/include
- SYSTEM_INFO specifies the form name field of the generated OPP runfile after build. Set it as needed.
Assume that the operating system is CentOS and the architecture is AArch64, set this parameter to centos_aarch64. Then, the generated OPP runfile is named custom_opp_centos_aarch64.run.
If the SYSTEM_INFO environment variable is not set, the form name is automatically obtained based on the OS and architecture configuration.
After modifying the path, run the :wq command to save the modifications and exit.
- ASCEND_TENSOR_COMPLIER_INCLUDE specifies the path of the ATC header files.
- Caffe
Run the following command in the op/caffe directory of the custom Caffe operator project:
./build.sh
After successful build, an OPP runfile custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.
If you need to rebuild the project, run the ./build.sh clean command to clear the build outputs.
TensorFlow + Caffe
- In the op/all/custom.proto file of the custom operator project, add the definition of the Caffe custom operator.
The custom.proto file is as follows:
syntax = "proto2"; package domi.caffe; message NetParameter { optional string name = 1; // LayerParameter definition. Retain the default definition. repeated LayerParameter layer = 100; // ID 100 so layers are printed last. } message LayerParameter { optional string name = 1; // Definition for model parsing. Retain the default definition. optional string type = 2; // Definition for model parsing. Retain the default definition. // Add the definition of the custom operator layer to LayerParameter. The ID must be unique in the built-in caffe.proto file and must be less than 5000. // The built-in caffe.proto file is stored in atc/include/proto/caffe.proto in the ATC installation path. optional CustomTestParameter custom_test_param = 1000; } // Add the definition of the custom layer. message CustomTestParameter { optional bool adj_x1 = 1 [default = false]; optional bool adj_x2 = 2 [default = false]; }
- You are advised to keep the parameter type (in bold and italic) unique and not the same as that defined in the built-in caffe.proto file in the atc/include/proto directory.
- The custom.proto file in the sample code contains the definition of the custom Caffe operator. If there are other custom operators, add their definitions to this file.
- Configure the environment variables and build settings in the build.sh script based on the actual development environment.Configure the following environment variables in the header of the build.sh script:
- ASCEND_OPP_PATH: environment variable required for AI CPU operator build. Set this parameter to the OPP installation path. It has no default value.Uncomment this environment variable and change it to the actual OPP installation path, for example:
export ASCEND_OPP_PATH=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/opp
- ASCEND_TENSOR_COMPLIER_INCLUDE: path of the ATC header files
- If it is not set, the default path /usr/local/Ascend/atc/include is used.
- If the actual ATC path is not the default path, uncomment this environment variable and change it to the actual path of the ATC header files, for example:
export ASCEND_TENSOR_COMPLIER_INCLUDE=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/atc/include
- TOOLCHAIN_DIR: directory of the HCC cross-compiler in the Toolkit component, without default value specified. This compiler is used to build the AI CPU operator in the sample.Uncomment this environment variable and change it to the actual path of the HCC compiler. For example:
export TOOLCHAIN_DIR=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/toolkit/toolchain/hcc
- AICPU_KERNEL_TARGET: name of the dynamic library file generated after the implementation file of the AI CPU operator is built.
Retain the default value: cust_aicpu_kernels.
- SYSTEM_INFO: name of the OPP form generated after compilation. If the environment variable SYSTEM_INFO is not set, the value is automatically obtained based on the OS type and architecture.
If you need to customize the OPP name, uncomment the environment variable and change it as required. For example, if the OS version is CentOS and the architecture is AArch64, set the environment variable as follows:
export SYSTEM_INFO=centos_aarch64
The built OPP name is custom_opp_centos_aarch64.run.
- ASCEND_OPP_PATH: environment variable required for AI CPU operator build. Set this parameter to the OPP installation path. It has no default value.
- Build the operator project.
Run the following command in the op/all directory of the custom operator project to build the custom operator project:
./build.sh
After successful build, an OPP runfile custom_opp_<target os>_<target architecture>.run is generated in the build_out directory.
If you need to rebuild the project, run the ./build.sh clean command to clear the build outputs.