CANN/asc-devkit LeakyRelu算子文档

📅 2026/7/16 18:57:58
CANN/asc-devkit LeakyRelu算子文档
# 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 推理系列产品支持Atlas 200I/500 A2 推理产品支持Atlas 推理系列产品AI Core支持Atlas 推理系列产品Vector Core不支持Atlas 训练系列产品不支持Kirin X90支持Kirin 9030支持功能说明头文件路径为basic_api/kernel_operator_vec_binary_scalar_intf.h。LeakyRelu属于双目标量类计算接口输入为一个矢量tensor和一个标量scalarLeakyRelu负责将矢量tensor内的每个元素执行Leaky ReLuLeaky Rectified Linear Unit操作。计算公式如下$dst_i \begin{cases} src_i, src_i \geq 0 \ scalarValue \times src_i, src_i 0 \end{cases}$Leaky ReLU带泄露线性整流函数是一种人工神经网络中常用的激活函数其数学表达式为$f(x) \begin{cases} x, x \geq 0 \ \alpha x, x 0 \end{cases}$其中 $\alpha$ 为负斜率对应参数scalarValue。和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操作数数据类型。UscalarValue的数据类型。仅在dst和src使用TensorTrait类型时有效。isSetMask是否在接口内部设置mask。• true表示在接口内部设置mask。• false表示在接口外部设置mask开发者需要使用SetVectorMask接口设置mask值。这种模式下• 针对tensor前n个数据计算接口接口入参中的count不生效建议设置成1。• 针对tensor高维切分计算接口接口入参中的mask值设置为占位符MASK_PLACEHOLDER用于占位无实际含义。表2参数说明参数名输入/输出描述dst输出目的操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。地址对齐约束参考通用地址对齐约束。src输入源操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。地址对齐约束参考通用地址对齐约束。scalarValue输入源操作数数据类型需要与目的操作数中的元素类型保持一致。count输入参与计算的元素个数。关于该参数的具体说明请参考连续计算。mask[]/mask输入mask用于控制每次迭代内参与计算的元素。详细设置参考掩码。repeatTime输入重复迭代次数。矢量计算单元每次读取连续的256Bytes数据进行计算为完成对输入数据的处理必须通过多次迭代repeat才能完成所有数据的读取与计算。repeatTime表示迭代的次数。关于该参数的具体说明请参考高维切分。repeatParams输入控制操作数地址步长的参数。UnaryRepeatParams类型包含操作数相邻迭代间相同DataBlock的地址步长操作数同一迭代内不同DataBlock的地址步长等参数。相邻迭代间的地址步长参数说明请参考repeatStride同一迭代内DataBlock的地址步长参数说明请参考dataBlockStride。数据类型T和U支持的数据类型为half、float。返回值说明无约束说明操作数地址对齐要求请参见通用地址对齐约束。操作数地址重叠约束请参考通用地址重叠约束。当参数count或repeatTime取值为0时该接口的行为如下针对如下型号当参数count或repeatTime取值为0时不会执行计算操作不会对目的操作数进行写入该接口将被视为NOP空操作。Atlas A3 训练系列产品/Atlas A3 推理系列产品Atlas A2 训练系列产品/Atlas A2 推理系列产品针对Ascend 950PR/Ascend 950DT该接口通过VF调用Reg矢量计算API实现兼容当参数count或repeatTime取值为0时软仿行为不保证该接口被视为NOP空操作。对UB空间的占用说明。针对Ascend 950PR/Ascend 950DTtensor高维切分计算占用8KB Unified Buffer。tensor前n个数据连续计算不涉及8KB Unified Buffer的占用。针对Ascend 950PR/Ascend 950DTtensor前n个数据计算API中的isSetMask参数不生效保持默认值即可。调用示例tensor高维切分计算样例-mask连续模式uint64_t mask 128; half scalar 0.001; AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});tensor高维切分计算样例-mask逐bit模式uint64_t mask[2] { UINT64_MAX, UINT64_MAX }; half scalar 0.001; AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});tensor前n个数据计算样例half scalar 0.001; AscendC::LeakyRelu(dstLocal, srcLocal, scalar, 512);结果示例如下输入数据src0Local[-287. 246. -438. 177. 596. -950. -293. 322. ... -900.] 输入数据scalar 0.001 输出数据dstLocal[-0.287 246. -0.438 177. 596. -0.950 -0.293 322. ... -0.900]【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考