SwiGLU
Function Description
SwiGLU is a GLU variant that uses Swish as the activation function. The specific calculation formula is as follows, where PAR represents the number of elements that can be processed by the vector unit in one iteration:

The formula of the Swish activation function is as follows (β is a constant):

Prototype
- Pass the temporary space through the sharedTmpBuffer input parameter.
- All or part of the source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void SwiGLU(LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor0, const LocalTensor<T>& srcTensor1, const float& scalarValue, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount)
- All source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void SwiGLU(LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor0, const LocalTensor<T>& srcTensor1, const float& scalarValue, const LocalTensor<uint8_t>& sharedTmpBuffer)
- All or part of the source operand tensors are involved in computation.
- Allocate the temporary space through the API framework.
- All or part of the source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void SwiGLU(LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor0, const LocalTensor<T>& srcTensor1, const float& scalarValue, const uint32_t calCount)
- All source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void SwiGLU(LocalTensor<T>& dstTensor, LocalTensor<T>& srcTensor0, LocalTensor<T>& srcTensor1, const float& scalarValue)
- All or part of the source operand tensors are involved in computation.
Due to the complex mathematical computation involved in the internal implementation of this API, additional temporary space is required to store intermediate variables generated during computation. The temporary space can be passed by developers through the sharedTmpBuffer input parameter or allocated through the API framework.
- When the sharedTmpBuffer input parameter is used for passing the temporary space, the tensor serves as the temporary space. In this case, the API framework is not required for temporary space allocation. This enables developers to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated and deallocated, improving the flexibility and buffer utilization.
- When the API framework is used for temporary space allocation, developers do not need to allocate the space, but must reserve the required size for the space.
If sharedTmpBuffer is used, developers must allocate space for the tensor. If the API framework is used, developers must reserve the temporary space. To obtain the size of the temporary space (BufferSize) to be reserved, use the API provided in GetSwiGLUMaxMinTmpSize.
Parameters
Parameter |
Description |
|---|---|
T |
Data type of the operand. |
isReuseSource |
Whether the source operand can be modified. This parameter is reserved. Pass the default value false. |
Parameter |
Input/Output |
Description |
|---|---|---|
dstTensor |
Output |
Destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. |
srcTensor0/srcTensor1 |
Input |
Source operand. The source operand must have the same data type as the destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. |
scalarValue |
Input |
β in the activation function. The supported data type is float. |
calCount |
Input |
Number of actually computed data elements. The value range is [0, min(srcTensor0.GetSize(),srcTensor1.GetSize(),dstTensor.GetSize)]. |
sharedTmpBuffer |
Input |
Temporary buffer. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. This parameter is used to store intermediate variables during complex computation in SwiGLU and is provided by developers. For details about how to obtain the temporary space size (BufferSize), see SwiGLU Tiling. |
Returns
None
Availability
Constraints
- For details about the alignment requirements of the operand address offset, see General Restrictions.
- The source operand address must not overlap the destination operand address.
- Currently, only the ND format is supported.
- If all source operand tensors are used for computation, that is, if calCount is not passed, the sizes of srcTensor0, srcTensor1, and dstTensor must be the same.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include "kernel_operator.h" template <typename srcType> class KernelSwiGLU { public: __aicore__ inline KernelSwiGLU(){} __aicore__ inline void Init(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm, uint32_t srcSize, float beta) { betaValue = beta; dataSize = srcSize; src0Global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(src0Gm), dataSize); src1Global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(src1Gm), dataSize); dst_global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(dstGm), dataSize); pipe.InitBuffer(inQueueX, 1, dataSize * sizeof(srcType)); pipe.InitBuffer(inQueueY, 1, dataSize * sizeof(srcType)); pipe.InitBuffer(outQueue, 1, dataSize * sizeof(srcType)); if (sizeof(srcType) != sizeof(float)) { pipe.InitBuffer(calcBufs, dataSize * (sizeof(float) / sizeof(uint8_t)) * 3); } } __aicore__ inline void Process() { CopyIn(); Compute(); CopyOut(); } private: __aicore__ inline void CopyIn() { AscendC::LocalTensor<srcType> src0Local = inQueueX.AllocTensor<srcType>(); AscendC::LocalTensor<srcType> src1Local = inQueueY.AllocTensor<srcType>(); AscendC::DataCopy(src0Local, src0Global, dataSize); AscendC::DataCopy(src1Local, src1Global, dataSize); inQueueX.EnQue(src0Local); inQueueY.EnQue(src1Local); } __aicore__ inline void Compute() { AscendC::LocalTensor<srcType> dstLocal = outQueue.AllocTensor<srcType>(); AscendC::LocalTensor<srcType> src0Local = inQueueX.DeQue<srcType>(); AscendC::LocalTensor<srcType> src1Local = inQueueY.DeQue<srcType>(); AscendC::LocalTensor<uint8_t> tmpLocal; if (sizeof(srcType) != sizeof(float)) { tmpLocal = calcBufs.Get<uint8_t>(); AscendC::SwiGLU<srcType, false>(dstLocal, src0Local, src1Local, betaValue, tmpLocal, dataSize); } else { AscendC::SwiGLU<srcType, false>(dstLocal, src0Local, src1Local, betaValue, dataSize); } outQueue.EnQue<srcType>(dstLocal); inQueueX.FreeTensor(src0Local); inQueueY.FreeTensor(src1Local); } __aicore__ inline void CopyOut() { AscendC::LocalTensor<srcType> dstLocal = outQueue.DeQue<srcType>(); AscendC::DataCopy(dst_global, dstLocal, dataSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensor<srcType> src0Global; AscendC::GlobalTensor<srcType> src1Global; AscendC::GlobalTensor<srcType> dst_global; AscendC::TPipe pipe; AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX; AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueY; AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueue; AscendC::TBuf<AscendC::TPosition::VECCALC> calcBufs; uint32_t dataSize = 0; float betaValue = 0; }; template <typename dataType> __aicore__ void kernel_swiglu_operator(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm, uint32_t srcSize) { KernelSwiGLU<dataType> op; float scalarValue = 1; op.Init(src0Gm, src1Gm, dstGm, srcSize, scalarValue); op.Process(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Input data (srcTensor0): [ 0.4065 -0.2167 -0.963 -3.895 -0.7275 3.227 -0.522 -2.299 -1.813 -1.569 3.764 1.407 -1.633 3.908 -0.9927 -2.234 1.545 2. -3.06 1.94 0.765 -1.313 3.27 2.055 2.842 2.979 2.732 2.533 2.03 1.154 -2.363 -2.451 ] Input data (srcTensor1) [-2.285 -1.502 2.783 -3.72 0.352 -2.615 0.8604 0.612 3.582 -3.102 -3.86 2.88 -0.2117 -0.592 -0.5586 1.315 0.4087 3.771 2.69 0.755 -2.154 -1.03 -3.459 -3.125 3.531 -0.657 3.885 2.807 0.469 -1.434 -3.455 -1.3 ] Output data (dstLocal): [-0.0858 0.05927 -2.523 0.3425 -0.1504 -0.575 -0.3157 -0.912 -6.32 0.2095 -0.2998 3.838 0.1545 -0.8237 0.2018 -2.316 0.3794 7.375 -7.707 0.9966 -0.1713 0.356 -0.345 -0.2703 9.75 -0.6685 10.4 6.703 0.5854 -0.3186 0.25 0.6826 ] |