【CANN训练营】CANN:AICPU算子开发

在这里插入图片描述


在这里插入图片描述


创建一个LessEqual算子,对标torch.le
https://pytorch.org/docs/1.5.0/torch.html?highlight=torch%20le#torch.le
下载mindstudio免安装版本
https://www.hiascend.com/software/mindstudio/download
clone canndev

cd ~
git clone https://gitee.com/ascend_wuyongkang/canndev.git
cd canndev
./build.sh --aicpu -u -j100

报错
CMake 3.14 or higher is required. You are running version 3.10.2

sudo apt remove --purge cmake 
hash -r 
sudo snap install cmake --classic
 
cmake --version

在这里插入图片描述

export ASCEND_CUSTOM_PATH=$HOME/Ascend/ascend-toolkit/latest

重新执行./build.sh --aicpu -u -j100
find …/…/…/ -name “*”

算子还是太难了。
那我们先参考这个做个单算子调用
https://gitee.com/ascend/samples/wikis/%E8%AE%AD%E7%BB%83%E8%90%A5/CANN%E8%AE%AD%E7%BB%83%E8%90%A5–%E5%8D%95%E7%AE%97%E5%AD%90%E8%B0%83%E7%94%A8

单算子调用

conv2d算子验证
https://gitee.com/ascend/samples/tree/master/cplusplus/level1_single_api/4_op_dev/2_verify_op/acl_execute_conv2d

cd samples/cplusplus/level1_single_api/4_op_dev/2_verify_op/acl_execute_conv2d
export DDK_PATH=$HOME/Ascend/ascend-toolkit/latest
export NPU_HOST_LIB=$DDK_PATH/acllib/lib64/stub
#用于设置python3.7.5库文件路径
export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
#如果用户环境存在多个python3版本,则指定使用python3.7.5版本
export PATH=/usr/local/python3.7.5/bin:$PATH
cd run/out/
atc --singleop=test_data/config/conv2d_tik_op.json --soc_version=Ascend310 --output=op_models


然后就报错了

EZ3003: No supported Ops kernel and engine are found for [Conv2DTik], optype [Conv2DTik].

查了一圈,没查到,只能暂且先放弃了,走下一步

在这里插入图片描述


前面跳过了一步,没有acl文件,猜想是不是因为aclLite没有初始化编译?

cd ${HOME}/samples/cplusplus/common/acllite
make 
make install

貌似还真是第一次编译,不然不会这么大串信息

在这里插入图片描述

最后还是失败了,那我没办法了。

在这里插入图片描述


然后我们屡败屡战,看下面这个高清图像修复,用到了matmul_27648.json算子
https://gitee.com/ascend/samples/tree/master/python/level2_simple_inference/6_other/imageinpainting_hifill

首先看版本,我们的是符合要求的

在这里插入图片描述


安装第三方依赖
https://gitee.com/ascend/samples/tree/master/python/environment

在这里插入图片描述


在这里插入图片描述


这个算子又转换成功了

在这里插入图片描述


与官方文档不太一样,这里可以直接python3.7.5

在这里插入图片描述


效果很不错,真的很高清,不过这个算法是怎么识别到右上角是主角,然后留下他的呢
回到前面的问题,
是不是算子名字变了,但是文档还没改呢?

cd samples/cplusplus/level1_single_api/4_op_dev/2_verify_op/acl_execute_conv2d
cd run/out/

cp test_data/config/conv2d_tik_op.json  test_data/config/Conv2D.json
vi test_data/config/Conv2D.json

atc --singleop=test_data/config/Conv2D.json --soc_version=Ascend310 --output=op_models

在这里插入图片描述

终于成功了

在这里插入图片描述


但是前面那个问题还是没有解决

在这里插入图片描述

在这里插入图片描述


参考:https://www.hiascend.com/document/detail/zh/mindstudio/50RC1/msug/msug_000215.html

使用AICPU算子开发开发方式实现LessEqual算子,对标torch.le

torch.le:https://pytorch.org/docs/stable/generated/torch.le.html
算子原型定义

/**
 * Copyright (C)  2020. Huawei Technologies Co., Ltd. All rights reserved.

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License Version 2.0.You may not use this file except in compliance with the License.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Apache License for more details at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @file add_dsl.h
 *
 * @brief
 *
 * @version 1.0
 *
 */

#ifndef GE_OPS_OP_PROTO_ADDDSL_H_
#define GE_OPS_OP_PROTO_ADDDSL_H_
#include "graph/operator_reg.h"
namespace ge {
    REG_OP(AddDsl)
    .INPUT(x1, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .INPUT(x2,
    TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .OUTPUT(y,
    TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .OP_END_FACTORY_REG(AddDsl)
}

#endif //GE_OPS_OP_PROTO_ADDDSL_H_

lessequal.cc实现

/**
 * Copyright (C)  2020. Huawei Technologies Co., Ltd. All rights reserved.

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License Version 2.0.You may not use this file except in compliance with the License.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Apache License for more details at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @file add_dsl.h
 *
 * @brief
 *
 * @version 1.0
 *
 */

#ifndef GE_OPS_OP_PROTO_ADDDSL_H_
#define GE_OPS_OP_PROTO_ADDDSL_H_
#include "graph/operator_reg.h"
namespace ge {
    REG_OP(AddDsl)
    .INPUT(x1, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .INPUT(x2,
    TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .OUTPUT(y,
    TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
    DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
    DT_COMPLEX64, DT_STRING}))
    .OP_END_FACTORY_REG(AddDsl)
}

#endif //GE_OPS_OP_PROTO_ADDDSL_H_

算子代码实现

cpukernel/impl/lessequal_kernel.h
cpukernel/impl/lessequal_kernel.cc

算子信息库定义

cpukernel/op_info_cfg/aicpu_kernel/reshape_cust.ini

算子适配插件实现

framework/tf_plugin/tensorflow_lessequal_plugin.cc

UT测试

这里遇到了一个问题,就是按照文档来做,右键没有找到New Cases > AI CPU UT Case

在这里插入图片描述


但是就算没有自动生成模板,我们也可以自己写下:
testcases/ut/aicpu_test/lessequal/test_lessequal_impl.cc
testcases/ut/aicpu_test/lessequal/test_lessequal_proto.cc

算子工程编译

连接远程云服务器成功后,进行编译
旧版本需要在130行往后添加代码,我们这次新版本就不用

在这里插入图片描述


单独打开算子工程文件夹,
然后进行编译

在这里插入图片描述


在这里插入图片描述


我在ascendtoolkit的安装路径是/home/HwHiAiUser/Ascend/ascend-toolkit,因此配置环境变量

ASCEND_OPP_PATH=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/opp;
TOOLCHAIN_DIR=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/toolkit/toolchain/hcc; 
ASCEND_TENSOR_COMPILER_INCLUDE=/home/HwHiAiUser/Ascend/ascend-toolkit/latest/include;
ASCEND_AICPU_PATH=/home/HwHiAiUser/Ascend/ascend-toolkit/latest

在这里插入图片描述


貌似我在中文路径下,这就不太行,那么我们改到I盘根目录。

在这里插入图片描述

这样就大题实现了算子功能了

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340