自定义算子工程编译、打包和部署样例
【免费下载链接】cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。项目地址: https://gitcode.com/cann/cann-learning-hub
概述
本样例以简单的自定义算子为示例,展示了其编译、打包成自定义算子包,并部署到CANN环境中的流程。
支持的产品
本样例支持如下产品型号:
- Ascend 950PR/Ascend 950DT
- Atlas A3 训练系列产品/Atlas A3 推理系列产品
- Atlas A2 训练系列产品/Atlas A2 推理系列产品
- Atlas 200I/500 A2 推理产品
- Atlas 推理系列产品
注意: 本样例中涉及多个算子示例,请以各个算子示例实际支持的产品型号为准。
目录结构介绍
├── CMakeLists.txt ├── framework │ ├── CMakeLists.txt │ ├── onnx_plugin │ │ ├── CMakeLists.txt │ │ └── leaky_relu_custom_plugin.cc │ └── tf_plugin │ ├── CMakeLists.txt │ └── tensorflow_add_custom_plugin.cc ├── op_host │ ├── CMakeLists.txt │ ├── add_custom │ │ └── add_custom_host.cpp │ ├── add_custom_template │ │ └── add_custom_template.cpp │ ├── add_custom_tiling_sink │ │ ├── add_custom_tiling_sink.cpp │ │ ├── add_custom_tiling_sink_tiling.cpp │ │ └── add_custom_tiling_sink_tiling.h │ └── leaky_relu_custom │ └── leaky_relu_custom_host.cpp └── op_kernel ├── CMakeLists.txt ├── add_custom │ ├── add_custom_kernel.cpp │ └── add_custom_tiling.h ├── add_custom_template │ ├── add_custom_template.cpp │ ├── add_custom_template_tiling.h │ └── tiling_key_add_custom_template.h ├── add_custom_tiling_sink │ ├── add_custom_tiling_sink_kernel.cpp │ └── add_custom_tiling_sink_tiling_struct.h └── leaky_relu_custom ├── leaky_relu_custom_kernel.cpp └── leaky_relu_custom_tiling.h算子描述
Add算子实现两个数据相加,返回相加结果的功能。对应的数学表达式为:
z = x + yAddCustomTilingSink、AddCustomTemplate与Add功能一致。其中,AddCustomTemplate展示了Tiling模板编程,添加的模板参数包括输入的数据类型、shape等,根据模板参数,简化或统一算子的实现逻辑,开发者可以在模板参数中定义需要的信息,如输入输出的数据类型,其他扩展参数等;AddCustomTilingSink用于展示Tiling下沉场景,y为Tiling值依赖输入,用于区分编译期或运行时的workspace配置。
LeakyRelu算子实现了将数据按element做带泄露线性整流,返回处理结果的功能。 对应的数学表达式为:
$$ y= \begin{cases} x, \quad x\geq 0\ a*x, \quad x<0 \end{cases} $$ 其中a为scalar值。
算子规格描述
Add
算子类型(OpType) Add 算子输入 name shape data type format x 8 * 2048 float ND y 8 * 2048 float ND 算子输出 z 8 * 2048 float ND 核函数名 add_custom, add_custom_tiling_sink AddCustomTemplate
算子类型(OpType) Add 算子输入 name shape data type format x 8 * 2048 float ND y 8 * 2048 float ND 算子输出 z 8 * 2048 float ND 核函数名 add_template_custom 模板参数 template<typename D_T_X, typename D_T_Y, typename D_T_Z, int TILE_NUM, int IS_SPLIT> D_T_X typename 数据类型(float16,float) D_T_Y typename 数据类型(float16,float) D_T_Z typename 数据类型(float16,float) TILE_NUM int 切分数量 IS_SPLIT int 是否切分 LeakyRelu
算子类型(OpType) LeakyRelu 算子输入 name shape data type format x 8 * 200 * 1024 float ND negative_slope 0.0 float ND 算子输出 y 8 * 200 * 1024 float ND 核函数名 leaky_relu_custom
代码实现介绍
Add
kernel实现:
Ascend C提供的矢量计算接口Add的操作元素都为LocalTensor,输入数据需要先搬运进片上存储,然后使用计算接口完成两个输入参数相加,得到最终结果,再搬出到外部存储上。
Add算子的实现流程分为3个基本任务:CopyIn,Compute,CopyOut。CopyIn任务负责将Global Memory上的输入TensorxGm和yGm搬运到Local Memory,分别存储在xLocal、yLocal,Compute任务负责对xLocal、yLocal执行加法操作,计算结果存储在zLocal中,CopyOut任务负责将输出数据从zLocal搬运至Global Memory上的输出TensorzGm中。tiling实现:
TilingData参数设计,AddCustomTilingData参数本质上是和并行数据切分相关的参数,本示例算子使用了2个tiling参数:totalLength、tileNum。totalLength是指需要计算的数据量大小,tileNum是指每个核上总计算数据分块个数。比如,totalLength这个参数传递到kernel侧后,可以通过除以参与计算的核数,得到每个核上的计算量,这样就完成了多核数据的切分。
AddCustomTemplate
功能与Add一致,kernel实现采用同样的CopyIn、Compute、CopyOut三阶段流水。tiling实现分为Tiling模板设计以及TilingData参数设计:Tiling模板设计,本示例使用了5个模板参数,
D_T_X、D_T_Y、D_T_Z分别是指输入x、输入y、输出z的数据类型,TILE_NUM是指每个核上总计算数据分块个数,IS_SPLIT是是否使能数据分块计算,IS_SPLIT为0时TILE_NUM无效。通过模板参数组合替代传统的TilingKey。TilingData参数设计,本示例算子使用了1个tiling参数,
totalLength是指所有核需要计算的数据量总大小。
AddCustomTilingSink
功能与Add一致,kernel实现采用同样的CopyIn、Compute、CopyOut三阶段流水,并通过KERNEL_TASK_TYPE_DEFAULT接口将算子强制指定在AIC、AIV混合场景运行,满足Tiling下沉算子条件。将所有的Tiling函数逻辑单独在add_custom_tiling_sink_tiling.cpp中实现,并通过DEVICE_IMPL_OP_OPTILING接口注册下沉的Tiling函数。LeakyRelu
kernel实现:
Ascend C提供的矢量计算接口LeakyRelu的操作元素为LocalTensor,输入数据需要先搬运进片上存储,然后根据LeakyReLU的计算规则处理,最终结果搬出到外部存储上。
LeakyReluCustom算子的实现流程分为3个基本任务:CopyIn,Compute,CopyOut。CopyIn任务负责将Global Memory上的输入TensorxGm搬运到Local Memory,存储在xLocal,Compute任务负责对xLocal执行LeakyRelu操作,计算结果存储在yLocal中,CopyOut任务负责将输出数据从yLocal搬运至Global Memory上的输出TensoryGm中。tiling实现:
TilingData参数设计,LeakyReluCustomTilingData参数本质上是和并行数据切分相关的参数,本示例算子使用了3个tiling参数:totalLength、tileNum、negativeSlope。totalLength、tileNum与Add算子类似,negativeSlope表示LeakyRelu的负半轴斜率系数,作为计算参数传递给kernel侧。
编译运行
在本样例根目录下执行如下步骤,编译、打包并部署自定义算子包。
配置环境变量
请根据当前环境上CANN开发套件包的安装方式,选择对应配置环境变量的命令。
默认路径,root用户安装CANN软件包
source /usr/local/Ascend/cann/set_env.sh默认路径,非root用户安装CANN软件包
source $HOME/Ascend/cann/set_env.sh指定路径install_path,安装CANN软件包
source ${install_path}/cann/set_env.sh
编译、打包算子并部署
mkdir -p build && cd build cmake .. && make -j binary package ./custom_opp_*.run执行结果如下,说明执行成功。
SUCCESS
【免费下载链接】cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。项目地址: https://gitcode.com/cann/cann-learning-hub
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考