1 //
2 // PoolBufExecution.cpp
3 // MNN
4 //
5 // Created by MNN on 2019/02/28.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8
9 #ifndef MNN_OPENCL_BUFFER_CLOSED
10
11 #include "backend/opencl/execution/buffer/PoolBufExecution.hpp"
12 #include "core/Macro.h"
13 #include "core/TensorUtils.hpp"
14 #include "backend/opencl/core/OpenCLBackend.hpp"
15
16 namespace MNN {
17 namespace OpenCL {
18
PoolBufExecution(const std::vector<Tensor * > & inputs,const MNN::Op * op,Backend * backend)19 PoolBufExecution::PoolBufExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
20 : Execution(backend) {
21 mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
22 mPoolParams = op->main_as_Pool();
23 mPoolType = mPoolParams->type();
24
25 mStrides[0] = mPoolParams->strideY();
26 mStrides[1] = mPoolParams->strideX();
27 mKernels[0] = mPoolParams->kernelY();
28 mKernels[1] = mPoolParams->kernelX();
29
30 mPaddings[0] = mPoolParams->padY() * 2;
31 mPaddings[1] = mPoolParams->padX() * 2;
32 mPadType = mPoolParams->padType();
33 if (mPadType == PoolPadType_VALID) {
34 mPaddings[0] = 0;
35 mPaddings[1] = 0;
36 }
37 }
38
onResize(const std::vector<Tensor * > & inputs,const std::vector<Tensor * > & outputs)39 ErrorCode PoolBufExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
40 #ifdef LOG_VERBOSE
41 MNN_PRINT("start PoolBufExecution onResize !\n");
42 #endif
43 auto input = inputs[0];
44 auto output = outputs[0];
45
46 if (mPoolParams->isGlobal()) {
47 std::vector<int> inputShape = tensorShapeFormat(inputs[0]);
48 mKernels = {inputShape.at(1), inputShape.at(2)};
49 mStrides = {inputShape.at(1), inputShape.at(2)};
50 mPaddings = {0, 0};
51 }
52
53 if (mPadType == PoolPadType_SAME) {
54 int padNeededHeight = std::max(0, (output->height() - 1) * mStrides[0] + mKernels[0] - input->height());
55 int padNeededWidth = std::max(0, (output->width() - 1) * mStrides[1] + mKernels[1] - input->width());
56
57 mPaddings[0] = padNeededHeight;
58 mPaddings[1] = padNeededWidth;
59 }
60
61 MNN_ASSERT(mDilations[0] == 1 && mDilations[1] == 1);
62
63 std::vector<int> inputShape = tensorShapeFormat(input);
64 std::vector<int> outputShape = tensorShapeFormat(output);
65
66 const int batch = outputShape.at(0);
67 const int outputHeight = outputShape.at(1);
68 const int outputWidth = outputShape.at(2);
69 const int channels = outputShape.at(3);
70
71 const int inputHeight = inputShape.at(1);
72 const int inputWidth = inputShape.at(2);
73 int channelBlocks = (channels + 3) / 4;
74
75 std::set<std::string> buildOptions;
76 std::string kernelName = "pooling";
77 auto runtime = mOpenCLBackend->getOpenCLRuntime();
78
79 if (mPoolType == PoolType_AVEPOOL) {
80 buildOptions.emplace("-DPOOL_AVG");
81 }
82
83 mKernel = runtime->buildKernel("pooling_buf", kernelName, buildOptions);
84 mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
85
86 mGlobalWorkSize = {
87 static_cast<uint32_t>(outputWidth),
88 static_cast<uint32_t>(batch * outputHeight),
89 static_cast<uint32_t>(channelBlocks),
90 };
91
92 int inputImageShape[2] = {inputHeight, inputWidth};
93 int outputImageShape[2] = {outputHeight, outputWidth};
94 int paddingShape[2] = {mPaddings[0] / 2, mPaddings[1] / 2};
95 int strideShape[2] = {mStrides[0], mStrides[1]};
96 int kernelShape[2] = {mKernels[0], mKernels[1]};
97
98 uint32_t idx = 0;
99 mKernel.setArg(idx++, mGlobalWorkSize[0]);
100 mKernel.setArg(idx++, mGlobalWorkSize[1]);
101 mKernel.setArg(idx++, mGlobalWorkSize[2]);
102 mKernel.setArg(idx++, openCLBuffer(input));
103 mKernel.setArg(idx++, sizeof(inputImageShape), inputImageShape);
104 mKernel.setArg(idx++, sizeof(outputImageShape), outputImageShape);
105 mKernel.setArg(idx++, sizeof(paddingShape), paddingShape);
106 mKernel.setArg(idx++, sizeof(strideShape), strideShape);
107 mKernel.setArg(idx++, sizeof(kernelShape), kernelShape);
108 mKernel.setArg(idx++, openCLBuffer(output));
109 mKernel.setArg(idx++, channelBlocks);
110
111 std::string kernelNameTune = "pooling_buf";
112 mLocalWorkSize =
113 localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelNameTune, mKernel).first;
114
115 #ifdef LOG_VERBOSE
116 MNN_PRINT("end PoolBufExecution onResize !\n");
117 #endif
118 return NO_ERROR;
119 }
120
onExecute(const std::vector<Tensor * > & inputs,const std::vector<Tensor * > & outputs)121 ErrorCode PoolBufExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
122 #ifdef LOG_VERBOSE
123 MNN_PRINT("start PoolBufExecution onExecute !\n");
124 #endif
125
126 #ifdef ENABLE_OPENCL_TIME_PROFILER
127 cl::Event event;
128 run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize,
129 mOpenCLBackend->getOpenCLRuntime(), &event);
130
131 int costTime = (int)mOpenCLBackend->getOpenCLRuntime()->getCostTime(&event);
132 MNN_PRINT("kernel cost:%d us Pooling\n",costTime);
133 #else
134 run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize,
135 mOpenCLBackend->getOpenCLRuntime());
136 #endif
137
138 #ifdef LOG_VERBOSE
139 MNN_PRINT("end PoolBufExecution onExecute !\n");
140 #endif
141 return NO_ERROR;
142 }
143
144 OpenCLCreatorRegister<TypedCreator<PoolBufExecution>> __PoolBuf_op(OpType_Pooling, BUFFER);
145 } // namespace OpenCL
146 } // namespace MNN
147 #endif /* MNN_OPENCL_BUFFER_CLOSED */
148