This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
trace_gen_wrapper.py
423 lines (332 loc) · 12.6 KB
/
trace_gen_wrapper.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import math
import dram_trace as dram
import sram_traffic_os as sram
import sram_traffic_ws as sram_ws
import sram_traffic_is as sram_is
def gen_all_traces(
array_h = 4,
array_w = 4,
ifmap_h = 7, ifmap_w = 7,
filt_h = 3, filt_w = 3,
num_channels = 3,
strides = 1, num_filt = 8,
data_flow = 'os',
word_size_bytes = 1,
filter_sram_size = 64, ifmap_sram_size= 64, ofmap_sram_size = 64,
filt_base = 1000000, ifmap_base=0, ofmap_base = 2000000,
sram_read_trace_file = "sram_read.csv",
sram_write_trace_file = "sram_write.csv",
dram_filter_trace_file = "dram_filter_read.csv",
dram_ifmap_trace_file = "dram_ifmap_read.csv",
dram_ofmap_trace_file = "dram_ofmap_write.csv"
):
sram_cycles = 0
util = 0
print("Generating traces and bw numbers")
if data_flow == 'os':
sram_cycles, util = \
sram.sram_traffic(
dimension_rows= array_h,
dimension_cols= array_w,
ifmap_h=ifmap_h, ifmap_w=ifmap_w,
filt_h=filt_h, filt_w=filt_w,
num_channels=num_channels,
strides=strides, num_filt=num_filt,
filt_base=filt_base, ifmap_base=ifmap_base,
ofmap_base = ofmap_base,
sram_read_trace_file=sram_read_trace_file,
sram_write_trace_file=sram_write_trace_file
)
elif data_flow == 'ws':
sram_cycles, util = \
sram_ws.sram_traffic(
dimension_rows = array_h,
dimension_cols = array_w,
ifmap_h = ifmap_h, ifmap_w = ifmap_w,
filt_h = filt_h, filt_w = filt_w,
num_channels = num_channels,
strides = strides, num_filt = num_filt,
ofmap_base = ofmap_base, filt_base = filt_base, ifmap_base = ifmap_base,
sram_read_trace_file = sram_read_trace_file,
sram_write_trace_file = sram_write_trace_file
)
elif data_flow == 'is':
sram_cycles, util = \
sram_is.sram_traffic(
dimension_rows = array_h,
dimension_cols = array_w,
ifmap_h = ifmap_h, ifmap_w = ifmap_w,
filt_h = filt_h, filt_w = filt_w,
num_channels = num_channels,
strides = strides, num_filt = num_filt,
ofmap_base = ofmap_base, filt_base = filt_base, ifmap_base = ifmap_base,
sram_read_trace_file = sram_read_trace_file,
sram_write_trace_file = sram_write_trace_file
)
#print("Generating DRAM traffic")
dram.dram_trace_read_v2(
sram_sz=ifmap_sram_size,
word_sz_bytes=word_size_bytes,
min_addr=ifmap_base, max_addr=filt_base,
sram_trace_file=sram_read_trace_file,
dram_trace_file=dram_ifmap_trace_file,
)
dram.dram_trace_read_v2(
sram_sz= filter_sram_size,
word_sz_bytes= word_size_bytes,
min_addr=filt_base, max_addr=ofmap_base,
sram_trace_file= sram_read_trace_file,
dram_trace_file= dram_filter_trace_file,
)
dram.dram_trace_write(
ofmap_sram_size= ofmap_sram_size,
data_width_bytes= word_size_bytes,
sram_write_trace_file= sram_write_trace_file,
dram_write_trace_file= dram_ofmap_trace_file
)
print("Average utilization : \t" + str(util) + " %")
print("Cycles for compute : \t" + str(sram_cycles) + " cycles")
bw_numbers, detailed_log = gen_bw_numbers(dram_ifmap_trace_file, dram_filter_trace_file,
dram_ofmap_trace_file, sram_write_trace_file,
sram_read_trace_file)
#array_h, array_w)
return bw_numbers, detailed_log, util, sram_cycles
def gen_max_bw_numbers( dram_ifmap_trace_file, dram_filter_trace_file,
dram_ofmap_trace_file, sram_write_trace_file, sram_read_trace_file
):
max_dram_activation_bw = 0
num_bytes = 0
max_dram_act_clk = ""
f = open(dram_ifmap_trace_file, 'r')
for row in f:
clk = row.split(',')[0]
num_bytes = len(row.split(',')) - 2
if max_dram_activation_bw < num_bytes:
max_dram_activation_bw = num_bytes
max_dram_act_clk = clk
f.close()
max_dram_filter_bw = 0
num_bytes = 0
max_dram_filt_clk = ""
f = open(dram_filter_trace_file, 'r')
for row in f:
clk = row.split(',')[0]
num_bytes = len(row.split(',')) - 2
if max_dram_filter_bw < num_bytes:
max_dram_filter_bw = num_bytes
max_dram_filt_clk = clk
f.close()
max_dram_ofmap_bw = 0
num_bytes = 0
max_dram_ofmap_clk = ""
f = open(dram_ofmap_trace_file, 'r')
for row in f:
clk = row.split(',')[0]
num_bytes = len(row.split(',')) - 2
if max_dram_ofmap_bw < num_bytes:
max_dram_ofmap_bw = num_bytes
max_dram_ofmap_clk = clk
f.close()
max_sram_ofmap_bw = 0
num_bytes = 0
f = open(sram_write_trace_file, 'r')
for row in f:
num_bytes = len(row.split(',')) - 2
if max_sram_ofmap_bw < num_bytes:
max_sram_ofmap_bw = num_bytes
f.close()
max_sram_read_bw = 0
num_bytes = 0
f = open(sram_read_trace_file, 'r')
for row in f:
num_bytes = len(row.split(',')) - 2
if max_sram_read_bw < num_bytes:
max_sram_read_bw = num_bytes
f.close()
#print("DRAM IFMAP Read BW, DRAM Filter Read BW, DRAM OFMAP Write BW, SRAM OFMAP Write BW")
log = str(max_dram_activation_bw) + ",\t" + str(max_dram_filter_bw) + ",\t"
log += str(max_dram_ofmap_bw) + ",\t" + str(max_sram_read_bw) + ",\t"
log += str(max_sram_ofmap_bw) + ","
# Anand: Enable the following for debug print
#log += str(max_dram_act_clk) + ",\t" + str(max_dram_filt_clk) + ",\t"
#log += str(max_dram_ofmap_clk) + ","
#print(log)
return log
def gen_bw_numbers( dram_ifmap_trace_file, dram_filter_trace_file,
dram_ofmap_trace_file, sram_write_trace_file,
sram_read_trace_file
#sram_read_trace_file,
#array_h, array_w # These are needed for utilization calculation
):
min_clk = 100000
max_clk = -1
detailed_log = ""
num_dram_activation_bytes = 0
f = open(dram_ifmap_trace_file, 'r')
start_clk = 0
first = True
for row in f:
num_dram_activation_bytes += len(row.split(',')) - 2
elems = row.strip().split(',')
clk = float(elems[0])
if first:
first = False
start_clk = clk
if clk < min_clk:
min_clk = clk
stop_clk = clk
detailed_log += str(start_clk) + ",\t" + str(stop_clk) + ",\t" + str(num_dram_activation_bytes) + ",\t"
f.close()
num_dram_filter_bytes = 0
f = open(dram_filter_trace_file, 'r')
first = True
for row in f:
num_dram_filter_bytes += len(row.split(',')) - 2
elems = row.strip().split(',')
clk = float(elems[0])
if first:
first = False
start_clk = clk
if clk < min_clk:
min_clk = clk
stop_clk = clk
detailed_log += str(start_clk) + ",\t" + str(stop_clk) + ",\t" + str(num_dram_filter_bytes) + ",\t"
f.close()
num_dram_ofmap_bytes = 0
f = open(dram_ofmap_trace_file, 'r')
first = True
for row in f:
num_dram_ofmap_bytes += len(row.split(',')) - 2
elems = row.strip().split(',')
clk = float(elems[0])
if first:
first = False
start_clk = clk
stop_clk = clk
detailed_log += str(start_clk) + ",\t" + str(stop_clk) + ",\t" + str(num_dram_ofmap_bytes) + ",\t"
f.close()
if clk > max_clk:
max_clk = clk
num_sram_ofmap_bytes = 0
f = open(sram_write_trace_file, 'r')
first = True
for row in f:
num_sram_ofmap_bytes += len(row.split(',')) - 2
elems = row.strip().split(',')
clk = float(elems[0])
if first:
first = False
start_clk = clk
stop_clk = clk
detailed_log += str(start_clk) + ",\t" + str(stop_clk) + ",\t" + str(num_sram_ofmap_bytes) + ",\t"
f.close()
if clk > max_clk:
max_clk = clk
num_sram_read_bytes = 0
total_util = 0
#print("Opening " + sram_trace_file)
f = open(sram_read_trace_file, 'r')
first = True
for row in f:
#num_sram_read_bytes += len(row.split(',')) - 2
elems = row.strip().split(',')
clk = float(elems[0])
if first:
first = False
start_clk = clk
#util, valid_bytes = parse_sram_read_data(elems[1:-1], array_h, array_w)
valid_bytes = parse_sram_read_data(elems[1:])
num_sram_read_bytes += valid_bytes
#total_util += util
#print("Total Util " + str(total_util) + ", util " + str(util))
stop_clk = clk
detailed_log += str(start_clk) + ",\t" + str(stop_clk) + ",\t" + str(num_sram_read_bytes) + ",\t"
f.close()
sram_clk = clk
if clk > max_clk:
max_clk = clk
delta_clk = max_clk - min_clk
dram_activation_bw = num_dram_activation_bytes / delta_clk
dram_filter_bw = num_dram_filter_bytes / delta_clk
dram_ofmap_bw = num_dram_ofmap_bytes / delta_clk
sram_ofmap_bw = num_sram_ofmap_bytes / delta_clk
sram_read_bw = num_sram_read_bytes / delta_clk
#print("total_util: " + str(total_util) + ", sram_clk: " + str(sram_clk))
#avg_util = total_util / sram_clk * 100
units = " Bytes/cycle"
print("DRAM IFMAP Read BW : \t" + str(dram_activation_bw) + units)
print("DRAM Filter Read BW : \t" + str(dram_filter_bw) + units)
print("DRAM OFMAP Write BW : \t" + str(dram_ofmap_bw) + units)
#print("Average utilization : \t" + str(avg_util) + " %")
#print("SRAM OFMAP Write BW, Min clk, Max clk")
log = str(dram_activation_bw) + ",\t" + str(dram_filter_bw) + ",\t" + str(dram_ofmap_bw) + ",\t" + str(sram_read_bw) + ",\t" + str(sram_ofmap_bw) + ","
# Anand: Enable the following line for debug
#log += str(min_clk) + ",\t" + str(max_clk) + ","
#print(log)
#return log, avg_util
return log, detailed_log
#def parse_sram_read_data(elems, array_h, array_w):
def parse_sram_read_data(elems):
#half = int(len(elems) /2)
#nz_row = 0
#nz_col = 0
data = 0
for i in range(len(elems)):
e = elems[i]
if e != ' ':
data += 1
#if i < half:
#if i < array_h:
# nz_row += 1
#else:
# nz_col += 1
#util = (nz_row * nz_col) / (half * half)
#util = (nz_row * nz_col) / (array_h * array_w)
#data = nz_row + nz_col
#return util, data
return data
def test():
test_fc1_24x24 = [27, 37, 512, 27, 37, 512, 1, 24, 1]
test_yolo_tiny_conv1_24x24 = [418, 418, 3, 3, 3, 16, 1, 24, 1]
test_mdnet_conv1_24x24 = [107, 107, 3, 7, 7, 96, 2, 24, 1]
#param = test_fc1_24x24
#param = test_yolo_tiny_conv1_24x24
param = test_mdnet_conv1_24x24
# The parameters for 1st layer of yolo_tiny
ifmap_h = param[0]
ifmap_w = param[1]
num_channels = param[2]
filt_h = param[3]
filt_w = param[4]
num_filters = param[5] #16
strides = param[6]
# Model parameters
dimensions = param[7] #32 #16
word_sz = param[8]
filter_sram_size = 1 * 1024
ifmap_sram_size = 1 * 1024
ofmap_sram_size = 1 * 1024
filter_base = 1000000
ifmap_base = 0
ofmap_base = 2000000
# Trace files
sram_read_trace = "test_sram_read.csv"
sram_write_trace = "test_sram_write.csv"
dram_filter_read_trace = "test_dram_filt_read.csv"
dram_ifmap_read_trace = "test_dram_ifamp_read.csv"
dram_write_trace = "test_dram_write.csv"
gen_all_traces(
array_h = dimensions,
array_w = dimensions,
ifmap_h= ifmap_h, ifmap_w= ifmap_w, num_channels=num_channels,
filt_h= filt_h, filt_w= filt_w, num_filt= num_filters,
strides= strides,
filter_sram_size= filter_sram_size, ifmap_sram_size= ifmap_sram_size, ofmap_sram_size= ofmap_sram_size,
word_size_bytes= word_sz, filt_base= filter_base, ifmap_base= ifmap_base,
sram_read_trace_file= sram_read_trace, sram_write_trace_file= sram_write_trace,
dram_filter_trace_file= dram_filter_read_trace,
dram_ifmap_trace_file= dram_ifmap_read_trace,
dram_ofmap_trace_file= dram_write_trace
)
if __name__ == "__main__":
test()