自定义算子工程编译、打包和部署样例【免费下载链接】cann-learning-hubCANN 学习中心仓支持在线互动运行、边学边练提供教程、示例与优化方案一站式助力昇腾开发者快速上手。项目地址: https://gitcode.com/cann/cann-learning-hub概述本样例以简单的自定义算子为示例展示了其编译、打包成自定义算子包并部署到CANN环境中的流程。支持的产品本样例支持如下产品型号Ascend 950PR/Ascend 950DTAtlas 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 x0 \end{cases} $$ 其中a为scalar值。算子规格描述Add算子类型(OpType)Add算子输入nameshapedata typeformatx8 * 2048floatNDy8 * 2048floatND算子输出z8 * 2048floatND核函数名add_custom, add_custom_tiling_sinkAddCustomTemplate算子类型(OpType)Add算子输入nameshapedata typeformatx8 * 2048floatNDy8 * 2048floatND算子输出z8 * 2048floatND核函数名add_template_custom模板参数templatetypename D_T_X, typename D_T_Y, typename D_T_Z, int TILE_NUM, int IS_SPLITD_T_Xtypename数据类型(float16float)D_T_Ytypename数据类型(float16float)D_T_Ztypename数据类型(float16float)TILE_NUMint切分数量IS_SPLITint是否切分LeakyRelu算子类型(OpType)LeakyRelu算子输入nameshapedata typeformatx8 * 200 * 1024floatNDnegative_slope0.0floatND算子输出y8 * 200 * 1024floatND核函数名leaky_relu_custom代码实现介绍Addkernel实现Ascend C提供的矢量计算接口Add的操作元素都为LocalTensor输入数据需要先搬运进片上存储然后使用计算接口完成两个输入参数相加得到最终结果再搬出到外部存储上。Add算子的实现流程分为3个基本任务CopyInComputeCopyOut。CopyIn任务负责将Global Memory上的输入TensorxGm和yGm搬运到Local Memory分别存储在xLocal、yLocalCompute任务负责对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函数。LeakyRelukernel实现Ascend C提供的矢量计算接口LeakyRelu的操作元素为LocalTensor输入数据需要先搬运进片上存储然后根据LeakyReLU的计算规则处理最终结果搬出到外部存储上。LeakyReluCustom算子的实现流程分为3个基本任务CopyInComputeCopyOut。CopyIn任务负责将Global Memory上的输入TensorxGm搬运到Local Memory存储在xLocalCompute任务负责对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),仅供参考