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

CANN/asc-devkit LeakyRelu API文档

LeakyRelu

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

产品支持情况

产品

是否支持

Ascend 950PR/Ascend 950DT

Atlas A3 训练系列产品 / Atlas A3 推理系列产品

Atlas A2 训练系列产品 / Atlas A2 推理系列产品

Kirin X90

Kirin 9030

功能说明

按元素执行Leaky ReLU(Leaky Rectified Linear Unit)操作,计算公式如下:

Leaky ReLU带泄露线性整流函数是一种人工神经网络中常用的激活函数,其数学表达式为:

和ReLU的区别是:ReLU是将所有的负值都设为零,而Leaky ReLU是给所有负值赋予一个斜率。下图表示了Relu和Leaky ReLU的区别:

函数原型

  • tensor前n个数据计算

    template <typename T, bool isSetMask = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, const int32_t& count)
  • tensor高维切分计算

    • mask逐bit模式

      template <typename T, bool isSetMask = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
    • mask连续模式

      template <typename T, bool isSetMask = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)

dst和src使用TensorTrait类型时,其数据类型TensorTrait和scalarValue的数据类型(对应TensorTrait中的LiteType类型)不一致。因此新增模板类型U表示scalarValue的数据类型,并通过std::enable_if检查T中萃取出的LiteType和U是否完全一致,一致则接口通过编译,否则编译失败。接口原型定义如下:

  • tensor前n个数据计算

    template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, const int32_t& count)
  • tensor高维切分计算

    • mask逐bit模式

      template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
    • mask连续模式

      template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)

参数说明

表 1模板参数说明

参数名

描述

T

操作数数据类型。

Atlas A2 训练系列产品 / Atlas A2 推理系列产品,支持的数据类型为:half、float。

Atlas A3 训练系列产品 / Atlas A3 推理系列产品,支持的数据类型为:half、float。

Ascend 950PR/Ascend 950DT,支持的数据类型为:half、float。

Kirin X90,支持的数据类型为:half、float。

Kirin 9030,支持的数据类型为:half、float。

U

scalarValue数据类型。

Atlas A2 训练系列产品 / Atlas A2 推理系列产品,支持的数据类型为:half、float。

Atlas A3 训练系列产品 / Atlas A3 推理系列产品,支持的数据类型为:half、float。

Ascend 950PR/Ascend 950DT,支持的数据类型为:half、float。

Kirin X90,支持的数据类型为:half、float。

Kirin 9030,支持的数据类型为:half、float。

isSetMask

是否在接口内部设置mask模式和mask值。

针对以下型号,tensor前n个数据计算API中的isSetMask参数不生效,保持默认值即可。

表 2参数说明

参数名称

类型

说明

dst

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

LocalTensor的起始地址需要32字节对齐。

src

输入

源操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

LocalTensor的起始地址需要32字节对齐。

数据类型需要与目的操作数保持一致。

scalarValue

输入

源操作数,数据类型需要与目的操作数Tensor中的元素保持一致。

count

输入

参与计算的元素个数。

mask/mask[]

输入

mask用于控制每次迭代内参与计算的元素。

  • 连续模式:表示前面连续的多少个元素参与计算。取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。当操作数为16位时,mask∈[1, 128];当操作数为32位时,mask∈[1, 64];当操作数为64位时,mask∈[1, 32]。

repeatTime

输入

重复迭代次数。 矢量计算单元,每次读取连续的256Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTime表示迭代的次数。

repeatParams

输入

元素操作控制结构信息,具体请参考UnaryRepeatParams。

返回值说明

约束说明

  • 操作数地址对齐要求请参见通用地址对齐约束。
  • 操作数地址重叠约束请参考通用地址重叠约束。

调用示例

更多样例可参考LINK。

  • tensor高维切分计算样例-mask连续模式

    #include "kernel_operator.h" class KernelBinaryScalar { public: __aicore__ inline KernelBinaryScalar() {} __aicore__ inline void Init(__gm__ uint8_t* src, __gm__ uint8_t* dstGm) { srcGlobal.SetGlobalBuffer((__gm__ half*)src); dstGlobal.SetGlobalBuffer((__gm__ half*)dstGm); pipe.InitBuffer(inQueueSrc, 1, 512 * sizeof(half)); pipe.InitBuffer(outQueueDst, 1, 512 * sizeof(half)); } __aicore__ inline void Process() { CopyIn(); Compute(); CopyOut(); } private: __aicore__ inline void CopyIn() { AscendC::LocalTensor<half> srcLocal = inQueueSrc.AllocTensor<half>(); AscendC::DataCopy(srcLocal, srcGlobal, 512); inQueueSrc.EnQue(srcLocal); } __aicore__ inline void Compute() { AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>(); AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>(); uint64_t mask = 128; half scalar = 2; // repeatTime = 4, 128 elements one repeat, 512 elements total // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride =8, no gap between repeats AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8}); outQueueDst.EnQue<half>(dstLocal); inQueueSrc.FreeTensor(srcLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensor<half> dstLocal = outQueueDst.DeQue<half>(); AscendC::DataCopy(dstGlobal, dstLocal, 512); outQueueDst.FreeTensor(dstLocal); } private: AscendC::TPipe pipe; AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrc; AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueDst; AscendC::GlobalTensor<half> srcGlobal, dstGlobal; }; extern "C" __global__ __aicore__ void binary_scalar_simple_kernel(__gm__ uint8_t* src, __gm__ uint8_t* dstGm) { KernelBinaryScalar op; op.Init(src, dstGm); op.Process(); }
  • tensor高维切分计算样例-mask逐bit模式

    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; half scalar = 2; // repeatTime = 4, 单次迭代处理128个数,计算512个数需要迭代4次 // dstBlkStride, srcBlkStride = 1, 每个迭代内src0参与计算的数据地址间隔为1个datablock,表示单次迭代内数据连续读取和写入 // dstRepStride, srcRepStride = 8, 相邻迭代间的地址间隔为8个datablock,表示相邻迭代间数据连续读取和写入 AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});
  • tensor前n个数据计算样例

    half scalar = 2; AscendC::LeakyRelu(dstLocal, srcLocal, scalar, 512);

结果示例如下:

输入数据src0Local:[1. 2. 3. ... 512.] 输入数据scalar = 2 输出数据dstLocal:[1. 2. 3. ... 512.]

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

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

http://www.jsqmd.com/news/795951/

相关文章:

  • Visual C++运行库一键修复方案:解决软件依赖问题的终极指南
  • 从零开始:手把手教你用OpenCore打造完美黑苹果系统
  • 房屋租赁|房屋出租|房屋租赁系统|基于Springboot的房屋租赁系统设计与实现(源码+数据库+文档)
  • 2026 津门展陈新标杆:优质展厅设计搭建公司实力解码 - 资讯焦点
  • 手把手教你用STM32F103C8T6(正点原子mini板)驱动SHT31温湿度传感器(附完整工程)
  • Gadfly.jl自定义几何元素开发:轻松扩展图形语法功能的完整指南
  • 120MHz Cortex-M3+150DMIPS+ART加速器:STM32F205RBT6的性能参数解析
  • 江苏B端营销变局:GEO时代来临,为何“本土化”服务商正取代“通用型”巨头? - 资讯焦点
  • Origin Pro 2023 保姆级教程:从数据导入到论文配图,手把手教你搞定科研绘图
  • 《QGIS空间数据处理与高级制图》004:内置地理处理工具箱
  • 别再手动改模型名了!用LaTeX的\newcommand命令,5分钟搞定论文变量定义
  • AD域组策略更新报错8007071a?手把手教你用防火墙规则搞定Win7/Win10远程RPC
  • Chapter核心功能深度解析:从章节管理到活动策划的全流程教程
  • 2026培训系统怎么选?选型指南全解析 - 资讯焦点
  • 当点云遇上核技巧:一文搞懂K-PCA为何能处理非线性数据(附Sklearn对比实验)
  • CANN/ops-nn RMS归一化动态量化算子
  • 终极解决方案:如何用VisualCppRedist AIO一键修复Windows运行库问题
  • 2026年上海厨房卫生间改造哪家好?最新权威TOP5实测推荐 - 资讯焦点
  • Sherpa-Onnx:跨平台离线语音处理技术的革命性突破
  • 别再只用split了!Python字符串转列表的3种实战场景与性能对比(含LeetCode真题)
  • 储能出海架构重构:摒弃传统x86工控机,基于ARM边缘节点的EMS策略下沉实战
  • CAN总线终端电阻:从120Ω与0.25W的选型,看信号完整性与系统鲁棒性设计
  • 3分钟掌握Windows界面自定义神器:让你的桌面焕然一新
  • m4s-converter:B站缓存视频转换终极指南,快速实现m4s到MP4的无损转换
  • CANN/GE AIPP内存获取API
  • 图神经网络终于能“上生产”了?SITS 2026发布首个支持实时增量训练的AI原生图引擎(附Benchmark对比:吞吐提升6.8×,延迟压至12ms)
  • 娱乐圈天降紫微星终结乱象,海棠山铁哥终结资源咖霸屏时代
  • 5分钟搞定!iperf3 Windows版:专业网络性能测试工具完全指南
  • DSU-Sideloader核心架构解析:深入理解Android动态系统更新的实现原理
  • 别再只用翻转和裁剪了!盘点CV项目中那些真正提升模型泛化能力的数据增强技巧(附PyTorch代码)