-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvlayer_im2row.hpp
312 lines (269 loc) · 12.6 KB
/
convlayer_im2row.hpp
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
#ifndef __STANN_CONV_IM2ROW_HPP__
#define __STANN_CONV_IM2ROW_HPP__
#include "stann.hpp"
/**
* Namespace for conolutional layers.
*/
namespace ConvLayer {
/**
* im2row implementation of convolutional layer.
*/
namespace im2row {
/**
* Anonymous namespace for internal functions.
*/
namespace {
/**
* Adding the bias values after the main convolution.
*
* @tparam INPUT_HEIGHT height of input image
* @tparam INPUT_WIDTH width of input image
* @tparam OUTPUT_CHANNELS number of output channels (equal to number of input channels)
* @tparam KERNEL_SIZE size of kernel (NxN)
*
* @param[in] input input stream
* @param{in] biases biases
* @param[out] output output stream
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_HEIGHT, int INPUT_WIDTH, int OUTPUT_CHANNELS, int KERNEL_SIZE>
void add_bias(hls::stream<float> &input, float *biases, hls::stream<float> &output, int reps) {
const int KSUB = (KERNEL_SIZE / 2) * 2;
for (int r = 0; r < reps; r++) {
for (int m = 0; m < OUTPUT_CHANNELS; m++) {
for (int i = 0; i < (INPUT_WIDTH-KSUB) * (INPUT_HEIGHT-KSUB); i++) {
float val = input.read();
val += biases[m];
output.write(val);
}
}
}
}
/**
* Applying activation functions.
*
* @tparam DIM size of inputs and outputs
*
* @param[in] input input stream
* @param[out] output output stream
* @param[in] act activation constant
* @param[in] reps number of repetitions
*/
template<int DIM>
void apply_activation_float(hls::stream<float> &input, hls::stream<float> &output, activation_t act, int reps) {
for (int r = 0; r < reps; r++) {
for (int i = 0; i < DIM; i++) {
float val = input.read();
float out_val = val;
if (act == LEAKY_RELU) {
out_val = Activation::leaky_relu_simple(val);
} else if (act == RELU) {
out_val = Activation::relu_simple(val);
} else if (act == LIN_TANH) {
out_val = Activation::lin_tanh_simple(val);
}
output.write(out_val);
}
}
}
/**
* Applying activation functions. Quantized to uint8.
*
* @tparam DIM size of inputs and outputs
*
* @param[in] input input stream
* @param[out] output output stream
* @param[in] act activation constant
* @param[in] reps number of repetitions
*/
template<int DIM>
void apply_activation_quantized(hls::stream<ap_uint<8>> &input, hls::stream<ap_uint<8>> &output, int reps) {
for (int r = 0; r < reps; r++) {
for (int i = 0; i < DIM; i++) {
ap_uint<8> val = input.read();
ap_uint<8> out_val = val;
ap_uint<8> lower = 10;
if (out_val < lower) {
out_val = 0;
}
output.write(out_val);
}
}
}
/**
* im2row preparation function for the input.
*
* @tparam INPUT_WI width of input image
* @tparam INPUT_HI height of input image
* @tparam INPUT_CHANNELS number of input channels
* @tparam FILTER_SIZE size of kernel (NxN)
*
* @param[in] input input stream
* @param[out] output output stream
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_WI,int INPUT_HI,int INPUT_CHANNEL, int FILTER_SIZE, typename T>
void im2row(hls::stream<T> &input, hls::stream<T> &output, int reps) {
const int OUTPUT_WI = INPUT_WI - FILTER_SIZE + 1;
const int OUTPUT_HI = INPUT_HI - FILTER_SIZE + 1;
T input_buffer[INPUT_WI*INPUT_HI*INPUT_CHANNEL];
T output_buffer[FILTER_SIZE*FILTER_SIZE*INPUT_CHANNEL*OUTPUT_WI*OUTPUT_HI];
for (int r = 0; r < reps; r++) {
StreamUtil::toarray<INPUT_WI*INPUT_HI*INPUT_CHANNEL, T>(input, input_buffer, 1);
for (int ic = 0; ic < INPUT_CHANNEL; ic++){
for (int oh = 0; oh < OUTPUT_HI; oh++){
for (int ow = 0; ow < OUTPUT_WI; ow++){
for (int fw = 0; fw < FILTER_SIZE; fw++){
for (int fh = 0; fh < FILTER_SIZE; fh++){
#pragma HLS pipeline II=3
output_buffer[ow + oh * OUTPUT_WI + fw * OUTPUT_HI * OUTPUT_WI + fh*OUTPUT_HI * OUTPUT_WI*FILTER_SIZE + ic *OUTPUT_HI * OUTPUT_WI*FILTER_SIZE*FILTER_SIZE ]
= input_buffer[ic * INPUT_WI * INPUT_HI + (oh+fh) * INPUT_WI + (ow+fw)];
//= input_buffer[ow + oh + oh * OUTPUT_WI + fw + fh * INPUT_WI + ic * INPUT_WI * INPUT_HI ];
}
}
}
}
}
StreamUtil::tostream<FILTER_SIZE*FILTER_SIZE*INPUT_CHANNEL*OUTPUT_WI*OUTPUT_HI, T>(output_buffer, output, 1);
}
}
/**
* Internal base function for convolution inference.
*
* @tparam INPUT_HEIGHT height of input image
* @tparam INPUT_WIDTH width of input image
* @tparam INPUT_CHANNELS number of input channels
* @tparam OUTPUT_CHANNELS number of output channels
* @tparam KERNEL_SIZE size of kernel (NxN)
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
*
* @param[in] input input stream
* @param{in] kernel kernel weights
* @param[out] output output stream
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_HEIGHT, int INPUT_WIDTH, int INPUT_CHANNELS, int OUTPUT_CHANNELS, int KERNEL_SIZE, int PE1, int PE2, int PE3>
void conv_base(hls::stream<float> &input, float *kernel, hls::stream<float> &output, int reps) {
const int OUTPUT_WI = INPUT_WIDTH - KERNEL_SIZE + 1;
const int OUTPUT_HI = INPUT_HEIGHT - KERNEL_SIZE + 1;
float input_buffer[KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS*OUTPUT_WI*OUTPUT_HI];
float output_buffer[OUTPUT_WI * OUTPUT_HI * OUTPUT_CHANNELS];
for (int r = 0; r < reps; r++) {
StreamUtil::toarray<KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS*OUTPUT_WI*OUTPUT_HI>(input, input_buffer, 1);
// standard image sizes: C x H x W
// direct convolution kernel format: OC x IC x K^2
// Kernel matrix: OC x (K^2 * IC)
// im2row matrix: (K^2 * IC) x (H * W)
// Output: OC x (H * W)
Matrix::blockmatmul<OUTPUT_CHANNELS,KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS,OUTPUT_WI*OUTPUT_HI,PE1,PE2,PE3,float,80>(kernel, input_buffer, output_buffer);
StreamUtil::tostream<OUTPUT_WI * OUTPUT_HI * OUTPUT_CHANNELS>(output_buffer, output, 1);
}
}
/**
* Internal base function for convolution inference.
*
* @tparam INPUT_HEIGHT height of input image
* @tparam INPUT_WIDTH width of input image
* @tparam INPUT_CHANNELS number of input channels
* @tparam OUTPUT_CHANNELS number of output channels
* @tparam KERNEL_SIZE size of kernel (NxN)
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
*
* @param[in] input input stream
* @param{in] kernel kernel weights
* @param{in] biases biases
* @param{in] m constant for quantized computations
* @param{in] n constant for quantized computations
* @param{in] z1 constant for quantized computations
* @param{in] z2 constant for quantized computations
* @param{in] z3 constant for quantized computations
* @param[out] output output stream
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_HEIGHT, int INPUT_WIDTH, int INPUT_CHANNELS, int OUTPUT_CHANNELS, int KERNEL_SIZE, int PE1, int PE2, int PE3>
void conv_base_quantized(hls::stream<ap_uint<8>> &input, ap_uint<8> *kernel, hls::stream<ap_uint<8>> &output, ap_uint<32> *biases, ap_uint<32> m, ap_uint<32> n, ap_uint<8> z1, ap_uint<8> z2, ap_uint<8> z3, int reps) {
const int OUTPUT_WI = INPUT_WIDTH - KERNEL_SIZE + 1;
const int OUTPUT_HI = INPUT_HEIGHT - KERNEL_SIZE + 1;
ap_uint<8> input_buffer[KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS*OUTPUT_WI*OUTPUT_HI];
ap_uint<8> output_buffer[OUTPUT_WI * OUTPUT_HI * OUTPUT_CHANNELS];
for (int r = 0; r < reps; r++) {
StreamUtil::toarray<KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS*OUTPUT_WI*OUTPUT_HI, ap_uint<8>>(input, input_buffer, 1);
Matrix::blockmatmul_quantized<OUTPUT_CHANNELS,KERNEL_SIZE*KERNEL_SIZE*INPUT_CHANNELS,OUTPUT_WI*OUTPUT_HI,PE1,PE2,PE3,5>(kernel, input_buffer, output_buffer, biases, m, n, z1, z2, z3);
StreamUtil::tostream<OUTPUT_WI * OUTPUT_HI * OUTPUT_CHANNELS, ap_uint<8>>(output_buffer, output, 1);
}
}
}
namespace Float {
/**
* Internal base function for convolution inference.
*
* @tparam INPUT_HEIGHT height of input image
* @tparam INPUT_WIDTH width of input image
* @tparam INPUT_CHANNELS number of input channels
* @tparam OUTPUT_CHANNELS number of output channels
* @tparam KERNEL_SIZE size of kernel (NxN)
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
*
* @param[in] input input stream
* @param{in] kernel kernel weights
* @param{in] bias biases
* @param[out] output output stream
* @param[in] act activation constant
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_HEIGHT, int INPUT_WIDTH, int INPUT_CHANNELS, int OUTPUT_CHANNELS, int KERNEL_SIZE, int PE1, int PE2, int PE3>
void forward(hls::stream<float> &input, float *kernel, float *bias, hls::stream<float> &output, activation_t act, int reps) {
#pragma HLS Dataflow
hls::stream<float> im2row_stream;
hls::stream<float> output_nobias;
hls::stream<float> output_noact;
im2row<INPUT_WIDTH, INPUT_HEIGHT, INPUT_CHANNELS, KERNEL_SIZE, float>(input, im2row_stream, reps);
conv_base<INPUT_HEIGHT, INPUT_WIDTH, INPUT_CHANNELS, OUTPUT_CHANNELS, KERNEL_SIZE, PE1, PE2, PE3>(im2row_stream, kernel, output_nobias, reps);
add_bias<INPUT_HEIGHT, INPUT_WIDTH, OUTPUT_CHANNELS, KERNEL_SIZE>(output_nobias, bias, output_noact, reps);
apply_activation_float<(INPUT_HEIGHT-KERNEL_SIZE+1)*(INPUT_WIDTH-KERNEL_SIZE+1)*OUTPUT_CHANNELS>(output_noact, output, act, reps);
}
} // namespace Float
namespace UInt8 {
/**
* Internal base function for convolution inference.
*
* @tparam INPUT_HEIGHT height of input image
* @tparam INPUT_WIDTH width of input image
* @tparam INPUT_CHANNELS number of input channels
* @tparam OUTPUT_CHANNELS number of output channels
* @tparam KERNEL_SIZE size of kernel (NxN)
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
* @tparam PE1 constant for paralellism
*
* @param[in] input input stream
* @param{in] kernel kernel weights
* @param{in] bias biases
* @param[out] output output stream
* @param[in] act activation constant
* @param{in] m constant for quantized computations
* @param{in] n constant for quantized computations
* @param{in] z1 constant for quantized computations
* @param{in] z2 constant for quantized computations
* @param{in] z3 constant for quantized computations
* @param[in] reps number of repetitions (similar to batch size)
*/
template<int INPUT_HEIGHT, int INPUT_WIDTH, int INPUT_CHANNELS, int OUTPUT_CHANNELS, int KERNEL_SIZE, int PE1, int PE2, int PE3>
void forward(hls::stream<ap_uint<8>> &input, ap_uint<8> *kernel, ap_uint<32> *bias, hls::stream<ap_uint<8>> &output, activation_t act, ap_uint<32> m, ap_uint<32> n, ap_uint<8> z1, ap_uint<8> z2, ap_uint<8> z3, int reps) {
#pragma HLS Dataflow
hls::stream<ap_uint<8>> im2row_stream;
hls::stream<ap_uint<8>> output_noact;
im2row<INPUT_WIDTH, INPUT_HEIGHT, INPUT_CHANNELS, KERNEL_SIZE, ap_uint<8>>(input, im2row_stream, reps);
conv_base_quantized<INPUT_HEIGHT, INPUT_WIDTH, INPUT_CHANNELS, OUTPUT_CHANNELS, KERNEL_SIZE, PE1, PE2, PE3>(im2row_stream, kernel, output_noact, bias, m, n, z1, z2, z3, reps);
apply_activation_quantized<(INPUT_HEIGHT-KERNEL_SIZE+1)*(INPUT_WIDTH-KERNEL_SIZE+1)*OUTPUT_CHANNELS>(output_noact, output, reps);
}
} // namespace UInt8
} // namespace im2row
} // namespace ConvLayer
#endif