The simple custom operator project described in this section is a simplified version of the custom operator project described above. It simplifies the operator compilation, packaging, and deployment processes so that you can integrate the project into your own operator projects.
The following figure shows the operator development process based on the simple custom operator project.

Similar to Operator Project Creation, a simple custom operator project is generated through msOpGen. The operator project is output based on the operator prototype definition, including the code implementation file on the host, implementation file on the kernel, and project compilation configuration file. The main difference is as follows: To create a simple operator project, you need to set the framework to aclnn by setting the -f option.
To create a simple operator development project using msOpGen, perform the following steps:
[
{
"op": "AddCustom",
"input_desc": [
{
"name": "x",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"fp16",
"float",
"int32"
]
},
{
"name": "y",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"fp16",
"float",
"int32"
]
}
],
"output_desc": [
{
"name": "z",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"fp16",
"float",
"int32"
]
}
]
}
]
${INSTALL_DIR}/python/site-packages/bin/msopgen gen -i $HOME/sample/add_custom.json -c ai_core-<soc_version> -lan cpp -out $HOME/sample/AddCustom -f aclnn
The AI processor model <soc_version> can be obtained in the following ways:
id indicates the device ID, which is the NPU ID obtained by running the npu-smi info -l command.
Basic functions (operator development, build, and deployment based on the project) are applicable across operator projects created based on the same AI processor series.
AddCustom ├── build.sh // Build entry point script ├── cmake │ ├── config.cmake // Compilation configuration item │ ├── func.cmake │ ├── intf.cmake │ └── util // Directory that stores the scripts used for operator project compilation and common build files ├── CMakeLists.txt // Build script of the operator project ├── op_host // Implementation file on the host │ ├── add_custom_tiling.h // Operator tiling definition file. │ ├── add_custom.cpp // Content file for operator prototype registration and tiling implementation │ ├── CMakeLists.txt ├── op_kernel // Implementation file on the kernel │ ├── CMakeLists.txt │ ├── add_custom.cpp // Operator code implementation file
You simply need to pay attention to the files in bold during subsequent development.
Implement the operator by referring to Operator Implementation on the Kernel, Tiling Implementation on the Host, and Operator Prototype Definition.
After the operator is developed on the kernel and host sides, you need to compile the operator to generate a static operator library. The aclnn calling implementation code and header file are automatically generated, and the aclnn dynamic library is generated by linking the static operator library to support subsequent single-operator API calling (aclnn). Compilation process:
The following figure shows the compilation process.
The preceding process is encapsulated in the compilation script. To compile the script, perform the following steps:
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
if (NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "")
endif()
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/build_out" CACHE PATH "" FORCE)
endif()
if (NOT DEFINED ASCEND_CANN_PACKAGE_PATH)
set(ASCEND_CANN_PACKAGE_PATH /usr/local/Ascend/cann CACHE PATH "") // Replace it with the actual path of the installed CANN software package.
endif()
if (NOT DEFINED ASCEND_PYTHON_EXECUTABLE)
set(ASCEND_PYTHON_EXECUTABLE python3 CACHE STRING "")
endif()
if (NOT DEFINED ASCEND_COMPUTE_UNIT)
set(ASCEND_COMPUTE_UNIT ascendxxx CACHE STRING "")
endif()
if (NOT DEFINED ENABLE_TEST)
set(ENABLE_TEST FALSE CACHE BOOL "")
endif()
if (NOT DEFINED ENABLE_CROSS_COMPILE)
set(ENABLE_CROSS_COMPILE FALSE CACHE BOOL "")
endif()
if (NOT DEFINED CMAKE_CROSS_PLATFORM_COMPILER)
set(CMAKE_CROSS_PLATFORM_COMPILER "/your/cross/compiler/path" CACHE PATH "")
endif()
set(ASCEND_TENSOR_COMPILER_PATH ${ASCEND_CANN_PACKAGE_PATH}/compiler)
set(ASCEND_CCEC_COMPILER_PATH ${ASCEND_TENSOR_COMPILER_PATH}/ccec_compiler/bin)
set(ASCEND_AUTOGEN_PATH ${CMAKE_BINARY_DIR}/autogen)
file(MAKE_DIRECTORY ${ASCEND_AUTOGEN_PATH})
set(CUSTOM_COMPILE_OPTIONS "custom_compile_options.ini")
execute_process(COMMAND rm -rf ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS}
COMMAND touch ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS})
|
Parameter |
Description |
Default Value |
|---|---|---|
|
ASCEND_CANN_PACKAGE_PATH |
Installation path of the CANN package. Change it based on the actual situation. |
/usr/local/Ascend/cann |
|
CMAKE_BUILD_TYPE |
Compilation mode options:
|
Release |
|
CMAKE_INSTALL_PREFIX |
Directory for storing compilation results. If this parameter is not specified, the default value is used. |
${CMAKE_SOURCE_DIR}/build_out: build_out directory in the operator project directory |
|
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. 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) |
# set custom compile options
if ("${CMAKE_BUILD_TYPE}x" STREQUAL "Debugx")
add_ops_compile_options(ALL OPTIONS -g -O0)
endif()
# For multi-operator compilation, run the add_kernel_compile command to add the operator source code file.
add_kernel_compile(AddCustom ${CMAKE_CURRENT_SOURCE_DIR}/add_custom.cpp)
add_kernel_compile(SubCustom ${CMAKE_CURRENT_SOURCE_DIR}/sub_custom.cpp)
if(ENABLE_TEST AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testcases)
add_subdirectory(testcases)
endif()
./build.sh
After the compilation is successful, the generated aclnn header file and library are stored in the CMAKE_INSTALL_PREFIX/op_api directory. Each operator corresponds to an independent header file. The directory structure is as follows:
├── op_api │ ├── include │ ├── aclnn_optype1.h │ └── aclnn_optype2.h │ └── aclnn_optypexxx.h │ ├── lib │ ├── libcust_opapi.so
You can use the msobjdump tool to parse the library file generated in the lib directory to obtain the kernel information. For details, see msobjdump.
Complete single-operator API call.