Skip to content

Commit 5ecab69

Browse files
committed
Pointwise conv2d for resource strategy
1 parent 62662fa commit 5ecab69

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

hls4ml/templates/vivado/nnet_utils/nnet_conv2d.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ void conv_2d_cl(
8686
}
8787
}
8888

89+
template<class data_T, class res_T, typename CONFIG_T>
90+
void pointwise_conv_2d_cl(
91+
data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_T::n_chan],
92+
res_T res[CONFIG_T::out_height * CONFIG_T::out_width * CONFIG_T::n_filt],
93+
typename CONFIG_T::weight_t weights[CONFIG_T::n_chan * CONFIG_T::n_filt],
94+
typename CONFIG_T::bias_t biases[CONFIG_T::n_filt])
95+
{
96+
assert(CONFIG_T::filt_width == 1);
97+
98+
if (CONFIG_T::strategy == nnet::latency) {
99+
pointwise_conv_2d_latency_cl<data_T, res_T, CONFIG_T>(data, res, weights, biases);
100+
} else {
101+
pointwise_conv_2d_resource_cl<data_T, res_T, CONFIG_T>(data, res, weights, biases);
102+
}
103+
}
104+
89105
}//end namespace
90106

91107
#endif

hls4ml/templates/vivado/nnet_utils/nnet_conv2d_latency.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void conv_2d_latency_cl(
282282
}//end conv2d
283283

284284
template<class data_T, class res_T, typename CONFIG_T>
285-
void pointwise_conv_2d_cl(
285+
void pointwise_conv_2d_latency_cl(
286286
data_T data[CONFIG_T::in_height*CONFIG_T::in_width*CONFIG_T::n_chan],
287287
res_T res[CONFIG_T::out_height*CONFIG_T::out_width*CONFIG_T::n_filt],
288288
typename CONFIG_T::weight_t weights[CONFIG_T::n_chan * CONFIG_T::n_filt],

hls4ml/templates/vivado/nnet_utils/nnet_conv2d_resource.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,32 @@ void im2col_2d_cl(
174174
}
175175
}
176176

177+
template<class data_T, typename CONFIG_T>
178+
void im2col_2d_pointwise_cl(
179+
data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_T::n_chan],
180+
data_T data_col[CONFIG_T::n_chan],
181+
const int row,
182+
const int col)
183+
{
184+
int index = 0;
185+
int input_row = -CONFIG_T::pad_top + row * CONFIG_T::stride_height;
186+
187+
ChannelLoop:
188+
for (int channel = 0; channel < CONFIG_T::n_chan; channel++) {
189+
#pragma HLS UNROLL
190+
if (input_row < 0 || input_row >= CONFIG_T::in_height) {
191+
data_col[index++] = 0;
192+
} else {
193+
int input_col = -CONFIG_T::pad_left + col * CONFIG_T::stride_width;
194+
if (input_col >= 0 && input_col < CONFIG_T::in_width) {
195+
data_col[index++] = data[input_row * CONFIG_T::in_width * CONFIG_T::n_chan + input_col * CONFIG_T::n_chan + channel];
196+
} else {
197+
data_col[index++] = 0;
198+
}
199+
}
200+
}
201+
}
202+
177203
template<class data_T, class res_T, typename CONFIG_T>
178204
void conv_2d_resource_cl(
179205
data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_T::n_chan],
@@ -212,5 +238,47 @@ void conv_2d_resource_cl(
212238
}
213239
}
214240

241+
242+
template<class data_T, class res_T, typename CONFIG_T>
243+
void pointwise_conv_2d_resource_cl(
244+
data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_T::n_chan],
245+
res_T res[CONFIG_T::out_height * CONFIG_T::out_width * CONFIG_T::n_filt],
246+
typename CONFIG_T::weight_t weights[CONFIG_T::n_chan * CONFIG_T::n_filt],
247+
typename CONFIG_T::bias_t biases[CONFIG_T::n_filt]
248+
)
249+
{
250+
assert(CONFIG_T::filt_height == 1 && CONFIG_T::filt_width == 1);
251+
252+
const int nin = CONFIG_T::n_chan;
253+
const int nout = CONFIG_T::n_filt;
254+
const int rufactor = CONFIG_T::reuse_factor;
255+
const int block_factor = DIV_ROUNDUP(nin*nout, rufactor);
256+
257+
//#pragma HLS function_instantiate variable=weights,biases
258+
//#pragma HLS RESOURCE variable=weights core=RAM_2P_BRAM Commenting out the deisgnation HLS seems to choose correctly
259+
//#pragma HLS ARRAY_RESHAPE variable=weights block factor=block_factor
260+
//#pragma HLS ARRAY_PARTITION variable=biases complete
261+
262+
data_T data_col[CONFIG_T::n_chan];
263+
res_T res_col[CONFIG_T::n_filt];
264+
265+
#pragma HLS ARRAY_PARTITION variable=data_col complete
266+
#pragma HLS ARRAY_PARTITION variable=res_col complete
267+
268+
HeightLoop:
269+
for (int i = 0; i < CONFIG_T::out_height; i++) {
270+
WidthLoop:
271+
for (int j = 0; j < CONFIG_T::out_width; j++) {
272+
#pragma HLS PIPELINE
273+
im2col_2d_pointwise_cl<data_T, CONFIG_T>(data, data_col, i, j);
274+
dense<data_T, res_T, typename CONFIG_T::mult_config>(data_col, res_col, weights, biases);
275+
FiltLoop:
276+
for (int k = 0; k < CONFIG_T::n_filt; k++) {
277+
res[i * CONFIG_T::out_width * CONFIG_T::n_filt + j * CONFIG_T::n_filt + k] = res_col[k];
278+
}
279+
}
280+
}
281+
}
282+
215283
}
216284
#endif

0 commit comments

Comments
 (0)