CANN开发套件包中提供了自定义算子工程生成工具msopgen,可基于算子原型定义输出算子工程:包括算子host侧代码实现文件、算子kernel侧实现文件、算子适配插件以及工程编译配置文件等。
使用msopgen工具创建算子开发工程的步骤如下:
[ { "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" ] } ] } ]
[ { "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 } ] } ]
配置字段 |
类型 |
含义 |
是否必选 |
|
---|---|---|---|---|
op |
- |
字符串 |
算子的Operator Type。 |
是 |
input_desc |
- |
列表 |
输入参数描述。 |
否 |
name |
字符串 |
算子输入参数的名称。 |
||
param_type |
字符串 |
参数类型:
未配置默认为required。 |
||
format |
列表 |
针对类型为tensor的参数,配置为tensor支持的数据排布格式,具体请参考数据排布格式。 包含如下取值: ND,NHWC,NCHW,HWCN,NC1HWC0,FRACTAL_Z等。 format与type的数量需保持一致。 |
||
type |
列表 |
算子参数的类型。 取值范围:float16(fp16), float32(fp32), int8, int16, int32, uint8, uint16, bool, bfloat16(bf16)等。
说明:
不同计算操作支持的数据类型不同,详细请参见API参考。 format与type的数量需保持一致。 |
||
output_desc |
- |
列表 |
输出参数描述。 |
是 |
name |
字符串 |
算子输出参数的名称。 |
||
param_type |
字符串 |
参数类型:
未配置默认为required。 |
||
format |
列表 |
针对类型为tensor的参数,配置为tensor支持的数据排布格式,具体请参考数据排布格式。 包含如下取值: ND,NHWC,NCHW,HWCN,NC1HWC0,FRACTAL_Z等。 format与type的数量需保持一致。 |
||
type |
列表 |
算子参数的类型。 取值范围:float16(fp16), float32(fp32), int8, int16, int32, uint8, uint16, bool, bfloat16(bf16)等。
说明:
不同计算操作支持的数据类型不同,详细请参见API参考。 format与type的数量需保持一致。 |
||
attr |
- |
列表 |
属性描述。 |
否 |
name |
字符串 |
算子属性参数的名称。 |
||
param_type |
字符串 |
参数类型:
未配置默认为required。 |
||
type |
字符串 |
算子参数的类型。 包含如下取值: int、bool、float、string、list_int、list_float等。 |
||
default_value |
- |
默认值。 |
${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
AddCustom ├── build.sh // 编译入口脚本 ├── cmake │ ├── config.cmake │ ├── func.cmake │ ├── intf.cmake │ ├── makeself.cmake │ └── util // 算子工程编译所需脚本及公共编译文件存放目录 ├── CMakeLists.txt // 算子工程的CMakeLists.txt ├── CMakePresets.json // 编译配置项 ├── framework // 算子插件实现文件目录,单算子模型文件的生成不依赖算子适配插件,无需关注 ├── op_host // host侧实现文件 │ ├── add_custom_tiling.h // 算子tiling定义文件 │ ├── add_custom.cpp // 算子原型注册、shape推导、信息库、tiling实现等内容文件 │ ├── CMakeLists.txt ├── op_kernel // kernel侧实现文件 │ ├── CMakeLists.txt │ ├── add_custom.cpp // 算子代码实现文件 └── scripts // 自定义算子工程打包相关脚本所在目录
上述目录结构中的粗体文件为后续算子开发过程中需要修改的文件,其他文件无需修改。