创建算子工程(msOpGen)
msOpGen工具用于算子开发时,可生成自定义算子工程,方便用户专注于算子的核心逻辑和算法实现,而无需花费大量时间在项目搭建、编译配置等重复性工作上,从而大大提高了开发效率。
- 生成算子目录。
- 把算子定义的AddCustom.json文件放到工作目录当中,json文件的配置参数详细说明请参考表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" ] } ] } ]
- 执行以下命令,生成算子开发工程,参数说明请参见表2。
msopgen gen -i AddCustom.json -f tf -c ai_core-ascendxxxyy -lan cpp -out AddCustom # xxxyy为用户实际使用的具体芯片类型
- 执行以下命令,查看生成目录。
tree -C -L 2 AddCustom/
- 在指定目录下生成的算子工程目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
AddCustom ├── build.sh ├── cmake ├── CMakeLists.txt ├── CMakePresets.json ├── framework │ ├── CMakeLists.txt │ └── tf_plugin ├── op_host │ ├── add_custom.cpp │ ├── add_custom_tiling.h │ └── CMakeLists.txt ├── op_kernel │ ├── add_custom.cpp │ └── CMakeLists.txt └── scripts
- 把算子定义的AddCustom.json文件放到工作目录当中,json文件的配置参数详细说明请参考表1。
- 单击Link,获取算子核函数开发和Tiling实现的代码样例。执行以下命令,将样例目录中的算子实现文件移动至msOpGen步骤1生成的目录中。
cp -r ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AddCustom/* AddCustom/
- 编译算子工程。
- 在自定义算子包所在路径下,执行如下命令,部署算子包。
./build_out/custom_opp_<target_os>_<target_architecture>.run
- 验证算子功能,生成可执行文件execute_add_op。
- 切换到AclNNInvocation仓的目录。
cd ${git_clone_path}/samples/operator/ascendc/0_introduction/1_add_frameworklaunch/AclNNInvocation
- 执行以下命令。
./run.sh
- 成功对比精度,并生成可执行文件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
- 切换到AclNNInvocation仓的目录。
父主题: 算子开发工具快速入门