forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLConcat.cpp
More file actions
100 lines (81 loc) · 2.92 KB
/
Copy pathGLConcat.cpp
File metadata and controls
100 lines (81 loc) · 2.92 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
//
// GLConcat.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "GLConcat.hpp"
#include "AllShader.hpp"
#include "GLBackend.hpp"
#include "Macro.h"
namespace MNN {
namespace OpenGL {
GLConcat::GLConcat(const std::vector<Tensor *> &inputs, const Op *op, Backend *bn): Execution(bn) {
mAxis = op->main_as_Axis()->axis();
mProgram = ((GLBackend *)backend())->getProgram("blit", glsl_blit_glsl);
}
GLConcat::~GLConcat() {
}
ErrorCode GLConcat::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
auto outputTensor = outputs[0];
std::vector<int> outputShape = tensorShapeFormat(outputTensor);
int dx = 0;
int dy = 0;
int dz = 0;
for (int i = 0; i < inputs.size(); ++i) {
auto inputTensor = inputs[i];
std::vector<int> inputShape = tensorShapeFormat(inputTensor);
int sy = inputShape.at(1);
int sx = inputShape.at(2);
int ic = inputShape.at(3);
int sz = UP_DIV(ic, 4);
mProgram->useProgram();
glBindImageTexture(0, (GLuint)outputTensor->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, TEXTURE_FORMAT);
glBindImageTexture(1, (GLuint)inputTensor->deviceId(), 0, GL_TRUE, 0, GL_READ_ONLY, TEXTURE_FORMAT);
OPENGL_CHECK_ERROR;
glUniform3i(2, 0, 0, 0);
glUniform3i(3, dx, dy, dz);
glUniform3i(4, sx, sy, sz);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(sx, 4), UP_DIV(sy, 4), UP_DIV(sz, 4));
OPENGL_CHECK_ERROR;
if (sx != outputShape.at(2)) {
dx += sx;
} else if (sy != outputShape.at(1)) {
dy += sy;
} else {
dz += sz;
}
}
return NO_ERROR;
}
class ConcatCreator : public GLBackend::Creator {
public:
virtual ~ConcatCreator() = default;
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
auto axis = op->main_as_Axis()->axis();
if (0 > axis) {
axis = outputs[0]->dimensions() + axis;
}
for (int i = 0; i < inputs.size(); ++i) {
if (inputs[i]->getDimensionType() != Tensor::CAFFE) {
// TODO Support NHWC format
return nullptr;
}
}
if (axis == 1) {
for (int i = 0; i < inputs.size() - 1; ++i) {
if (inputs[i]->channel() % 4 != 0) {
MNN_PRINT("concat only support 4 alignment, back to cpu !!! \n");
return nullptr;
}
}
}
return new GLConcat(inputs, op, backend);
}
};
GLCreatorRegister<ConcatCreator> __concat_op(OpType_Concat);
} // namespace OpenGL
} // namespace MNN