The CANN development kit provides the custom operator project generation tool msOpGen, which can generate operator projects based on the operator prototype definition. An operator project includes the operator code file for implementation on the host, operator implementation file on the kernel, and project compilation configuration file.
Before using msOpGen to create the operator project, install the driver firmware and CANN package by referring to Environment Setup to set up the development environment and operating environment.
To create an operator development project using msOpGen, perform the following steps:
[
{
"op": "AddCustom",
"input_desc": [
{
"name": "x",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"float16",
"float",
"int32"
]
},
{
"name": "y",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"float16",
"float",
"int32"
]
}
],
"output_desc": [
{
"name": "z",
"param_type": "required",
"format": [
"ND",
"ND",
"ND"
],
"type": [
"float16",
"float",
"int32"
]
}
]
}
]
[
{
"op": "ReduceMaxCustom",
"input_desc": [
{
"name": "x",
"param_type": "required",
"format": ["ND"],
"type": ["float16"]
}
],
"output_desc": [
{
"name": "y",
"param_type": "required",
"format": ["ND"],
"type": ["float16"]
},
{
"name": "idx",
"param_type": "required",
"format": ["ND"],
"type": ["int32"]
}
],
"attr": [
{
"name": "reduceDim",
"param_type": "required",
"type": "int"
},
{
"name": "isKeepDim",
"param_type": "optional",
"type": "int",
"default_value": 1
}
]
}
]
The value of the field op in the prototype definition JSON file must use upper camel case, where uppercase letters distinguish different semantic parts.
${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
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 // Compilation entry script ├── cmake // Directory that stores the scripts used for operator project build and common build files ├── CMakeLists.txt // Build script of the operator project ├── CMakePresets.json // Compilation configuration item ├── framework // Directory of the operator plugin implementation files during AI framework adaptation ├── op_host // Implementation file on the host │ ├── add_custom_tiling.h // Operator tiling definition file │ ├── add_custom.cpp // Content file for operator prototype registration, shape derivation, information library, and tiling implementation │ ├── CMakeLists.txt ├── op_kernel // Implementation file on the kernel │ ├── CMakeLists.txt │ ├── add_custom.cpp // Operator implementation file └── scripts // Directory of scripts used for custom operator project packing
op_kernel and op_host in the project directory contain the core implementation files of the operator. Operator implementation on the kernel is stored in op_kernel. Code implementation on the host is stored in op_host, including the operator prototype definition and tiling implementation on the host. For details about the operator implementation on the kernel and tiling implementation on the host, see SIMD Operator Implementation. This section focuses on the programming mode and API usage after the CANN framework is connected. You can use the CMakePresets.json file in the project directory to configure project compilation and deploy the project.