forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLConvolutionDepthwise.cpp
More file actions
161 lines (135 loc) · 5.29 KB
/
Copy pathGLConvolutionDepthwise.cpp
File metadata and controls
161 lines (135 loc) · 5.29 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
//
// GLConvolutionDepthwise.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "GLConvolutionDepthwise.hpp"
#include "AutoTime.hpp"
#include <sstream>
#include "AllShader.hpp"
#include "GLBackend.hpp"
#include "Macro.h"
namespace MNN {
namespace OpenGL {
static const int gXLocal = 8;
static const int gYLocal = 8;
static const int gZLocal = 1;
GLConvolutionDepthwise::~GLConvolutionDepthwise() {
}
GLConvolutionDepthwise::GLConvolutionDepthwise(const std::vector<Tensor *> &inputs, const Op *convOp, Backend *bn) : GPUConvolution(convOp, bn) {
auto extra = (GLBackend *)bn;
mBiasBuffer.reset(new GLSSBOBuffer(sizeof(float) * ALIGN_UP4(mCommon->outputCount())));
int fw = mCommon->kernelX();
int fh = mCommon->kernelY();
int unit = 4;
int srcDepthQuad = UP_DIV(mInputDepth, unit);
auto kernelBuffer = std::shared_ptr<GLSSBOBuffer>(new GLSSBOBuffer(sizeof(float) * fw * fh * srcDepthQuad * 4));
auto weight = kernelBuffer->map(GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
if(weight != nullptr){
::memset(weight, 0, fw * fh * srcDepthQuad * 4 * sizeof(float));
::memcpy(weight, convOp->main_as_Convolution2D()->weight()->data(),
convOp->main_as_Convolution2D()->weight()->size() * sizeof(float));
}
kernelBuffer->unmap();
auto bias = mBiasBuffer->map(GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
if(bias != nullptr){
::memset(bias, 0, ALIGN_UP4(mCommon->outputCount()) * sizeof(float));
::memcpy(bias, convOp->main_as_Convolution2D()->bias()->data(),
convOp->main_as_Convolution2D()->bias()->size() * sizeof(float));
}
mBiasBuffer->unmap();
std::vector<std::string> prefix;
if (mCommon->relu()) {
prefix.push_back("#define RELU");
}
if (mCommon->relu6()) {
prefix.push_back("#define RELU6");
}
{
std::ostringstream os;
os << "#define XLOCAL " << gXLocal;
prefix.push_back(os.str());
}
{
std::ostringstream os;
os << "#define YLOCAL " << gYLocal;
prefix.push_back(os.str());
}
{
std::ostringstream os;
os << "#define ZLOCAL " << gZLocal;
prefix.push_back(os.str());
}
mProgram = extra->getProgram("convolution_depthwise", glsl_convlutionDepthwise_glsl, prefix);
mKernelTexture = std::shared_ptr<GLTexture>(new GLTexture(srcDepthQuad, fw, fh, GL_TEXTURE_3D, false));
auto transform = extra->getProgram("transform_kernel_image_depthwise", glsl_kernel2ImageDepthwise_glsl);
transform->useProgram();
glBindImageTexture(0, mKernelTexture->id(), 0, GL_TRUE, 0, GL_WRITE_ONLY, TEXTURE_FORMAT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, kernelBuffer->getId());
OPENGL_CHECK_ERROR;
glUniform1i(3, fw);
glUniform1i(4, fh);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(srcDepthQuad, fw, fh);
OPENGL_CHECK_ERROR;
}
ErrorCode GLConvolutionDepthwise::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
GPUConvolution::onResize(inputs, outputs);
int kx = mCommon->kernelX();
int ky = mCommon->kernelY();
int sx = mCommon->strideX();
int sy = mCommon->strideY();
int dx = mCommon->dilateX();
int dy = mCommon->dilateY();
mSetUniform = [=]() {
glUniform2i(4, mPadX, mPadY);
glUniform2i(5, kx, ky);
glUniform2i(6, sx, sy);
glUniform2i(7, dx, dy);
};
return NO_ERROR;
}
ErrorCode GLConvolutionDepthwise::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
{
auto convLayer = mCommon;
auto input = inputs[0];
auto output = outputs[0];
auto inputTexture = input->deviceId();
auto outputTexture = output->deviceId();
int dst_depth_quad = UP_DIV(output->channel(), 4);
mProgram->useProgram();
glBindImageTexture(0, outputTexture, 0, GL_TRUE, 0, GL_WRITE_ONLY, TEXTURE_FORMAT);
OPENGL_CHECK_ERROR;
{
int texId = 0;
glActiveTexture(GL_TEXTURE0 + texId);
glUniform1i(1, texId);
glBindTexture(GL_TEXTURE_3D, inputTexture);
OPENGL_CHECK_ERROR;
}
{
int texId = 1;
glActiveTexture(GL_TEXTURE0 + texId);
OPENGL_CHECK_ERROR;
glUniform1i(2, texId);
OPENGL_CHECK_ERROR;
glBindTexture(GL_TEXTURE_3D, mKernelTexture->id());
OPENGL_CHECK_ERROR;
}
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, mBiasBuffer->getId());
OPENGL_CHECK_ERROR;
mSetUniform();
glUniform3i(10, output->width(), output->height(), UP_DIV(output->channel(), 4));
glUniform3i(11, input->width(), input->height(), UP_DIV(input->channel(), 4));
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(output->width(), (gXLocal)), UP_DIV(output->height(), gYLocal),
UP_DIV(dst_depth_quad, gZLocal));
OPENGL_CHECK_ERROR;
}
return NO_ERROR;
}
GLCreatorRegister<TypedCreator<GLConvolutionDepthwise>> __depthwise_conv_op(OpType_ConvolutionDepthwise);
} // namespace OpenGL
} // namespace MNN