-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_cfg.py
99 lines (91 loc) · 3.2 KB
/
generate_cfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import argparse
import hls4ml
import json
import os
import sympy
import subprocess
def main(part, precision):
complexity_of_operators = {}
with open('ops.json') as f:
data = json.load(f)
for key, val in data['binary'].items():
equation = val.replace("x", "x0").replace("y", "x1")
expression = sympy.parsing.sympy_parser.parse_expr(equation)
hls_model = hls4ml.converters.convert_from_symbolic_expression(
expression,
n_symbols=2,
output_dir='tmp_test',
precision=precision,
part=part
)
hls_model.write()
process = subprocess.run(
["vivado_hls",
"-f",
"build_prj.tcl",
"\"reset=1 synth=1 csim=0 cosim=0 validation=0 export=0\""],
cwd='tmp_test')
result = subprocess.check_output(
["awk",
"NR==32",
"myproject_prj/solution1/syn/report/myproject_csynth.rpt"],
cwd='tmp_test')
cc = result.decode("utf-8").replace(" ", "").split("|")[1]
complexity_of_operators[key] = max(int(cc), 1)
for key, val in data['unary'].items():
equation = val.replace('sympy.', '')
if 'x' in equation and key != 'exp':
equation = equation.replace("x", "x0")
else:
equation += '(x0)'
expression = sympy.parsing.sympy_parser.parse_expr(equation)
hls_model = hls4ml.converters.convert_from_symbolic_expression(
expression,
n_symbols=1,
output_dir='tmp_test',
precision=precision,
part=part
)
hls_model.write()
process = subprocess.run(
["vivado_hls",
"-f",
"build_prj.tcl",
"\"reset=1 synth=1 csim=0 cosim=0 validation=0 export=0\""],
cwd='tmp_test')
result = subprocess.check_output(
["awk",
"NR==32",
"myproject_prj/solution1/syn/report/myproject_csynth.rpt"],
cwd='tmp_test')
cc = result.decode("utf-8").replace(" ", "").split("|")[1]
complexity_of_operators[key] = max(int(cc), 1)
config = {}
config['part'] = part
config['precision'] = precision
config['complexity_of_operators'] = complexity_of_operators
os.makedirs('configs/{part}/{precision}'.format(
part=part,
precision=precision)
)
with open('configs/{part}/{precision}/config.json'.format(
part=part,
precision=precision
), 'w') as outfile:
json.dump(config, outfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser('Generate SR configs for hls4ml')
parser.add_argument(
'--part',
type=str,
help='Part name',
default='xcvu9p-flga2577-2-e'
)
parser.add_argument(
'--precision',
type=str,
help='Precision setting',
default='ap_fixed<12, 6>'
)
args = parser.parse_args()
main(args.part, args.precision)