-
Notifications
You must be signed in to change notification settings - Fork 2
/
csim_tb.cpp
310 lines (283 loc) · 11.4 KB
/
csim_tb.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//=========================================================================
// csim_tb.cpp
//=========================================================================
// @brief: HLS csim testbench for Ultranet CNN accelerator
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ap_int.h>
#include <hls_stream.h>
#include "ultranet.h"
using namespace std;
const unsigned IMG_H = 360;
const unsigned IMG_W = 640;
const unsigned GRID_H = 20;
const unsigned GRID_W = 40;
const unsigned IN_CH = 3;
const unsigned OUT_CH = 36;
const unsigned IN_SIZE = IMG_H * IMG_W * IN_CH;
const unsigned OUT_SIZE = GRID_H * GRID_W * OUT_CH;
const int IMG_BW = 8;
const int IN_BW_ACQ = 8;
const int DMA_BW = 64;
//------------------------------------------------------------------------
// Correctness Check
//------------------------------------------------------------------------
// dump input image into the input stream
void data_feeder(
uint8_t test_img[IN_SIZE],
hls::stream<my_ap_axis> &input_stream
) {
// cout << "Tracing --\ndata_feeder(\n";
// cout << "\ttest_img @ " << hex << (void*)&(test_img[0]) << "\n";
// cout << "\tout @ " << hex << (void*)&input_stream << "\n";
// cout << "\tIN_SIZE = " << dec << IN_SIZE << "\n";
// cout << ");\n";
unsigned pack_cnt = 0;
my_ap_axis temp;
for (size_t row = 0; row < IMG_H; row++) {
for (size_t col = 0; col < IMG_W; col++) {
for (size_t ch = 0; ch < IN_CH; ch++) {
uint8_t pixel_component = test_img[ch + col * IN_CH + row * IMG_W * IN_CH];
// ap_uint<IN_BW_ACQ> pacq = (pixel_component & 0xF0) >> 4;
ap_uint<IN_BW_ACQ> pacq = pixel_component;
temp.data(IN_BW_ACQ * (pack_cnt + 1) - 1, IN_BW_ACQ * pack_cnt) = pacq;
pack_cnt++;
if (pack_cnt == DMA_BW / IN_BW_ACQ) {
pack_cnt = 0;
input_stream.write(temp);
}
}
}
}
}
// receive output from the output stream
void update_counter(int &row, int &col, int &ch) {
ch++;
if (ch == OUT_CH) {
ch = 0;
col ++;
if (col == GRID_W) {
col = 0;
row ++;
if (row == GRID_H) {
row = 0;
}
}
}
}
void data_collector(
hls::stream<my_ap_axis> &output_stream,
int32_t hls_results[GRID_H][GRID_W][OUT_CH]
) {
bool last = false;
int ch = 0;
int col = 0;
int row = 0;
size_t cnt = 0;
while (!last) {
if (output_stream.empty()) {
cout << "Error! output stream empty in C-sim!" << endl;
exit(EXIT_FAILURE);
}
if (cnt > OUT_SIZE) {
cout << "Error! Output size exceed limit (" << cnt << " > " << OUT_SIZE << ")!" << endl;
exit(EXIT_FAILURE);
}
my_ap_axis in = output_stream.read();
last = (in.last == 1);
hls_results[row][col][ch] = in.data(31, 0);
cnt++;
update_counter(row, col, ch);
hls_results[row][col][ch] = in.data(63, 32);
update_counter(row, col, ch);
cnt++;
}
}
void ultranet_csim_wrapper(
uint8_t test_img[IN_SIZE],
int32_t hls_results[GRID_H][GRID_W][OUT_CH],
ap_uint<CONV_0_IN_BIT> before_resize[IN_IMAGE_HEIGHT][IN_IMAGE_WIDTH][3],
ap_uint<CONV_0_IN_BIT> after_resize[RESIZE_IMAGE_HEIGHT][RESIZE_IMAGE_WIDTH][3],
ap_uint<CONV_0_OUT_BIT> conv0_out[CONV_0_OFM_ROW][CONV_0_OFM_COL][CONV_0_OFM_CH],
ap_uint<CONV_0_OUT_BIT> pool0_out[CONV_1_IFM_ROW][CONV_1_IFM_COL][CONV_1_IFM_CH],
ap_uint<CONV_1_OUT_BIT> conv1_out[CONV_1_OFM_ROW][CONV_1_OFM_COL][CONV_1_OFM_CH],
ap_uint<CONV_1_OUT_BIT> pool1_out[CONV_2_IFM_ROW][CONV_2_IFM_COL][CONV_2_IFM_CH],
ap_uint<CONV_2_OUT_BIT> conv2_out[CONV_2_OFM_ROW][CONV_2_OFM_COL][CONV_2_OFM_CH],
ap_uint<CONV_2_OUT_BIT> pool2_out[CONV_3_IFM_ROW][CONV_3_IFM_COL][CONV_3_IFM_CH],
ap_uint<CONV_3_OUT_BIT> conv3_out[CONV_3_OFM_ROW][CONV_3_OFM_COL][CONV_3_OFM_CH],
ap_uint<CONV_3_OUT_BIT> pool3_out[CONV_4_IFM_ROW][CONV_4_IFM_COL][CONV_4_IFM_CH],
ap_uint<CONV_4_OUT_BIT> conv4_out[CONV_4_OFM_ROW][CONV_4_OFM_COL][CONV_4_OFM_CH],
ap_uint<CONV_5_OUT_BIT> conv5_out[CONV_5_OFM_ROW][CONV_5_OFM_COL][CONV_5_OFM_CH],
ap_uint<CONV_6_OUT_BIT> conv6_out[CONV_6_OFM_ROW][CONV_6_OFM_COL][CONV_6_OFM_CH],
ap_uint<CONV_7_OUT_BIT> conv7_out[CONV_7_OFM_ROW][CONV_7_OFM_COL][CONV_7_OFM_CH]
){
hls::stream<my_ap_axis> input_stream("input stream");
hls::stream<my_ap_axis> output_stream("output stream");
data_feeder(test_img, input_stream);
cout << "Successfully feed data into xcel" << endl;
do_compute(
before_resize,
after_resize,
conv0_out,
pool0_out,
conv1_out,
pool1_out,
conv2_out,
pool2_out,
conv3_out,
pool3_out,
conv4_out,
conv5_out,
conv6_out,
conv7_out,
input_stream, output_stream, 1);
cout << "xcel complete!" << endl;
data_collector(output_stream, hls_results);
cout << "Successfully collected results from xcel" << endl;
}
// store results
template<unsigned H, unsigned W, unsigned C, unsigned B>
void save_results(
ap_uint<B> data[H][W][C],
string filename
) {
ofstream out_result_handle(filename);
if (!out_result_handle) {
cout << "ERROR when opening/creating output file: " << filename << endl;
exit(EXIT_FAILURE);
}
for (size_t row = 0; row < H; row++) {
for (size_t col = 0; col < W; col++) {
for (size_t ch = 0; ch < C; ch++) {
out_result_handle << data[row][col][ch] << endl;
}
}
}
out_result_handle.close();
}
template<unsigned H, unsigned W, unsigned C, unsigned B>
void save_results(
int32_t data[H][W][C],
string filename
) {
ofstream out_result_handle(filename);
if (!out_result_handle) {
cout << "ERROR when opening/creating output file: " << filename << endl;
exit(EXIT_FAILURE);
}
for (size_t row = 0; row < H; row++) {
for (size_t col = 0; col < W; col++) {
for (size_t ch = 0; ch < C; ch++) {
out_result_handle << dec << data[row][col][ch] << endl;
}
}
}
out_result_handle.close();
}
// space for output
// declare them here to prevent stack overflow
int32_t results[GRID_H][GRID_W][OUT_CH];
ap_uint<CONV_0_IN_BIT> before_resize[IN_IMAGE_HEIGHT][IN_IMAGE_WIDTH][3];
ap_uint<CONV_0_IN_BIT> after_resize[RESIZE_IMAGE_HEIGHT][RESIZE_IMAGE_WIDTH][3];
ap_uint<CONV_0_OUT_BIT> conv0_out[CONV_0_OFM_ROW][CONV_0_OFM_COL][CONV_0_OFM_CH];
ap_uint<CONV_0_OUT_BIT> pool0_out[CONV_1_IFM_ROW][CONV_1_IFM_COL][CONV_1_IFM_CH];
ap_uint<CONV_1_OUT_BIT> conv1_out[CONV_1_OFM_ROW][CONV_1_OFM_COL][CONV_1_OFM_CH];
ap_uint<CONV_1_OUT_BIT> pool1_out[CONV_2_IFM_ROW][CONV_2_IFM_COL][CONV_2_IFM_CH];
ap_uint<CONV_2_OUT_BIT> conv2_out[CONV_2_OFM_ROW][CONV_2_OFM_COL][CONV_2_OFM_CH];
ap_uint<CONV_2_OUT_BIT> pool2_out[CONV_3_IFM_ROW][CONV_3_IFM_COL][CONV_3_IFM_CH];
ap_uint<CONV_3_OUT_BIT> conv3_out[CONV_3_OFM_ROW][CONV_3_OFM_COL][CONV_3_OFM_CH];
ap_uint<CONV_3_OUT_BIT> pool3_out[CONV_4_IFM_ROW][CONV_4_IFM_COL][CONV_4_IFM_CH];
ap_uint<CONV_4_OUT_BIT> conv4_out[CONV_4_OFM_ROW][CONV_4_OFM_COL][CONV_4_OFM_CH];
ap_uint<CONV_5_OUT_BIT> conv5_out[CONV_5_OFM_ROW][CONV_5_OFM_COL][CONV_5_OFM_CH];
ap_uint<CONV_6_OUT_BIT> conv6_out[CONV_6_OFM_ROW][CONV_6_OFM_COL][CONV_6_OFM_CH];
ap_uint<CONV_7_OUT_BIT> conv7_out[CONV_7_OFM_ROW][CONV_7_OFM_COL][CONV_7_OFM_CH];
int main(int argc, char** argv){
if (argc != 3) {
cout << "Usage: " << argv[0] << " <img_path> <output_path>" << endl;
cout << "Aborting..." << endl;
exit(EXIT_FAILURE);
}
string img_path = argv[1];
string output_dir = argv[2];
// load test image
uint8_t test_img[IN_SIZE];
ifstream in_img_handle(img_path);
if (!in_img_handle) {
cout << "ERROR when opening input file: " << img_path << endl;
exit(EXIT_FAILURE);
}
string line;
int ln = 0;
// cout << "Tracing --\nload test image in main(\n";
// cout << "\ttest_img @ " << hex << (void*)&(test_img[0]) << "\n";
// cout << "\tin_img_handle @ " << hex << (void*)&in_img_handle << "\n";
// cout << "\tIN_SIZE = " << dec << IN_SIZE << "\n";
// cout << ");\n";
while (getline(in_img_handle, line, '\n')) {
int val = stoi(line);
// cout << dec << ln << ": " << line;
// cout << " => " << hex << val << "\n";
test_img[ln] = val;
ln++;
}
in_img_handle.close();
if (ln != IN_SIZE) {
cout << "ERROR loading input image: size mismatch " << endl;
cout << "\tExpected:" << IN_SIZE << endl;
cout << "\tActual :" << ln << endl;
exit(EXIT_FAILURE);
}
cout << "Input image loading success!" << endl;
// invoke test wraper
ultranet_csim_wrapper(
test_img, results,
before_resize,
after_resize,
conv0_out,
pool0_out,
conv1_out,
pool1_out,
conv2_out,
pool2_out,
conv3_out,
pool3_out,
conv4_out,
conv5_out,
conv6_out,
conv7_out
);
save_results<IN_IMAGE_HEIGHT, IN_IMAGE_WIDTH, 3, CONV_0_IN_BIT>(
before_resize, output_dir + "/xcel_before_resize.dat");
save_results<RESIZE_IMAGE_HEIGHT, RESIZE_IMAGE_WIDTH, 3, CONV_0_IN_BIT>(
after_resize, output_dir + "/xcel_after_resize.dat");
save_results<CONV_0_OFM_ROW, CONV_0_OFM_COL, CONV_0_OFM_CH, CONV_0_OUT_BIT>(
conv0_out, output_dir + "/xcel_conv0.dat");
save_results<CONV_1_OFM_ROW, CONV_1_OFM_COL, CONV_1_OFM_CH, CONV_1_OUT_BIT>(
conv1_out, output_dir + "/xcel_conv1.dat");
save_results<CONV_2_OFM_ROW, CONV_2_OFM_COL, CONV_2_OFM_CH, CONV_2_OUT_BIT>(
conv2_out, output_dir + "/xcel_conv2.dat");
save_results<CONV_3_OFM_ROW, CONV_3_OFM_COL, CONV_3_OFM_CH, CONV_3_OUT_BIT>(
conv3_out, output_dir + "/xcel_conv3.dat");
save_results<CONV_4_OFM_ROW, CONV_4_OFM_COL, CONV_4_OFM_CH, CONV_4_OUT_BIT>(
conv4_out, output_dir + "/xcel_conv4.dat");
save_results<CONV_5_OFM_ROW, CONV_5_OFM_COL, CONV_5_OFM_CH, CONV_5_OUT_BIT>(
conv5_out, output_dir + "/xcel_conv5.dat");
save_results<CONV_6_OFM_ROW, CONV_6_OFM_COL, CONV_6_OFM_CH, CONV_6_OUT_BIT>(
conv6_out, output_dir + "/xcel_conv6.dat");
save_results<CONV_7_OFM_ROW, CONV_7_OFM_COL, CONV_7_OFM_CH, CONV_7_OUT_BIT>(
conv7_out, output_dir + "/xcel_conv7.dat");
save_results<GRID_H, GRID_W, OUT_CH, 32>(
results, output_dir + "/xcel_result.dat");
save_results<CONV_1_IFM_ROW, CONV_1_IFM_COL, CONV_1_IFM_CH, CONV_0_OUT_BIT>(
pool0_out, output_dir + "/xcel_pool0.dat");
save_results<CONV_2_IFM_ROW, CONV_2_IFM_COL, CONV_2_IFM_CH, CONV_1_OUT_BIT>(
pool1_out, output_dir + "/xcel_pool1.dat");
save_results<CONV_3_IFM_ROW, CONV_3_IFM_COL, CONV_3_IFM_CH, CONV_2_OUT_BIT>(
pool2_out, output_dir + "/xcel_pool2.dat");
save_results<CONV_4_IFM_ROW, CONV_4_IFM_COL, CONV_4_IFM_CH, CONV_3_OUT_BIT>(
pool3_out, output_dir + "/xcel_pool3.dat");
return 0;
}