Silu
产品支持情况
功能说明
按元素做Silu运算,计算公式如下:


函数原型
template <typename T, bool isReuseSource = false>
__aicore__ inline void Silu(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, uint32_t dataSize)
参数说明
表 1 模板参数说明
|
Atlas A3 训练系列产品/Atlas A3 推理系列产品,支持的数据类型为:half、float。 Atlas A2 训练系列产品/Atlas A2 推理系列产品,支持的数据类型为:half、float。 |
|
表 2 接口参数说明
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
返回值说明
无
约束说明
- 操作数地址偏移对齐要求请参见通用说明和约束。
- 不支持源操作数与目的操作数地址重叠。
- 当前仅支持ND格式的输入,不支持其他格式。
调用示例
#include "kernel_operator.h"
template <typename srcType>
class KernelSilu
{
public:
__aicore__ inline KernelSilu() {}
__aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t inputSize)
{
dataSize = inputSize;
srcGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(srcGm), dataSize);
dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(dstGm), dataSize);
pipe.InitBuffer(inQueueX, 1, dataSize * sizeof(srcType));
pipe.InitBuffer(outQueue, 1, dataSize * sizeof(srcType));
}
__aicore__ inline void Process()
{
CopyIn();
Compute();
CopyOut();
}
private:
__aicore__ inline void CopyIn()
{
AscendC::LocalTensor<srcType> srcLocal = inQueueX.AllocTensor<srcType>();
AscendC::DataCopy(srcLocal, srcGlobal, dataSize);
inQueueX.EnQue(srcLocal);
}
__aicore__ inline void Compute()
{
AscendC::LocalTensor<srcType> dstLocal = outQueue.AllocTensor<srcType>();
AscendC::LocalTensor<srcType> srcLocal = inQueueX.DeQue<srcType>();
AscendC::Silu(dstLocal, srcLocal, dataSize);
outQueue.EnQue<srcType>(dstLocal);
inQueueX.FreeTensor(srcLocal);
}
__aicore__ inline void CopyOut()
{
AscendC::LocalTensor<srcType> dstLocal = outQueue.DeQue<srcType>();
AscendC::DataCopy(dstGlobal, dstLocal, dataSize);
outQueue.FreeTensor(dstLocal);
}
private:
AscendC::GlobalTensor<srcType> srcGlobal;
AscendC::GlobalTensor<srcType> dstGlobal;
AscendC::TPipe pipe;
AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX;
AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue;
uint32_t dataSize = 0;
};
template <typename dataType>
__aicore__ void kernel_Silu_operator(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t dataSize)
{
KernelSilu<dataType> op;
op.Init(srcGm, dstGm, dataSize);
op.Process();
}
结果示例如下:
输入数据(srcLocal):[3.304723 1.04788 ... -1.0512]
输出数据(dstLocal): [3.185546875 0.77587890625 ... -0.272216796875]