-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConvLayer.hpp
More file actions
412 lines (372 loc) · 14.9 KB
/
Copy pathConvLayer.hpp
File metadata and controls
412 lines (372 loc) · 14.9 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
#include <cmath>
#include <stdexcept>
#include <thread>
#include <vector>
#include "layers/Layer.hpp"
namespace it_lab_ai {
class ConvolutionalLayer : public Layer {
private:
size_t stride_;
size_t pads_;
size_t dilations_;
Tensor kernel_;
Tensor bias_;
ImplType implType_;
public:
ConvolutionalLayer() : Layer(kConvolution) {
stride_ = 0;
pads_ = 0;
dilations_ = 0;
implType_ = kDefault;
}
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
const Tensor& kernel, const Tensor& bias = Tensor(),
ImplType implType = kDefault)
: Layer(kConvolution) {
stride_ = step;
pads_ = pads;
dilations_ = dilations;
kernel_ = kernel;
bias_ = bias;
implType_ = implType;
}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override { return kernel_; }
#endif
};
template <typename ValueType>
class ConvImpl : public LayerImpl<ValueType> {
private:
int input_width_;
int input_height_;
int input_flow_;
size_t stride_;
size_t pads_;
size_t dilations_;
size_t input_size_;
std::vector<ValueType> bias_;
public:
ConvImpl() = delete;
ConvImpl(size_t stride, size_t pads, size_t dilations, int input_width,
int input_height, int input_flow, size_t input_size,
const std::vector<ValueType>& bias)
: input_width_(input_width),
input_height_(input_height),
input_flow_(input_flow),
stride_(stride),
pads_(pads),
dilations_(dilations),
input_size_(input_size),
bias_(bias) {}
ConvImpl(const ConvImpl& c) = default;
std::vector<ValueType> run(
const std::vector<ValueType>& input) const override {
return input;
}
std::vector<ValueType> run(std::vector<ValueType> startmatrix, int new_rows,
int new_cols, std::vector<ValueType> startkernel,
size_t start_kernel_size, size_t kernel_size,
int center_distance) const {
std::vector<ValueType> matrix(new_rows * new_cols * input_flow_, 0);
for (int i = 0; i < input_height_; ++i) {
for (int j = 0; j < input_width_; ++j) {
for (int f = 0; f < input_flow_; ++f) {
matrix[((i + pads_) * new_cols + j + pads_) * input_flow_ + f] =
startmatrix[(i * input_width_ + j) * input_flow_ + f];
}
}
}
std::vector<ValueType> kernel(kernel_size * kernel_size, 0);
for (int i = 0; i < static_cast<int>(start_kernel_size); ++i) {
for (int j = 0; j < static_cast<int>(start_kernel_size); ++j) {
kernel[(dilations_ + i) * static_cast<int>(kernel_size) + j +
(j + 1) * dilations_] =
startkernel[i * static_cast<int>(start_kernel_size) + j];
}
}
std::vector<ValueType> outputvec;
for (int i = input_width_ + center_distance;
i < static_cast<int>(input_size_); i += static_cast<int>(stride_)) {
for (int x = 0; x < input_flow_; ++x) {
ValueType color = 0;
for (int coloms = -input_width_; coloms < input_width_ + 1;
coloms += input_width_) {
for (int str = -1; str < 2; ++str) {
if (input_width_ == 0) {
throw std::out_of_range("Input = 0");
}
auto kercol = static_cast<size_t>(coloms / input_width_ + 1);
color +=
matrix[(i + coloms + str) * input_flow_ + x] *
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
}
}
if (!bias_.empty() && static_cast<size_t>(x) < bias_.size()) {
color += bias_[x];
}
outputvec.push_back(color);
}
if ((i + center_distance + 1) % input_width_ == 0) {
if (i + input_width_ + center_distance * 2 ==
static_cast<int>(input_size_)) {
i += input_width_ + center_distance * 2 + 1;
} else {
i += input_width_ * (static_cast<int>(stride_) - 1) +
(3 - static_cast<int>(stride_));
}
}
}
return outputvec;
}
};
// NCHW -> NCHW only
template <typename ValueType>
void Conv4D(const Tensor& input, const Tensor& kernel_, const Tensor& bias_,
Tensor& output, size_t stride_, size_t pads_, size_t dilations_) {
size_t batch_size = input.get_shape()[0];
size_t in_height = input.get_shape()[2];
size_t in_width = input.get_shape()[3];
size_t in_channels = input.get_shape()[1];
size_t kernel_height = kernel_.get_shape()[0];
size_t kernel_width = kernel_.get_shape()[1];
size_t kernel_in_channels = kernel_.get_shape()[2];
size_t kernel_out_channels = kernel_.get_shape()[3];
std::vector<std::vector<std::vector<std::vector<ValueType>>>> padded_input =
std::vector<std::vector<std::vector<std::vector<ValueType>>>>(
batch_size, std::vector<std::vector<std::vector<ValueType>>>(
in_height + 2 * pads_,
std::vector<std::vector<ValueType>>(
in_width + 2 * pads_,
std::vector<ValueType>(in_channels, 0))));
for (size_t b = 0; b < batch_size; ++b) {
for (size_t h = 0; h < in_height; ++h) {
for (size_t w = 0; w < in_width; ++w) {
for (size_t c = 0; c < in_channels; ++c) {
padded_input[b][h + pads_][w + pads_][c] =
input.get<ValueType>({b, c, h, w});
}
}
}
}
std::vector<std::vector<std::vector<std::vector<ValueType>>>> dil_kernel =
std::vector<std::vector<std::vector<std::vector<ValueType>>>>(
kernel_height * dilations_ + 1 - dilations_,
std::vector<std::vector<std::vector<ValueType>>>(
kernel_width * dilations_ + 1 - dilations_,
std::vector<std::vector<ValueType>>(
kernel_in_channels,
std::vector<ValueType>(kernel_out_channels, 0))));
for (size_t b = 0; b < kernel_out_channels; ++b) {
for (size_t h = 0; h < kernel_height; ++h) {
for (size_t w = 0; w < kernel_width; ++w) {
for (size_t c = 0; c < kernel_in_channels; ++c) {
dil_kernel[h * dilations_][w * dilations_][c][b] =
kernel_.get<ValueType>({h, w, c, b});
}
}
}
}
size_t crat = 0;
if ((in_height + 2 * pads_ - dilations_ * (kernel_height - 1)) % stride_ != 0)
crat = 1;
size_t out_height =
(in_height + 2 * pads_ - dilations_ * (kernel_height - 1)) / stride_ +
crat;
crat = 0;
if ((in_width + 2 * pads_ - dilations_ * (kernel_width - 1)) % stride_ != 0)
crat = 1;
size_t out_width =
(in_width + 2 * pads_ - dilations_ * (kernel_width - 1)) / stride_ + crat;
std::vector<std::vector<std::vector<std::vector<ValueType>>>> output_tensor(
batch_size, std::vector<std::vector<std::vector<ValueType>>>(
kernel_out_channels,
std::vector<std::vector<ValueType>>(
out_height, std::vector<ValueType>(out_width, 0))));
for (size_t b = 0; b < batch_size; ++b) {
for (size_t c = 0; c < kernel_out_channels; ++c) {
for (size_t i = 0; i < out_height; i += stride_) {
for (size_t j = 0; j < out_width; j += stride_) {
ValueType value = 0;
for (size_t ic = 0; ic < in_channels; ++ic) {
for (size_t h = 0; h < kernel_height * dilations_ + 1 - dilations_;
++h) {
for (size_t w = 0; w < kernel_width * dilations_ + 1 - dilations_;
++w) {
value +=
padded_input[b][i + h][j + w][ic] * dil_kernel[h][w][ic][c];
}
}
}
if (!bias_.empty()) {
output_tensor[b][c][i][j] = value + (*bias_.as<ValueType>())[c];
} else {
output_tensor[b][c][i][j] = value;
}
}
}
}
}
Shape sh({batch_size, kernel_out_channels, out_height, out_width});
std::vector<ValueType> one_d_vector(batch_size * out_height * out_width *
kernel_out_channels);
size_t index_1d = 0;
for (size_t i = 0; i < batch_size; ++i) {
for (size_t l = 0; l < kernel_out_channels; ++l) {
for (size_t j = 0; j < out_height; ++j) {
for (size_t k = 0; k < out_width; ++k) {
one_d_vector[index_1d++] = output_tensor[i][l][j][k];
}
}
}
}
output = make_tensor<ValueType>(one_d_vector, sh);
}
// NCHW -> NCHW only
template <typename ValueType>
void Conv4DSTL(const Tensor& input, const Tensor& kernel_, const Tensor& bias_,
Tensor& output, size_t stride_, size_t pads_,
size_t dilations_) {
size_t batch_size = input.get_shape()[0];
size_t in_height = input.get_shape()[2];
size_t in_width = input.get_shape()[3];
size_t in_channels = input.get_shape()[1];
size_t kernel_height = kernel_.get_shape()[0];
size_t kernel_width = kernel_.get_shape()[1];
size_t kernel_in_channels = kernel_.get_shape()[2];
size_t kernel_out_channels = kernel_.get_shape()[3];
unsigned num_threads = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
size_t chunk_size = batch_size / num_threads;
std::vector<std::vector<std::vector<std::vector<ValueType>>>> padded_input =
std::vector<std::vector<std::vector<std::vector<ValueType>>>>(
batch_size, std::vector<std::vector<std::vector<ValueType>>>(
in_height + 2 * pads_,
std::vector<std::vector<ValueType>>(
in_width + 2 * pads_,
std::vector<ValueType>(in_channels, 0))));
auto pad_input = [&](size_t start_b, size_t end_b) {
for (size_t b = start_b; b < end_b; ++b) {
for (size_t h = 0; h < in_height; ++h) {
for (size_t w = 0; w < in_width; ++w) {
for (size_t c = 0; c < in_channels; ++c) {
padded_input[b][h + pads_][w + pads_][c] =
input.get<ValueType>({b, c, h, w});
}
}
}
}
};
for (unsigned i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end = (i == num_threads - 1) ? batch_size : start + chunk_size;
threads.emplace_back(pad_input, start, end);
}
for (auto& t : threads) t.join();
threads.clear();
std::vector<std::vector<std::vector<std::vector<ValueType>>>> dil_kernel =
std::vector<std::vector<std::vector<std::vector<ValueType>>>>(
kernel_height * dilations_ + 1 - dilations_,
std::vector<std::vector<std::vector<ValueType>>>(
kernel_width * dilations_ + 1 - dilations_,
std::vector<std::vector<ValueType>>(
kernel_in_channels,
std::vector<ValueType>(kernel_out_channels, 0))));
auto dilate_kernel = [&](size_t start_b, size_t end_b) {
for (size_t b = start_b; b < end_b; ++b) {
for (size_t h = 0; h < kernel_height; ++h) {
for (size_t w = 0; w < kernel_width; ++w) {
for (size_t c = 0; c < kernel_in_channels; ++c) {
dil_kernel[h * dilations_][w * dilations_][c][b] =
kernel_.get<ValueType>({h, w, c, b});
}
}
}
}
};
chunk_size = kernel_out_channels / num_threads;
for (unsigned i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end =
(i == num_threads - 1) ? kernel_out_channels : start + chunk_size;
threads.emplace_back(dilate_kernel, start, end);
}
for (auto& t : threads) t.join();
threads.clear();
size_t crat = 0;
if ((in_height + 2 * pads_ - dilations_ * (kernel_height - 1)) % stride_ != 0)
crat = 1;
size_t out_height =
(in_height + 2 * pads_ - dilations_ * (kernel_height - 1)) / stride_ +
crat;
crat = 0;
if ((in_width + 2 * pads_ - dilations_ * (kernel_width - 1)) % stride_ != 0)
crat = 1;
size_t out_width =
(in_width + 2 * pads_ - dilations_ * (kernel_width - 1)) / stride_ + crat;
std::vector<std::vector<std::vector<std::vector<ValueType>>>> output_tensor(
batch_size, std::vector<std::vector<std::vector<ValueType>>>(
kernel_out_channels,
std::vector<std::vector<ValueType>>(
out_height, std::vector<ValueType>(out_width, 0))));
auto compute_conv = [&](size_t start_b, size_t end_b) {
for (size_t b = start_b; b < end_b; ++b) {
for (size_t c = 0; c < kernel_out_channels; ++c) {
for (size_t i = 0; i < out_height; i += stride_) {
for (size_t j = 0; j < out_width; j += stride_) {
ValueType value = 0;
for (size_t ic = 0; ic < in_channels; ++ic) {
for (size_t h = 0;
h < kernel_height * dilations_ + 1 - dilations_; ++h) {
for (size_t w = 0;
w < kernel_width * dilations_ + 1 - dilations_; ++w) {
value += padded_input[b][i + h][j + w][ic] *
dil_kernel[h][w][ic][c];
}
}
}
if (!bias_.empty()) {
output_tensor[b][c][i][j] = value + (*bias_.as<ValueType>())[c];
} else {
output_tensor[b][c][i][j] = value;
}
}
}
}
}
};
chunk_size = batch_size / num_threads;
for (unsigned i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end = (i == num_threads - 1) ? batch_size : start + chunk_size;
threads.emplace_back(compute_conv, start, end);
}
for (auto& t : threads) t.join();
threads.clear();
Shape sh({batch_size, kernel_out_channels, out_height, out_width});
std::vector<ValueType> one_d_vector(batch_size * out_height * out_width *
kernel_out_channels);
auto flatten_output = [&](size_t start_b, size_t end_b) {
size_t index_1d = start_b * kernel_out_channels * out_height * out_width;
for (size_t i = start_b; i < end_b; ++i) {
for (size_t l = 0; l < kernel_out_channels; ++l) {
for (size_t j = 0; j < out_height; ++j) {
for (size_t k = 0; k < out_width; ++k) {
one_d_vector[index_1d++] = output_tensor[i][l][j][k];
}
}
}
}
};
chunk_size = batch_size / num_threads;
for (unsigned i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end = (i == num_threads - 1) ? batch_size : start + chunk_size;
threads.emplace_back(flatten_output, start, end);
}
for (auto& t : threads) t.join();
output = make_tensor<ValueType>(one_d_vector, sh);
}
} // namespace it_lab_ai