ReGlu
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
√ |
|
x |
|
x |
Function
ReGlu is a GLU variant. It uses Relu as the activation function. Below is the formula.

The formula for calculating the ReLU activation function is as follows:

Prototype
- Pass to the temporary space through the sharedTmpBuffer input parameter.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void ReGlu(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor0, const LocalTensor<T>& srcTensor1, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount)
- Allocate the temporary space through the API framework.
1 2
template <typename T, bool isReuseSource = false> __aicore__ inline void ReGlu(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor0, const LocalTensor<T>& srcTensor1, const uint32_t calCount)
Due to the complex mathematical computation involved in the internal implementation of this API, extra 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 temporary space (BufferSize) to be reserved, use the API provided in GetReGluMaxMinTmpSize.
Parameters
Parameter |
Description |
|---|---|
T |
Data type of the operand. For the For the For the |
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 |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The source operand must have the same data type as the destination operand. |
srcTensor1 |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The source operand must have the same data type as the destination operand. |
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 ReGlu and is provided by developers. For details about how to obtain the temporary space size (BufferSize), see GetReGluMaxMinTmpSize. |
calCount |
Input |
Number of elements involved in the computation. |
Returns
None
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- The source operand address must not overlap the destination operand address.
- The address of sharedTmpBuffer must not overlap that of the source or destination operand.
- Currently, only the ND format is supported.
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 | #include "kernel_operator.h" template <typename srcType> class KernelReGlu { public: __aicore__ inline KernelReGlu() {} __aicore__ inline void Init(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm, uint32_t srcSize) { dataSize = srcSize; src0Global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(src0Gm), dataSize); src1Global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(src1Gm), dataSize); dstGlobal.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::ReGlu<srcType, false>(dstLocal, src0Local, src1Local, tmpLocal, dataSize); } else { AscendC::ReGlu<srcType, false>(dstLocal, src0Local, src1Local, dataSize); } outQueue.EnQue<srcType>(dstLocal); inQueueX.FreeTensor(src0Local); inQueueY.FreeTensor(src1Local); } __aicore__ inline void CopyOut() { AscendC::LocalTensor<srcType> dstLocal = outQueue.DeQue<srcType>(); AscendC::DataCopy(dstGlobal, dstLocal, dataSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensor<srcType> src0Global; AscendC::GlobalTensor<srcType> src1Global; AscendC::GlobalTensor<srcType> dstGlobal; AscendC::TPipe pipe; AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX; AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueY; AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue; AscendC::TBuf<AscendC::TPosition::VECCALC> calcBufs; uint32_t dataSize = 0; }; template <typename dataType> __aicore__ void kernel_reglu_operator(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm, uint32_t srcSize) { KernelReGlu<dataType> op; op.Init(src0Gm, src1Gm, dstGm, srcSize); op.Process(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Input data (srcLocal0): [ 22.28125 78.375 -10.3515625 -80.75 -22.8125 84.375 -8.96875 70.5 -51.75 66.875 69.8125 5.2734375 -51. 50.5 -30.765625 -52.125 8.03125 75.8125 50.4375 -97.1875 -80.6875 17.125 -30.640625 -13.671875 92.375 68.8125 53.75 5.1054688 39.6875 -46.71875 90.25 67.75 ] Input data (srcLocal1): [ 61.46875 -36.5625 -93.3125 -87.6875 -17.96875 -88.125 -46.65625 -18.78125 13.4921875 -87.875 65.75 -25.96875 -44.5625 53. -69.375 96.5 -24.703125 77.5625 78.875 -6.0898438 -40.5625 -69.625 57. 18.640625 -73.875 94.375 91.5 -9.7109375 84.125 79.0625 88.5 96.3125 ] Output data (dstLocal): [ 0.0000e+00 0.0000e+00 0.0000e+00 -6.5450e+02 0.0000e+00 1.2544e+02 3.7880e+03 1.0519e+02 -0.0000e+00 -0.0000e+00 -0.0000e+00 0.0000e+00 -2.0110e+03 0.0000e+00 -2.8020e+03 -0.0000e+00 0.0000e+00 -2.6120e+03 6.8840e+03 -0.0000e+00 8.6550e+02 -0.0000e+00 0.0000e+00 -7.4120e+03 -1.9700e+03 2.3140e+03 -0.0000e+00 0.0000e+00 -0.0000e+00 7.6760e+03 -4.8828e-01 -0.0000e+00] |