LeakyRelu
产品支持情况
|
产品 |
是否支持 |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
x |
功能说明
按元素执行Leaky ReLU(Leaky Rectified Linear Unit)操作,计算公式如下:

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

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

函数原型
- tensor前n个数据计算
1 2
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模式
1 2
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连续模式
1 2
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逐bit模式
dst和src使用TensorTrait类型时,其数据类型TensorTrait和scalarValue的数据类型(对应TensorTrait中的LiteType类型)不一致。因此新增模板类型U表示scalarValue的数据类型,并通过std::enable_if检查T中萃取出的LiteType和U是否完全一致,一致则接口通过编译,否则编译失败。接口原型定义如下:
- tensor前n个数据计算
1 2
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模式
1 2
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连续模式
1 2
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逐bit模式
参数说明
|
参数名 |
描述 |
|---|---|
|
T |
操作数数据类型。 |
|
U |
scalarValue数据类型。 |
|
isSetMask |
是否在接口内部设置mask模式和mask值。
针对以下型号,tensor前n个数据计算API中的isSetMask参数不生效,保持默认值即可。
|
|
参数名称 |
类型 |
说明 |
|---|---|---|
|
dst |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
|
src |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 数据类型需要与目的操作数保持一致。 |
|
scalarValue |
输入 |
源操作数,数据类型需要与目的操作数Tensor中的元素保持一致。 |
|
count |
输入 |
参与计算的元素个数。 |
|
mask/mask[] |
输入 |
|
|
repeatTime |
输入 |
重复迭代次数。 矢量计算单元,每次读取连续的256Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTime表示迭代的次数。 |
|
repeatParams |
输入 |
元素操作控制结构信息,具体请参考UnaryRepeatParams。 |
返回值说明
无
调用示例
- tensor高维切分计算样例-mask连续模式
1 2 3 4 5 6 7 8 9 10
// 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模式
1 2 3 4 5 6
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个数据计算样例
1 2 3
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]