当前位置:网站首页>【CANN文档速递06期】初识TBE DSL算子开发
【CANN文档速递06期】初识TBE DSL算子开发
2022-06-24 18:58:00 【华为云】

昇腾开发者可以基于TBE(Tensor Boost Engine)提供的python接口进行自定义算子开发,TBE算子开发有DSL ( Domain-Specific Language )和TIK ( Tensor Iterator Kernel )两种方式,两种方式的适用场景及优缺点如下所示:

本期我们主要介绍较容易上手的DSL算子开发方式。
DSL功能框架

- 开发者调用DSL接口进行计算逻辑的描述,指明算子的计算方法和步骤。
- 计算逻辑开发完后,开发者可调用Auto Schedule接口启动自动调度,自动调度时TBE根据计算类型自动选择合适的调度模板,完成数据切块和数据流向的划分,确保在硬件执行上达到最优。调度完成后,会生成类似于TVM的IR(Intermediate Representation)中间表示。
- IR生成后,Pass会自动对生成的IR进行编译优化,优化的方式有双缓冲(Double Buffer)、流水线(Pipeline)同步、内存分配管理、指令映射等。
- 算子经Pass处理后,会自动由CodeGen生成类C代码的临时文件,这个临时文件再通过编译器生成算子的二进制文件,可被网络模型直接加载调用。
DSL算子代码实现
在实现算子代码之前需要分析算子的数学表达式,输入、输出,明确需要调用的DSL接口,然后进行算子实现代码的开发。基于DSL的代码实现流程如下图所示:

算子实现的代码结构如下所示:

下面我们以实现两输入shape相同、数据类型为“float32”的Add算子为例,讲述DSL算子实现的代码流程。
1. 首先引入开发时依赖的Python模块
常用的Python模块如下所示:
- “tbe.dsl”:包含TBE DSL的计算接口、调度接口以及编译接口
- “tbe.tvm”:TBE是基于TVM框架扩展而来的,开发者在实现算子的时候可以使用TVM接口
- “tbe.common.utils.para_check”:TBE提供的算子参数校验接口
- “tbe.common.utils.shape_util”:TBE提供的算子shape处理接口
2. 声明算子接口
算子接口定义函数中包含算子的输入输出信息以及内核名称。
如下所示为Add算子的定义:

上述示例中,add为算子的type,input_x、input_y为算子的输入输出tensor,采用字典的形式定义,包含shape、ori_shape、format、ori_format与dtype信息,kernel_name为算子在内核中的名称,与算子type保持一致即可。
开发者在定义算子接口函数时可以使用TBE提供的参数校验接口check_input_type校验算子的参数类型是否合法,check_input_type为装饰器函数,使用方法如下所示:

当然,您也可以自定义实现相关参数的校验功能,基本的参数校验有助于在算子编译阶段提前发现问题。
3. 对输入tensor进行占位

获取输入数据的shape、dtype(此示例为float32的固定数据类型),使用TVM的placeholder接口对输入tensor进行占位,返回一个tensor对象,此位置中的数据在程序运行时才被指定。
4. 进行计算逻辑的实现
算子的计算逻辑可以通过TBE的DSL计算接口实现,例如Add算子可以通过DSL的vadd接口实现input_x与input_y的相加操作。

5. 调度与编译
计算逻辑实现完成后,需要调用auto_schedule接口,自动生成相应的调度;然后调用build接口进行算子的编译,编译出算子专用内核。

其中config为编译参数配置的map,配置信息包括是否需要打印IR、算子内核名称以及输入、输出张量。
恭喜您,至此您已经完成了Add算子实现代码的开发。
更多介绍
以上仅对DSL算子开发的关键代码进行了简要介绍,更多算子实现时的细节及技巧可登录昇腾社区,阅读相关文档:https://www.hiascend.com/

昇腾CANN文档中心致力于为开发者提供更优质的内容和更便捷的开发体验,助力CANN开发者共建AI生态。任何意见和建议都可以在昇腾社区反馈,您的每一份关注都是我们前进的动力。
边栏推荐
- Real time rendering: the difference between real-time, offline, cloud rendering and hybrid rendering
- Redis error: -bash: redis cli: command not found
- cdc+mysql connector从维表中join的日期时间字段会被+8:00,请问阿里云托管的
- 敏捷之道 | 敏捷开发真的过时了么?
- Introduction: continuously update the self-study version of the learning manual for junior test development engineers
- R for Data Science (notes) -- data transformation (used by filter)
- cdc sql表里面的datetime要用什么类型替换
- 物联网?快来看 Arduino 上云啦
- How to deal with the problem that the Flink CDC reads MySQL in full and always reports this error
- SQL export CSV data, unlimited number of entries
猜你喜欢

STM32 uses time delay to realize breathing lamp register version

Huawei machine learning service speech recognition function enables applications to paint "sound" and color

What are the functions of IBPs open source form designer?

LCD1602 string display (STM32F103)

怎么使用R包ggtreeExtra绘制进化树

php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载

Drawing DEM with GEE gracefully

Starring develops httpjson access point + Database

敏捷之道 | 敏捷开发真的过时了么?

Camera module and hardware interface of Camera1 camera
随机推荐
Landcover100, planned land cover website
Oracle create tablespaces and tables
Error in Android connection database query statement
数字孪生行业案例:智慧港口数字化
技术实现 | Apache Doris 冷热数据存储(一)
Win7 10 tips for installing Office2010 five solutions for installing MSXML components
What about the Golden Angel of thunder one? Golden Angel mission details
Camera module and hardware interface of Camera1 camera
Kubernetes cluster deployment
Xiaobai, let me ask you guys, is MySQL binlog extracted by CDC in strict order
The group offsets of the Kafka of the Flink SQL. If the specified groupid is not mentioned
Eureka source code shallow reading - automatic fault removal
The agile way? Is agile development really out of date?
finkcdc支持sqlserver2008么?
Module V
Methods for comparing float types in the kernel
Unityshader world coordinates do not change with the model
Will the CDC read out of order when I use SQL
请问一下2.2.0版本支持动态新增mysql同步表吗
Internet of things? Come and see Arduino on the cloud