forked from HinPeng/TFCompGraphSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname2id.py
141 lines (114 loc) · 3.77 KB
/
name2id.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
import os
import logger
import logging
node2id_filename = "1_node2id.txt"
recomp_filename = "recompute.log"
swap_filename = "swapping_decision.log"
rswap_filename = "r_swap.log"
rrecomp_filename = "r_recompute.log"
tensor_access = "tensor_access.txt"
rtensor_access = "rtensor_access.txt"
node2id = dict()
id2node = dict()
def tensorname2id(name):
# NOTE: assert slot is less than 10
if name[-2] != '_':
# on-demand trigger
return "24:0"
node_name = name[:-2]
slot = name[-1]
assert node_name in node2id.keys()
id_name = str(node2id[node_name]) + ':' + slot
return id_name
def id2tensorname(t_id):
n_id = t_id.split(':')[0]
slot = t_id.split(':')[1]
node_name = id2node[int(n_id)]
return (node_name+'_'+slot)
def NodeToId(metadir):
rp_trans = False
sp_trans = False
if os.path.exists(metadir+recomp_filename):
rp_trans = True
if os.path.exists(metadir+swap_filename):
sp_trans = True
with open(metadir+node2id_filename) as fin:
for line in fin:
tmp = line.split()
assert len(tmp) == 2
node_name = tmp[0]
node_id = int(tmp[1])
assert node_name not in node2id.keys()
node2id[node_name] = node_id
id2node[node_id] = node_name
# fout_tac = open(metadir+rtensor_access, 'w')
# with open(metadir+tensor_access) as fin:
# for line in fin:
# tmp = line.split()
# assert len(tmp) == 2
# t_ = tmp[0]
# t_name = id2tensorname(t_)
# fout_tac.write("%s\t%s\n" % (t_name, tmp[1]))
# fout_tac.close()
# for checking
t_ids = []
i_ids = []
if sp_trans:
fout_s = open(metadir+rswap_filename, 'w')
with open(metadir+swap_filename) as fin:
for line in fin:
tmp = line.split()
assert len(tmp) == 6
t_idname = tensorname2id(tmp[0])
in_tri_idname = tensorname2id(tmp[3])
fout_s.write("%s\t%s\t%s\t%s\t%s\t%s\n" % (t_idname,
tmp[1],
tmp[2],
in_tri_idname,
tmp[4],
tmp[5]))
fout_s.close()
if rp_trans:
fout_r = open(metadir+rrecomp_filename, 'w')
with open(metadir+recomp_filename) as fin:
for line in fin:
tmp = line.split()
# index: 0, 3, 6-
assert len(tmp) >= 6
t_idname = tensorname2id(tmp[0])
in_tri_idname = tensorname2id(tmp[3])
# for check
t_id = int(t_idname[:-2])
# px: not accurate yet, just an indication
for t_id_ in t_ids:
if abs(t_id-t_id_) == 1:
logging.info("Continuous number: %d, %d" % (t_id, t_id_))
continue
# break
# exit(1)
t_ids.append(int(t_idname[:-2]))
fout_r.write("%s\t%s\t%s\t%s\t%s\t%s\t" % (t_idname,
tmp[1],
tmp[2],
in_tri_idname,
tmp[4],
tmp[5]))
for i in range(6, len(tmp)):
fout_r.write("%s\t" % tensorname2id(tmp[i]))
i_id = int(tensorname2id(tmp[i])[:-2])
if i_id not in i_ids:
i_ids.append(i_id)
fout_r.write("\n")
fout_r.close()
inters = list(set(t_ids).intersection(set(i_ids)))
if len(inters) != 0:
print("error!\n")
for i in inters:
print(i)
if __name__ == "__main__":
# pass
# metadir = "./vgg16_226_p100/"
# metadir = "./inception3_160_p100/"
# metadir = "./resnet50_190_p100/"
metadir = "./bert_66_p100/"
NodeToId(metadir)