Skip to content

Commit

Permalink
Fix up some pragmas to align unrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
David Burnette committed Apr 30, 2024
1 parent b25f41a commit e753456
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hls4ml/templates/catapult/nnet_utils/nnet_conv2d_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void conv_2d_encoded_cl(
constexpr int ce_reuse_factor =
CONFIG_T::reuse_factor * (CONFIG_T::strategy == nnet::latency && data_T::size / CONFIG_T::n_chan == 1);
(void)ce_reuse_factor;
#pragma hls_pipeline_init_interval ce_reuse_factor
#pragma hls_pipeline_init_interval 1
ReadInputHeight:
for (unsigned i_ih = 0; i_ih < CONFIG_T::in_height; i_ih++) {
ReadInputWidth:
Expand Down Expand Up @@ -82,7 +82,7 @@ void conv_2d_buffer_cl(

constexpr int ce_reuse_factor = CONFIG_T::reuse_factor * (CONFIG_T::strategy == nnet::latency);
(void)ce_reuse_factor;
#pragma hls_pipeline_init_interval ce_reuse_factor
#pragma hls_pipeline_init_interval 1
ReadInputHeight:
for (unsigned i_ih = 0; i_ih < CONFIG_T::in_height; i_ih++) {
ReadInputWidth:
Expand Down
12 changes: 6 additions & 6 deletions hls4ml/templates/catapult/nnet_utils/nnet_dense_latency.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ void dense_latency(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_out],
typename CONFIG_T::weight_t weights[CONFIG_T::n_in * CONFIG_T::n_out],
typename CONFIG_T::bias_t biases[CONFIG_T::n_out]) {
constexpr int ce_reuse_factor = CONFIG_T::reuse_factor;

// Partial unroll config
constexpr int prod1_unroll =
(ce_reuse_factor < CONFIG_T::n_in) ? CONFIG_T::n_in : (int)(CONFIG_T::n_in * CONFIG_T::n_out) / ce_reuse_factor;
constexpr int prod2_unroll = (int)CONFIG_T::n_out / ce_reuse_factor;

(void)ce_reuse_factor; // to silence compiler warnings
(void)prod1_unroll;
(void)prod2_unroll;
(void)prod1_unroll;

// For Catapult, add an extra scope so that we can apply the pipeline pragma as if it applied to the function
#pragma hls_pipeline_init_interval ce_reuse_factor
Expand Down Expand Up @@ -68,17 +69,17 @@ void dense_latency(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_out],
}

// Initialize accumulator with input biases
#pragma hls_unroll
#pragma hls_unroll prod2_unroll
ResetAccum:
for (unsigned int iacc = 0; iacc < CONFIG_T::n_out; iacc++) {
acc[iacc] = (typename CONFIG_T::accum_t)biases[iacc];
}

// Accumulate multiplication result
#pragma hls_unroll
#pragma hls_unroll prod1_unroll
Accum1:
for (unsigned int ii = 0; ii < CONFIG_T::n_in; ii++) {
#pragma hls_unroll
#pragma hls_unroll prod2_unroll
Accum2:
for (unsigned int jj = 0; jj < CONFIG_T::n_out; jj++) {
int index = ii * CONFIG_T::n_out + jj;
Expand All @@ -87,15 +88,14 @@ void dense_latency(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_out],
}

// Cast to "res_t" type
#pragma hls_unroll
#pragma hls_unroll prod2_unroll
Result:
for (unsigned int ires = 0; ires < CONFIG_T::n_out; ires++) {
// res[ires] = (res_T) (acc[ires]);
res[ires] = cast<data_T, res_T, CONFIG_T>(acc[ires]);
}
} while (false); // one iteration loop
}

} // namespace nnet

#endif

0 comments on commit e753456

Please sign in to comment.