Skip to content

Commit f07b432

Browse files
author
Quentin Berthet
committed
Python black formating
1 parent 9bf6fe9 commit f07b432

File tree

3 files changed

+351
-193
lines changed

3 files changed

+351
-193
lines changed

hls4ml/backends/vitis_accelerator/vitis_accelerator_backend.py

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

1010
class VitisAcceleratorBackend(VitisBackend):
1111
def __init__(self):
12-
super(VivadoBackend, self).__init__(name='VitisAccelerator')
12+
super(VivadoBackend, self).__init__(name="VitisAccelerator")
1313
self._register_layer_attributes()
1414
self._register_flows()
1515

1616
def create_initial_config(
1717
self,
18-
board='alveo-u55c',
18+
board="alveo-u55c",
1919
part=None,
2020
clock_period=5,
21-
io_type='io_parallel',
21+
io_type="io_parallel",
2222
num_kernel=1,
2323
num_thread=1,
2424
batchsize=8192,
2525
hw_quant=False,
26-
vivado_directives=[]
26+
vivado_directives=[],
2727
):
28-
'''
28+
"""
2929
Create initial accelerator config with default parameters
3030
3131
Args:
@@ -38,26 +38,42 @@ def create_initial_config(
3838
vivado_directives: Directives passed down to Vivado that controls the hardware synthesis and implementation steps
3939
Returns:
4040
populated config
41-
'''
42-
board = board if board is not None else 'alveo-u55c'
41+
"""
42+
board = board if board is not None else "alveo-u55c"
4343
config = super().create_initial_config(part, clock_period, io_type)
44-
config['AcceleratorConfig'] = {}
45-
config['AcceleratorConfig']['Board'] = board
46-
config['AcceleratorConfig']['Num_Kernel'] = num_kernel
47-
config['AcceleratorConfig']['Num_Thread'] = num_thread
48-
config['AcceleratorConfig']['Batchsize'] = batchsize
49-
config['AcceleratorConfig']['HW_Quant'] = hw_quant
50-
config['AcceleratorConfig']['Vivado_Directives'] = vivado_directives
44+
config["AcceleratorConfig"] = {}
45+
config["AcceleratorConfig"]["Board"] = board
46+
config["AcceleratorConfig"]["Num_Kernel"] = num_kernel
47+
config["AcceleratorConfig"]["Num_Thread"] = num_thread
48+
config["AcceleratorConfig"]["Batchsize"] = batchsize
49+
config["AcceleratorConfig"]["HW_Quant"] = hw_quant
50+
config["AcceleratorConfig"]["Vivado_Directives"] = vivado_directives
5151
return config
5252

53-
def build(self, model, reset=False, synth=True, vsynth=True, csim=False, cosim=False, debug=False, **kwargs):
54-
if 'linux' in sys.platform:
55-
if 'XILINX_VITIS' not in os.environ:
56-
raise Exception("XILINX_VITIS environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
57-
if 'XILINX_XRT' not in os.environ:
58-
raise Exception("XILINX_XRT environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
59-
if 'XILINX_VIVADO' not in os.environ:
60-
raise Exception("XILINX_VIVADO environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
53+
def build(
54+
self,
55+
model,
56+
reset=False,
57+
synth=True,
58+
vsynth=True,
59+
csim=False,
60+
cosim=False,
61+
debug=False,
62+
**kwargs,
63+
):
64+
if "linux" in sys.platform:
65+
if "XILINX_VITIS" not in os.environ:
66+
raise Exception(
67+
"XILINX_VITIS environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building"
68+
)
69+
if "XILINX_XRT" not in os.environ:
70+
raise Exception(
71+
"XILINX_XRT environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building"
72+
)
73+
if "XILINX_VIVADO" not in os.environ:
74+
raise Exception(
75+
"XILINX_VIVADO environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building"
76+
)
6177

6278
curr_dir = os.getcwd()
6379
os.chdir(model.config.get_output_dir())
@@ -68,7 +84,7 @@ def build(self, model, reset=False, synth=True, vsynth=True, csim=False, cosim=F
6884
if synth:
6985
os.system("make cleanhls")
7086
os.system("rm -rf host")
71-
87+
7288
if vsynth:
7389
if synth:
7490
target = "all "
@@ -90,35 +106,41 @@ def build(self, model, reset=False, synth=True, vsynth=True, csim=False, cosim=F
90106
command = "make " + target
91107

92108
# Pre-loading libudev
93-
ldconfig_output = subprocess.check_output(["ldconfig", "-p"]).decode("utf-8")
109+
ldconfig_output = subprocess.check_output(["ldconfig", "-p"]).decode(
110+
"utf-8"
111+
)
94112
for line in ldconfig_output.split("\n"):
95113
if "libudev.so" in line and "x86" in line:
96-
command = "LD_PRELOAD=" + line.split("=>")[1].strip() + " " + command
114+
command = (
115+
"LD_PRELOAD=" + line.split("=>")[1].strip() + " " + command
116+
)
97117
break
98118
os.system(command)
99-
119+
100120
os.chdir(curr_dir)
101121
else:
102122
raise Exception("Currently untested on non-Linux OS")
103123

104124
def numpy_to_dat(self, model, x):
105125
if len(model.get_input_variables()) != 1:
106126
raise Exception("Currently unsupported for multi-input/output projects")
107-
127+
108128
# Verify numpy array of correct shape
109129
expected_shape = model.get_input_variables()[0].size()
110130
actual_shape = np.prod(x.shape[1:])
111131
if expected_shape != actual_shape:
112-
raise Exception(f'Input shape mismatch, got {x.shape}, expected (_, {expected_shape})')
113-
132+
raise Exception(
133+
f"Input shape mismatch, got {x.shape}, expected (_, {expected_shape})"
134+
)
135+
114136
# Write to tb_data/tb_input_features.dat
115137
samples = x.reshape(x.shape[0], -1)
116-
input_dat = f'{model.config.get_output_dir()}/tb_data/tb_input_features.dat'
117-
np.savetxt(input_dat, samples, fmt='%.4e')
138+
input_dat = f"{model.config.get_output_dir()}/tb_data/tb_input_features.dat"
139+
np.savetxt(input_dat, samples, fmt="%.4e")
118140

119141
def dat_to_numpy(self, model):
120142
expected_shape = model.get_output_variables()[0].size()
121-
output_file = f'{model.config.get_output_dir()}/tb_data/hw_results.dat'
143+
output_file = f"{model.config.get_output_dir()}/tb_data/hw_results.dat"
122144
y = np.loadtxt(output_file, dtype=float).reshape(-1, expected_shape)
123145
return y
124146

@@ -134,21 +156,37 @@ def hardware_predict(self, model, x):
134156

135157
def _register_flows(self):
136158
validation_passes = [
137-
'vitisaccelerator:validate_conv_implementation',
138-
'vitisaccelerator:validate_strategy',
159+
"vitisaccelerator:validate_conv_implementation",
160+
"vitisaccelerator:validate_strategy",
139161
]
140-
validation_flow = register_flow('validation', validation_passes, requires=['vivado:init_layers'], backend=self.name)
162+
validation_flow = register_flow(
163+
"validation",
164+
validation_passes,
165+
requires=["vivado:init_layers"],
166+
backend=self.name,
167+
)
141168

142169
# Any potential templates registered specifically for Vitis backend
143170
template_flow = register_flow(
144-
'apply_templates', self._get_layer_templates, requires=['vivado:init_layers'], backend=self.name
171+
"apply_templates",
172+
self._get_layer_templates,
173+
requires=["vivado:init_layers"],
174+
backend=self.name,
145175
)
146176

147-
writer_passes = ['make_stamp', 'vitisaccelerator:write_hls']
148-
self._writer_flow = register_flow('write', writer_passes, requires=['vitis:ip'], backend=self.name)
177+
writer_passes = ["make_stamp", "vitisaccelerator:write_hls"]
178+
self._writer_flow = register_flow(
179+
"write", writer_passes, requires=["vitis:ip"], backend=self.name
180+
)
149181

150-
ip_flow_requirements = get_flow('vivado:ip').requires.copy()
151-
ip_flow_requirements.insert(ip_flow_requirements.index('vivado:init_layers'), validation_flow)
152-
ip_flow_requirements.insert(ip_flow_requirements.index('vivado:apply_templates'), template_flow)
182+
ip_flow_requirements = get_flow("vivado:ip").requires.copy()
183+
ip_flow_requirements.insert(
184+
ip_flow_requirements.index("vivado:init_layers"), validation_flow
185+
)
186+
ip_flow_requirements.insert(
187+
ip_flow_requirements.index("vivado:apply_templates"), template_flow
188+
)
153189

154-
self._default_flow = register_flow('ip', None, requires=ip_flow_requirements, backend=self.name)
190+
self._default_flow = register_flow(
191+
"ip", None, requires=ip_flow_requirements, backend=self.name
192+
)
Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
import json
22
import os
33

4+
45
class VitisAcceleratorConfig:
56
def __init__(self, config):
67
self.config = config.config
7-
accel_config = self.config.get('AcceleratorConfig', None)
8+
accel_config = self.config.get("AcceleratorConfig", None)
89
if accel_config is None:
9-
raise Exception('Missing AcceleratorConfig')
10-
11-
self.board = accel_config.get('Board', 'alveo-u55c')
12-
self.supported_boards = json.load(open(os.path.dirname(__file__) + '/supported_boards.json'))
10+
raise Exception("Missing AcceleratorConfig")
11+
12+
self.board = accel_config.get("Board", "alveo-u55c")
13+
self.supported_boards = json.load(
14+
open(os.path.dirname(__file__) + "/supported_boards.json")
15+
)
1316
if self.board in self.supported_boards.keys():
1417
board_info = self.supported_boards[self.board]
15-
self.board_type = board_info['board_type']
16-
self.part = board_info['part']
17-
self.platform = board_info['platform']
18-
self.memory_type = board_info['memory']['type']
19-
self.memory_channel_count = board_info['memory']['channels']
18+
self.board_type = board_info["board_type"]
19+
self.part = board_info["part"]
20+
self.platform = board_info["platform"]
21+
self.memory_type = board_info["memory"]["type"]
22+
self.memory_channel_count = board_info["memory"]["channels"]
2023
else:
21-
raise Exception('The board does not appear in supported_boards.json file')
24+
raise Exception("The board does not appear in supported_boards.json file")
2225

23-
if self.config.get('Part') is not None:
24-
if self.config.get('Part') != self.part:
26+
if self.config.get("Part") is not None:
27+
if self.config.get("Part") != self.part:
2528
print(
26-
'WARNING: You set a Part that does not correspond to the Board you specified.'
27-
'The correct Part is now set.'
29+
"WARNING: You set a Part that does not correspond to the Board you specified."
30+
"The correct Part is now set."
2831
)
29-
self.config['Part'] = self.part
30-
31-
self.num_kernel = accel_config.get('Num_Kernel', 1)
32-
self.num_thread = accel_config.get('Num_Thread', 1)
33-
self.batchsize = accel_config.get('Batchsize', 8192)
34-
self.hw_quant = accel_config.get('HW_Quant', False)
32+
self.config["Part"] = self.part
33+
34+
self.num_kernel = accel_config.get("Num_Kernel", 1)
35+
self.num_thread = accel_config.get("Num_Thread", 1)
36+
self.batchsize = accel_config.get("Batchsize", 8192)
37+
self.hw_quant = accel_config.get("HW_Quant", False)
3538

36-
self.vivado_directives = accel_config.get('Vivado_Directives', [])
39+
self.vivado_directives = accel_config.get("Vivado_Directives", [])
3740

3841
def get_board_type(self):
3942
return self.board_type
4043

4144
def get_platform(self):
4245
return self.platform
43-
46+
4447
def get_num_thread(self):
4548
return self.num_thread
46-
49+
4750
def get_num_kernel(self):
4851
return self.num_kernel
49-
52+
5053
def get_batchsize(self):
5154
return self.batchsize
52-
55+
5356
def get_memory_type(self):
5457
return self.memory_type
55-
58+
5659
def get_memory_channel_count(self):
5760
return self.memory_channel_count
58-
61+
5962
def get_hw_quant(self):
6063
return self.hw_quant
61-
64+
6265
def get_vivado_directives(self):
6366
return self.vivado_directives

0 commit comments

Comments
 (0)