msOpGen (Operator Project Generation)
The msOpGen tool can be used to generate custom operator projects during operator development. This allows users to focus on the core logic and algorithm implementation of operators without spending a lot of time on repetitive work such as project setup, compilation, and configuration, greatly improving the development efficiency.
- Generate the operator directory.
- Save the AddCustom.json operator definition file to the working directory. For details about the configuration parameters of the .json file, see Table 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[ { "op": "AddCustom", "language": "cpp", "input_desc": [ { "name": "x", "param_type": "required", "format": [ "ND" ], "type": [ "float16" ] }, { "name": "y", "param_type": "required", "format": [ "ND" ], "type": [ "float16" ] } ], "output_desc": [ { "name": "z", "param_type": "required", "format": [ "ND" ], "type": [ "float16" ] } ] } ]
- Run the following command to generate an operator development project. For details about the parameters, see Table 2.
msopgen gen -i AddCustom.json -f tf -c ai_core-ascendxxxyy -lan cpp -out AddCustom # xxxyy indicates the type of the processor used by the user.
- Run the following command to view the generated directory.
tree -C -L 2 AddCustom/
- The following shows the directory of the operator project generated in the specified directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
AddCustom ├── build.sh // Compilation entry script ├── cmake ├── CMakeLists.txt // CMakeLists.txt script of the operator project ├── CMakePresets.json // Compilation configuration item ├── framework // Directory for storing the implementation file of the operator plugin. The generation of single-operator model files does not depend on the operator plugin and can be ignored. │ ├── CMakeLists.txt │ └── tf_plugin ├── op_host // Implementation file on the host. │ ├── add_custom.cpp // Content file for operator prototype registration, shape derivation, information library, and tiling implementation. │ ├── add_custom_tiling.h // Operator tiling definition file. │ └── CMakeLists.txt ├── op_kernel // Implementation file on the kernel │ ├── add_custom.cpp // Operator implementation file │ └── CMakeLists.txt └── scripts // Directory of scripts used for custom operator project packing.
- Save the AddCustom.json operator definition file to the working directory. For details about the configuration parameters of the .json file, see Table 1.
- Click Link to obtain the code samples for operator kernel function development and Tiling implementation. Run the following command to move the operator implementation files in the sample directory to the directory generated by msOpGen in Step 1:
cp -r ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AddCustom/* AddCustom/
- ${git_clone_path} is the installation path of the sample repository.
- After an operator project is created, you need to develop an operator as instructed in Ascend C Operator Development Guide. This step only describes the functions of the operator development tools. Therefore, the sample code is used.
- When downloading the code sample, run the following command to specify the branch version:
git clone https://gitee.com/ascend/samples.git -b r0.2
- Build the operator project.
- In the directory where the custom operator package is stored, run the following command to deploy the operator package:
./build_out/custom_opp_<target_os>_<target_architecture>.run
- Verify the operator function and generate the executable file execute_add_op.
- Switch to the directory of the AclNNInvocation repository.
cd ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AclNNInvocation - Run the following command:
./run.sh
- Compare the accuracy and generate the executable file execute_add_op.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
INFO: execute op! [INFO] Set device[0] success [INFO] Get RunMode[1] success [INFO] Init resource success [INFO] Set input success [INFO] Copy input[0] success [INFO] Copy input[1] success [INFO] Create stream success [INFO] Execute aclnnAddCustomGetWorkspaceSize success, workspace size 0 [INFO] Execute aclnnAddCustom success [INFO] Synchronize stream success [INFO] Copy output[0] success [INFO] Write output success [INFO] Run op success [INFO] Reset Device success [INFO] Destroy resource success INFO: acl executable run success! error ratio: 0.0000, tolerance: 0.0010 test pass
- Switch to the directory of the AclNNInvocation repository.
Parent topic: Operator Development Tools Quick Start