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 151
/
sram_traffic_ws.py
569 lines (471 loc) · 20.7 KB
/
sram_traffic_ws.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import math
from tqdm import tqdm
def sram_traffic(
dimension_rows=4,
dimension_cols=4,
ifmap_h=7, ifmap_w=7,
filt_h=3, filt_w=3,
num_channels=3,
strides=1, num_filt=8,
ofmap_base=2000000, filt_base=1000000, ifmap_base=0,
sram_read_trace_file="sram_read.csv",
sram_write_trace_file="sram_write.csv"
):
# Dimensions of output feature map channel
E_h = math.floor((ifmap_h - filt_h + strides) / strides)
E_w = math.floor((ifmap_w - filt_w + strides) / strides)
# Number of pixels in one convolution window
px_per_conv_window = filt_h * filt_w * num_channels
r2c = px_per_conv_window
# Total number of ofmap px across all channels
num_ofmap_px = E_h * E_w * num_filt
e2 = E_h * E_w
e2m = num_ofmap_px
# Variables to calculate folds in runtime
num_h_fold = 1
num_v_fold = 1
max_parallel_window = 1
# Variables for utilization calculation
util = 0
compute_cycles = 0
if dimension_rows < px_per_conv_window:
num_h_fold = math.ceil(px_per_conv_window/dimension_rows)
else:
max_parallel_window = math.floor(dimension_rows/ px_per_conv_window)
reqd_cols = num_filt # Total number of cols to be mapped
max_cols_per_v_fold = max_parallel_window * dimension_cols
num_v_folds = math.ceil(reqd_cols / max_cols_per_v_fold)
remaining_cols = reqd_cols
cycles = 0
prev_cycl = 0
#print("Vertical folds = " +str(num_v_folds))
# These are the starting addresses of filter weights in the memory
all_col_addr_list = []
for c in range(num_filt):
addr = (c) * r2c + filt_base
all_col_addr_list.append(addr)
# These are the starting addresses of ifmap windows in the memory
hc = ifmap_w * num_channels
all_ifmap_base_addr = []
for px in range(int(e2)): #number of ofmap px in a ofmap channel
addr = (px / E_w) * strides * hc + (px%E_w) * strides
all_ifmap_base_addr.append(addr)
for v in tqdm(range(int(num_v_folds))):
#print("V fold id: " + str(v))
# Take a slice of the starting addresses that are relevant for this v_fold
cols_this_fold = min(remaining_cols, max_parallel_window * dimension_cols)
idx_start = v * dimension_cols
idx_end = idx_start + cols_this_fold
col_addr_list = all_col_addr_list[idx_start:idx_end]
if num_h_fold > 1 :
rem_h = r2c # Tracks the elements processed within a conv filter
next_ifmap_addr = ifmap_base # Starts from the top left corner of the IFMAP matrix
for h in range(num_h_fold):
rows_this_fold = min(rem_h, dimension_rows)
#print("h fold id: " + str(h))
# Values returned
# cycles -> Cycle count for the next operation ie. cycles elapsed + 1
# col_addr_list -> The starting filter address for the next iteration
cycles, col_addr_list = gen_trace_filter_partial(
col_addrs = col_addr_list,
cycle = cycles,
num_rows = dimension_rows,
remaining = rows_this_fold,
sram_read_trace_file = sram_read_trace_file
)
#print("Weights loaded by " + str(cycles) + " cycles")
data_out_cycles = cycles #Store this cycle for parallel readout
cycles_ifmap = gen_trace_ifmap_partial(
cycle = cycles,
num_rows = dimension_rows, num_cols = dimension_cols,
num_filters = num_filt,
remaining = rem_h,
remaining_filters = remaining_cols,
ifmap_h = ifmap_h, ifmap_w = ifmap_w,
filt_h = filt_h, filt_w = filt_w,
num_channels = num_channels,
stride = strides, ifmap_base = ifmap_base,
sram_read_trace_file = sram_read_trace_file
)
cycles_ofmap = gen_trace_ofmap(
cycle = data_out_cycles,
num_rows = dimension_rows,
num_cols = dimension_cols,
ofmap_base = ofmap_base,
window_size= rows_this_fold,
parallel_window =1,
num_ofmap_px = int(e2),
filters_done = (v * dimension_cols),
num_filter = num_filt,
sram_write_trace_file = sram_write_trace_file
)
#print("IFMAPS processed by " + str(cycles) + " cycles")
util_this_fold = (rows_this_fold * cols_this_fold) /(dimension_rows * dimension_cols)
rem_h -= rows_this_fold
cycles = max(cycles_ifmap, cycles_ofmap)
del_cycl = cycles - prev_cycl
util += util_this_fold * del_cycl
compute_cycles += del_cycl
prev_cycl = cycles
else:
#filters_this_fold = min(remaining_cols, max_cols_per_v_fold)
filt_done = v * max_parallel_window * dimension_cols
rem = num_filt - filt_done
parallel_window = math.ceil(rem / dimension_cols)
parallel_window = int(min(max_parallel_window, parallel_window))
cycles_filter = gen_filter_trace(
cycle = cycles,
num_rows = dimension_rows, num_cols = dimension_cols,
filt_h = filt_h, filt_w = filt_w, num_channels = num_channels,
col_addr = col_addr_list,
parallel_window=parallel_window,
filters_this_fold=cols_this_fold,
sram_read_trace_file=sram_read_trace_file
)
cycles_ifmap, rows_this_fold\
= gen_ifmap_trace(
cycle = cycles_filter,
num_rows = dimension_rows, num_cols = dimension_cols,
ifmap_h = ifmap_h, ifmap_w = ifmap_w,
filt_h = filt_h, filt_w = filt_w,
num_channels = num_channels, stride = strides,
parallel_window = parallel_window,
sram_read_trace_file = sram_read_trace_file
)
cycles_ofmap = gen_trace_ofmap(
cycle = cycles_filter,
num_rows = dimension_rows, num_cols = dimension_cols,
ofmap_base = ofmap_base,
parallel_window = parallel_window,
window_size = r2c,
num_ofmap_px = int(e2),
filters_done = int(v * max_parallel_window * dimension_cols),
num_filter = num_filt,
sram_write_trace_file = sram_write_trace_file
)
cycles = max(cycles_ifmap, cycles_ofmap)
del_cycl = cycles - prev_cycl
# Since multiple filters are being mapped on a single col due to large number of rows
# util calculation is a little involved,
# cols_this_fold --> number of filters mapped this fold
rem = cols_this_fold
tmp_util = 0
for _ in range(parallel_window):
col_used = min(rem, dimension_cols)
row_used = r2c # Number of row used will always be in multiple of r2c,
# parallel window calc took care of this
tmp_util += row_used * col_used
rem -= col_used
#util_this_fold = (rows_this_fold * cols_this_fold) /(dimension_rows * dimension_cols)
util_this_fold = tmp_util /(dimension_rows * dimension_cols)
util += util_this_fold * del_cycl
compute_cycles += del_cycl
prev_cycl = cycles
remaining_cols -= cols_this_fold
final = str(cycles)
final_util = (util / compute_cycles) * 100
#print("Compute finished at: " + str(final) + " cycles")
return (final, final_util)
def gen_filter_trace(
cycle = 0,
num_rows = 4, num_cols = 4,
filt_h = 3, filt_w = 3, num_channels = 3,
col_addr = [],
parallel_window = 1,
filters_this_fold = 4,
sram_read_trace_file = "sram_read.csv"
):
outfile = open(sram_read_trace_file,'a')
# There is no data from the left side till the weights are fed in
# This prefix is to mark the blanks
prefix = ""
for r in range(num_rows):
prefix += ", "
# Calculate the convolution window size
r2c = filt_h * filt_w * num_channels
rem = filters_this_fold # Track the number of filters yet to process
#For each wrap around
for w in range(parallel_window):
# Number of active columns in this wrap
cols = min(num_cols, rem)
rem -= cols
# For each row in the window
for r in range(r2c):
entry = str(cycle) + ", " + prefix
cycle += 1
# In each cycle, for each column feed one weight
for c in range(cols):
indx = w * num_cols + c
entry += str(col_addr[indx]) + ", "
col_addr[indx] += 1
if cols < num_cols:
for _ in range(c, num_cols):
entry += ", "
entry += "\n"
outfile.write(entry)
outfile.close()
return cycle
def gen_ifmap_trace(
cycle = 0,
num_rows = 4, num_cols = 4,
ifmap_h = 7, ifmap_w = 7,
filt_h = 3, filt_w = 3,
num_channels = 3, stride = 1,
parallel_window = 1,
sram_read_trace_file = "sram_read.csv"
):
outfile = open(sram_read_trace_file,'a')
postfix = ""
for c in range(num_cols):
postfix += ", "
E_h = math.floor((ifmap_h - filt_h + stride) / stride)
E_w = math.floor((ifmap_w - filt_w + stride) / stride)
e2 = E_h * E_w
r2c = filt_h * filt_w * num_channels
rc = filt_w * num_channels
hc = ifmap_w * num_channels
idle = num_rows - (r2c * parallel_window)
idle = max(idle, 0)
used_rows = num_rows - idle
# Adding entries for columns and empty rows
#print("Idle lanes = " + str(idle))
idle += num_cols
for i in range(idle):
postfix += ", "
postfix += "\n"
base_addr = 0
for e in range(int(e2)):
entry = str(cycle) + ", "
cycle += 1
#print("Cycle= " + str(cycle))
#Inner loop for all the rows in array
num_rows = r2c
row_entry = []
for r in range(num_rows):
row_idx = math.floor(r / rc) # math.floor to get in integral value
col_idx = r % rc
add = base_addr + row_idx * hc + col_idx
#print("Row idx " + str(row_idx) + " col_idx " + str(col_idx) +" add " + str(add))
row_entry.append(add)
# Reverse the printing order
# Reversal is needed because the filter are stored in upside down order in the array
# ie. last row has the first weight element
l = len(row_entry)
#print("Parallel windows = " + str(parallel_window))
for w in range(parallel_window):
#print("Window = " + str(w))
for ridx in range(l):
entry += str(row_entry[l - ridx -1]) + ", "
entry += postfix
outfile.write(entry)
# Calculate the IFMAP addresses for next cycle
px_this_row = (e+1) % E_w
if px_this_row == 0:
#print("New row")
ifmap_row = math.floor(base_addr / hc)
base_addr = (ifmap_row + stride) * hc
else:
base_addr += stride * num_channels
#print("OFAMP px = " + str(e+1) + " base_addr: " + str(base_addr))
outfile.close()
return cycle, used_rows
def gen_trace_filter_partial(
col_addrs=[], #Ensure that this takes care of the v_folding
cycle=0,
num_rows=4,
remaining=4,
sram_read_trace_file="sram_read.csv"
):
outfile = open(sram_read_trace_file, 'a')
num_cols = len(col_addrs)
# output formatting: Add empty commas for row addresses as no element is fed from the left
prefix = ""
for r in range(num_rows):
prefix += ", "
# Entries per cycle
for r in range(remaining): # number of rows this cycle
entry = str(cycle) + ", " + prefix
for c in range(num_cols):
entry += str(col_addrs[c]) + ", "
col_addrs[c] += 1
cycle += 1
entry += "\n"
outfile.write(entry)
outfile.close()
return cycle, col_addrs
def gen_trace_ifmap_partial(
cycle = 0,
num_rows = 4, num_cols = 4,
remaining=4,
num_filters = 8, #
remaining_filters = 0, # These two are used to track the reads of PS
ifmap_h = 4, ifmap_w = 4,
filt_h = 3, filt_w = 3,
num_channels = 3,
stride = 1,
ifmap_base = 0, ofmap_base = 2000000,
sram_read_trace_file = "sram_read.csv"
):
outfile = open(sram_read_trace_file, 'a')
postfix = ""
for c in range(num_cols):
postfix += ", "
postfix += "\n"
r2c = filt_h * filt_w * num_channels
rc = filt_w * num_channels
hc = ifmap_w * num_channels
E_w = (ifmap_w - filt_w + stride) / stride
E_h = (ifmap_h - filt_h + stride) / stride
num_ofmap_px = E_h * E_w
index = r2c - remaining
base_addr = 0
filter_done = num_filters - remaining_filters
#outfile.write(str(filter_done) + ", " + str(num_filters)+", "+str(remaining_filters)+", "+ "\n")
#ofmap_offset = filter_done * num_ofmap_px
ofmap_offset = filter_done
effective_cols = min(remaining_filters, num_cols)
tick = 0 # Proxy for clock to track input skewing
# Outerloop for all ofmap pixels in an ofmap channel
for e in range(int(num_ofmap_px)):
entry = str(cycle) + ", "
cycle += 1
#print("Cycle= " + str(cycle))
#Inner loop for all the rows in array
num_rows = min(num_rows, remaining)
row_entry = []
for r in range(num_rows):
row_idx = math.floor((index+r) / rc) # math.floor to get in integral value
col_idx = (index+r) % rc
add = base_addr + row_idx * hc + col_idx
#print("Row idx " + str(row_idx) + " col_idx " + str(col_idx) +" add " + str(add))
row_entry.append(add)
# Reverse the printing order
# Reversal is needed because the filter are stored in upside down order in the array
# ie. last row has the first weight element
l = len(row_entry)
for ridx in range(l):
entry += str(row_entry[l - ridx -1]) + ", "
# In case of partial mapping
# index > 0 implies that there is a partial sum generated from prev h_fold
# This partial sum is now fed from the top to be summed with the PS generated in this h_fold
# The following part print the read addresses for PS
# Anand : TODO, Implementation choice, do not support right now
'''
if index > 0:
postfix = ""
for c in range(effective_cols):
if (tick - c) > -1: # Track PS reads for skew
a = (e - c) * num_filters + c # e - c: Taking care of skew by c cycles
a = a + ofmap_base + ofmap_offset
postfix += str(a) + ", "
else:
postfix += ", "
tick += 1
#print("Tick =", str(tick) + "Postfix= " + postfix)
postfix += "\n"
'''
entry += postfix
outfile.write(entry)
px_this_row = (e+1) % E_w
if px_this_row == 0:
#print("New row")
ifmap_row = math.floor(base_addr / hc)
base_addr = (ifmap_row + stride) * hc
else:
base_addr += stride * num_channels
#print("OFAMP px = " + str(e+1) + " base_addr: " + str(base_addr))
outfile.close()
return cycle
def gen_trace_ofmap(
cycle = 0,
num_rows = 4, num_cols =4,
ofmap_base = 2000000,
parallel_window = 1,
window_size = 27,
num_ofmap_px = 16, # This is per ofmap channel
filters_done = 0, # To track v fold
num_filter = 8, # To track if all filters have finished
sram_write_trace_file = "sram_write.csv"
):
outfile = open(sram_write_trace_file,'a')
#cycle = num_cols + cycle # Accounts for the time taken to reduce accross all cols
# Corner case when parallel_window = 1, but num_filter < num_cols
if parallel_window > 1:
cycle += num_cols
cycle += window_size # window_size == r2c
else:
rem = (num_filter - filters_done)
cycle += min(rem, num_cols)
cycle += window_size
#ofmap_add_offset = filters_done * num_ofmap_px
ofmap_add_offset = filters_done
remaining_filters = num_filter - filters_done
effective_cols = num_cols * parallel_window
effective_cols = min(effective_cols, remaining_filters)
for e in range(int(num_ofmap_px)):
entry = str(cycle) + ", "
cycle += 1
done = filters_done
for col in range(effective_cols):
if done < num_filter:
a = e * num_filter + col # z first row major
a = a + ofmap_add_offset + ofmap_base
entry += str(a) + ", "
else:
# Code should not enter this part
entry += "!, "
entry += "\n"
outfile.write(entry)
outfile.close()
return cycle
# Trace generation for moving generated ofmap data in cases when only partial window fits
# This implementation prints out the ofmap pixel in the exact cycle it is generated
# Not used in scale sim at the moment.
# SCALE sim waits till all the columns finish generating OFMAP.
def gen_trace_ofmap_partial_imm(
cycle = 0,
num_rows = 4, num_cols =4,
ofmap_base = 2000000,
num_ofmap_px = 16,
num_filter = 8,
filters_done = 0,
sram_write_trace_file = "sram_write.csv"
):
outfile = open(sram_write_trace_file,'a')
start_cycle = num_rows + cycle
col_addr = []
for col in range(int(num_cols)):
a = (filters_done + col)
col_addr.append(a)
for tick in range(int(num_ofmap_px + num_cols)):
cycle = start_cycle + tick
entry = str(cycle) + ", "
for col in range(int(num_cols)):
# Condition to maintain skew
if tick >= col and (tick - col)< num_ofmap_px:
entry += str(col_addr[col]) + ", "
col_addr[col] += num_filter
else:
entry += ", "
entry += "\n"
outfile.write(entry)
outfile.close()
if __name__ == "__main__":
h_h = 5
h_w = 5
r_h = 2
r_w = 2
c = 2
u =1
m = 9
dim_h = 16
dim_v = 5
sram_traffic(
dimension_rows = dim_h,
dimension_cols = dim_v,
ifmap_h = h_h, ifmap_w = h_w,
filt_h = r_h, filt_w = r_w,
num_channels = c,
strides = u,
num_filt = m
)