当前位置: 首页 > news >正文

华为Ascend C算子开发(中级)考试

华为Ascend C算子开发(中级)考试题

提示:这个是河北廊坊Ascend C算子开发考试题和答案,仅供参考,因为不确定其他城市的考试题是否也是一样


文章目录

  • 华为Ascend C算子开发(中级)考试题
  • 一、op_host文件夹下的sinh_custom_tiling.h文件
  • 二、op_host文件夹下的sinh_custom.cpp文件
  • 三、op_kernel文件夹下的sinh_custom.cpp文件


一、op_host文件夹下的sinh_custom_tiling.h文件

请添加图片描述

#include "register/tilingdata_base.h"
namespace optiling {
BEGIN_TILING_DATA_DEF(SinhCustomTilingData)//考生自行定义 tiling 结构体成员变量
TILING_DATA_FIELD_DEF(uint32_t, totalLength);
TILING_DATA_FIELD_DEF(uint32_t, tileNum);
END_TILING_DATA_DEF;
REGISTER_TILING_DATA_CLASS(SinhCustom, SinhCustomTilingData)
}

二、op_host文件夹下的sinh_custom.cpp文件

请添加图片描述

#include "sinh_custom_tiling.h"
#include "register/op_def_registry.h"
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{SinhCustomTilingData tiling;//考生自行填充const uint32_t BLOCK_DIM = 8;const uint32_t TILE_NUM = 8;uint32_t totalLength = context->GetInputShape(0)->GetOriginShape().GetShapeSize();context->SetBlockDim(BLOCK_DIM);tiling.set_totalLength(totalLength);tiling.set_tileNum(TILE_NUM);tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), 
context->GetRawTilingData()->GetCapacity());context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());size_t *currentWorkspace = context->GetWorkspaceSizes(1);currentWorkspace[0] = 0;return ge::GRAPH_SUCCESS;
}
}
namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{const gert::Shape* x1_shape = context->GetInputShape(0);gert::Shape* y_shape = context->GetOutputShape(0);*y_shape = *x1_shape;return GRAPH_SUCCESS;
}
}
namespace ops {
class SinhCustom : public OpDef {
public:explicit SinhCustom(const char* name) : OpDef(name){this->Input("x").ParamType(REQUIRED).DataType({ge::DT_FLOAT16}).Format({ge::FORMAT_ND}).UnknownShapeFormat({ge::FORMAT_ND});this->Output("y").ParamType(REQUIRED).DataType({ge::DT_FLOAT16}).Format({ge::FORMAT_ND}).UnknownShapeFormat({ge::FORMAT_ND});this->SetInferShape(ge::InferShape);this->AICore().SetTiling(optiling::TilingFunc);this->AICore().AddConfig("ascend310b");}
};
OP_ADD(SinhCustom);
}

三、op_kernel文件夹下的sinh_custom.cpp文件

请添加图片描述

#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;
class KernelSinh {
public:__aicore__ inline KernelSinh() {}__aicore__ inline void Init(GM_ADDR x, GM_ADDR y, uint32_t totalLength, uint32_t 
tileNum){//考生补充初始化代码ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");this->blockLength = totalLength / GetBlockNum();this->tileNum = tileNum;ASSERT(tileNum != 0 && "tile num can not be zero!");this->tileLength = this->blockLength / tileNum / BUFFER_NUM;xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x + this->blockLength * GetBlockIdx(), 
this->blockLength);yGm.SetGlobalBuffer((__gm__ DTYPE_Y *)y + this->blockLength * GetBlockIdx(), 
this->blockLength);pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Y));pipe.InitBuffer(tmpBuffer1, this->tileLength * sizeof(DTYPE_X));pipe.InitBuffer(tmpBuffer2, this->tileLength * sizeof(DTYPE_X));pipe.InitBuffer(tmpBuffer3, this->tileLength * sizeof(DTYPE_X));pipe.InitBuffer(tmpBuffer4, this->tileLength * sizeof(DTYPE_X));}__aicore__ inline void Process(){//考生补充对“loopCount”的定义,注意对 Tiling 的处理int32_t loopCount = this->tileNum * BUFFER_NUM;for (int32_t i = 0; i < loopCount; i++) {CopyIn(i);Compute(i);CopyOut(i);}}
private:__aicore__ inline void CopyIn(int32_t progress){//考生补充算子代码LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);inQueueX.EnQue(xLocal);}__aicore__ inline void Compute(int32_t progress){//考生补充算子计算代码LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();LocalTensor<DTYPE_X> tmpTensor1 = tmpBuffer1.Get<DTYPE_X>();LocalTensor<DTYPE_X> tmpTensor2 = tmpBuffer2.Get<DTYPE_X>();LocalTensor<DTYPE_X> tmpTensor3 = tmpBuffer3.Get<DTYPE_X>();LocalTensor<DTYPE_X> tmpTensor4 = tmpBuffer4.Get<DTYPE_X>();DTYPE_X inputVal1 = -1;DTYPE_X inputVal2 = 0.5;//sinh(x) = (exp(x) - exp(-x)) / 2.0Muls(tmpTensor1, xLocal, inputVal1, this->tileLength);Exp(tmpTensor2, tmpTensor1, this->tileLength);Exp(tmpTensor3, xLocal, this->tileLength);Sub(tmpTensor4, tmpTensor3, tmpTensor2, this->tileLength);Muls(yLocal, tmpTensor4, inputVal2, this->tileLength);outQueueY.EnQue<DTYPE_Y>(yLocal);inQueueX.FreeTensor(xLocal);}__aicore__ inline void CopyOut(int32_t progress){//考生补充算子代码LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();DataCopy(yGm[progress * this->tileLength], yLocal, this->tileLength);outQueueY.FreeTensor(yLocal);}
private:TPipe pipe;//create queue for input, in this case depth is equal to buffer numTQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;//create queue for output, in this case depth is equal to buffer numTQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;GlobalTensor<half> xGm;GlobalTensor<half> yGm;//考生补充自定义成员变量TBuf<QuePosition::VECCALC> tmpBuffer1, tmpBuffer2, tmpBuffer3, tmpBuffer4;uint32_t blockLength;uint32_t tileNum;uint32_t tileLength;
};
extern "C" __global__ __aicore__ void sinh_custom(GM_ADDR x, GM_ADDR y, GM_ADDR 
workspace, GM_ADDR tiling) {GET_TILING_DATA(tiling_data, tiling);KernelSinh op;//补充 init 和 process 函数调用内容op.Init(x, y, tiling_data.totalLength, tiling_data.tileNum);op.Process();
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • web网站组成
  • 《华为数据之道》读书笔记六---面向自助消费的数据服务建设
  • powershell自定义命令别名
  • pglogical扩展的基本用法介绍
  • leetcode日记(51)不同路径Ⅱ
  • 数据治理之“财务一张表”
  • python-爬虫实例(1):获取京东商品评论
  • linux-conda环境安装配置教程
  • Angular哪些方法适合写在constructor中?
  • 总结一些vue3小知识3
  • Spring IoC控制反转思想 DI依赖注入(五大注解+一个方法注解)
  • 人工智能与机器学习原理精解【3】
  • 关卡1-2:Python关卡
  • Python练手小项目
  • 概率论三大分布
  • echarts花样作死的坑
  • Java 内存分配及垃圾回收机制初探
  • JS题目及答案整理
  • Leetcode 27 Remove Element
  • Redux系列x:源码分析
  • Ruby 2.x 源代码分析:扩展 概述
  • Spring Boot MyBatis配置多种数据库
  • vue--为什么data属性必须是一个函数
  • Webpack 4 学习01(基础配置)
  • 阿里云Kubernetes容器服务上体验Knative
  • 从tcpdump抓包看TCP/IP协议
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 盘点那些不知名却常用的 Git 操作
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 算法-插入排序
  • 通过npm或yarn自动生成vue组件
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 与 ConTeXt MkIV 官方文档的接驳
  • 关于Android全面屏虚拟导航栏的适配总结
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • #{}和${}的区别?
  • #android不同版本废弃api,新api。
  • (¥1011)-(一千零一拾一元整)输出
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (java)关于Thread的挂起和恢复
  • (Java企业 / 公司项目)点赞业务系统设计-批量查询点赞状态(二)
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (动态规划)5. 最长回文子串 java解决
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (六)软件测试分工
  • (三)SvelteKit教程:layout 文件
  • (四十一)大数据实战——spark的yarn模式生产环境部署
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • (转载)PyTorch代码规范最佳实践和样式指南
  • *算法训练(leetcode)第三十九天 | 115. 不同的子序列、583. 两个字符串的删除操作、72. 编辑距离
  • .naturalWidth 和naturalHeight属性,
  • .net core docker部署教程和细节问题
  • .net mvc 获取url中controller和action