Silu
功能说明
按元素做Silu运算,计算公式如下,其中PAR表示矢量计算单元一个迭代能够处理的元素个数 :


函数原型
template <typename T, bool isReuseSource = false>
__aicore__ inline void Silu(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, uint32_t dataSize)
参数说明
参数名 |
输入/输出 |
描述 |
|---|---|---|
dstTensor |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 Atlas推理系列产品AI Core,支持的数据类型为:half/float Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float |
srcTensor |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 源操作数的数据类型需要与目的操作数保持一致。 Atlas推理系列产品AI Core,支持的数据类型为:half/float Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float |
dataSize |
输入 |
输入数据元素的个数。 |
isReuseSource |
输入 |
中间变量是否能够复用输入内存。该接口不支持isReuseSource参数,传入默认值false即可。 |
返回值
无
支持的型号
Atlas推理系列产品AI Core
Atlas A2训练系列产品/Atlas 800I A2推理产品
注意事项
- 操作数地址偏移对齐要求请参见通用约束。源操作数与目的操作数不允许同时使用(即地址不重叠)。
- 当前仅支持ND格式的输入,不支持其他格式。
- dataSize需要保证小于或等于srcTensor和dstTensor存储的元素范围。
调用示例
#ifdef __CCE_KT_TEST__
#include "tikicpulib.h"
#endif
#include "kernel_operator.h"
using namespace AscendC;
template <typename srcType> class KernelSilu {
public:
__aicore__ inline KernelSilu() {}
__aicore__ inline void Init(GM_ADDR src_gm, GM_ADDR dst_gm, uint32_t inputSize)
{
dataSize = inputSize;
src_global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType*>(src_gm), dataSize);
dst_global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType*>(dst_gm), 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()
{
LocalTensor<srcType> srcLocal = inQueueX.AllocTensor<srcType>();
DataCopy(srcLocal, src_global, dataSize);
inQueueX.EnQue(srcLocal);
}
__aicore__ inline void Compute()
{
LocalTensor<srcType> dstLocal = outQueue.AllocTensor<srcType>();
LocalTensor<srcType> srcLocal = inQueueX.DeQue<srcType>();
Silu(dstLocal, srcLocal, dataSize);
// 或等价调用Silu<srcType, false>(dstLocal, srcLocal, dataSize);
outQueue.EnQue<srcType>(dstLocal);
inQueueX.FreeTensor(srcLocal);
}
__aicore__ inline void CopyOut()
{
LocalTensor<srcType> dstLocal = outQueue.DeQue<srcType>();
DataCopy(dst_global, dstLocal, dataSize);
outQueue.FreeTensor(dstLocal);
}
private:
GlobalTensor<srcType> src_global;
GlobalTensor<srcType> dst_global;
TPipe pipe;
TQue<QuePosition::VECIN, 1> inQueueX;
TQue<QuePosition::VECOUT, 1> outQueue;
uint32_t dataSize = 0;
};
template <typename dataType> __aicore__ void kernel_Silu_operator(GM_ADDR src_gm, GM_ADDR dst_gm, uint32_t dataSize)
{
KernelSilu<dataType> op;
op.Init(src_gm, dst_gm, dataSize);
op.Process();
}
结果示例如下:
输入数据(srcLocal):[3.304723 1.04788 ... -1.0512] 输出数据(dstLocal): [3.185546875 0.77587890625 ... -0.272216796875]
父主题: Silu