forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthwiseConvExecution.cpp
More file actions
198 lines (168 loc) · 8.24 KB
/
Copy pathDepthwiseConvExecution.cpp
File metadata and controls
198 lines (168 loc) · 8.24 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
//
// DepthwiseConvExecution.cpp
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "DepthwiseConvExecution.hpp"
#include <Macro.h>
#include <string.h>
#include "TensorUtils.hpp"
namespace MNN {
namespace OpenCL {
DepthwiseConvExecution::DepthwiseConvExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
: ConvCommonExecution(op->main_as_Convolution2D(), backend) {
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
mCon2dParams = op->main_as_Convolution2D();
mConv2dCommonParams = mCon2dParams->common();
mStrides = {mConv2dCommonParams->strideY(), mConv2dCommonParams->strideX()};
mDilations = {mConv2dCommonParams->dilateY(), mConv2dCommonParams->dilateX()};
mPaddings[0] = mConv2dCommonParams->padY() * 2;
mPaddings[1] = mConv2dCommonParams->padX() * 2;
PadMode padMode = mConv2dCommonParams->padMode();
if (padMode == PadMode_VALID) {
mPaddings[0] = 0;
mPaddings[1] = 0;
}
int kernelWidth = mConv2dCommonParams->kernelX();
int kernelHeight = mConv2dCommonParams->kernelY();
int outputChannel = mConv2dCommonParams->outputCount();
std::vector<int> filterShape{1, outputChannel, kernelHeight, kernelWidth};
std::vector<int> filterImageShape{(int)kernelHeight * kernelWidth, (int)UP_DIV(outputChannel, 4)};
const float *filterDataPtr = mCon2dParams->weight()->data();
mFilter.reset(Tensor::createDevice<float>({1, filterImageShape[1], 1, 4 * filterImageShape[0]}));
std::shared_ptr<Tensor> filterBuffer(Tensor::createDevice<float>(filterShape));
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);
mOpenCLBackend->onAcquireBuffer(mFilter.get(), Backend::STATIC);
MNN::OpenCL::ImageBufferConvertor imageBufferConvertor{mOpenCLBackend->getOpenCLRuntime()};
imageBufferConvertor.convertBufferToImage(filterBuffer.get(), MNN::OpenCL::DW_CONV2D_FILTER, mFilter.get());
auto runtime = mOpenCLBackend->getOpenCLRuntime();
std::set<std::string> buildOptions;
std::string kernelName = "depthwise_conv2d";
if (mConv2dCommonParams->strideX() == 1 && mConv2dCommonParams->strideY() == 1 &&
mConv2dCommonParams->dilateX() == 1 && mConv2dCommonParams->dilateY() == 1) {
kernelName = "depthwise_conv2d_s1";
}
if (mConv2dCommonParams->relu() == true) {
buildOptions.emplace("-DRELU");
} else if (mConv2dCommonParams->relu6() == true) {
buildOptions.emplace("-DRELU6");
}
mKernel = runtime->buildKernel("depthwise_conv2d", kernelName, buildOptions);
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
}
DepthwiseConvExecution::~DepthwiseConvExecution() {
mOpenCLBackend->onReleaseBuffer(mFilter.get(), Backend::STATIC);
}
ErrorCode DepthwiseConvExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
auto input = inputs[0];
auto output = outputs[0];
std::vector<int> inputShape = tensorShapeFormat(input);
std::vector<int> outputShape = tensorShapeFormat(output);
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 = depthwiseConvLocalWS(mGlobalWorkSize, mMaxWorkGroupSize);
if (mConv2dCommonParams->padMode() == PadMode_SAME) {
int kernelHeightSize = (mConv2dCommonParams->kernelY() - 1) * mConv2dCommonParams->dilateY() + 1;
int padNeededHeight =
(output->height() - 1) * mConv2dCommonParams->strideY() + kernelHeightSize - input->height();
int kernelWidthSize = (mConv2dCommonParams->kernelX() - 1) * mConv2dCommonParams->dilateX() + 1;
int padNeededWidth =
(output->width() - 1) * mConv2dCommonParams->strideX() + kernelWidthSize - input->width();
mPaddings[0] = padNeededHeight;
mPaddings[1] = padNeededWidth;
}
const int outputHeight = outputShape.at(1);
const int outputWidth = 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);
const int filterHeight = mCon2dParams->common()->kernelY();
const int filterWidth = mCon2dParams->common()->kernelX();
uint32_t idx = 0;
auto kernel = &mKernel;
int inputImageShape[2] = {inputHeight, inputWidth};
int outputImageShape[2] = {outputHeight, outputWidth};
int strideShape[2] = {mStrides[0], mStrides[1]};
int paddingShape[2] = {mPaddings[0] / 2, mPaddings[1] / 2};
int kernelShape[2] = {filterHeight, filterWidth};
int dilationShape[2] = {mDilations[0], mDilations[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(kernelShape), kernelShape);
kernel->setArg(idx++, sizeof(paddingShape), paddingShape);
if (mStrides[0] != 1 || mStrides[1] != 1 || mDilations[0] != 1 || mDilations[1] != 1) {
kernel->setArg(idx++, sizeof(dilationShape), dilationShape);
kernel->setArg(idx++, sizeof(strideShape), strideShape);
}
return NO_ERROR;
}
std::vector<uint32_t> DepthwiseConvExecution::depthwiseConvLocalWS(const 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 * 4;
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);
return lws;
}
ErrorCode DepthwiseConvExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
#ifdef LOG_VERBOSE
MNN_PRINT("start DepthwiseConvExecution onExecute !\n");
#endif
runKernel2D(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
#ifdef LOG_VERBOSE
MNN_PRINT("end DepthwiseConvExecution onExecute !\n");
#endif
return NO_ERROR;
}
OpenCLCreatorRegister<TypedCreator<DepthwiseConvExecution>> __DepthwiseConv_op(OpType_ConvolutionDepthwise);
} // namespace OpenCL
} // namespace MNN