资讯动态

CANN/asc-devkit LeakyRelu API文档

发布时间:2026/8/20 12:15:58 来源:尧图企业网站定制
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 ReLULeaky 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 LocalTensorT dst, const LocalTensorT src, const T scalarValue, const int32_t count)tensor高维切分计算mask逐bit模式template typename T, bool isSetMask true __aicore__ inline void LeakyRelu(const LocalTensorT dst, const LocalTensorT 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 LocalTensorT dst, const LocalTensorT 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_ifStd::is_samePrimTT, U::value, bool::type true __aicore__ inline void LeakyRelu(const LocalTensorT dst, const LocalTensorT src, const U scalarValue, const int32_t count)tensor高维切分计算mask逐bit模式template typename T, typename U, bool isSetMask true, typename Std::enable_ifStd::is_samePrimTT, U::value, bool::type true __aicore__ inline void LeakyRelu(const LocalTensorT dst, const LocalTensorT 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_ifStd::is_samePrimTT, U::value, bool::type true __aicore__ inline void LeakyRelu(const LocalTensorT dst, const LocalTensorT 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。UscalarValue数据类型。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值。true表示在接口内部设置。tensor高维切分计算API/tensor前n个数据计算API内部使用了mask的Normal模式/Counter模式一般情况下保持isSetMask默认值即可表示在API内部进行根据开发者传入的mask/count参数进行mask模式和mask值的设置。false表示在接口外部设置。针对tensor高维切分计算接口对性能要求较高的部分场景下开发者需要使用SetMaskNorm/SetMaskCount设置mask模式并通过SetVectorMask接口设置mask值。本接口入参中的mask值必须设置为MASK_PLACEHOLDER。针对tensor前n个数据计算接口对性能要求较高的部分场景下开发者需要使用SetMaskCount设置mask模式为Counter模式并通过SetVectorMask接口设置mask值。本接口入参中的count不生效建议设置成1。针对以下型号tensor前n个数据计算API中的isSetMask参数不生效保持默认值即可。针对Ascend 950PR/Ascend 950DT表 2参数说明参数名称类型说明dst输出目的操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。LocalTensor的起始地址需要32字节对齐。src输入源操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。LocalTensor的起始地址需要32字节对齐。数据类型需要与目的操作数保持一致。scalarValue输入源操作数数据类型需要与目的操作数Tensor中的元素保持一致。count输入参与计算的元素个数。mask/mask[]输入mask用于控制每次迭代内参与计算的元素。逐bit模式可以按位控制哪些元素参与计算bit位的值为1表示参与计算0表示不参与。mask为数组形式数组长度和数组元素的取值范围和操作数的数据类型有关。当操作数为16位时数组长度为2mask[0]、mask[1]∈[0, 264-1]并且不同时为0当操作数为32位时数组长度为1mask[0]∈(0, 264-1]当操作数为64位时数组长度为1mask[0]∈(0, 232-1]。例如mask[8, 0]80b1000表示仅第4个元素参与计算。连续模式表示前面连续的多少个元素参与计算。取值范围和操作数的数据类型有关数据类型不同每次迭代内能够处理的元素个数最大值不同。当操作数为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::LocalTensorhalf srcLocal inQueueSrc.AllocTensorhalf(); AscendC::DataCopy(srcLocal, srcGlobal, 512); inQueueSrc.EnQue(srcLocal); } __aicore__ inline void Compute() { AscendC::LocalTensorhalf srcLocal inQueueSrc.DeQuehalf(); AscendC::LocalTensorhalf dstLocal outQueueDst.AllocTensorhalf(); 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.EnQuehalf(dstLocal); inQueueSrc.FreeTensor(srcLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensorhalf dstLocal outQueueDst.DeQuehalf(); AscendC::DataCopy(dstGlobal, dstLocal, 512); outQueueDst.FreeTensor(dstLocal); } private: AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECIN, 1 inQueueSrc; AscendC::TQueAscendC::TPosition::VECOUT, 1 outQueueDst; AscendC::GlobalTensorhalf 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),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价