Skip to content

Commit aace320

Browse files
authored
Merge pull request #986 from vloncar/namespaces_and_other_emulator_goodies
Add namespaces and optional writer config
2 parents ba08ca1 + 253fabd commit aace320

File tree

16 files changed

+339
-50
lines changed

16 files changed

+339
-50
lines changed

hls4ml/backends/vitis/vitis_backend.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,44 @@ def _register_flows(self):
3434
self._default_flow = register_flow('ip', None, requires=ip_flow_requirements, backend=self.name)
3535

3636
def create_initial_config(
37-
self, part='xcvu13p-flga2577-2-e', clock_period=5, clock_uncertainty='27%', io_type='io_parallel', **_
37+
self,
38+
part='xcvu13p-flga2577-2-e',
39+
clock_period=5,
40+
clock_uncertainty='27%',
41+
io_type='io_parallel',
42+
namespace=None,
43+
write_weights_txt=True,
44+
write_tar=False,
45+
**_,
3846
):
47+
"""Create initial configuration of the Vitis backend.
48+
49+
Args:
50+
part (str, optional): The FPGA part to be used. Defaults to 'xcvu13p-flga2577-2-e'.
51+
clock_period (int, optional): The clock period. Defaults to 5.
52+
clock_uncertainty (str, optional): The clock uncertainty. Defaults to 27%.
53+
io_type (str, optional): Type of implementation used. One of
54+
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
55+
namespace (str, optional): If defined, place all generated code within a namespace. Defaults to None.
56+
write_weights_txt (bool, optional): If True, writes weights to .txt files which speeds up compilation.
57+
Defaults to True.
58+
write_tar (bool, optional): If True, compresses the output directory into a .tar.gz file. Defaults to False.
59+
60+
Returns:
61+
dict: initial configuration.
62+
"""
3963
config = {}
4064

4165
config['Part'] = part if part is not None else 'xcvu13p-flga2577-2-e'
4266
config['ClockPeriod'] = clock_period if clock_period is not None else 5
4367
config['ClockUncertainty'] = clock_uncertainty if clock_uncertainty is not None else '27%'
4468
config['IOType'] = io_type if io_type is not None else 'io_parallel'
4569
config['HLSConfig'] = {}
70+
config['WriterConfig'] = {
71+
'Namespace': namespace,
72+
'WriteWeightsTxt': write_weights_txt,
73+
'WriteTar': write_tar,
74+
}
4675

4776
return config
4877

hls4ml/backends/vivado/vivado_backend.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,44 @@ def get_writer_flow(self):
175175
return self._writer_flow
176176

177177
def create_initial_config(
178-
self, part='xcvu13p-flga2577-2-e', clock_period=5, clock_uncertainty='12.5%', io_type='io_parallel', **_
178+
self,
179+
part='xcvu13p-flga2577-2-e',
180+
clock_period=5,
181+
clock_uncertainty='12.5%',
182+
io_type='io_parallel',
183+
namespace=None,
184+
write_weights_txt=True,
185+
write_tar=False,
186+
**_,
179187
):
188+
"""Create initial configuration of the Vivado backend.
189+
190+
Args:
191+
part (str, optional): The FPGA part to be used. Defaults to 'xcvu13p-flga2577-2-e'.
192+
clock_period (int, optional): The clock period. Defaults to 5.
193+
clock_uncertainty (str, optional): The clock uncertainty. Defaults to 12.5%.
194+
io_type (str, optional): Type of implementation used. One of
195+
'io_parallel' or 'io_stream'. Defaults to 'io_parallel'.
196+
namespace (str, optional): If defined, place all generated code within a namespace. Defaults to None.
197+
write_weights_txt (bool, optional): If True, writes weights to .txt files which speeds up compilation.
198+
Defaults to True.
199+
write_tar (bool, optional): If True, compresses the output directory into a .tar.gz file. Defaults to False.
200+
201+
Returns:
202+
dict: initial configuration.
203+
"""
180204
config = {}
181205

182206
config['Part'] = part if part is not None else 'xcvu13p-flga2577-2-e'
183207
config['ClockPeriod'] = clock_period if clock_period is not None else 5
184208
config['ClockUncertainty'] = clock_uncertainty if clock_uncertainty is not None else '12.5%'
185209
config['IOType'] = io_type if io_type is not None else 'io_parallel'
186210
config['HLSConfig'] = {}
211+
config['WriterConfig'] = {
212+
'Namespace': namespace,
213+
'WriteWeightsTxt': write_weights_txt,
214+
'WriteTar': write_tar,
215+
}
187216

188217
return config
189218

hls4ml/model/graph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ def __init__(self, config):
5151

5252
self.pipeline_style = 'pipeline'
5353

54+
if 'WriterConfig' in self.config:
55+
self.writer_config = self.config['WriterConfig']
56+
else:
57+
self.writer_config = {
58+
'Namespace': None,
59+
'WriteWeightsTxt': True,
60+
'WriteTar': False,
61+
}
62+
5463
self._parse_hls_config()
5564
self._validate_hls_config()
5665

@@ -183,6 +192,9 @@ def get_compression(self, layer):
183192

184193
return compression
185194

195+
def get_writer_config(self):
196+
return self.writer_config
197+
186198
def _parse_hls_config(self):
187199
hls_config = self.config['HLSConfig']
188200

hls4ml/templates/vivado/firmware/defines.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
// hls-fpga-machine-learning insert numbers
1111

12+
// hls-fpga-machine-learning insert namespace-start
13+
1214
// hls-fpga-machine-learning insert layer-precision
1315

16+
// hls-fpga-machine-learning insert namespace-end
17+
1418
#endif

hls4ml/templates/vivado/firmware/myproject.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33
#include "myproject.h"
44
#include "parameters.h"
55

6+
// hls-fpga-machine-learning insert namespace-start
7+
68
void myproject(
79
// hls-fpga-machine-learning insert header
810
) {
911

1012
// hls-fpga-machine-learning insert IO
1113

12-
#ifndef __SYNTHESIS__
13-
static bool loaded_weights = false;
14-
if (!loaded_weights) {
15-
// hls-fpga-machine-learning insert load weights
16-
loaded_weights = true;
17-
}
18-
#endif
14+
// hls-fpga-machine-learning insert load weights
1915

2016
// ****************************************
2117
// NETWORK INSTANTIATION
2218
// ****************************************
2319

2420
// hls-fpga-machine-learning insert layers
2521
}
22+
23+
// hls-fpga-machine-learning insert namespace-end

hls4ml/templates/vivado/firmware/myproject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
#include "defines.h"
99

10+
// hls-fpga-machine-learning insert namespace-start
11+
1012
// Prototype of top level function for C-synthesis
1113
void myproject(
1214
// hls-fpga-machine-learning insert header
1315
);
1416

17+
// hls-fpga-machine-learning insert namespace-end
18+
1519
#endif

hls4ml/templates/vivado/firmware/parameters.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
// hls-fpga-machine-learning insert weights
1212

13+
// hls-fpga-machine-learning insert namespace-start
14+
1315
// hls-fpga-machine-learning insert layer-config
1416

17+
// hls-fpga-machine-learning insert namespace-end
18+
1519
#endif

hls4ml/templates/vivado/myproject_bridge.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ void collect_trace_output(struct trace_data *c_trace_outputs) {
5252
void myproject_float(
5353
// hls-fpga-machine-learning insert header #float
5454
) {
55+
// hls-fpga-machine-learning insert namespace
5556

5657
// hls-fpga-machine-learning insert wrapper #float
5758
}
5859

5960
void myproject_double(
6061
// hls-fpga-machine-learning insert header #double
6162
) {
63+
// hls-fpga-machine-learning insert namespace
64+
6265
// hls-fpga-machine-learning insert wrapper #double
6366
}
6467
}

hls4ml/templates/vivado/myproject_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ size_t trace_type_size = sizeof(double);
2121
} // namespace nnet
2222

2323
int main(int argc, char **argv) {
24+
// hls-fpga-machine-learning insert namespace
25+
2426
// load input data from text file
2527
std::ifstream fin("tb_data/tb_input_features.dat");
2628
// load predictions from text file

hls4ml/writer/vitis_writer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ def write_hls(self, model):
3030
"""
3131
super().write_hls(model)
3232
self.write_nnet_utils_overrides(model)
33-
os.remove(model.config.get_output_dir() + '.tar.gz')
3433
self.write_tar(model)

0 commit comments

Comments
 (0)