Ascend Pytorch算子适配层开发
Ascend Pytorch算子适配层开发
适配方法
找到和PyTorch算子功能对应的NPU TBE算子,根据算子功能计算出输出Tensor的size,再根据TBE算子原型构造对应的input/output/attr,传递给ACL完成TBE算子的执行。
说明:
TBE算子实现的源文件存放路径由开发套件包Toolkit的安装方式决定:
- 若使用root用户安装,则存放在:/usr/local/Ascend/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe/impl/
- 若使用非root用户安装,则存放在:~/.local/Ascend/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe/impl/
开发者可以通过查看算子实现源文件,确定算子的功能。
存放路径和命名格式
对NPU的TBE算子适配文件保存在pytorch/aten/src/ATen/native/npu目录下,命名风格采用大驼峰,命名格式:<算子名> + <KernelNpu>.cpp,如:AddKernelNpu.cpp。
适配步骤
须知:
适配代码基于C++开发。
- 引入依赖头文件。
2. #include "ATen/native/npu/utils/CalcuOpUtil.h"
3. #include "ATen/native/npu/utils/KernelNpuOutputSize.h"
#include "ATen/native/npu/utils/NpuUtils.h"
说明:
"CalcuOpUtil.h"文件中主要包含与ACL接口相关的函数。
"KernelNpuOutputSize.h"中主要包含算子输出shape的推导函数。
"NpuUtils.h"文件中主要包含公共能力的函数。
- 定义Add算子适配主体函数。
结合native_functions.yaml 中 add算子的分发定义,算子适配中应包含如下函数:
- add_npu_input 构造输入的NPUTensorDesc对象
- add_npu_output 构造输出的NPUTensorDesc对象
- add_npu_attr 构造NPU TBE Add算子attr属性
- add_out_npu 算子适配函数(yaml中npu派发函数,支持传入输出tensor),other参数支持 Tensor & Scalar
- add_npu 算子适配函数(yaml中npu派发函数),other参数支持 Tensor & Scalar
- 实现函数 add_npu_input。
将NPU适配函数(add_npu_input)的输入构造成NPUTensorDesc对象。
// 输入参数为"self": "Tensor"和"other": "Tensor"时,适配函数add_npu_input的实现
SmallVector<NPUTensorDesc, N> add_npu_input(const Tensor& self,const Tensor& other) {
bool isSelfWrapped = CalcuOpUtil::is_scalar_wrapped_to_tensor(self);
bool isOtherWrapped = CalcuOpUtil::is_scalar_wrapped_to_tensor(other);
auto inputs = CalcuOpUtil::create_npu_input_tensor_desc({self, other});
// 't + 2' to work with any type of tensor, not just LongTensor (which is what
// integersin Python represent).
if (isSelfWrapped && (!isOtherWrapped)) {
inputs[0].scalarType = other.scalar_type();
} else if (isOtherWrapped && (!isSelfWrapped)) {
inputs[1].scalarType = self.scalar_type();
}
return inputs;
}
// 输入参数为"self": "Tensor"和"other": "Scalar"时,适配函数add_npu_input的实现
SmallVector<NPUTensorDesc, N> add_npu_input(const Tensor& self,const Scalar& other) {
return CalcuOpUtil::create_npu_input_tensor_desc({self});
}
- 实现函数 add_npu_output。
将函数 add_npu_output的输出tensor对象构造成NPUTensorDesc对象。
// 输出参数为 "Tensor" 时,适配函数add_npu_output的实现
SmallVector<NPUTensorDesc, N> add_npu_output(const Tensor& result) {
return CalcuOpUtil::create_npu_output_tensor_desc({result});
}
说明:
一般来说,算子的输出不需要特殊处理,直接调用CreateNpuOutputTensorDesc即可。
- 实现函数 add_npu_attr。
根据NPU TBE算子原型中所需的attr规格,将参数适配成NPU TBE算子原型所需要的attr属性。
// 输入参数为"other": "Tensor"和"alpha": "Scalar"时,对应的适配函数add_npu_attr实现
SmallVector<NPUAttrDesc, N> add_npu_attr(const Tensor& self, const Tensor& other, Scalar alpha) {
float value = CalcuOpUtil::get_scalar_float_value(alpha);
NPUAttrDesc npuAttrScalar = NPUAttrDesc("alpha", value);
SmallVector<NPUAttrDesc, N> attrs = {npuAttrScalar};
return attrs;
}
// 输入参数为"other": "Scalar"和"alpha": "Scalar"时,对应的适配函数adds_npu_attr实现
SmallVector<NPUAttrDesc, N> adds_npu_attr(const Tensor& self,const Scalar& other,const Scalar& alpha) {
float otherValue = CalcuOpUtil::get_scalar_float_value(other);
float alphaValue = CalcuOpUtil::get_scalar_float_value(alpha);
float value = otherValue * alphaValue;
NPUAttrDesc npuAttrValue = NPUAttrDesc("value", value);
SmallVector<NPUAttrDesc, N> attrs = {npuAttrValue};
return attrs;
}
- 实现函数 add_out_npu。
9. Tensor& add_out_npu(Tensor& result, const Tensor& self, const Tensor& other, Scalar alpha) {
10. if (other.dim() == 0 && !other.is_npu()) {
11. adds_out_npu(result, self, other.item(), alpha);
12. } else if (self.dim() == 0 && !self.is_npu()) {
13. adds_out_npu(result, other, self.item(), alpha);
14. } else {
15. // constructs the input and output NPUTensorDesc
16. auto inputs = add_npu_input(self, other);
17. auto outputs = add_npu_output({result});
18.
19. // constructs the attr of the NPUAttrDesc
20. auto attrs = add_npu_attr(self, other, alpha);
21. // executing the NPU operator
22. CalcuOpUtil::execute_npu_operate("Axpy", inputs, outputs, attrs);
23. }
24.
25. return result;
}
说明:
add_out_npu和add_npu的差别是add_out_npu支持显示指定输出tensor,往输出tensor中写入结果。
- 实现函数 add_npu。
- 定义并实现算子的shape推导函数,根据输入参数计算输出的size。
Shape推导函数定义规范:
"NPU适配函数名称" + "_" + "output" + "_" + "size",如add_npu_output_size();
说明:
- Shape推导函数定义和实现存放在 pytorch/aten/src/ATen/native/npu/utils,对应的头文件和实现在 KernelNpuOutPutSize.h 和 KernelNpuOutPutSize.cpp中。
- 在KernelNpuOutPutSize.h中,函数存放位置按照函数名字排序。
//输入参数为"self": "Tensor"和"other": "Tensor"时,Shape推导该函数
SmallVector<int64_t, SIZE> add_npu_output_size(const Tensor& self,const Tensor& other) {
return broadcast_ops_npu_output_size(self, other); //定义Shape推导函数
}
// 输入参数为"self": "Tensor"和"other": "Scalar"时,Shape推导该函数
IntArrayRef add_npu_output_size(const Tensor& self, const Scalar& other) {
return input_same_output_size(self);
}
说明:
broadcast_ops_npu_output_size函数的作用是:当两个参数符合PyTorch广播机制时,函数会将两个参数自动扩展为相等大小
- 调用对应的shape推导函数计算输出的size。
- 根据输出的size调用at::empty_with_ format创建输出Tensor,函数支持指定输出Tensor的format,默认为NCHW格式。
说明:
当前制定的Format设置规则为重型算子锚点扩散+连续性法则混合规则。
- 重型算子如卷积、Matmul,只支持某种特定format,适配时显示指定为其需要的format,format向周边扩散。
- 而连续性法则指的是算子对格式不敏感,算子format指定为与第一个输入tensor的format相同即可。
- NPU中的卷积只支持NC1HWC0格式,所以需要显式指定为NC1HWC0格式
- 将构造好的输出Tensor和其他参数传给add_out_npu进行运算
e. // 输入参数为"self": "Tensor"和"other": "Tensor"时,对应的适配函数add_npu实现
f. //调用对应的Shape推导函数计算输出的size
g. Tensor add_npu(const Tensor& self, const Tensor& other, Scalar alpha) {
h. Tensor outputTensor = add_dest_output(self, other);
i. auto outputSize = add_npu_output_size(self, other);
j.
k. //根据输出的size调用at::empty_with_format创建输出Tensor,函数支持指定输出Tensor的format,默认为NCHW格式
l. Tensor result = at::empty_with_format(outputSize, outputTensor.options(), CalcuOpUtil::get_tensor_npu_format(outputTensor));
m.
n. //将构造好的输出Tensor和其他参数传给add_out_npu进行运算
o. add_out_npu(result, self, other, alpha);
p. return result;
q. }
r.
s. // 输入参数为"self": "Tensor"和"other": "Scalar"时,对应的适配函数add_npu实现
t. //调用对应的Shape推导函数计算输出的size
u. Tensor add_npu(const Tensor& self, Scalar other, Scalar alpha) {
v. auto outputSize = add_npu_output_size(self, other);
w.
x. //根据输出的size调用at::empty_with_format创建输出Tensor,函数支持指定输出Tensor的format,默认为NCHW格式
y. Tensor result = at::empty_with_format(outputSize, self.options(), CalcuOpUtil::get_tensor_npu_format(self));
z.
aa. //将构造好的输出Tensor和其他参数传给add_out_npu进行运算
bb. adds_out_npu(result, self, other, alpha);
cc. return result;
}
Ascend Pytorch算子适配层开发的更多相关文章
- Ascend Pytorch算子功能验证
Ascend Pytorch算子功能验证 编写测试用例 以add算子为例,测试脚本文件命名为:add_testcase.py.以下示例仅为一个简单的用例实现,具体算子的实现,需要根据算子定义进行完整的 ...
- 使用xshell+xmanager+pycharm搭建pytorch远程调试开发环境
1. 相关软件版本 xshell: xmanager: pycharm: pycharm破解服务器:https://jetlicense.nss.im/ 2. 将相应的软件安装(pojie好) a&g ...
- 深入浅出PyTorch(算子篇)
Tensor 自从张量(Tensor)计算这个概念出现后,神经网络的算法就可以看作是一系列的张量计算.所谓的张量,它原本是个数学概念,表示各种向量或者数值之间的关系.PyTorch的张量(torch. ...
- 昇思MindSpore全场景AI框架 1.6版本,更高的开发效率,更好地服务开发者
摘要:本文带大家快速浏览昇思MindSpore全场景AI框架1.6版本的关键特性. 全新的昇思MindSpore全场景AI框架1.6版本已发布,此版本中昇思MindSpore全场景AI框架易用性不断改 ...
- 带你学习MindSpore中算子使用方法
摘要:本文分享下MindSpore中算子的使用和遇到问题时的解决方法. 本文分享自华为云社区<[MindSpore易点通]算子使用问题与解决方法>,作者:chengxiaoli. 简介 算 ...
- pytorch 入门指南
两类深度学习框架的优缺点 动态图(PyTorch) 计算图的进行与代码的运行时同时进行的. 静态图(Tensorflow <2.0) 自建命名体系 自建时序控制 难以介入 使用深度学习框架的优点 ...
- 深度学习框架PyTorch一书的学习-第五章-常用工具模块
https://github.com/chenyuntc/pytorch-book/blob/v1.0/chapter5-常用工具/chapter5.ipynb 希望大家直接到上面的网址去查看代码,下 ...
- Halcon算子学习
* dev前缀的算子表示开发编译器相关的函数 dev_update_window ('off')//halcon编辑器图形输出界面参数是否都更新,off表示手动,on表示全部更新 dev_close_ ...
- 那些最全面的Windows10安装pytorch踩过的坑以及如何应用
那些最全面的Windows10安装pytorch踩过的坑以及如何应用 一.pytorch简介 2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch.它是一个基 ...
随机推荐
- 测开需要熟悉的Linux基本知识
安装软件-编辑 yum -y install 安装软件-上传下载 yum -y install lrzsz 查看当前目录 ls 查看所有 ls -a 创建一个文件夹 mkdir Foldername ...
- flex 的 三个参数 flex:1 0 auto
flex :flex-group flex-shirk flex-basis ①.flex-group 剩余空间索取 默认值为0,不索取 eg:父元素400,子元素A为100px,B为200px. ...
- Thinkphp5之ajax分页实现_paginate()参数详细
Thinkphp5 做数据搜索需要带关键词分页,如何将查询条件带入到分页中,本文详细介绍Thinkphp5 分页带参数 一.基本使用方法: $list = Db::name('user')->w ...
- Android的so注入( inject)和函数Hook(基于got表) - 支持arm和x86
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/53942648 前面深入学习了古河的Libinject注入Android进程,下面来 ...
- Python中对序列数据的汇总(collections模块)
目录 Counter most_common 对于序列如字符串str.列表list和tuple可以统计里面数据出现的次数.我们使用的是 collections 模块. collections模块的常用 ...
- Ubuntu Linux DNS服务器 BIND9配置文件命令介绍
BIND9配置方法 转载▼ 配置语法 named.conf acl 定义访问控制列表 controls 定义rndc命令使用的控制通道,若省略,则只允许经过rndc.key认证的127.0.0 ...
- 【python】Leetcode每日一题-丑数
[python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...
- 【dependencyManagement版本管理】dependencies.dependency.version is missing
maven 的gav的v(版本问题) 报错dependencies.dependency.version is missing 出现的场景 一个项目中有多个模块 父模块中出现dependencies. ...
- python多进程、多线程服务器和客户端的简单实现
使用了多进程的服务器: from SocketServer import TCPServer, ForkingMixIn, ThreadingMixIn, StreamRequestHandler c ...
- CSS层叠性
比较id,类,标签的数量 谁多就谁在上面 255个类的权重等于一个id 当权重一样时,以后设置的为准 通过继承而来的,权重为0 !important (设置权重无限大)可以影响权重,但只能影响选中的, ...