LeakyRelu
产品支持情况
功能说明
按元素执行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模板参数说明
表 2参数说明
返回值说明
无
约束说明
- 操作数地址对齐要求请参见通用地址对齐约束。
- 操作数地址重叠约束请参考通用地址重叠约束。
调用示例
更多样例可参考LINK。
tensor高维切分计算样例-mask连续模式
// dstLocal: 存放LeakyRelu计算结果的输入Tensor // srcLocal: 存放LeakyRelu计算的输入Tensor // scalar: 负斜率系数 uint64_t mask = 128; half scalar = 0.001; // 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高维切分计算样例-mask逐bit模式
uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; half scalar = 0.001; // 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 = 0.001; // 算子输入的数据类型为half, 需要参与计算的元素个数为512 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]创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考