-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
550 lines (511 loc) · 21 KB
/
utils.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
import pandas as pd
import numpy as np
import tqdm
import csv
import torch
from tqdm import tqdm
import igraph
import networkx as nx
import random
import pickle
import gzip
from shutil import copy
from copy import deepcopy
SUBG_INDI = {0: [],
1: [],
2: [0],
3: [1],
4: [0, 1],
5: [0, 1],
6: [2],
7: [2],
8: [2],
9: [2],
10: [1, 2],
11: [1, 2],
12: [1, 2],
13: [1, 2],
14: [0, 2],
15: [0, 2],
16: [0, 2],
17: [0, 2],
18: [1, 0, 2],
19: [1, 0, 2],
20: [1, 0, 2],
21: [1, 0, 2],
22: [1, 0, 2],
23: [1, 0, 2],
24: [1, 0, 2],
25: [1, 0, 2]
}
def load_module_state(model, state_name, device):
pretrained_dict0 = torch.load(state_name, map_location=device) #, map_location=torch.device('cpu')
model_dict = model.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict0.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 3. load the new state dict
model.load_state_dict(pretrained_dict)
return
def performance_readout(num_graphs, file_dir='circuit', name = 'ckt_simulation_summary_10000.txt'):
num_graphs = 10000
pbar = tqdm(range(num_graphs))
gain = []
bw = []
pm = []
fom = []
valid = []
#with open('ckt_simulation_summary_10000.txt', 'r') as f:
file_name = os.path.join(file_dir, name)
with open(file_name, 'r') as f:
for i in pbar:
row = f.readline().strip().split()
if not row[1] == 'Simulation':
g = float(row[1])/100.0
p = float(row[2])/-90.0
b = float(row[3])/1e9
gain.append(g)
pm.append(p)
bw.append(b)
fo = 1.2 * np.abs(g) + 1.6 * p + 10 * b
fom.append(fo)
valid.append(1)
else:
gain.append(0)
pm.append(0)
bw.append(0)
fom.append(0)
valid.append(0)
gain = np.array(gain) - np.min(gain) + 0.00001
pm = np.array(pm) - np.min(pm) + 0.00001
perform = {'valid':valid, 'gain':gain, 'pm':pm, 'bw':bw, 'fom':fom}
perform_df = pd.DataFrame(perform)
out_name = os.path.join(file_dir, perform.csv)
perform_df.to_csv(out_name)
return perform_df
class MyException(Exception):
def __init__(self, msg):
self.msg = msg
def is_same_DAG(g0, g1):
# note that it does not check isomorphism
if g0.vcount() != g1.vcount():
return False
for vi in range(g0.vcount()):
if g0.vs[vi]['type'] != g1.vs[vi]['type']:
return False
if set(g0.neighbors(vi, 'in')) != set(g1.neighbors(vi, 'in')):
return False
return True
def pygraph_to_igraph(pygraph):
n_v = pygraph.x.shape[0]
attr_v = pygraph.x
edge_idxs = pygraph.edge_index
g = igraph.Graph(directed=True)
g.add_vertices(n_v)
for i in range(n_v):
g.vs[i]['type'] = torch.argmax(attr_v[i,:-1]).item()
g.vs[i]['feat'] = attr_v[i,-1].item()
edges = []
for src, tgt in zip(edge_idxs[1], edge_idxs[0]):
edges += [(src, tgt)]
g.add_edges(edges)
return g
def is_same_igraph_pygraph(g0, g1):
g0_ = pygraph_to_igraph(g0)
return is_same_DAG(g0_, g1)
def ratio_same_DAG(G0, G1):
# how many G1 are in G0
res = 0
for g1 in tqdm(G1):
for g0 in G0:
if is_same_DAG(g1, g0):
res += 1
break
return res / len(G1)
def is_valid_DAG(g, subg=True):
# Check if the given igraph g is a valid DAG computation graph
# first need to have no directed cycles
# second need to have no zero-indegree nodes except input
# third need to have no zero-outdegree nodes except output
# i.e., ensure nodes are connected
# fourth need to have exactly one input node
# finally need to have exactly one output node
if subg:
START_TYPE=0
END_TYPE=1
else:
START_TYPE=8
END_TYPE=9
res = g.is_dag()
#return res
n_start, n_end = 0, 0
for v in g.vs:
if v['type'] == START_TYPE:
n_start += 1
elif v['type'] == END_TYPE:
n_end += 1
if v.outdegree() == 0 and v['type'] != END_TYPE:
return False
return res and n_start == 1 and n_end == 1
def is_valid_Circuit(g, subg=True):
# Check if the given igraph g is a amp circuits
# first checks whether the circuit topology is a DAG
# second checks the node type in the main path
if subg:
cond1 = is_valid_DAG(g, subg=True)
cond2 = True
for v in g.vs:
pos = v['pos']
subg_feats = [v['r'], v['c'], v['gm']]
if pos in [2,3,4]: # i.e. in the main path
if v['type'] in [8,9]:
cond2 = False
return cond1 and cond2
else:
cond1 = is_valid_DAG(g, subg=False)
cond2 = True
diameter_path = g.get_diameter(directed=True) #find the main path the diameter path must start/end at the sudo input/end node
if len(diameter_path) < 3:
cond2 = False
for i, v_ in enumerate(diameter_path):
v = g.vs[v_]
if i == 0:
if v['type'] != 8:
cond2 = False
elif i == len(diameter_path) - 1:
if v['type'] != 9:
cond2 = False
else:
#if v['type'] not in [1,2,3]: # main path nodes must come from subg_type = 6 or 7 or 10 or 11
if v['type'] in [4, 5]:
cond2 = False
predecessors_ = g.predecessors(i)
successors_ = g.successors(i)
for v_p in predecessors_:
v_p_succ = g.successors(v_p)
for v_cand in v_p_succ:
inster_set = set(g.successors(v_cand)) & set(successors_)
if g.vs[v_cand]['type'] in [0,1] and len(inster_set) > 0:
cond2 = True
return cond1 and cond2
def extract_latent_z(data, model, data_type='igraph', start_idx=0, infer_batch_size=64):
model.eval()
Z = []
g_batch = []
for i, g in enumerate(tqdm(data)):
if data_type== 'tensor':
g_ = g.to(device)
elif data_type== 'pygraph':
g_ = deepcopy(g)
else:
g_ = g.copy()
g_batch.append(g_)
if len(g_batch) == infer_batch_size or i == len(data) - 1:
g_batch = model._collate_fn(g_batch)
mu, _ = model.encode(g_batch)
mu = mu.cpu().detach().numpy()
Z.append(mu)
g_batch = []
return np.concatenate(Z, 0)
def prior_validity(train_data, model, infer_batch_size=64, data_type='igraph', subg=True, device=None, scale_to_train_range=False):
# data_type: igraph, pygraph
if scale_to_train_range:
Z_train = extract_latent_z(train_data, model, data_type, 0, infer_batch_size)
z_mean, z_std = Z_train.mean(0), Z_train.std(0)
z_mean, z_std = torch.FloatTensor(z_mean).to(device), torch.FloatTensor(z_std).to(device)
n_latent_points = 1000
decode_times = 10
valid_dags = 0
valid_ckts = 0
print('Prior validity experiment begins...')
G = []
G_valid = []
Ckt_valid = []
if data_type == 'igraph':
G_train = train_data
elif data_type == 'pygraph':
G_train = [pygraph_to_igraph(g) for g in train_data]
elif data_type == 'tensor':
G_train = [g.to(device) for g in G_train]
G_train = model._collate_fn(G_train)
G_train = model.construct_igraph(G_train[:, :, :model.nvt], G_train[:, :, model.nvt:], False)
else:
raise NotImplementedError()
pbar = tqdm(range(n_latent_points))
cnt = 0
for i in pbar:
cnt += 1
if cnt == infer_batch_size or i == n_latent_points - 1:
z = torch.randn(cnt, model.nz).to(model.get_device())
if scale_to_train_range:
z = z * z_std + z_mean # move to train's latent range
for j in range(decode_times):
g_batch = model.decode(z)
G.extend(g_batch)
for g in g_batch:
if is_valid_DAG(g, subg):
valid_dags += 1
G_valid.append(g)
if is_valid_Circuit(g, subg=subg):
valid_ckts += 1
Ckt_valid.append(g)
cnt = 0
r_valid_dag = valid_dags / (n_latent_points * decode_times)
print('Ratio of valid DAG decodings from the prior: {:.4f}'.format(r_valid_dag))
r_valid_ckt = valid_ckts / (n_latent_points * decode_times)
print('Ratio of valid Circuits decodings from the prior: {:.4f}'.format(r_valid_ckt))
r_novel = 1 - ratio_same_DAG(G_train, G_valid)
print('Ratio of novel graphs out of training data: {:.4f}'.format(r_novel))
return r_valid_dag, r_valid_ckt, r_novel
#### train test dataset loader
# load datasets
def train_test_generator_topo_simple(ng=10000, name='circuit_example'):
g_list = []
n_graph = ng
with open(name, 'r') as f:
for g_id in tqdm(range(n_graph)):
all_rows= []
row = f.readline().strip().split()
num_subg, num_node, stage = [int(w) for w in row]
# loading subg based graph information
g = igraph.Graph(directed=True)
g.add_vertices(num_subg)
for i in range(num_subg):
# ith row is the node with index i
row_ = f.readline().strip().split()
row = [float(w) for w in row_]
all_rows.append(row)
subg_type = int(row[0])
#i = int(row[1])
g.vs[i]['type'] = subg_type
num_edges = int(row[3])
vid = int(row[2])
g.vs['vid'] = vid
predecessors = [int(row[w]) for w in range(4, 4 + num_edges)]
if i != 0:
for j in predecessors:
g.add_edge(j, i)
if i == 0:
#subg_nod = row[4 + num_edges]
subg_nod_types = [8]
subg_nod_feats = [0.0]
subg_flat_adj = [1]
#elif i == 1:
# subg_nod_types = [9]
# subg_nod_feats = [0.0]
# subg_flat_adj = [1]
else:
#print(i)
if num_edges == 0:
subg_nod = int(row[5])
else:
subg_nod = int(row[4 + num_edges])
#subg_nod = int(row[4 + num_edges])
#print([row[w] for w in range(5 + num_edges, 5 + num_edges + subg_nod)])
subg_nod_types = [int(row[w]) for w in range(5 + num_edges, 5 + num_edges + subg_nod)]
subg_nod_feats = [row[w] for w in range(5 + num_edges + subg_nod, 5 + num_edges + 2 * subg_nod)]
subg_flat_adj = [int(row[w]) for w in range(5 + num_edges + 2 * subg_nod, 5 + num_edges + 2 * subg_nod + subg_nod * subg_nod)]
#print(subg_flat_adj)
g.vs[i]['subg_ntypes'] = subg_nod_types
g.vs[i]['subg_nfeats'] = subg_nod_feats
g.vs[i]['subg_adj'] = subg_flat_adj
# loading overall graph information
g_all = igraph.Graph(directed=True)
g_all.add_vertices(num_node)
for i in range(num_node):
row_ = f.readline().strip().split()
row = [float(w) for w in row_]
all_rows.append(row)
type_ = int(row[0])
vid_ = int(row[1])
feat_ = row[2]
g_all.vs[i]['type'] = type_
g_all.vs[i]['feat'] = feat_
g_all.vs[i]['vid'] = vid_
if len(row) > 3:
predecessors = [int(row[w]) for w in range(3, len(row))]
for j in predecessors:
g_all.add_edge(j,i)
subg_order = g.topological_sorting()
allg_order = g_all.topological_sorting()
subg_row_info = all_rows[:num_subg]
allg_row_info = all_rows[num_subg:]
g_sort = igraph.Graph(directed=True)
g_sort.add_vertices(num_subg)
dic_order = {i:j for i,j in zip(subg_order,range(num_subg))}
#print(dic_order)
for i, idx in enumerate(subg_order):
#print(row)
row = subg_row_info[idx]
subg_type = int(row[0])
g_sort.vs[i]['type'] = subg_type
vid = int(row[2])
g_sort.vs[i]['vid'] = vid
num_edges = int(row[3])
predecessors = [dic_order[int(row[w])] for w in range(4, 4 + num_edges)]
if i != 0:
for j in predecessors:
g_sort.add_edge(j, i)
if i == 0:
#subg_nod = row[4 + num_edges]
subg_nod_types = [8]
subg_nod_feats = [0.0]
subg_flat_adj = [1]
#elif i == 1:
# subg_nod_types = [9]
# subg_nod_feats = [0.0]
# subg_flat_adj = [1]
else:
#print(i)
if num_edges == 0:
subg_nod = int(row[5])
else:
subg_nod = int(row[4 + num_edges])
#subg_nod = int(row[4 + num_edges])
#print([row[w] for w in range(5 + num_edges, 5 + num_edges + subg_nod)])
subg_nod_types = [int(row[w]) for w in range(5 + num_edges, 5 + num_edges + subg_nod)]
subg_nod_feats = [row[w] for w in range(5 + num_edges + subg_nod, 5 + num_edges + 2 * subg_nod)]
subg_flat_adj = [int(row[w]) for w in range(5 + num_edges + 2 * subg_nod, 5 + num_edges + 2 * subg_nod + subg_nod * subg_nod)]
#print(subg_flat_adj)
g_sort.vs[i]['subg_ntypes'] = subg_nod_types
g_sort.vs[i]['subg_nfeats'] = subg_nod_feats
g_sort.vs[i]['subg_adj'] = subg_flat_adj
g_all_sort = igraph.Graph(directed=True)
g_all_sort.add_vertices(num_node)
dic_order = {i:j for i,j in zip(allg_order,range(num_node))}
for i, idx in enumerate(allg_order):
row = allg_row_info[idx]
type_ = int(row[0])
vid_ = int(row[1])
feat_ = row[2]
g_all_sort.vs[i]['type'] = type_
g_all_sort.vs[i]['feat'] = feat_
g_all_sort.vs[i]['vid'] = vid_
if len(row) > 3:
predecessors = [dic_order[int(row[w])] for w in range(3, len(row))]
for j in predecessors:
g_all_sort.add_edge(j,i)
g_sort = r_c_gm_extractor(g_sort, subg_indi = SUBG_INDI)
g_list.append((g_sort, g_all_sort))
return g_list[:int(ng*0.9)], g_list[int(ng*0.9):]
def train_test_generator_topo_order_dist(ng=10000, name='circuit_example'):
g_list = []
n_graph = ng
with open(name, 'r') as f:
for g_id in tqdm(range(n_graph)):
all_rows= []
row = f.readline().strip().split()
num_subg, num_node, stage = [int(w) for w in row]
# loading subg based graph information
g = igraph.Graph(directed=True)
g.add_vertices(num_subg)
for i in range(num_subg):
# ith row is the node with index i
row_ = f.readline().strip().split()
row = [int(w) for w in row_]
all_rows.append(row)
subg_type = int(row[0])
#i = int(row[1])
g.vs[i]['type'] = subg_type
num_edges = int(row[3])
predecessors = [int(row[w]) for w in range(4, 4 + num_edges)]
if i != 0:
for j in predecessors:
g.add_edge(j, i)
if i == 0:
#subg_nod = row[4 + num_edges]
subg_nod_types = [8]
subg_nod_feats = [0.0]
subg_flat_adj = [1]
#elif i == 1:
# subg_nod_types = [9]
# subg_nod_feats = [0.0]
# subg_flat_adj = [1]
else:
#print(i)
if num_edges == 0:
subg_nod = int(row[5])
else:
subg_nod = int(row[4 + num_edges])
#subg_nod = int(row[4 + num_edges])
#print([row[w] for w in range(5 + num_edges, 5 + num_edges + subg_nod)])
subg_nod_types = [int(row[w]) for w in range(5 + num_edges, 5 + num_edges + subg_nod)]
subg_nod_feats = [row[w] for w in range(5 + num_edges + subg_nod, 5 + num_edges + 2 * subg_nod)]
subg_flat_adj = [int(row[w]) for w in range(5 + num_edges + 2 * subg_nod, 5 + num_edges + 2 * subg_nod + subg_nod * subg_nod)]
#print(subg_flat_adj)
g.vs[i]['subg_ntypes'] = subg_nod_types
g.vs[i]['subg_nfeats'] = subg_nod_feats
g.vs[i]['subg_adj'] = subg_flat_adj
# loading overall graph information
g_all = igraph.Graph(directed=True)
g_all.add_vertices(num_node)
for i in range(num_node):
row_ = f.readline().strip().split()
row = [int(w) for w in row_]
all_rows.append(row)
type_ = int(row[0])
feat_ = row[1]
g_all.vs[i]['type'] = type_
g_all.vs[i]['feat'] = feat_
if len(row) > 2:
predecessors = [int(row[w]) for w in range(2, len(row))]
for j in predecessors:
g_all.add_edge(j,i)
subg_order = g.topological_sorting()
allg_order = g_all.topological_sorting()
subg_row_info = all_rows[:num_subg]
allg_row_info = all_rows[num_subg:]
g_sort = igraph.Graph(directed=True)
g_sort.add_vertices(num_subg)
dic_order = {i:j for i,j in zip(subg_order,range(num_subg))}
#print(dic_order)
for i, idx in enumerate(subg_order):
#print(row)
row = subg_row_info[idx]
subg_type = int(row[0])
g_sort.vs[i]['type'] = subg_type
num_edges = int(row[3])
predecessors = [dic_order[int(row[w])] for w in range(4, 4 + num_edges)]
if i != 0:
for j in predecessors:
g_sort.add_edge(j, i)
if i == 0:
#subg_nod = row[4 + num_edges]
subg_nod_types = [8]
subg_nod_feats = [0.0]
subg_flat_adj = [1]
#elif i == 1:
# subg_nod_types = [9]
# subg_nod_feats = [0.0]
# subg_flat_adj = [1]
else:
#print(i)
if num_edges == 0:
subg_nod = int(row[5])
else:
subg_nod = int(row[4 + num_edges])
#subg_nod = int(row[4 + num_edges])
#print([row[w] for w in range(5 + num_edges, 5 + num_edges + subg_nod)])
subg_nod_types = [int(row[w]) for w in range(5 + num_edges, 5 + num_edges + subg_nod)]
subg_nod_feats = [row[w] for w in range(5 + num_edges + subg_nod, 5 + num_edges + 2 * subg_nod)]
subg_flat_adj = [int(row[w]) for w in range(5 + num_edges + 2 * subg_nod, 5 + num_edges + 2 * subg_nod + subg_nod * subg_nod)]
#print(subg_flat_adj)
g_sort.vs[i]['subg_ntypes'] = subg_nod_types
g_sort.vs[i]['subg_nfeats'] = subg_nod_feats
g_sort.vs[i]['subg_adj'] = subg_flat_adj
g_all_sort = igraph.Graph(directed=True)
g_all_sort.add_vertices(num_node)
dic_order = {i:j for i,j in zip(allg_order,range(num_node))}
for i, idx in enumerate(allg_order):
row = allg_row_info[idx]
type_ = int(row[0])
feat_ = row[1]
g_all_sort.vs[i]['type'] = type_
g_all_sort.vs[i]['feat'] = feat_
if len(row) > 2:
predecessors = [dic_order[int(row[w])] for w in range(2, len(row))]
for j in predecessors:
g_all_sort.add_edge(j,i)
g_list.append((g_sort, g_all_sort))
return g_list[:int(ng*0.9)], g_list[int(ng*0.9):]