news 2026/5/9 17:33:09

CANN/Ascend Boost Comm自定义算子开发示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/Ascend Boost Comm自定义算子开发示例

Ascend Boost Comm 库开发单算子示例

【免费下载链接】ascend-boost-comm算子公共平台,南向对接不同组织开发的算子库,北向支撑不同加速库应用,实现M x N算子能力复用项目地址: https://gitcode.com/cann/ascend-boost-comm

本教程以add算子为例,提供能够基于Ascend Boost Comm运行的算子编写教程.

算子功能

两个输入张量在指定维度相加成一个输出张量。

新增文件

  • examples/ops下新增addcustom目录,该目录下主要存放算子实现部分的代码,文件具体内容见后文,目录结构如下:

    addcustom ├── op_kernel // device侧实现文件(包括核函数入口、以及实现文件) │ └── addcustom.cpp ├── tiling // 新增算子tiling │ ├── addcustom_tiling.cpp // tiling实现核心算法 │ ├── addcustom_tiling.h // 算子tiling接口 │ └── tiling_data.h // tiling和kernel传递结构体tiling_data的定义 ├── CMakeLists.txt // 新增算子编译CMake ├── addcustom_kernel.cpp // 校验 └── addcustom_operation.cpp // shape验证
  • 新增example/include/asdops/params/addcustom.h文件定义Addcustom操作的参数结构体,内容如下:

    #ifndef ATBOPS_PARAMS_ADDCUSTOM_H #define ATBOPS_PARAMS_ADDCUSTOM_H #include <cstdint> #include <string> #include <sstream> #include <mki/utils/SVector/SVector.h> namespace Mki { namespace OpParam { struct Addcustom { bool operator==(const Addcustom &other) const { (void)other; return true; } }; } // namespace OpParam } // namespace Mki #endif

修改文件

  • example/include/asdops/params/params.h中包含新增的头文件example/include/asdops/params/addcustom.h:
    #include "atbops/params/addcustom.h"

环境准备

您可参考环境准备进行编译编译和测试环境搭建,环境准备好之后,就可以开始算子开发之旅。

算子实现

算子实现主要包括:device侧算子实现和host侧tiling实现。

tiling开发

tiling开发的核心概念:TilingDataWorkspaceTilingKeyBlockDim等,可访问术语表-昇腾社区查看。

tiling_data.h

文件路径:example/ops/addcustom/tiling/tiling_data.h
主要功能:描述执行tiling计算所需的结构化数据定义。

#ifndef ASCEND_OPS_ADDCUSTOM_TILING_DATA #define ASCEND_OPS_ADDCUSTOM_TILING_DATA #include <cstdint> namespace Mki { struct AddcustomTilingData { uint32_t totalLength; // 总数据长度 uint32_t tileNum; // Tiling 块数 }; } #endif
addcustom_tiling.h

文件路径:example/ops/addcustom/tiling/addcustom_tiling.h
主要功能:tiling过程主要是完成数据的切分,这里则是函数声明。

#ifndef ASCEND_OPS_ADDCUSTOM_TILING_H #define ASCEND_OPS_ADDCUSTOM_TILING_H #include <mki/launch_param.h> #include <mki/kernel_info.h> #include <mki/utils/status/status.h> namespace Mki { Status AddcustomTiling(const LaunchParam &launchParam, KernelInfo &kernelInfo); } #endif
addcustom_tiling.cpp

文件路径:example/ops/addcustom/tiling/addcustom_tiling.cpp
主要功能:实现切分功能的主体函数(具体函数实现请参考上面文件路径下的内容)

#include "addcustom_tiling.h" #include <mki/utils/assert/assert.h> #include <mki/utils/log/log.h> #include <mki/utils/platform/platform_info.h> #include <mki/utils/math/math.h> #include <mki/utils/SVector/SVector.h> #include "atbops/params/addcustom.h" #include "tiling_data.h" // 定义最小的块长度 constexpr uint32_t MIN_BLOCK_LENGTH = 32; namespace Mki { Status AddcustomTiling(const LaunchParam &launchParam, KernelInfo &kernelInfo) { // 具体函数实现请参考上面文件路径下的内容 } } // namespace Mki

kernel开发

kernel相关的ComputeCopyInCopyOut等概念,可访问术语表-昇腾社区查看。

addcustom.cpp

文件路径:example/ops/addcustom/op_kernel/addcustom.cpp
主要功能:根据tiling信息完成所有数据的搬运和计算。

#include "kernel_operator.h" #include "ops/utils/common/kernel/kernel_utils.h" #include "ops/addcustom/tiling/tiling_data.h" static constexpr uint32_t BUFFER_NUM = 1; static constexpr uint32_t MAX_UB_SIZE = 188 * 1024; // double buffer, 每块94KB共188KB class Addcustom { public: __aicore__ inline Addcustom() {} __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, uint32_t totalLength, uint32_t tileNum) { // 具体函数实现请参考上面文件路径下的内容 } __aicore__ inline void Process() { // 具体函数实现请参考上面文件路径下的内容 } private: __aicore__ inline void CopyIn(int32_t progress) { // 具体函数实现请参考上面文件路径下的内容 } __aicore__ inline void Compute(int32_t progress) { // 具体函数实现请参考上面文件路径下的内容 } __aicore__ inline void CopyOut(int32_t progress) { // 具体函数实现请参考上面文件路径下的内容 } private: AscendC::TPipe pipe; AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX, inQueueY; AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ; AscendC::GlobalTensor<half> xGm; AscendC::GlobalTensor<half> yGm; AscendC::GlobalTensor<half> zGm; uint32_t blockLength; uint32_t tileNum; uint32_t tileLength; }; inline __aicore__ void InitTilingData(const __gm__ uint8_t *p_tilingdata, Mki::AddcustomTilingData *tilingdata) { // 具体函数实现请参考上面文件路径下的内容 } extern "C" __global__ __aicore__ void addcustom(GM_ADDR x, GM_ADDR y, GM_ADDR z, GM_ADDR tiling) { // 具体函数实现请参考上面文件路径下的内容 }
addcustom_kernel.cpp

文件路径:example/ops/addcustom/addcustom_kernel.cpp主要功能:启动device侧实现前,对输入和输出进行了检查,以及进行device侧的初始化。

#include <mki/base/kernel_base.h> #include <mki_loader/op_register.h> #include <mki/utils/assert/assert.h> #include <mki/utils/log/log.h> #include "atbops/params/params.h" #include "ops/addcustom/tiling/addcustom_tiling.h" #include "ops/addcustom/tiling/tiling_data.h" namespace Mki { class AddcustomKernel : public KernelBase { public: explicit AddcustomKernel(const std::string &kernelName, const BinHandle *handle) noexcept : KernelBase(kernelName, handle) { } /* --------- 框架回调 --------- */ bool CanSupport(const LaunchParam &launchParam) const override { // 具体函数实现请参考上面文件路径下的内容 } uint64_t GetTilingSize(const LaunchParam &launchParam) const override { // 具体函数实现请参考上面文件路径下的内容 } Status InitImpl(const LaunchParam &launchParam) override { // 具体函数实现请参考上面文件路径下的内容 } }; /* ---------- 注册到框架 ---------- */ REG_KERNEL_BASE(AddcustomKernel); } // namespace Mki
addcustom_operation.cpp

文件路径:example/ops/addcustom/addcustom_operation.cpp
主要功能:定义 Operation 行为(Operation 是 Ascend Boost Comm 框架下 op 的最高程度抽象),包括选择最佳 kernel 的策略。

#include <mki/base/operation_base.h> #include <mki_loader/op_register.h> #include <mki/utils/log/log.h> #include "atbops/params/params.h" namespace Mki { using namespace Mki; static constexpr int32_t INPUT_NUM = 2; static constexpr int32_t OUTPUT_NUM = 1; class AddcustomOperation : public OperationBase { public: explicit AddcustomOperation(const std::string &opName) noexcept : OperationBase(opName) {} /* ---------- 选择最佳kernel策略 ---------- */ Kernel *GetBestKernel(const LaunchParam &launchParam) const override { // 具体函数实现请参考上面文件路径下的内容 } /* ---------- 张量数量 ---------- */ int64_t GetInputNum(const Any &specificParam) const override { // 具体函数实现请参考上面文件路径下的内容 } int64_t GetOutputNum(const Any &specificParam) const override { // 具体函数实现请参考上面文件路径下的内容 } protected: /* ---------- 形状推导 ---------- */ Status InferShapeImpl(const LaunchParam &launchParam, SVector<Tensor> &outTensors) const override { // 具体函数实现请参考上面文件路径下的内容 } }; /* ---------- 注册 ---------- */ REG_OPERATION(AddcustomOperation); } // namespace Mki

构建与测试

CMakeLists.txt

文件路径:example/ops/addcustom/CMakeLists.txt
主要功能:文件编译。

set(addcustom_srcs ${CMAKE_CURRENT_LIST_DIR}/addcustom_operation.cpp ${CMAKE_CURRENT_LIST_DIR}/addcustom_kernel.cpp ${CMAKE_CURRENT_LIST_DIR}/tiling/addcustom_tiling.cpp ) add_operation(AddcustomOperation "${addcustom_srcs}") add_kernel(addcustom ascend910b vector op_kernel/addcustom.cpp AddcustomKernel)

算子编译与环境变量设置

Ascend Boost Comm仓的构建脚本文件为scripts/build.sh,初次执行算子测试需要先编译测试框架testframework:

bash scripts/build.sh testframework

随后编译example中的算子:

bash scripts/build.sh example

编译后需要设置环境变量:

source output/mki/./set_env.sh

测试

为了对所编写的算子进行测试,可在example/tests/pythontest/optest/目录下新增test_addcustom.py:

import os import unittest import numpy as np import torch import sys import logging sys.path.append(f"{os.environ['MKI_HOME_PATH']}/tests/pythontest") import op_test OP_NAME = "AddcustomOperation" OP_PARAM0 = {"addcustomDim": 0} class TestAddcustom(op_test.OpTest): def golden_calc(self, in_tensors): a = in_tensors[0] b = in_tensors[1] return [a + b] def golden_compare(self, out_tensors, golden_out_tensors): return torch.allclose(out_tensors[0], golden_out_tensors[0], rtol=0.001, atol=0.001) def test_2d_half(self): shape = (2 * 16,) a = torch.randn(shape).to(torch.float16) b = torch.randn(shape).to(torch.float16) self.set_param(OP_NAME, OP_PARAM0) self.execute([a, b], [torch.ones(shape).to(torch.float16)]) if __name__ == '__main__': unittest.main()

通过以下命令执行测试脚本:

python example/tests/pythontest/optest/test_addcustom.py

【免费下载链接】ascend-boost-comm算子公共平台,南向对接不同组织开发的算子库,北向支撑不同加速库应用,实现M x N算子能力复用项目地址: https://gitcode.com/cann/ascend-boost-comm

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 17:33:01

GTA5线上小助手:提升游戏体验的终极免费工具

GTA5线上小助手&#xff1a;提升游戏体验的终极免费工具 【免费下载链接】GTA5OnlineTools GTA5线上小助手 项目地址: https://gitcode.com/gh_mirrors/gt/GTA5OnlineTools GTA5线上小助手是一款专为《侠盗猎车手5》线上模式玩家设计的免费开源工具&#xff0c;旨在帮助…

作者头像 李华
网站建设 2026/5/9 17:32:53

生成式AI早期采纳研究:教育是弥合数字鸿沟的关键

1. 项目概述&#xff1a;当生成式AI撞上旧有的社会断层线ChatGPT横空出世那会儿&#xff0c;我和很多圈内朋友一样&#xff0c;兴奋地讨论着这个“新玩具”能怎么改变我们的工作流。写代码、做策划、处理文档&#xff0c;效率肉眼可见地提升。但很快&#xff0c;一个更现实、也…

作者头像 李华
网站建设 2026/5/9 17:29:30

CANN/pyasc图像加载API

asc.language.basic.load_image_to_local 【免费下载链接】pyasc 本项目为Python用户提供算子编程接口&#xff0c;支持在昇腾AI处理器上加速计算&#xff0c;接口与Ascend C一一对应并遵守Python原生语法。 项目地址: https://gitcode.com/cann/pyasc asc.language.bas…

作者头像 李华
网站建设 2026/5/9 17:27:47

华为CANN/hcomm内存注册API

HcommMemReg 【免费下载链接】hcomm HCOMM&#xff08;Huawei Communication&#xff09;是HCCL的通信基础库&#xff0c;提供通信域以及通信资源的管理能力。 项目地址: https://gitcode.com/cann/hcomm 产品支持情况 Ascend 950PR/Ascend 950DT&#xff1a;支持Atlas…

作者头像 李华
网站建设 2026/5/9 17:21:34

Android 离线肤质分析

Android 离线肤质分析 Demo 这是一套完全离线运行的 Android 肤质分析 MVP Demo。 项目地址&#xff1a;肤质分析https://gitcode.com/mushike/skin 功能 支持选择相册照片分析支持拍照体验分析全流程本地运行&#xff0c;不上传服务器使用 ML Kit bundled Face Detection 检…

作者头像 李华