forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvExecution.cpp
More file actions
362 lines (323 loc) · 15 KB
/
Copy pathConvExecution.cpp
File metadata and controls
362 lines (323 loc) · 15 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// ConvExecution.cpp
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "execution/ConvExecution.hpp"
#include "ConvWinograd.hpp"
#include "ConvolutionIntFactory.hpp"
#include "Macro.h"
#include "TensorUtils.hpp"
#include "core/OpenCLBackend.hpp"
#include "core/OpenCLRunningUtils.hpp"
namespace MNN {
namespace OpenCL {
std::vector<uint32_t> ConvExecution::conv2d1x1LocalWS(std::vector<uint32_t> &gws, const uint32_t maxWorkGroupSize) {
uint32_t deviceComputeUnits = mOpenCLBackend->getOpenCLRuntime()->deviceComputeUnits();
std::vector<uint32_t> lws(4, 0);
int coreNum = deviceComputeUnits * 2;
int remain = gws[0] % coreNum;
int groupSize = gws[0] / coreNum;
if (remain == 0) {
lws[0] = groupSize;
} else {
while (groupSize) {
int remain = gws[0] % groupSize;
if (remain == 0 && groupSize <= maxWorkGroupSize) {
lws[0] = groupSize;
break;
}
groupSize--;
}
}
lws[0] = std::max<uint32_t>(std::min<uint32_t>(maxWorkGroupSize, lws[0]), 1);
remain = gws[1] % coreNum;
groupSize = gws[1] / coreNum;
if (remain == 0) {
lws[1] = groupSize;
} else {
while (groupSize) {
int remain = gws[1] % groupSize;
if (remain == 0) {
lws[1] = groupSize;
break;
}
groupSize--;
}
}
lws[1] = std::max<uint32_t>(std::min<uint32_t>(maxWorkGroupSize / lws[0], lws[1]), 1);
// MNN_PRINT("deviceComputeUnits : %d , maxWorkGroupSize : %d\n", deviceComputeUnits, maxWorkGroupSize);
// MNN_PRINT("[%d, %d, %d] -- [%d, %d, %d] \n", gws[0], gws[1], gws[2], lws[0], lws[1], lws[2]);
return lws;
}
std::vector<uint32_t> ConvExecution::conv2dGeneralLocalWS(const std::vector<uint32_t> &gws, const uint32_t kernelSize,
const uint32_t maxWorkGroupSize) {
uint32_t deviceComputeUnits = mOpenCLBackend->getOpenCLRuntime()->deviceComputeUnits();
GpuType gpuType = mOpenCLBackend->getOpenCLRuntime()->getGpuType();
std::vector<uint32_t> lws(4, 0);
if (gpuType == GpuType::ADRENO) {
int coreNum = deviceComputeUnits;
int remain = gws[0] % coreNum;
int groupSize = gws[0] / coreNum;
if (remain == 0) {
lws[0] = groupSize;
} else {
while (groupSize) {
int remain = gws[0] % groupSize;
if (remain == 0 && groupSize <= maxWorkGroupSize) {
lws[0] = groupSize;
break;
}
groupSize--;
}
}
lws[0] = std::max<uint32_t>(std::min<uint32_t>(maxWorkGroupSize, lws[0]), 1);
remain = gws[1] % coreNum;
groupSize = gws[1] / coreNum;
if (remain == 0) {
lws[1] = groupSize;
} else {
while (groupSize) {
int remain = gws[1] % groupSize;
if (remain == 0) {
lws[1] = groupSize;
break;
}
groupSize--;
}
}
lws[1] = std::max<uint32_t>(std::min<uint32_t>(maxWorkGroupSize / lws[0], lws[1]), 1);
remain = gws[2] % coreNum;
groupSize = gws[2] / coreNum;
if (remain == 0) {
lws[2] = groupSize;
} else {
while (groupSize) {
int remain = gws[2] % groupSize;
if (remain == 0) {
lws[2] = groupSize;
break;
}
groupSize--;
}
}
lws[2] = std::max<uint32_t>(std::min<uint32_t>(maxWorkGroupSize / (lws[0] * lws[1]), lws[2]), 1);
} else {
lws[0] = deviceComputeUnits * 2;
lws[1] = 4;
lws[2] = 1;
}
return lws;
}
ConvCommonExecution::ConvCommonExecution(const Convolution2D *conv2dParams, Backend *backend) : Execution(backend) {
auto openclBackend = (OpenCLBackend *)backend;
int biasSize = conv2dParams->bias()->size();
const float *biasDataPtr = conv2dParams->bias()->data();
cl::Buffer biasBuffer(openclBackend->getOpenCLRuntime()->context(), CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
UP_DIV(biasSize, 4) * 4 * sizeof(float));
auto biasPtrCL = openclBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(
biasBuffer, true, CL_MAP_WRITE, 0, ALIGN_UP4(biasSize) * sizeof(float));
if(biasPtrCL != nullptr){
::memset(biasPtrCL, 0, ALIGN_UP4(biasSize) * sizeof(float));
::memcpy(biasPtrCL, biasDataPtr, biasSize * sizeof(float));
}else{
MNN_ERROR("Map error biasPtrCL == nullptr \n");
}
openclBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(biasBuffer, biasPtrCL);
std::shared_ptr<Tensor> bias;
bias.reset(Tensor::createDevice<float>({1, 1, 1, biasSize}));
backend->onAcquireBuffer(bias.get(), Backend::STATIC);
copyBufferToImage(openclBackend->getOpenCLRuntime(), biasBuffer, openCLImage(bias.get()), UP_DIV(biasSize, 4), 1);
mBias = bias;
}
ConvCommonExecution::~ConvCommonExecution() {
MNN_ASSERT(nullptr != mBias);
backend()->onReleaseBuffer(mBias.get(), Backend::STATIC);
}
ConvExecution::ConvExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
: ConvCommonExecution(op->main_as_Convolution2D(), backend) {
#ifdef LOG_VERBOSE
MNN_PRINT("Start ConvExecution init !\n");
#endif
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
const auto *conv2dParams = op->main_as_Convolution2D();
const auto *conv2dCommonParams = conv2dParams->common();
mConv2dCommonParams = conv2dCommonParams;
mStrides = {conv2dCommonParams->strideY(), conv2dCommonParams->strideX()};
mDilations = {conv2dCommonParams->dilateY(), conv2dCommonParams->dilateX()};
mPaddings[0] = conv2dCommonParams->padY() * 2;
mPaddings[1] = conv2dCommonParams->padX() * 2;
PadMode padMode = conv2dCommonParams->padMode();
if (padMode == PadMode_VALID) {
mPaddings[0] = 0;
mPaddings[1] = 0;
}
int kernelWidth = conv2dCommonParams->kernelX();
int kernelHeight = conv2dCommonParams->kernelY();
int outputChannel = conv2dCommonParams->outputCount();
int weightSize = 0;
const float *filterDataPtr = nullptr;
std::shared_ptr<MNN::ConvolutionIntFactory::Int8Common> quanCommon;
if (nullptr != conv2dParams->quanParameter()) {
quanCommon = ConvolutionIntFactory::load(conv2dParams->quanParameter(), true);
if (nullptr == quanCommon) {
MNN_ERROR("Memory not Enough, can't extract IDST Convolution: %s \n", op->name()->c_str());
}
if (quanCommon->weightFloat.get() == nullptr) {
MNN_PRINT("quanCommon->weightFloat.get() == nullptr \n");
}
// Back to float
filterDataPtr = quanCommon->weightFloat.get();
weightSize = quanCommon->weightFloat.size();
}
if (nullptr == filterDataPtr) {
weightSize = conv2dParams->weight()->size();
filterDataPtr = conv2dParams->weight()->data();
}
int inputChannel = weightSize / (kernelWidth * kernelHeight * outputChannel);
std::vector<int> filter_shape{outputChannel, inputChannel, kernelHeight, kernelWidth};
std::vector<int> filterImageShape{(int)inputChannel, (int)(UP_DIV(outputChannel, 4) * kernelWidth * kernelHeight)};
std::shared_ptr<Tensor> filterBuffer(
Tensor::createDevice<float>({outputChannel, inputChannel, kernelHeight, kernelWidth}));
cl::Buffer filterBufferCL(mOpenCLBackend->getOpenCLRuntime()->context(), CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
filterBuffer->size());
filterBuffer->buffer().device = (uint64_t)(&filterBufferCL);
auto ptrCL = mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(filterBufferCL, true, CL_MAP_WRITE,
0, filterBuffer->size());
if(ptrCL != nullptr){
::memcpy(ptrCL, filterDataPtr, filterBuffer->size());
}else{
MNN_ERROR("Map error ptrCL == nullptr \n");
}
mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(filterBufferCL, ptrCL);
mFilter.reset(Tensor::createDevice<float>({1, filterImageShape[1], 1, 4 * filterImageShape[0]}));
mOpenCLBackend->onAcquireBuffer(mFilter.get(), Backend::STATIC);
MNN::OpenCL::ImageBufferConvertor imageBufferConvertor{mOpenCLBackend->getOpenCLRuntime()};
imageBufferConvertor.convertBufferToImage(filterBuffer.get(), MNN::OpenCL::CONV2D_FILTER, mFilter.get());
// Create Kernel
std::set<std::string> buildOptions;
if (mConv2dCommonParams->relu()) {
buildOptions.emplace("-DRELU");
} else if (mConv2dCommonParams->relu6()) {
buildOptions.emplace("-DRELU6");
}
std::string kernelName = "conv_2d";
if (kernelHeight == kernelWidth && kernelHeight == 1 && mConv2dCommonParams->padX() == 0 &&
mConv2dCommonParams->padY() == 0) {
kernelName = "conv_2d_1x1";
}
mKernel = mOpenCLBackend->getOpenCLRuntime()->buildKernel("conv_2d", kernelName, buildOptions);
mMaxWorkGroupSize = static_cast<uint32_t>(mOpenCLBackend->getOpenCLRuntime()->getMaxWorkGroupSize(mKernel));
#ifdef LOG_VERBOSE
MNN_PRINT("end ConvExecution init !\n");
#endif
}
ConvExecution::~ConvExecution() {
mOpenCLBackend->onReleaseBuffer(mFilter.get(), Backend::STATIC);
}
ErrorCode ConvExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
#ifdef LOG_VERBOSE
MNN_PRINT("Start ConvExecution onResize !\n");
#endif
auto input = inputs[0];
auto output = outputs[0];
std::vector<int> inputShape = tensorShapeFormat(input);
std::vector<int> outputShape = tensorShapeFormat(output);
const int height = outputShape.at(1);
const int width = outputShape.at(2);
const int inputHeight = inputShape.at(1);
const int inputWidth = inputShape.at(2);
const int inputChannels = inputShape.at(3);
const int inputChannelBlocks = UP_DIV(inputChannels, 4);
if (mConv2dCommonParams->padMode() == PadMode_SAME) {
int kernelHeightSize = (mConv2dCommonParams->kernelY() - 1) * mConv2dCommonParams->dilateY() + 1;
int padNeededHeight = (outputShape.at(1) - 1) * mConv2dCommonParams->strideY() +
kernelHeightSize - inputShape.at(1);
int kernelWidthSize = (mConv2dCommonParams->kernelX() - 1) * mConv2dCommonParams->dilateX() + 1;
int padNeededWidth = (outputShape.at(2) - 1) * mConv2dCommonParams->strideX() + kernelWidthSize -
inputShape.at(2);
mPaddings[0] = padNeededHeight;
mPaddings[1] = padNeededWidth;
}
int kernelHeight = mConv2dCommonParams->kernelY();
int kernelWidth = mConv2dCommonParams->kernelX();
if (kernelHeight == kernelWidth && kernelHeight == 1 && mPaddings[0] == 0 && mPaddings[1] == 0) {
mGlobalWorkSize = {
static_cast<uint32_t>(UP_DIV(outputShape.at(3), 4) * static_cast<uint32_t>(UP_DIV(outputShape.at(2), 4))),
static_cast<uint32_t>(outputShape.at(0) * outputShape.at(1))};
mLocalWorkSize = conv2d1x1LocalWS(mGlobalWorkSize, mMaxWorkGroupSize);
auto kernel = &mKernel;
uint32_t idx = 0;
int inputImageShape[2] = {inputHeight, inputWidth};
int outputImageShape[2] = {height, width};
int stideShape[2] = {mStrides[0], mStrides[1]};
kernel->setArg(idx++, mGlobalWorkSize[0]);
kernel->setArg(idx++, mGlobalWorkSize[1]);
kernel->setArg(idx++, openCLImage(input));
kernel->setArg(idx++, openCLImage(mFilter.get()));
kernel->setArg(idx++, openCLImage(mBias.get()));
kernel->setArg(idx++, openCLImage(output));
kernel->setArg(idx++, sizeof(inputImageShape), inputImageShape);
kernel->setArg(idx++, static_cast<int>(inputChannelBlocks));
kernel->setArg(idx++, sizeof(outputImageShape), outputImageShape);
kernel->setArg(idx++, sizeof(stideShape), stideShape);
kernel->setArg(idx++, UP_DIV(width, 4));
} else {
mGlobalWorkSize = {static_cast<uint32_t>(UP_DIV(outputShape.at(3), 4) * UP_DIV(outputShape.at(2), 4)),
static_cast<uint32_t>(outputShape.at(0) * outputShape.at(1))};
mLocalWorkSize = conv2dGeneralLocalWS(mGlobalWorkSize, kernelHeight * kernelWidth, mMaxWorkGroupSize);
int inputImageShape[2] = {inputHeight, inputWidth};
int outputImageShape[2] = {height, width};
int kernelShape[2] = {kernelHeight, kernelWidth};
int strideShape[2] = {mStrides[0], mStrides[1]};
int paddingShape[2] = {mPaddings[0] / 2, mPaddings[1] / 2};
int dilationShape[2] = {mDilations[0], mDilations[1]};
uint32_t idx = 0;
auto kernel = &mKernel;
kernel->setArg(idx++, mGlobalWorkSize[0]);
kernel->setArg(idx++, mGlobalWorkSize[1]);
kernel->setArg(idx++, openCLImage(input));
kernel->setArg(idx++, openCLImage(mFilter.get()));
kernel->setArg(idx++, openCLImage(mBias.get()));
kernel->setArg(idx++, openCLImage(output));
kernel->setArg(idx++, sizeof(inputImageShape), inputImageShape);
kernel->setArg(idx++, inputChannelBlocks);
kernel->setArg(idx++, sizeof(outputImageShape), outputImageShape);
kernel->setArg(idx++, sizeof(kernelShape), kernelShape);
kernel->setArg(idx++, sizeof(strideShape), strideShape);
kernel->setArg(idx++, sizeof(paddingShape), paddingShape);
kernel->setArg(idx++, sizeof(dilationShape), dilationShape);
kernel->setArg(idx++, UP_DIV(width, 4));
}
#ifdef LOG_VERBOSE
MNN_PRINT("end ConvExecution onResize !\n");
#endif
return NO_ERROR;
}
ErrorCode ConvExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
#ifdef LOG_VERBOSE
MNN_PRINT("Start ConvExecution onExecute !\n");
#endif
runKernel2D(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
#ifdef LOG_VERBOSE
MNN_PRINT("end ConvExecution onExecute !\n");
#endif
return NO_ERROR;
}
class ConvolutionCreator : public OpenCLBackend::Creator {
public:
virtual ~ConvolutionCreator() = default;
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
auto conv2D = op->main_as_Convolution2D();
if (ConvWinograd::valid(conv2D->common(), inputs[0])) {
return new ConvWinograd(conv2D, backend);
}
return new ConvExecution(inputs, op, backend);
}
};
OpenCLCreatorRegister<ConvolutionCreator> __conv_op(OpType_Convolution);
} // namespace OpenCL
} // namespace MNN