Skip to content

Commit 8ebeefe

Browse files
committed
jovan comments
1 parent 364a0b7 commit 8ebeefe

File tree

4 files changed

+62
-61
lines changed

4 files changed

+62
-61
lines changed

hls4ml/backends/vivado/passes/pointwise_codegen.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,65 @@
33
from hls4ml.model.types import Source
44

55

6+
def generate_pointwise_conv1d_fn(layer_idx, reuse_factor=1):
7+
"""Generate a C++ function for a pointwise convolution layer.
8+
9+
Args:
10+
layer_idx (int): Index of layer ('index' attribute).
11+
reuse_factor (int): Number of partitions to divide the input into.
12+
13+
Returns:
14+
str: Generated C++ function
15+
"""
16+
17+
generated_code = (
18+
"template<class data_T, class res_T, typename CONFIG_T>\n"
19+
"class pointwise_conv_{index} : public PointwiseConv1D<data_T, res_T, CONFIG_T> {{\n"
20+
" public:\n"
21+
" static void pointwise_conv(\n"
22+
" data_T data[CONFIG_T::in_width * CONFIG_T::n_chan],\n"
23+
" res_T res[CONFIG_T::out_width * CONFIG_T::n_filt],\n"
24+
" typename CONFIG_T::weight_t weights[CONFIG_T::n_chan * CONFIG_T::n_filt],\n"
25+
" typename CONFIG_T::bias_t biases[CONFIG_T::n_filt]) {{\n"
26+
" data_T data_tmp[CONFIG_T::reuse_factor][CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor];\n" # noqa: E501
27+
" #pragma HLS ARRAY_PARTITION variable=data_tmp complete dim=0\n"
28+
" res_T res_tmp[CONFIG_T::reuse_factor][CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor];\n" # noqa: E501
29+
" #pragma HLS ARRAY_PARTITION variable=res_tmp complete dim=0\n\n"
30+
" RFInputLoop:\n"
31+
" for (int jj = 0; jj < CONFIG_T::reuse_factor; jj++) {{\n"
32+
" #pragma HLS UNROLL\n"
33+
" InnerInputLoop:\n"
34+
" for (int ii = 0; ii < CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor; ii++) {{\n"
35+
" #pragma HLS UNROLL\n"
36+
" data_tmp[jj][ii] = data[jj * CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor + ii];\n" # noqa: E501
37+
" }}\n"
38+
" }}\n\n"
39+
).format(index=layer_idx)
40+
indent = " "
41+
for i in range(reuse_factor):
42+
generated_code += indent
43+
generated_code += (
44+
f"pointwise_conv_1d_latency_cl<data_T, res_T, CONFIG_T>(data_tmp[{i}], res_tmp[{i}], weights, biases);\n"
45+
)
46+
47+
generated_code += (
48+
"\n"
49+
" RFOutputLoop:\n"
50+
" for (int jj = 0; jj < CONFIG_T::reuse_factor; jj++) {\n"
51+
" #pragma HLS UNROLL\n"
52+
" InnerOutputLoop:\n"
53+
" for (int ii = 0; ii < CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor; ii++) {\n"
54+
" #pragma HLS UNROLL\n"
55+
" res[jj * CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor + ii] = res_tmp[jj][ii];\n" # noqa: E501
56+
" }\n"
57+
" }\n"
58+
" }\n"
59+
"};\n"
60+
)
61+
62+
return generated_code
63+
64+
665
class GeneratePointwiseConv1D(OptimizerPass):
766
'''Generates code for pointwise 1D convolution'''
867

@@ -17,7 +76,7 @@ def transform(self, model, node):
1776
raise Exception(f'Cannot generate instructions for node {node.name} ({node_class})')
1877

1978
def _generate_pointwise_conv1d(self, node):
20-
code_str = node.model.config.backend.generate_pointwise_conv1d_fn(
79+
code_str = generate_pointwise_conv1d_fn(
2180
node.get_attr('index'),
2281
node.get_attr('reuse_factor'),
2382
)

hls4ml/backends/vivado/vivado_backend.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -649,61 +649,3 @@ def init_garnet(self, layer):
649649
@layer_optimizer(GarNetStack)
650650
def init_garnet_stack(self, layer):
651651
self.init_garnet(layer)
652-
653-
def generate_pointwise_conv1d_fn(self, layer_idx, reuse_factor=1):
654-
"""Generate a C++ function for a pointwise convolution layer.
655-
656-
Args:
657-
layer_idx (int): Index of layer ('index' attribute).
658-
reuse_factor (int): Number of partitions to divide the input into.
659-
660-
Returns:
661-
str: Generated C++ function
662-
"""
663-
664-
generated_code = (
665-
"template<class data_T, class res_T, typename CONFIG_T>\n"
666-
"class pointwise_conv_{index} : public PointwiseConv1D<data_T, res_T, CONFIG_T> {{\n"
667-
" public:\n"
668-
" static void pointwise_conv(\n"
669-
" data_T data[CONFIG_T::in_width * CONFIG_T::n_chan],\n"
670-
" res_T res[CONFIG_T::out_width * CONFIG_T::n_filt],\n"
671-
" typename CONFIG_T::weight_t weights[CONFIG_T::n_chan * CONFIG_T::n_filt],\n"
672-
" typename CONFIG_T::bias_t biases[CONFIG_T::n_filt]) {{\n"
673-
" data_T data_tmp[CONFIG_T::reuse_factor][CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor];\n" # noqa: E501
674-
" #pragma HLS ARRAY_PARTITION variable=data_tmp complete dim=0\n"
675-
" res_T res_tmp[CONFIG_T::reuse_factor][CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor];\n" # noqa: E501
676-
" #pragma HLS ARRAY_PARTITION variable=res_tmp complete dim=0\n\n"
677-
" RFInputLoop:\n"
678-
" for (int jj = 0; jj < CONFIG_T::reuse_factor; jj++) {{\n"
679-
" #pragma HLS UNROLL\n"
680-
" InnerInputLoop:\n"
681-
" for (int ii = 0; ii < CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor; ii++) {{\n"
682-
" #pragma HLS UNROLL\n"
683-
" data_tmp[jj][ii] = data[jj * CONFIG_T::in_width * CONFIG_T::n_chan / CONFIG_T::reuse_factor + ii];\n" # noqa: E501
684-
" }}\n"
685-
" }}\n\n"
686-
).format(index=layer_idx)
687-
indent = " "
688-
for i in range(reuse_factor):
689-
generated_code += indent
690-
generated_code += (
691-
f"pointwise_conv_1d_latency_cl<data_T, res_T, CONFIG_T>(data_tmp[{i}], res_tmp[{i}], weights, biases);\n"
692-
)
693-
694-
generated_code += (
695-
"\n"
696-
" RFOutputLoop:\n"
697-
" for (int jj = 0; jj < CONFIG_T::reuse_factor; jj++) {\n"
698-
" #pragma HLS UNROLL\n"
699-
" InnerOutputLoop:\n"
700-
" for (int ii = 0; ii < CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor; ii++) {\n"
701-
" #pragma HLS UNROLL\n"
702-
" res[jj * CONFIG_T::out_width * CONFIG_T::n_filt / CONFIG_T::reuse_factor + ii] = res_tmp[jj][ii];\n" # noqa: E501
703-
" }\n"
704-
" }\n"
705-
" }\n"
706-
"};\n"
707-
)
708-
709-
return generated_code

hls4ml/templates/vitis/nnet_utils/nnet_conv1d_latency.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void pointwise_conv_1d_latency_cl(data_T data[CONFIG_T::in_width * CONFIG_T::n_c
127127
(ii * CONFIG_T::stride_width) >= (CONFIG_T::pad_left + CONFIG_T::in_width)) {
128128
mult[index_mult] = 0;
129129
} else {
130-
mult[index_mult] = data[index_data] * weights[index_weight];
130+
mult[index_mult] = CONFIG_T::template product<data_T, typename CONFIG_T::weight_t>::product(data[index_data], weights[index_weight]);
131131
}
132132
} // end channel loop
133133
} // end filter loop

hls4ml/templates/vivado/nnet_utils/nnet_conv1d_latency.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void pointwise_conv_1d_latency_cl(data_T data[CONFIG_T::in_width * CONFIG_T::n_c
126126
(ii * CONFIG_T::stride_width) >= (CONFIG_T::pad_left + CONFIG_T::in_width)) {
127127
mult[index_mult] = 0;
128128
} else {
129-
mult[index_mult] = data[index_data] * weights[index_weight];
129+
mult[index_mult] = CONFIG_T::template product<data_T, typename CONFIG_T::weight_t>::product(data[index_data], weights[index_weight]);
130130
}
131131
} // end channel loop
132132
} // end filter loop

0 commit comments

Comments
 (0)