Skip to content

Commit ea332d2

Browse files
cleaned up c++ code generation and added build functionality
1 parent 82bf14b commit ea332d2

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

hls4ml/backends/vitis_accelerator/vitis_accelerator_backend.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import os
2+
import sys
3+
import subprocess
4+
15
from hls4ml.backends import VitisBackend, VivadoBackend
26
from hls4ml.model.flow import get_flow, register_flow
37

@@ -39,6 +43,35 @@ def create_initial_config(
3943
config['AcceleratorConfig']['Batchsize'] = batchsize
4044
return config
4145

46+
def build(self, model, target="all"):
47+
if 'linux' in sys.platform:
48+
if 'XILINX_VITIS' not in os.environ:
49+
raise Exception("XILINX_VITIS environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
50+
if 'XILINX_XRT' not in os.environ:
51+
raise Exception("XILINX_XRT environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
52+
if 'XILINX_VIVADO' not in os.environ:
53+
raise Exception("XILINX_VIVADO environmental variable missing. Please install XRT and Vitis, and run the setup scripts before building")
54+
55+
if target not in ["all", "host", "hls", "xclbin"]:
56+
raise Exception("Invalid build target")
57+
58+
curr_dir = os.getcwd()
59+
os.chdir(model.config.get_output_dir())
60+
command = "make " + target
61+
# Pre-loading libudev
62+
ldconfig_output = subprocess.check_output(["ldconfig", "-p"]).decode("utf-8")
63+
for line in ldconfig_output.split("\n"):
64+
if "libudev.so" in line and "x86" in line:
65+
command = "LD_PRELOAD=" + line.split("=>")[1].strip() + " " + command
66+
break
67+
os.system(command)
68+
os.chdir(curr_dir)
69+
else:
70+
raise Exception("Currently untested on non-Linux OS")
71+
72+
def predict(self, model, x):
73+
raise Exception("TODO: Needs to be implemented")
74+
4275
def _register_flows(self):
4376
validation_passes = [
4477
'vitisaccelerator:validate_conv_implementation',

hls4ml/model/graph.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,9 @@ def _compute_n_samples(self, x):
734734
return int(n_sample)
735735

736736
def predict(self, x):
737+
if self.config.config.get('Backend', 'Vivado') == 'VitisAccelerator':
738+
return self.config.backend.predict(self, x)
739+
737740
top_function, ctype = self._get_top_function(x)
738741
n_samples = self._compute_n_samples(x)
739742
n_inputs = len(self.get_input_variables())

hls4ml/templates/vitis_accelerator/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ endif
2525
KERN_LIBRARIES += -I./ -I./firmware/ -I./firmware/weights -I./firmware/nnet_utils/
2626

2727
.PHONY: all
28-
all: host kernel
28+
all: host xclbin
2929

3030
# Building kernel
3131
./build/myproject_kernel.xo: kernel_wrapper.cpp
32-
mkdir -p ./build
33-
v++ -c -t hw --config ./accelerator_card.cfg kernel_wrapper.cpp firmware/myproject.cpp -o ./build/myproject_kernel.xo $(KERN_LIBRARIES)
32+
mkdir -p ./build && mkdir -p ./build/xo
33+
v++ -c -t hw --config ./accelerator_card.cfg --temp_dir build/xo kernel_wrapper.cpp firmware/myproject.cpp -o ./build/myproject_kernel.xo $(KERN_LIBRARIES)
3434

3535
# hls-fpga-machine-learning packaging
3636

@@ -43,8 +43,11 @@ LDFLAGS = -L$(XILINX_XRT)/lib/ -lstdc++ -lpthread -lrt -lOpenCL
4343
host: myproject_host_cl.cpp libs/xcl2.cpp
4444
$(CXX) $(CXXFLAGS) $^ -o $@ $(INCLUDES) $(LDFLAGS)
4545

46-
.PHONY: kernel
47-
kernel: kernel_wrapper.xclbin
46+
.PHONY: hls
47+
hls: ./build/myproject_kernel.xo
48+
49+
.PHONY: xclbin
50+
xclbin: ./build/kernel_wrapper.xclbin
4851

4952
# Cleaning stuff
5053
.PHONY: clean

hls4ml/templates/vitis_accelerator/accelerator_card.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
kernel=kernel_wrapper
2-
log_dir=build/logs
32
messageDb=build/kernel_wrapper.mdb
43
platform=MYPLATFORM
5-
report_dir=build/reports
64
save-temps=1
7-
temp_dir=build
85

96
[advanced]
107
prop=kernel.kernel_wrapper.kernel_flags=-std=c++11

hls4ml/writer/vitis_accelerator_writer.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,15 @@ def write_makefile(self, model):
185185
newline = line.replace('myproject', project_name)
186186
elif '# hls-fpga-machine-learning packaging' in line:
187187
if board_type == "alveo":
188-
newline = f'kernel_wrapper.xclbin: ./build/{project_name}_kernel.xo\n'
189-
newline += f' v++ -l -t hw --config ./accelerator_card.cfg ./build/{project_name}_kernel.xo -o kernel_wrapper.xclbin\n'
188+
newline = f'./build/kernel_wrapper.xclbin: ./build/{project_name}_kernel.xo\n'
189+
newline += f'\tmkdir -p ./build/xclbin\n'
190+
newline += f'\tv++ -l -t hw --config ./accelerator_card.cfg --temp_dir build/xclbin ./build/{project_name}_kernel.xo -o ./build/kernel_wrapper.xclbin\n'
190191
elif board_type == "versal":
191-
newline = f'kernel_wrapper.xsa: ./build/{project_name}_kernel.xo\n'
192-
newline += f' v++ -l -t hw --config ./accelerator_card.cfg ./build/{project_name}_kernel.xo -o kernel_wrapper.xsa\n\n'
193-
newline += f'kernel_wrapper.xclbin: ./kernel_wrapper.xsa\n'
194-
newline += f' v++ --package -t hw --config ./accelerator_card.cfg ./kernel_wrapper.xsa -o kernel_wrapper.xclbin\n'
192+
newline = f'./build/kernel_wrapper.xsa: ./build/{project_name}_kernel.xo\n'
193+
newline += f'\tmkdir -p ./build/xclbin\n'
194+
newline += f'\tv++ -l -t hw --config ./accelerator_card.cfg --temp_dir build/xclbin ./build/{project_name}_kernel.xo -o ./build/kernel_wrapper.xsa\n\n'
195+
newline += f'./build/kernel_wrapper.xclbin: ./build/kernel_wrapper.xsa\n'
196+
newline += f'\tv++ --package -t hw --config ./accelerator_card.cfg --temp_dir build/xclbin ./build/kernel_wrapper.xsa -o ./build/kernel_wrapper.xclbin\n'
195197
else:
196198
newline = line
197199
fout.write(newline)

0 commit comments

Comments
 (0)