-
Notifications
You must be signed in to change notification settings - Fork 14
/
evaluate.py
399 lines (327 loc) · 15.1 KB
/
evaluate.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
import os
import time
import math
import configparser as cp
import simArch.run_nets as r
import simHw.profiling as prof
import simEff.efficiency as eff
from absl import flags
from absl import app
import platform
import subprocess
import warnings
FLAGS = flags.FLAGS
# name of flag | default | explanation
flags.DEFINE_string("name", "template_run", "indicateing path to get config files")
# template is tpu_08b_ur_032c_ddr3_w_sram_alexnet
# architecture sim input config
# path/systolic.cfg: file to get systolic array architechture from
# path/network.csv: consecutive GEMM topologies to read
# hardware sim input config
# path/sram.cfg: SRAM configs for hardware simulation. Note that the sizes are specified in systolic.cfg
# path/dram.cfg: DRAM configs for hardware simulation
# path/pe.cfg: PE area and power data for hardware simulation
class evaluate:
def __init__(self, save = False, simArch = True, simHw = True, simEff = True):
self.save_space = save
self.simArch = simArch
self.simHw = simHw
self.simEff = simEff
def parse_config(self):
general = 'general'
arch_sec = 'architecture_presets'
hw_sec = 'hardware_presets'
path = os.getcwd() + "/config/" + FLAGS.name
if not os.path.exists(path):
raise ValueError("Input name is invalid.")
config_filename = path + "/systolic.cfg"
print("Using Architechture from ",config_filename)
config = cp.ConfigParser()
config.read(config_filename)
## Read the run name from the configuration path
self.run_name = config_filename.split('/')[-2]
## Read the architecture_presets
## Array height
ar_h = config.get(arch_sec, 'ArrayHeight').split(',')
self.ar_h = ar_h[0].strip()
## Array width
ar_w = config.get(arch_sec, 'ArrayWidth').split(',')
self.ar_w = ar_w[0].strip()
## IFMAP SRAM buffer
# in K-Word
ifmap_sram = config.get(arch_sec, 'IfmapSramSz').split(',')
self.isram = ifmap_sram[0].strip()
## FILTER SRAM buffer
# in K-Word
filter_sram = config.get(arch_sec, 'FilterSramSz').split(',')
self.fsram = filter_sram[0].strip()
## OFMAP SRAM buffer
# in K-Word
ofmap_sram = config.get(arch_sec, 'OfmapSramSz').split(',')
self.osram = ofmap_sram[0].strip()
self.dataflow= config.get(arch_sec, 'Dataflow')
self.df_string = "Weight Stationary"
assert self.dataflow == 'ws', "Input dataflow for uSystolic should be weight stationary."
# in Word
ifmap_offset = config.get(arch_sec, 'IfmapOffset')
self.ifmap_offset = int(ifmap_offset.strip())
# in Word
filter_offset = config.get(arch_sec, 'FilterOffset')
self.filter_offset = int(filter_offset.strip())
# in Word
ofmap_offset = config.get(arch_sec, 'OfmapOffset')
self.ofmap_offset = int(ofmap_offset.strip())
word_sz_bytes = config.get(arch_sec, 'WordByte')
self.word_sz_bytes = float(word_sz_bytes.strip())
self.wgt_bw_opt = to_bool(config.get(arch_sec, 'WeightBwOpt').strip())
# this parameter is used to evaluate power and energy
self.computing= config.get(arch_sec, 'Computing').strip()
self.zero_sram_ifmap = to_bool(config.get(hw_sec, 'ZeroIfmapSram').strip())
self.zero_sram_filter = to_bool(config.get(hw_sec, 'ZeroFilterSram').strip())
self.zero_sram_ofmap = to_bool(config.get(hw_sec, 'ZeroOfmapSram').strip())
self.sram_access_buf = to_bool(config.get(hw_sec, 'SramAccBuf').strip())
self.topology_file = path + "/network.csv"
self.sram_file = path + "/sram.cfg"
self.dram_file = path + "/dram.cfg"
self.pe_file = path + "/pe.cfg"
self.time_stamp = time.time()
def run_eval(self):
self.parse_config()
if self.simArch == True:
self.run_arch()
if self.simHw == True:
self.run_hw()
if self.simEff == True:
self.run_eff()
def run_arch(self):
print("====================================================")
print("************ uSystolic Architecture Sim ************")
print("====================================================")
print("Array Size: \t" + str(self.ar_h) + "x" + str(self.ar_w))
print("SRAM IFMAP: \t" + str(self.isram))
print("SRAM Filter: \t" + str(self.fsram))
print("SRAM OFMAP: \t" + str(self.osram))
print("Word Bytes: \t" + str(self.word_sz_bytes))
print("Dataflow: \t" + self.df_string)
print("Weight BW Opt: \t" + str(self.wgt_bw_opt))
print("CSV file: \t" + self.topology_file)
print("====================================================")
offset_list = [self.ifmap_offset, self.filter_offset, self.ofmap_offset] # in Word
# for arch sim, dram trace generation requires non-zero sram size
# now arch sim only reports the mac utilization, as well as generating ideal sram and dram traces
r.run_net(
ifmap_sram_size = int(self.isram), # in K-Word
filter_sram_size = int(self.fsram), # in K-Word
ofmap_sram_size = int(self.osram), # in K-Word
array_h = int(self.ar_h),
array_w = int(self.ar_w),
net_name = self.run_name,
data_flow = self.dataflow,
word_size_bytes = self.word_sz_bytes,
wgt_bw_opt = self.wgt_bw_opt,
topology_file = self.topology_file,
offset_list = offset_list
)
self.arch_cleanup()
print("******* uSystolic Architecture Sim Complete ********")
sec = time.time() - self.time_stamp
hours = int(math.floor(sec / 3600))
mins = int(math.floor((sec % 3600) / 60))
secs = int(math.floor(sec % 60))
print("Runtime: " + str(hours) + "-hours, " + str(mins) + "-mins, " + str(secs) + "-secs")
self.time_stamp = time.time()
print("Results: ./outputs/"+self.run_name+"/simArchOut/")
def run_hw(self):
print()
print()
print()
print("====================================================")
print("************** uSystolic Hardware Sim **************")
print("====================================================")
print("Computing: \t" + self.computing)
print("SRAM file: \t" + self.sram_file)
print("DRAM file: \t" + self.dram_file)
print("PE file: \t" + self.pe_file)
print("====================================================")
if self.zero_sram_ifmap == True:
# estimate the ifmap sram size to hide latency
print("IFMAP SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.isram = 0
if self.zero_sram_filter == True:
# estimate the filter sram size to hide latency
print("FILTER SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.fsram = 0
if self.zero_sram_ofmap == True:
# estimate the ofmap sram size to hide latency
print("OFMAP SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.osram = 0
prof.profiling(
array_h = int(self.ar_h),
array_w = int(self.ar_w),
ifmap_sram_size = int(self.isram), # in K-Word
filter_sram_size = int(self.fsram), # in K-Word
ofmap_sram_size = int(self.osram), # in K-Word
word_sz_bytes=self.word_sz_bytes, # bytes per word
ifmap_base=self.ifmap_offset, # in word
filter_base=self.filter_offset, # in word
ofmap_base=self.ofmap_offset, # in word
sram_cfg_file=self.sram_file,
dram_cfg_file=self.dram_file,
pe_cfg_file=self.pe_file,
computing=self.computing,
run_name=self.run_name,
topology_file=self.topology_file,
sram_access_buf=self.sram_access_buf
)
self.hw_cleanup()
print("********* uSystolic Hardware Sim Complete **********")
sec = time.time() - self.time_stamp
hours = int(math.floor(sec / 3600))
mins = int(math.floor((sec % 3600) / 60))
secs = int(math.floor(sec % 60))
print("Runtime: " + str(hours) + "-hours, " + str(mins) + "-mins, " + str(secs) + "-secs")
self.time_stamp = time.time()
print("Results: ./outputs/"+self.run_name+"/simHwOut/")
print()
def run_eff(self):
print()
print()
print()
print("====================================================")
print("************* uSystolic Efficiency Sim *************")
print("====================================================")
print("Computing: \t" + self.computing)
print("SRAM file: \t" + self.sram_file)
print("DRAM file: \t" + self.dram_file)
print("PE file: \t" + self.pe_file)
print("====================================================")
if self.zero_sram_ifmap == True:
# estimate the ifmap sram size to hide latency
print("IFMAP SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.isram = 0
if self.zero_sram_filter == True:
# estimate the filter sram size to hide latency
print("FILTER SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.fsram = 0
if self.zero_sram_ofmap == True:
# estimate the ofmap sram size to hide latency
print("OFMAP SRAM is disabled in actual hardware.")
if self.computing == "BinarySerial" or self.computing == "BinaryParallel":
print("\tFor binary computing, this is not suggested!\n")
self.osram = 0
eff.estimate(
array_h = int(self.ar_h),
array_w = int(self.ar_w),
ifmap_sram_size = int(self.isram), # in K-Word
filter_sram_size = int(self.fsram), # in K-Word
ofmap_sram_size = int(self.osram), # in K-Word
word_sz_bytes=self.word_sz_bytes, # bytes per word
ifmap_base=self.ifmap_offset, # in word
filter_base=self.filter_offset, # in word
ofmap_base=self.ofmap_offset, # in word
sram_cfg_file=self.sram_file,
dram_cfg_file=self.dram_file,
pe_cfg_file=self.pe_file,
computing=self.computing,
run_name=self.run_name,
topology_file=self.topology_file,
sram_access_buf=self.sram_access_buf
)
self.eff_cleanup()
print("******** uSystolic Efficiency Sim Complete *********")
sec = time.time() - self.time_stamp
hours = int(math.floor(sec / 3600))
mins = int(math.floor((sec % 3600) / 60))
secs = int(math.floor(sec % 60))
print("Runtime: " + str(hours) + "-hours, " + str(mins) + "-mins, " + str(secs) + "-secs")
self.time_stamp = time.time()
print("Results: ./outputs/"+self.run_name+"/simEffOut/")
print()
def arch_cleanup(self):
if not os.path.exists("./outputs/"):
os.system("mkdir ./outputs")
path = "./outputs/" + self.run_name + "/simArchOut"
if not os.path.exists(path):
os.system("mkdir -p " + path + "/")
else:
t = time.time()
new_path= path + "_" + str(t)
os.system("mv " + path + " " + new_path)
os.system("mkdir -p " + path + "/")
cmd = "mv " + self.run_name + "*.csv " + path
os.system(cmd)
cmd = "mkdir " + path +"/layer_wise"
os.system(cmd)
cmd = "mv " + path + "/" + self.run_name + "*sram* " + path +"/layer_wise"
os.system(cmd)
cmd = "mv " + path + "/" + self.run_name + "*dram* " + path +"/layer_wise"
os.system(cmd)
def hw_cleanup(self):
# for linux os
if not os.path.exists("./outputs/"):
os.system("mkdir ./outputs")
path = "./outputs/" + self.run_name + "/simHwOut"
if not os.path.exists(path):
os.system("mkdir -p " + path + "/")
else:
t = time.time()
new_path= path + "_" + str(t)
os.system("mv " + path + " " + new_path)
os.system("mkdir -p " + path + "/")
cmd = "mv " + self.run_name + "*.csv " + path
os.system(cmd)
if self.save_space == True:
cmd = "rm -rf " + "./outputs/" + self.run_name + "/simArchOut" +"/layer_wise"
os.system(cmd)
def eff_cleanup(self):
# for linux os
if not os.path.exists("./outputs/"):
os.system("mkdir ./outputs")
path = "./outputs/" + self.run_name + "/simEffOut"
if not os.path.exists(path):
os.system("mkdir -p " + path + "/")
else:
t = time.time()
new_path= path + "_" + str(t)
os.system("mv " + path + " " + new_path)
os.system("mkdir -p " + path + "/")
cmd = "mv " + self.run_name + "*.rpt " + path
os.system(cmd)
cmd = "mv " + self.run_name + "*.cfg* " + path
os.system(cmd)
cmd = "mv " + self.run_name + "*.csv " + path
os.system(cmd)
path = "./outputs/" + self.run_name + "/config"
if not os.path.exists(path):
os.system("mkdir -p " + path + "/")
else:
t = time.time()
new_path= path + "_" + str(t)
os.system("mv " + path + " " + new_path)
os.system("mkdir -p " + path + "/")
cmd = "cp ./config/" + self.run_name + "/* " + path
os.system(cmd)
def to_bool(value):
"""
Converts 'something' to boolean. Raises exception for invalid formats
Possible True values: 1, True, "1", "TRue", "yes", "y", "t"
Possible False values: 0, False, None, [], {}, "", "0", "faLse", "no", "n", "f", 0.0, ...
"""
if str(value).lower() in ("yes", "y", "true", "t", "1"): return True
if str(value).lower() in ("no", "n", "false", "f", "0", "0.0", "", "none", "[]", "{}"): return False
raise Exception('Invalid value for boolean conversion: ' + str(value))
def main(argv):
s = evaluate(save = True, simArch = True, simHw = True, simEff = True)
s.run_eval()
if __name__ == '__main__':
app.run(main)