@@ -70,6 +70,7 @@ struct pooling1d_config {
70
70
static const unsigned n_out = (n_in - pool_width) / stride_width + 1 ;
71
71
static const unsigned pad_left = 0 ;
72
72
static const unsigned pad_right = 0 ;
73
+ static const bool count_pad = false ;
73
74
// Pooling function
74
75
static const Pool_Op pool_op = Max;
75
76
};
@@ -88,14 +89,13 @@ void pooling1d_cl(data_T data[CONFIG_T::n_in * CONFIG_T::n_filt], res_T res[CONF
88
89
CONFIG_T::pool_op, typename CONFIG_T::accum_t > limit=limit
89
90
// Add any necessary padding
90
91
91
- unsigned padded_width = CONFIG_T::n_in + CONFIG_T::pad_left + CONFIG_T::pad_right;
92
- if (CONFIG_T::pad_left == 0 && CONFIG_T::pad_right == 0 ) {
93
- padded_width -= padded_width - (padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width);
94
- }
92
+ // Add padding and reduce input width to area covered by pooling function
93
+ static constexpr int full_padded_width = CONFIG_T::n_in + CONFIG_T::pad_left + CONFIG_T::pad_right;
94
+ static constexpr int restricted_padded_width = full_padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width;
95
95
96
96
for (int ff = 0 ; ff < CONFIG_T::n_filt; ff++) {
97
97
// Loop over input image x in steps of stride
98
- for (int ii = 0 ; ii < padded_width ; ii += CONFIG_T::stride_width) {
98
+ for (int ii = 0 ; ii < restricted_padded_width ; ii += CONFIG_T::stride_width) {
99
99
unsigned overlap_pixel = 0 ;
100
100
data_T pool[CONFIG_T::pool_width];
101
101
#pragma HLS ARRAY_PARTITION variable=pool complete dim=0
@@ -130,6 +130,7 @@ void global_pooling1d_cl(data_T data[CONFIG_T::n_in * CONFIG_T::n_filt], res_T r
130
130
131
131
for (int ff = 0 ; ff < CONFIG_T::n_filt; ff++) {
132
132
data_T pool[CONFIG_T::n_in];
133
+ #pragma HLS ARRAY_PARTITION variable=pool complete dim=0
133
134
for (int jj = 0 ; jj < CONFIG_T::n_in; jj++) {
134
135
pool[jj] = data[jj * CONFIG_T::n_filt + ff];
135
136
}
@@ -154,6 +155,7 @@ struct pooling2d_config {
154
155
static const unsigned pad_bottom = 0 ;
155
156
static const unsigned pad_left = 0 ;
156
157
static const unsigned pad_right = 0 ;
158
+ static const bool count_pad = false ;
157
159
// Pooling function
158
160
static const Pool_Op pool_op = Max;
159
161
// Reuse factor
@@ -176,18 +178,17 @@ void pooling2d_cl(data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_
176
178
const int limit = pool_op_limit<CONFIG_T>();
177
179
#pragma HLS ALLOCATION function instances=pool_op<data_T, CONFIG_T::pool_height*CONFIG_T::pool_width, \
178
180
CONFIG_T::pool_op, typename CONFIG_T::accum_t > limit=limit
179
- unsigned padded_height = CONFIG_T::in_height + CONFIG_T::pad_top + CONFIG_T::pad_bottom;
180
- unsigned padded_width = CONFIG_T::in_width + CONFIG_T::pad_left + CONFIG_T::pad_right;
181
- if (CONFIG_T::pad_top == 0 && CONFIG_T::pad_bottom == 0 && CONFIG_T::pad_left == 0 && CONFIG_T::pad_right == 0 ) {
182
- padded_height -= padded_height - (padded_height / CONFIG_T::stride_height * CONFIG_T::stride_height);
183
- padded_width -= padded_width - (padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width);
184
- }
181
+ // Add padding and reduce input width to area covered by pooling function
182
+ static constexpr int full_padded_width = CONFIG_T::in_width + CONFIG_T::pad_left + CONFIG_T::pad_right;
183
+ static constexpr int full_padded_height = CONFIG_T::in_height + CONFIG_T::pad_top + CONFIG_T::pad_bottom;
184
+ static constexpr int restricted_padded_width = full_padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width;
185
+ static constexpr int restricted_padded_height = full_padded_height / CONFIG_T::stride_height * CONFIG_T::stride_height;
185
186
186
187
for (int ff = 0 ; ff < CONFIG_T::n_filt; ff++) {
187
188
// Loop over input image y in steps of stride
188
- for (int ii = 0 ; ii < padded_height ; ii += CONFIG_T::stride_height) {
189
+ for (int ii = 0 ; ii < restricted_padded_height ; ii += CONFIG_T::stride_height) {
189
190
// Loop over input image x in steps of stride
190
- for (int jj = 0 ; jj < padded_width ; jj += CONFIG_T::stride_width) {
191
+ for (int jj = 0 ; jj < restricted_padded_width ; jj += CONFIG_T::stride_width) {
191
192
data_T pool[CONFIG_T::pool_height * CONFIG_T::pool_width];
192
193
#pragma HLS ARRAY_PARTITION variable=pool complete dim=0
193
194
@@ -231,34 +232,35 @@ void pooling2d_cf(data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_
231
232
const int limit = pool_op_limit<CONFIG_T>();
232
233
#pragma HLS ALLOCATION function instances=pool_op<data_T, CONFIG_T::pool_height*CONFIG_T::pool_width, \
233
234
CONFIG_T::pool_op, typename CONFIG_T::accum_t > limit=limit
234
- // Add any necessary padding
235
- unsigned padded_height = CONFIG_T::in_height + CONFIG_T::pad_top + CONFIG_T::pad_bottom;
236
- unsigned padded_width = CONFIG_T::in_width + CONFIG_T::pad_left + CONFIG_T::pad_right;
237
- if (CONFIG_T::pad_top == 0 && CONFIG_T::pad_bottom == 0 && CONFIG_T::pad_left == 0 && CONFIG_T::pad_right == 0 ) {
238
- padded_height -= padded_height - (padded_height / CONFIG_T::stride_height * CONFIG_T::stride_height);
239
- padded_width -= padded_width - (padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width);
240
- }
235
+ // Add padding and reduce input width to area covered by pooling function
236
+ static constexpr int full_padded_width = CONFIG_T::in_width + CONFIG_T::pad_left + CONFIG_T::pad_right;
237
+ static constexpr int full_padded_height = CONFIG_T::in_height + CONFIG_T::pad_top + CONFIG_T::pad_bottom;
238
+ static constexpr int restricted_padded_width = full_padded_width / CONFIG_T::stride_width * CONFIG_T::stride_width;
239
+ static constexpr int restricted_padded_height = full_padded_height / CONFIG_T::stride_height * CONFIG_T::stride_height;
241
240
242
241
for (int ff = 0 ; ff < CONFIG_T::n_filt; ff++) {
243
242
// Loop over input image y in steps of stride
244
- for (int ii = 0 ; ii < padded_height ; ii += CONFIG_T::stride_height) {
243
+ for (int ii = 0 ; ii < restricted_padded_height ; ii += CONFIG_T::stride_height) {
245
244
// Loop over input image x in steps of stride
246
- for (int jj = 0 ; jj < padded_width ; jj += CONFIG_T::stride_width) {
245
+ for (int jj = 0 ; jj < restricted_padded_width ; jj += CONFIG_T::stride_width) {
247
246
data_T pool[CONFIG_T::pool_height * CONFIG_T::pool_width];
247
+ #pragma HLS ARRAY_PARTITION variable=pool complete dim=0
248
248
// Keep track of number of pixels in image vs padding region
249
249
unsigned img_overlap = 0 ;
250
250
// Loop over pool window y
251
251
for (int kk = 0 ; kk < CONFIG_T::stride_height; kk++) {
252
252
// Loop over pool window x
253
253
for (int ll = 0 ; ll < CONFIG_T::stride_width; ll++) {
254
- if (ii + kk < CONFIG_T::pad_top || ii + kk >= (padded_height - CONFIG_T::pad_bottom) ||
255
- jj + ll < CONFIG_T::pad_left || jj + ll >= (padded_width - CONFIG_T::pad_right)) {
254
+ if (ii + kk < CONFIG_T::pad_top || ii + kk >= (full_padded_height - CONFIG_T::pad_bottom) ||
255
+ jj + ll < CONFIG_T::pad_left || jj + ll >= (full_padded_width - CONFIG_T::pad_right)) {
256
256
// Add padding
257
257
pool[kk * CONFIG_T::stride_width + ll] = pad_val<data_T, CONFIG_T::pool_op>();
258
+ if (CONFIG_T::count_pad)
259
+ img_overlap++;
258
260
} else {
259
261
pool[kk * CONFIG_T::stride_width + ll] =
260
- data[(ii + kk) * CONFIG_T::in_width + ff * CONFIG_T::in_width * CONFIG_T::in_height + ll +
261
- jj ];
262
+ data[(ii + kk - CONFIG_T::pad_top) * CONFIG_T::in_width +
263
+ ff * CONFIG_T::in_width * CONFIG_T::in_height + ll + jj - CONFIG_T::pad_left ];
262
264
img_overlap++;
263
265
}
264
266
}
0 commit comments