Skip to content

Commit 0d21e99

Browse files
committed
Updated oneAPI template: io_stream kernel template.
1 parent 8445de7 commit 0d21e99

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

hls4ml/templates/oneapi/firmware/nnet_utils/nnet_activation_stream.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,33 @@ template <class data_pipe, class res_pipe, typename CONFIG_T> void linear_stream
2929
// *************************************************
3030
// ReLU Activation
3131
// *************************************************
32-
template <class data_pipe, class res_pipe, typename CONFIG_T> void relu_stream() {
32+
template <class data_pipe, class res_pipe, typename CONFIG_T>
33+
[[intel::use_stall_enable_clusters]] void relu_stream() {
34+
using namespace nnet;
35+
using ResT = typename ExtractDataType<typename ExtractPipeType<res_pipe>::value_type>::value_type;
36+
[[intel::fpga_register]] typename ExtractPipeType<res_pipe>::value_type out_data;
37+
38+
bool keep_going = true;
3339
ReLUActLoop:
34-
[[intel::initiation_interval(
35-
1)]] for (int i = 0; i < CONFIG_T::n_in / std::tuple_size<typename ExtractPipeType<res_pipe>::value_type>{}; i++) {
36-
auto in_data = data_pipe::read();
37-
typename ExtractPipeType<res_pipe>::value_type out_data;
40+
[[intel::initiation_interval(1)]]
41+
while(keep_going) {
42+
for (int i = 0; i < CONFIG_T::n_in / std::tuple_size<ResT>{}; i++) {
43+
[[intel::fpga_register]] auto in_data = data_pipe::read();
44+
ReLUPackLoop:
45+
#pragma unroll
46+
for (int j = 0; j < std::tuple_size<ResT>{}; j++) {
47+
if (in_data.data[j] > 0)
48+
out_data.data[j] = in_data.data[j];
49+
else
50+
out_data.data[j] = 0;
51+
}
3852

39-
ReLUPackLoop:
40-
#pragma unroll
41-
for (int j = 0; j < std::tuple_size<typename ExtractPipeType<res_pipe>::value_type>{}; j++) {
42-
if (in_data[j] > 0)
43-
out_data[j] = in_data[j];
44-
else
45-
out_data[j] = 0;
46-
}
53+
out_data.sop = in_data.sop;
54+
out_data.eop = in_data.eop;
55+
res_pipe::write(out_data);
4756

48-
res_pipe::write(out_data);
57+
keep_going = !in_data.eop;
58+
}
4959
}
5060
}
5161

hls4ml/templates/oneapi/firmware/nnet_utils/nnet_dense_stream.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,34 @@
77

88
namespace nnet {
99

10-
// Note: DataPack logic removed, at least in the initial version
10+
// Restartable streaming kernel implementation.
11+
// Computation is carried out in a while-1 loop as long as there is valid input.
12+
// The loop breaks when the end-of-packet signal is asserted by upstream task.
1113
template <class data_pipe, class res_pipe, typename CONFIG_T>
12-
void dense_resource_stream(typename CONFIG_T::weight_t weights, typename CONFIG_T::bias_t biases) {
14+
[[intel::use_stall_enable_clusters]] void dense_resource_stream(const typename CONFIG_T::weight_t weights, const typename CONFIG_T::bias_t biases) {
15+
using namespace nnet;
16+
using DataT = typename ExtractDataType<typename ExtractPipeType<data_pipe>::value_type>::value_type;
17+
using ResT = typename ExtractDataType<typename ExtractPipeType<res_pipe>::value_type>::value_type;
18+
19+
[[intel::fpga_register]] typename ExtractPipeType<res_pipe>::value_type resbeat;
1320

14-
[[intel::fpga_register]] typename ExtractPipeType<res_pipe>::value_type res;
15-
[[intel::fpga_register]] auto data = data_pipe::read();
16-
dense_resource<typename ExtractPipeType<data_pipe>::value_type, typename ExtractPipeType<res_pipe>::value_type,
17-
CONFIG_T>(data, res, weights, biases);
18-
res_pipe::write(res);
21+
bool keep_going = true;
22+
bool did_read_input;
23+
[[intel::initiation_interval(1)]]
24+
while (keep_going) {
25+
did_read_input = false;
26+
[[intel::fpga_register]] auto databeat = data_pipe::read(did_read_input);
27+
28+
if (did_read_input) {
29+
dense_resource<DataT, ResT, CONFIG_T>(databeat.data, resbeat.data, weights, biases);
30+
31+
resbeat.sop = databeat.sop;
32+
resbeat.eop = databeat.eop;
33+
34+
res_pipe::write(resbeat);
35+
keep_going = !databeat.eop;
36+
}
37+
}
1938
}
2039

2140
} // namespace nnet

0 commit comments

Comments
 (0)