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
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
#include "../../precomp.hpp"
#include "common.hpp"
#include "internal.hpp"
#include "../include/op_concat.hpp"
namespace cv { namespace dnn { namespace vkcom {
#ifdef HAVE_VULKAN
#define LOCAL_SZ_X 256
struct ConcatParam {
int out_concat_axis;
int accumulated_concat_axis;
int concat_size;
int total_concat_size;
int thread_num;
};
OpConcat::OpConcat(const int axis)
{
init(axis);
type_ = "Concat";
}
bool OpConcat::init(const int axis)
{
axis_ = axis;
#define BUFFER_NUM 2
OpBase::initVulkanThing(BUFFER_NUM);
return true;
}
void OpConcat::reshapeOutTensor(std::vector<Tensor *>& in, Tensor& out)
{
int sum_axis = 0;
for (int i = 0; i < in.size(); ++i)
{
sum_axis += in[i]->dimSize(axis_);
}
Shape shape = in[0]->getShape();
shape[axis_] = sum_axis;
out.reshape(NULL, shape);
}
bool OpConcat::forward(std::vector<Tensor>& ins,
std::vector<Tensor>& blobs,
std::vector<Tensor>& outs)
{
return forward(ins, outs[0]);
}
bool OpConcat::forward(std::vector<Tensor>& ins, Tensor& out)
{
int input_num = ins.size();
Tensor& first_tensor = ins[0];
int sum_axis = first_tensor.dimSize(axis_);
int dim_num = first_tensor.dimNum();
for (int i = 1; i < input_num; ++i)
{
Tensor& tensor = ins[i];
assert(tensor.dimNum() == dim_num);
for (int d = 0; d < dim_num; ++d)
{
if (d == axis_)
{
sum_axis += tensor.dimSize(axis_);;
}
else
{
assert(first_tensor.dimSize(d) == tensor.dimSize(d));
}
}
}
assert(out.dimSize(axis_) == sum_axis);
for (int d = 0; d < dim_num; ++d)
{
if (d != axis_)
{
assert(out.dimSize(d) == first_tensor.dimSize(d));
}
}
out_concat_axis_ = sum_axis;
concat_size_ = out.count(axis_ + 1);
if (pipeline_ == VK_NULL_HANDLE)
{
config_.local_size_x = LOCAL_SZ_X;
config_.block_height = 1;
config_.block_width = 1;
config_.block_depth = 1;
createShaderModule(concat_spv, sizeof(concat_spv));
createPipeline(sizeof(ConcatParam));
}
accumulated_concat_axis_ = 0;
for (int i = 0; i < input_num; i++)
{
bindTensor(device_, ins[i], 0, descriptor_set_);
bindTensor(device_, out, 1, descriptor_set_);
total_concat_size_ = ins[i].count(axis_);
thread_num_ = ins[i].count();
computeGroupCount();
ConcatParam param = {out_concat_axis_,
accumulated_concat_axis_,
concat_size_,
total_concat_size_,
thread_num_};
recordCommandBuffer((void *)¶m, sizeof(ConcatParam));
runCommandBuffer();
accumulated_concat_axis_ += ins[i].dimSize(axis_);
}
return true;
}
bool OpConcat::computeGroupCount()
{
group_x_ = alignSize(thread_num_, config_.local_size_x) / config_.local_size_x;
group_y_ = 1;
group_z_ = 1;
return true;
}
#endif // HAVE_VULKAN
}}} // namespace cv::dnn::vkcom