#include "kernel_operator.h"using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2; constexpr int32_t TOTAL_LENGTH = 8 * 200 * 1024; constexpr int32_t TILE_NUM = 32; constexpr float alpha = 0.002;
class KernelPRelu {public: __aicore__ inline KernelPRelu() {} __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, uint32_t totalLength, uint32_t tileNum, float alpha) { PRINTF("[npu debug] >>> GetBlockNum() %d", GetBlockNum()); ASSERT(GetBlockNum() != 0 && "block dim can not be zero!"); this->blockLength = totalLength / GetBlockNum(); this->tileNum = tileNum; this->alpha = static_cast<float>(alpha); ASSERT(tileNum != 0 && "tile num can not be zero!"); this->tileLength = this->blockLength / tileNum / BUFFER_NUM;
// get start index for current core, core parallel xGm.SetGlobalBuffer((__gm__ float*)x + this->blockLength * GetBlockIdx(), this->blockLength); yGm.SetGlobalBuffer((__gm__ float*)y + this->blockLength * GetBlockIdx(), this->blockLength); // pipe alloc memory to queue, the unit is Bytes pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(float)); pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileLength * sizeof(float)); pipe.InitBuffer(tmpBuffer1, this->tileLength * sizeof(float)); //pipe.InitBuffer(tmpBuffer2, this->tileLength * sizeof(float)); } __aicore__ inline void Process() { // loop count need to be doubled, due to double buffer int32_t loopCount = this->tileNum * BUFFER_NUM; // tiling strategy, pipeline parallel for (int32_t i = 0; i < loopCount; i++) { CopyIn(i); Compute(i); CopyOut(i); } }
private: __aicore__ inline void CopyIn(int32_t progress) { // alloc tensor from queue memory LocalTensor<float> xLocal = inQueueX.AllocTensor<float>(); // copy progress_th tile from global tensor to local tensor DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength); // enque input tensors to VECIN queue inQueueX.EnQue(xLocal); } __aicore__ inline void Compute(int32_t progress) { // deque input tensors from VECIN queue LocalTensor<float> xLocal = inQueueX.DeQue<float>(); LocalTensor<float> yLocal = outQueueY.AllocTensor<float>(); LocalTensor<float> tmpTensor1 = tmpBuffer1.Get<float>(); float inputVal = 0.0; Maxs(tmpTensor1, xLocal, inputVal, this->tileLength); // x >= 0 --> x // x < 0 Mins(xLocal, xLocal, inputVal, this->tileLength); Muls(xLocal, xLocal, this->alpha, this->tileLength); Add(yLocal, xLocal, tmpTensor1, this->tileLength); outQueueY.EnQue<float>(yLocal); // free input tensors for reuse inQueueX.FreeTensor(xLocal); } __aicore__ inline void CopyOut(int32_t progress) { // deque output tensor from VECOUT queue LocalTensor<float> yLocal = outQueueY.DeQue<float>(); // copy progress_th tile from local tensor to global tensor DataCopy(yGm[progress * this->tileLength], yLocal, this->tileLength); // free output tensor for reuse outQueueY.FreeTensor(yLocal); }
private: TPipe pipe; TBuf<QuePosition::VECCALC> tmpBuffer1; //TBuf<QuePosition::VECCALC> tmpBuffer1, tmpBuffer2; // create queues for input, in this case depth is equal to buffer num TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX; // create queue for output, in this case depth is equal to buffer num TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY; GlobalTensor<float> xGm, yGm; uint32_t blockLength; uint32_t tileNum; uint32_t tileLength; float alpha;};extern "C" __global__ __aicore__ void p_relu_custom(GM_ADDR x, GM_ADDR y) { //GET_TILING_DATA(tiling_data, tiling); // TODO: user kernel impl KernelPRelu op; op.Init(x, y, TOTAL_LENGTH, TILE_NUM, alpha); op.Process();}
#ifndef __CCE_KT_TEST__// call of kernel functionvoid p_relu_custom_do(uint32_t blockDim, void* l2ctrl, void* stream, uint8_t* x, uint8_t* y){ p_relu_custom<<<blockDim, l2ctrl, stream>>>(x, y);}#endif
评论