-
Notifications
You must be signed in to change notification settings - Fork 10
/
plot_helper_funcs.py
302 lines (251 loc) · 12.8 KB
/
plot_helper_funcs.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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors
def heatmap(data, row_labels, col_labels, ax=None,
cbar_kw={}, cbarlabel="", **kwargs):
if not ax:
ax = plt.gca()
# Plot the heatmap
im = ax.imshow(data, **kwargs)
cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom", fontsize=11)
# We want to show all ticks...
ax.set_xticks(np.arange(data.shape[1]))
ax.set_yticks(np.arange(data.shape[0]))
# ... and label them with the respective list entries.
ax.set_xticklabels(col_labels)
ax.set_yticklabels(row_labels)
# Let the horizontal axes labeling appear on top.
ax.tick_params(top=True, bottom=False,
labeltop=True, labelbottom=False)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=0, ha="center",
rotation_mode="anchor")
# Turn spines off and create white grid.
for edge, spine in ax.spines.items():
spine.set_visible(False)
ax.set_xticks(np.arange(data.shape[1] + 1) - .5, minor=True)
ax.set_yticks(np.arange(data.shape[0] + 1) - .5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=3)
ax.tick_params(which="minor", bottom=False, left=False)
return im, cbar
def annotate_heatmap(im, data=None, valfmt="{x:.3f}",
textcolors=["black", "white"],
threshold=None, **textkw):
if not isinstance(data, (list, np.ndarray)):
data = im.get_array()
# Normalize the threshold to the images color range.
if threshold is not None:
threshold = im.norm(threshold)
else:
threshold = im.norm(data.max()) / 2.
# Set default alignment to center, but allow it to be
# overwritten by textkw.
kw = dict(horizontalalignment="center",
verticalalignment="center")
kw.update(textkw)
# Get the formatter in case a string is supplied
if isinstance(valfmt, str):
valfmt = matplotlib.ticker.StrMethodFormatter(valfmt)
# Loop over the data and create a `Text` for each "pixel".
# Change the text's color depending on the data.
texts = []
for i in range(data.shape[0]):
for j in range(data.shape[1]):
kw.update(color=textcolors[int(im.norm(data[i, j]) > threshold)])
text = im.axes.text(j, i, valfmt(data[i, j], None), **kw)
texts.append(text)
return texts
from classes.depthfirst.data_copy_layer import DataCopyLayer
from math import ceil
def get_dram_access(data):
dram_access_elem_copy_layer = 0
dram_access_elem_normal_layer_A = 0
dram_access_elem_normal_layer_W = 0
layer_op_to_mem_op = {'O': 'O', 'W': 'I2', 'I': 'I1'}
for idx, (cme, extra) in enumerate(data):
if isinstance(cme, DataCopyLayer):
for data_copy_layer, repeat_time in extra[1]:
for data_copy_act in data_copy_layer[0].data_copy_actions:
for mem in data_copy_act.data_copy_mem_chain:
if mem[0].name == 'dram':
dram_access_elem_copy_layer += ceil(data_copy_act.data_amount / 8) * repeat_time
else:
for normal_layer, repeat_time in extra[1]:
elem_move_collect = normal_layer[0].mapping.unit_mem_data_movement
for op, va in elem_move_collect.items():
dram_access_elem = 0
if len(va) == len(cme.accelerator.cores[0].mem_hierarchy_dict[layer_op_to_mem_op[op]]):
dram_access_elem = (va[-1].data_elem_move_count.rd_out_to_high +
va[-1].data_elem_move_count.rd_out_to_low +
va[-1].data_elem_move_count.wr_in_by_high +
va[-1].data_elem_move_count.wr_in_by_low) \
* repeat_time
if op == 'W':
dram_access_elem_normal_layer_W += dram_access_elem
else:
dram_access_elem_normal_layer_A += dram_access_elem
return int(dram_access_elem_copy_layer), int(dram_access_elem_normal_layer_A), int(dram_access_elem_normal_layer_W)
def get_gb_access(data):
gb_access_elem_copy_layer = 0
gb_access_elem_normal_layer_A = 0
gb_access_elem_normal_layer_W = 0
layer_op_to_mem_op = {'O': 'O', 'W': 'I2', 'I': 'I1'}
for idx, (cme, extra) in enumerate(data):
if isinstance(cme, DataCopyLayer):
for data_copy_layer, repeat_time in extra[1]:
for data_copy_act in data_copy_layer[0].data_copy_actions:
for mem in data_copy_act.data_copy_mem_chain:
if mem[0].name == 'sram_1MB_A':
gb_access_elem_copy_layer += ceil(data_copy_act.data_amount / 8) * repeat_time
else:
for normal_layer, repeat_time in extra[1]:
elem_move_collect = normal_layer[0].mapping.unit_mem_data_movement
for op, va in elem_move_collect.items():
gb_access_elem = 0
if len(va) == len(cme.accelerator.cores[0].mem_hierarchy_dict[layer_op_to_mem_op[op]]):
gb_access_elem = (va[-2].data_elem_move_count.rd_out_to_high +
va[-2].data_elem_move_count.rd_out_to_low +
va[-2].data_elem_move_count.wr_in_by_high +
va[-2].data_elem_move_count.wr_in_by_low) \
* repeat_time
elif len(va) == len(cme.accelerator.cores[0].mem_hierarchy_dict[layer_op_to_mem_op[op]]) - 1:
gb_access_elem = (va[-1].data_elem_move_count.rd_out_to_high +
va[-1].data_elem_move_count.rd_out_to_low +
va[-1].data_elem_move_count.wr_in_by_high +
va[-1].data_elem_move_count.wr_in_by_low) \
* repeat_time
if op == 'W':
gb_access_elem_normal_layer_W += gb_access_elem
else:
gb_access_elem_normal_layer_A += gb_access_elem
return int(gb_access_elem_copy_layer), int(gb_access_elem_normal_layer_A), int(gb_access_elem_normal_layer_W)
def get_lb_access(data):
lb_access_elem_copy_layer = 0
lb_access_elem_normal_layer_A = 0
lb_access_elem_normal_layer_W = 0
for idx, (cme, extra) in enumerate(data):
if isinstance(cme, DataCopyLayer):
for data_copy_layer, repeat_time in extra[1]:
for data_copy_act in data_copy_layer[0].data_copy_actions:
for mem in data_copy_act.data_copy_mem_chain:
if mem[0].name == 'sram_64KB':
lb_access_elem_copy_layer += ceil(data_copy_act.data_amount / 8) * repeat_time
else:
for normal_layer, repeat_time in extra[1]:
elem_move_collect = normal_layer[0].mapping.unit_mem_data_movement
for op, va in elem_move_collect.items():
if op == 'I':
lb_access_elem = (va[0].data_elem_move_count.rd_out_to_high +
va[0].data_elem_move_count.rd_out_to_low +
va[0].data_elem_move_count.wr_in_by_high +
va[0].data_elem_move_count.wr_in_by_low) \
* repeat_time
else:
lb_access_elem = (va[1].data_elem_move_count.rd_out_to_high +
va[1].data_elem_move_count.rd_out_to_low +
va[1].data_elem_move_count.wr_in_by_high +
va[1].data_elem_move_count.wr_in_by_low) \
* repeat_time
if op == 'W':
lb_access_elem_normal_layer_W += lb_access_elem
else:
lb_access_elem_normal_layer_A += lb_access_elem
return int(lb_access_elem_copy_layer), int(lb_access_elem_normal_layer_A), int(lb_access_elem_normal_layer_W)
def get_total_en_la(data):
total_en = 0
total_la = 0
for cme, extra_info in data:
total_en += cme.energy_total
total_la += cme.latency_total1
return total_en, total_la
def get_en_breakdown(data):
from classes.depthfirst.data_copy_layer import DataCopyLayer
en_break_down = {'MAC': 0, 'Normal Layer': 0, 'Data Copy Layer': 0}
for cme, extra in data:
if isinstance(cme, DataCopyLayer):
en_break_down['Data Copy Layer'] += cme.energy_total
else:
en_break_down['MAC'] += cme.MAC_energy
en_break_down['Normal Layer'] += cme.mem_energy
return en_break_down
def get_la_breakdown(data):
from classes.depthfirst.data_copy_layer import DataCopyLayer
la_break_down = {'Ideal Computation': 0, 'Spatial Stall': 0, 'Temporal Stall': 0, 'Data Preparation': 0}
for cme, extra in data:
if isinstance(cme, DataCopyLayer):
la_break_down['Data Preparation'] += cme.latency_total
else:
la_break_down['Ideal Computation'] += cme.ideal_cycle
la_break_down['Spatial Stall'] += (cme.ideal_temporal_cycle - cme.ideal_cycle)
la_break_down['Temporal Stall'] += (cme.latency_total0 - cme.ideal_temporal_cycle)
la_break_down['Data Preparation'] += (cme.latency_total1 - cme.latency_total0)
return la_break_down
def get_tile_type_count(data):
tile_type_count = 0
for idx, (cme, extra) in enumerate(data):
if isinstance(cme, DataCopyLayer):
continue
else:
tile_type_count = len(extra[1])
break
return tile_type_count
def get_MAC_count(data):
MAC_count = 0
for idx, (cme, extra) in enumerate(data):
if isinstance(cme, DataCopyLayer):
continue
else:
for normal_layer, repeat_time in extra[1]:
MAC_count += normal_layer[1][0].total_MAC_count * repeat_time
return MAC_count
def data_collect(result_saving_path):
import glob
import re
import pickle
class DataToPlot:
def __init__(self):
self.en_collect = {}
self.la_collect = {}
self.MAC_collect = {}
self.dram_access_collect = {}
self.dram_access_collect_copy_layer = {}
self.dram_access_collect_normal_layer_A = {}
self.dram_access_collect_normal_layer_W = {}
self.gb_access_collect = {}
self.gb_access_collect_copy_layer = {}
self.gb_access_collect_normal_layer_A = {}
self.gb_access_collect_normal_layer_W = {}
self.lb_access_collect = {}
self.lb_access_collect_copy_layer = {}
self.lb_access_collect_normal_layer_A = {}
self.lb_access_collect_normal_layer_W = {}
self.tile_type_collect = {}
data_to_plot = DataToPlot()
paths = glob.glob(f'{result_saving_path}/*.pkl')
for idx, path in enumerate(paths):
print(f'Reading in result -- {path}')
ky = re.split('[/ .]', path)[-2]
with open(path, 'rb') as f:
data = pickle.load(f)
f.close()
data_to_plot.en_collect[ky], data_to_plot.la_collect[ky] = get_total_en_la(data)
data_to_plot.MAC_collect[ky] = get_MAC_count(data)
dram_access1, dram_access2, dram_access3 = get_dram_access(data)
data_to_plot.dram_access_collect[ky] = dram_access1 + dram_access2 + dram_access3
data_to_plot.dram_access_collect_copy_layer[ky] = dram_access1
data_to_plot.dram_access_collect_normal_layer_A[ky] = dram_access2
data_to_plot.dram_access_collect_normal_layer_W[ky] = dram_access3
gb_access1, gb_access2, gb_access3 = get_gb_access(data)
data_to_plot.gb_access_collect[ky] = gb_access1 + gb_access2 + gb_access3
data_to_plot.gb_access_collect_copy_layer[ky] = gb_access1
data_to_plot.gb_access_collect_normal_layer_A[ky] = gb_access2
data_to_plot.gb_access_collect_normal_layer_W[ky] = gb_access3
lb_access1, lb_access2, lb_access3 = get_lb_access(data)
data_to_plot.lb_access_collect[ky] = lb_access1 + lb_access2 + lb_access3
data_to_plot.lb_access_collect_copy_layer[ky] = lb_access1
data_to_plot.lb_access_collect_normal_layer_A[ky] = lb_access2
data_to_plot.lb_access_collect_normal_layer_W[ky] = lb_access3
data_to_plot.tile_type_collect[ky] = get_tile_type_count(data)
return data_to_plot