Skip to content

Commit 82bf14b

Browse files
Updated host code and added more board support
1 parent e60cc17 commit 82bf14b

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
{
22
"alveo-u55c": {
3+
"board_type": "alveo",
34
"part": "xcu55c-fsvh2892-2L-e",
45
"platform": "xilinx_u55c_gen3x16_xdma_3_202210_1",
56
"memory": {"type": "hbm", "channels": 32, "capacity": 16}
67
},
78
"alveo-u50": {
9+
"board_type": "alveo",
810
"part": "xcu50-fsvh2104-2-e",
911
"platform": "xilinx_u50_gen3x16_xdma_5_202210_1",
1012
"memory": {"type": "hbm", "channels": 32, "capacity": 8}
1113
},
1214
"alveo-u250": {
15+
"board_type": "alveo",
1316
"part": "xcu250-figd2104-2L-e",
1417
"platform": "xilinx_u250_xdma_201830_2",
1518
"memory": {"type": "ddr", "channels": 4, "capacity": 64}
19+
},
20+
"vck5000": {
21+
"board_type": "versal",
22+
"part": "xcvc1902-2msevsvd1760",
23+
"platform": "xilinx_vck5000_gen4x8_qdma_2_202220_1",
24+
"memory":{"type": "ddr", "channels": 3, "capacity": 12}
1625
}
1726
}

hls4ml/backends/vitis_accelerator/vitis_accelerator_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(self, config):
1212
self.supported_boards = json.load(open(os.path.dirname(__file__) + '/supported_boards.json'))
1313
if self.board in self.supported_boards.keys():
1414
board_info = self.supported_boards[self.board]
15+
self.board_type = board_info['board_type']
1516
self.part = board_info['part']
1617
self.platform = board_info['platform']
1718
self.memory_type = board_info['memory']['type']
@@ -31,6 +32,9 @@ def __init__(self, config):
3132
self.num_thread = accel_config.get('Num_Thread')
3233
self.batchsize = accel_config.get('Batchsize')
3334

35+
def get_board_type(self):
36+
return self.board_type
37+
3438
def get_platform(self):
3539
return self.platform
3640

hls4ml/templates/vitis_accelerator/Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ all: host kernel
3131
./build/myproject_kernel.xo: kernel_wrapper.cpp
3232
mkdir -p ./build
3333
v++ -c -t hw --config ./accelerator_card.cfg kernel_wrapper.cpp firmware/myproject.cpp -o ./build/myproject_kernel.xo $(KERN_LIBRARIES)
34-
35-
myproject_kernel.xclbin: ./build/myproject_kernel.xo
36-
v++ -l -t hw --config ./accelerator_card.cfg ./build/myproject_kernel.xo -o kernel_wrapper.xclbin
34+
35+
# hls-fpga-machine-learning packaging
3736

3837
# Building Host
3938
INCLUDES += -I$(XILINX_XRT)/include/ -I$(XILINX_VIVADO)/include/ -I$(XILINX_HLS)/include/ \
@@ -45,7 +44,7 @@ host: myproject_host_cl.cpp libs/xcl2.cpp
4544
$(CXX) $(CXXFLAGS) $^ -o $@ $(INCLUDES) $(LDFLAGS)
4645

4746
.PHONY: kernel
48-
kernel: myproject_kernel.xclbin
47+
kernel: kernel_wrapper.xclbin
4948

5049
# Cleaning stuff
5150
.PHONY: clean

hls4ml/templates/vitis_accelerator/myproject_host_cl.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ int main(int argc, char **argv) {
8383
}
8484

8585
// Copying in testbench data
86-
int n = inputData.size();
86+
int n = std::min((int) inputData.size(), INSTREAMSIZE * NUM_CU * NUM_THREAD);
8787
for (int i = 0; i < n; i++) {
8888
fpga.source_in[i] = inputData[i];
8989
}
9090

9191
// Padding rest of buffer with arbitrary values
92-
for (int i = n; i < NUM_CU * NUM_THREAD * INSTREAMSIZE; i++) {
92+
for (int i = n; i < INSTREAMSIZE * NUM_CU * NUM_THREAD; i++) {
9393
fpga.source_in[i] = (in_buffer_t)(1234.567);
9494
}
9595

@@ -115,16 +115,34 @@ int main(int argc, char **argv) {
115115
float(std::chrono::duration_cast<std::chrono::nanoseconds>(ts_end - ts_start).count())) *
116116
1000000000.;
117117

118+
std::cout << "Throughput = "
119+
<< throughput
120+
<<" predictions/second\n" << std::endl;
121+
122+
std::cout << "Writing hw resaults to file" << std::endl;
123+
std::ofstream resultsFile;
124+
resultsFile.open("tb_data/hw_results.dat", std::ios::trunc);
125+
if (resultsFile.is_open()) {
126+
for (int i = 0; i < NUM_THREAD * NUM_CU * BATCHSIZE; i++) {
127+
std::stringstream line;
128+
for (int n = 0; n < DATA_SIZE_OUT; n++) {
129+
line << (float)fpga.source_hw_results[(i * DATA_SIZE_OUT) + n] << " ";
130+
}
131+
resultsFile << line.str() << "\n";
132+
}
133+
resultsFile.close();
134+
} else {
135+
std::cerr << "Error writing hw results to file" << std::endl;
136+
}
137+
138+
std::cout << "\nWriting run logs to file" << std::endl;
118139
std::ofstream outFile("u55c_executable_logfile.log", std::ios::trunc);
119140
if (outFile.is_open()) {
120141
outFile << fpga.ss.rdbuf();
121142
outFile.close();
122143
} else {
123-
std::cerr << "Error opening file for writing." << std::endl;
144+
std::cerr << "Error opening file for logging" << std::endl;
124145
}
125-
126-
std::cout << "Throughput = "
127-
<< throughput
128-
<<" predictions/second" << std::endl;
146+
129147
return EXIT_SUCCESS;
130148
}

hls4ml/writer/vitis_accelerator_writer.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,27 @@ def write_makefile(self, model):
171171
Args:
172172
model (ModelGraph): the hls4ml model.
173173
"""
174+
from hls4ml.backends import VitisAcceleratorConfig
174175

175176
filedir = os.path.dirname(os.path.abspath(__file__))
176177
f = open(os.path.join(filedir, '../templates/vitis_accelerator/Makefile'))
177178
fout = open(f'{model.config.get_output_dir()}/Makefile', 'w')
178179

180+
board_type = self.vitis_accelerator_config.get_board_type()
181+
project_name = format(model.config.get_project_name())
182+
179183
for line in f.readlines():
180184
if 'myproject' in line:
181-
newline = line.replace('myproject', format(model.config.get_project_name()))
185+
newline = line.replace('myproject', project_name)
186+
elif '# hls-fpga-machine-learning packaging' in line:
187+
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'
190+
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'
182195
else:
183196
newline = line
184197
fout.write(newline)
@@ -219,17 +232,18 @@ def write_accelerator_card_cfg(self, model):
219232
elif '# hls-fpga-machine-learning kernel control' in line:
220233
newline = '[connectivity]\n'
221234
newline += 'nk=kernel_wrapper:' + format(num_kernels) + '\n\n'
222-
if memory_type == 'hbm':
223-
for i in range(0, num_kernels):
224-
newline += 'sp=kernel_wrapper_{}.in:HBM[{}:{}]\n'.format(i + 1, (i*2)*num_channels_per_cu, ((i*2 + 1)*num_channels_per_cu) - 1)
225-
newline += 'sp=kernel_wrapper_{}.out:HBM[{}:{}]\n'.format(i + 1, (i*2 + 1)*num_channels_per_cu, ((i+1) * 2)*num_channels_per_cu - 1)
226-
elif memory_type == 'ddr':
227-
for i in range(0, num_kernels):
228-
newline += 'sp=kernel_wrapper_{}.in:DDR[{}]\n'.format(i + 1, i)
229-
newline += 'sp=kernel_wrapper_{}.out:HBM[{}]\n'.format(i + 1, i)
230-
newline += '\n'
231-
for i in range(0, num_kernels):
232-
newline += 'slr=kernel_wrapper_{}:SLR{}\n'.format(i + 1, i)
235+
if self.vitis_accelerator_config.get_board_type() == "alveo":
236+
if memory_type == 'hbm':
237+
for i in range(0, num_kernels):
238+
newline += 'sp=kernel_wrapper_{}.in:HBM[{}:{}]\n'.format(i + 1, (i*2)*num_channels_per_cu, ((i*2 + 1)*num_channels_per_cu) - 1)
239+
newline += 'sp=kernel_wrapper_{}.out:HBM[{}:{}]\n'.format(i + 1, (i*2 + 1)*num_channels_per_cu, ((i+1) * 2)*num_channels_per_cu - 1)
240+
elif memory_type == 'ddr':
241+
for i in range(0, num_kernels):
242+
newline += 'sp=kernel_wrapper_{}.in:DDR[{}]\n'.format(i + 1, i)
243+
newline += 'sp=kernel_wrapper_{}.out:HBM[{}]\n'.format(i + 1, i)
244+
newline += '\n'
245+
for i in range(0, num_kernels):
246+
newline += 'slr=kernel_wrapper_{}:SLR{}\n'.format(i + 1, i)
233247
else:
234248
newline = line
235249
fout.write(newline)

0 commit comments

Comments
 (0)