-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_testing_pytorch_output_softmax.py
594 lines (448 loc) · 20.2 KB
/
2_testing_pytorch_output_softmax.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import sys
import glob
import math
import re, random
from Ntk_Struct_PO_cmu import *
from Ntk_Parser_PO_cmu import *
from fflatch_only_graph_PO import *
import numpy as np
import networkx as nx
import collections
import h5py
from sklearn.model_selection import train_test_split
from numpy.random import seed
import h5py
from sklearn.preprocessing import MinMaxScaler
from numpy.random import seed
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
from torch.utils.data.sampler import SubsetRandomSampler
class MLP(nn.Module):
def __init__(self, input_dim, n_class):
super(MLP, self).__init__()
self.n_class=n_class
self.fc1=nn.Linear(input_dim, 100)
self.fc3 = nn.Linear(100, n_class)
def forward(self, x):
x=self.fc1(x)
x = F.relu(x)
x = self.fc3(x)
return x
def customgraph2networkx(netlist_graph):
G = nx.DiGraph()
for node in netlist_graph.object_list:
if node.name not in G.nodes:
G.add_node(node.name, type=netlist_graph.gateType_reverse[node.gate_type])
else: # already added by edge
G.nodes[node.name]['type'] = netlist_graph.gateType_reverse[node.gate_type]
for innode in node.fan_in_node:
G.add_edge(innode.name,node.name)
for outnode in node.fan_out_node:
G.add_edge(node.name,outnode.name)
return G
def feature_normalization(cur_data_X):
# fit scaler on training data
norm = MinMaxScaler().fit(cur_data_X)
# transform training data
cur_data_X = norm.transform(cur_data_X)
return cur_data_X
def add_q2latchname(benchname, filepath, q2latchname):
fi=open(filepath+'/'+benchname+'_latchname2Q_remove_LD','r')
for line in fi:
q, latchname=line.rstrip().split(':')
#print (q, latchname)
q2latchname[q]=latchname
fi.close()
def add_delay(node, benchname, report_dir, q2latchname, seq_sig):
latchname=q2latchname[node][:-7]
PIflag=False # this latch connects to input
f1=open(report_dir+latchname+'.from','r')
f2 = open(report_dir + latchname + '.to', 'r')
for line in f1:
if 'Required Time' in line:
#print (line.rstrip().split(' '))
fromreqtime=int(line.rstrip().split(' ')[-1])
if 'Launch Clock' in line:
#print (line.rstrip().split(' '))
fromlclk=int(line.rstrip().split(' ')[-1])
if 'Data Path' in line:
#print (line.rstrip().split(' '))
fromdelay=int(line.rstrip().split(' ')[-1])
for line in f2:
if 'Startpoint' in line:
if seq_sig not in line:
#print (node, line)
PIflag=True
if 'Required Time' in line:
#print (line.rstrip().split(' '))
toreqtime=int(line.rstrip().split(' ')[-1])
if 'Launch Clock' in line:
#print (line.rstrip().split(' '))
tolclk=int(line.rstrip().split(' ')[-1])
if 'Data Path' in line:
todelay=int(line.rstrip().split(' ')[-1])
f1.close()
f2.close()
if PIflag:
todelay=0
return fromdelay, fromreqtime-fromlclk, todelay, toreqtime-tolclk
def construct_dataset(curfile, benchpath, data_X, data_Y):
num_train=0
for idx, file in enumerate(glob.glob(benchpath + '/*')):
if file!=curfile:
num_train+=1
with h5py.File(file, 'r') as hf:
X = hf['X_train'][:]
Y = hf['Y_train'][:]
data_X = np.vstack((data_X, X))
data_Y = np.vstack((data_Y, Y))
return data_X, data_Y
def get_accuracy(preds, Ys):
max_preds = preds.argmax(dim=1, keepdim=True)
numcorrect=max_preds.squeeze(1).eq(Ys)
return numcorrect.sum()/torch.FloatTensor([Ys.shape[0]])
def train(model, train_loader, optimizer, criterion):
model.train()
epoch_loss = 0
epoch_acc = 0
for Xs, Ys in train_loader:
#print (Xs.shape, Ys.shape)
optimizer.zero_grad()
preds=model(Xs.float())
preds = preds.view(-1, preds.shape[-1])
Ys=Ys.view(-1)
loss = criterion(preds, Ys)
acc=get_accuracy(preds, Ys)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(train_loader), epoch_acc / len(train_loader)
def evaluate(model, validation_loader, criterion, all_results, benchname, num_classes):
global misclassification
epoch_loss = 0
epoch_acc = 0
allsoftmax_probs=[]
probs = nn.Softmax(dim=1)
confusion_matrix = [[0] * num_classes for i in range(num_classes)]
#dicres = {"LATCH_L0": 0, "LATCH_L1": 0, "LATCH_DD": 1}
model.eval()
with torch.no_grad():
for Xs, Ys in validation_loader:
preds = model(Xs.float())
preds = preds.view(-1, preds.shape[-1])
predsoftmax = probs(preds)
allsoftmax_probs.append(np.array(predsoftmax[0]))
Ys = Ys.view(-1)
preds_arg = np.argmax(preds, axis=1)
confusion_matrix[Ys[0]][preds_arg[0]] += 1
loss = criterion(preds, Ys)
acc = get_accuracy(preds, Ys)
epoch_loss += loss.item()
epoch_acc += acc.item()
print ("correct count:", epoch_acc, len(validation_loader))
np_cm = np.array(confusion_matrix)
print ('confusion matrix.')
print (np_cm)
all_results[benchname]=all_results.get(benchname, [])+[epoch_acc, len(validation_loader)]
misclassification+=len(validation_loader)-epoch_acc
return epoch_loss / len(validation_loader), epoch_acc / len(validation_loader), np.array(allsoftmax_probs)
def add_dataset(benchname, seq_sig, q2latchname,report_dir, DB, DF, G_fflatch_only, nxG, data_X, comb_data_X, data_Y, test_nodes):
for node in G_fflatch_only:
if not (G_fflatch_only.nodes[node]['type'] == 'LATCH_L0' or G_fflatch_only.nodes[node]['type'] == 'LATCH_L1' or G_fflatch_only.nodes[node]['type'] == 'LATCH_LD' or G_fflatch_only.nodes[node]['type'] == 'LATCH_DD'):
continue
tot_num_FI = len(G_fflatch_only.in_edges(node))
tot_num_FO = len(G_fflatch_only.out_edges(node))
visited = set() # add current node
visited.add(node)
# search backward
bdepthidx = 0
backl = []
backq = collections.deque([node])
while bdepthidx < DB:
unseen_fanin_latch = 0
seen_fanin_latch = 0
unseen_fanin_ff=0
seen_fanin_ff=0
unseen_PIs = 0
seen_PIs=0
#cur_visited = set()
for _ in range(len(backq)):
cur=backq.popleft()
#tot_fanin+=len(G_fflatch_only.in_edges(cur))
for inedge in G_fflatch_only.in_edges(cur):
innode=inedge[0]
#print (innode, visited)
if innode not in visited:
#if innode not in cur_visited:
if G_fflatch_only.nodes[innode]['type'] == 'IPT':
unseen_PIs += 1
elif G_fflatch_only.nodes[innode]['type'] == 'DFF': # FF
unseen_fanin_ff+=1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_L0':
#unseen_fanin_latch0+=1
unseen_fanin_latch+=1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_L1':
#unseen_fanin_latch1+=1
unseen_fanin_latch += 1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_LD':
#unseen_fanin_latchld+=1
unseen_fanin_latch += 1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_DD':
#unseen_fanin_latchdd+=1
unseen_fanin_latch += 1
backq.append(innode)
#cur_visited.add(innode)
visited.add(innode)
else: # seen this node before
#print ("seen", bdepthidx)
if G_fflatch_only.nodes[innode]['type'] == 'IPT':
seen_PIs += 1
elif G_fflatch_only.nodes[innode]['type'] == 'DFF': # FF
seen_fanin_ff+=1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_L0':
#seen_fanin_latch0+=1
seen_fanin_latch+=1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_L1':
#seen_fanin_latch1+=1
seen_fanin_latch += 1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_LD':
#seen_fanin_latchld+=1
seen_fanin_latch += 1
elif G_fflatch_only.nodes[innode]['type'] == 'LATCH_DD':
#seen_fanin_latchdd+=1
seen_fanin_latch += 1
backq.append(innode)
backl.append(unseen_PIs)
backl.append(unseen_fanin_ff)
backl.append(unseen_fanin_latch)
bdepthidx+=1
# search forward
visited = set() # add current node
visited.add(node)
fdepthidx = 0
forwl = []
forwq = collections.deque([node])
while fdepthidx<DF:
unseen_fanout_latch = 0
seen_fanout_latch = 0
unseen_fanout_ff=0
seen_fanout_ff=0
unseen_POs = 0
seen_POs=0
for _ in range(len(forwq)):
cur=forwq.popleft()
for outedge in G_fflatch_only.out_edges(cur):
outnode=outedge[1]
if outnode not in visited:
#if '_PO' in outnode:
if G_fflatch_only.nodes[outnode]['type'] == 'PO':
unseen_POs += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'DFF': # FF
unseen_fanout_ff+=1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_L0':
#unseen_fanout_latch0+=1
unseen_fanout_latch+=1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_L1':
#unseen_fanout_latch1+=1
unseen_fanout_latch += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_LD':
#unseen_fanout_latchld+=1
unseen_fanout_latch += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_DD':
#unseen_fanout_latchdd+=1
unseen_fanout_latch += 1
forwq.append(outnode)
#if not count_visited_FF:
visited.add(outnode)
else:
if G_fflatch_only.nodes[outnode]['type'] == 'PO':
seen_POs += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'DFF': # FF
seen_fanout_ff+=1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_L0':
#seen_fanout_latch0+=1
seen_fanout_latch+=1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_L1':
#seen_fanout_latch1+=1
seen_fanout_latch += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_LD':
#seen_fanout_latchld+=1
seen_fanout_latch += 1
elif G_fflatch_only.nodes[outnode]['type'] == 'LATCH_DD':
#seen_fanout_latchdd+=1
seen_fanout_latch += 1
forwq.append(outnode)
forwl.append(unseen_fanout_ff)
forwl.append(unseen_fanout_latch)
fdepthidx+=1
curFO=set()
childFO=set()
for outedge in G_fflatch_only.out_edges(node):
outnode=outedge[1]
curFO.add(outnode)
for choutedge in G_fflatch_only.out_edges(outnode):
choutnode=choutedge[1]
childFO.add(choutnode)
# compute the fraction
fraction=0
Sfraction=0
#Tfraction=0
curFI = set()
parentFI = set()
for inedge in G_fflatch_only.in_edges(node):
innode = inedge[0]
curFI.add(innode)
parentFO=set([outedge[1] for outedge in G_fflatch_only.out_edges(innode)])
if len(curFO & parentFO)>0:
fraction+=1
if len(childFO & parentFO)>0:
Sfraction+=1
for pinedge in G_fflatch_only.in_edges(innode):
pinnode=pinedge[0]
parentFI.add(pinnode)
Sfraction2=0 # detect 2nd DD in trapezoid shape
#Tfraction3=0
for outnode in curFO:
childFI = set([inedge[0] for inedge in G_fflatch_only.in_edges(outnode)])
if len(childFI & parentFI)>0:
Sfraction2+=1
latchloop=1 if len(childFO & curFI)>0 else 0
backl.reverse()
if tot_num_FI==0:
#print (fraction, Sfraction)
tot_num_FI=1
vector = backl + forwl + [fraction / tot_num_FI, Sfraction/tot_num_FI, Sfraction2/tot_num_FO,latchloop]
#print (node, vector)
# single path feature
if len(G_fflatch_only.out_edges(node))==1 or len(G_fflatch_only.in_edges(node))==1:
vector.append(1)
else:
vector.append(0)
#print (node)
if '_PO' in node:
nodename=node[:-3]
#print (nodename)
else:
nodename=node
fromdelay, fromdenominator, todelay, todenominator=add_delay(nodename, benchname, report_dir, q2latchname, seq_sig)
vector.append(fromdelay)
vector.append(todelay)
# 13 self-loop feature
all_fanout_self_loop=[]
for outedge in G_fflatch_only.out_edges(node):
outnode=outedge[1]
child_selfloop=None
child_num_fanin=len(G_fflatch_only.in_edges(outnode))
for choutedge in G_fflatch_only.out_edges(outnode):
choutnode = choutedge[1]
if choutnode==outnode:
child_selfloop=True
if child_selfloop:
#print (G_fflatch_only.nodes[node]['type'], outnode, G_fflatch_only.out_edges(outnode))
all_fanout_self_loop.append(round(1/child_num_fanin, 2))
if all_fanout_self_loop:
vector.append(max(all_fanout_self_loop))
#print (G_fflatch_only.nodes[node]['type'], max(all_fanout_self_loop))
else: # none fanout has self-loop
vector.append(0)
#print (vector)
data_X.append(vector)
test_nodes.append(node)
# add labels according to the latch's type
if G_fflatch_only.nodes[node]['type'] == 'LATCH_L0':
data_Y.append(np.array([1, 0]))
elif G_fflatch_only.nodes[node]['type'] == 'LATCH_L1':
data_Y.append(np.array([1, 0]))
elif G_fflatch_only.nodes[node]['type'] == 'LATCH_DD':
data_Y.append(np.array([0, 1]))
elif G_fflatch_only.nodes[node]['type'] == 'LATCH_LD':
data_Y.append(np.array([0, 1]))
def generate_dataset(specific_bench,benchpath,DB, DF, seq_sig):
all_results={}
num_classes = 2
global misclassification
misclassification = 0
for idx, filepath in enumerate(glob.glob(benchpath+'/*')):
seed(12)
torch.manual_seed(12)
torch.cuda.manual_seed(12)
torch.cuda.manual_seed_all(12)
np.random.seed(12)
random.seed(12)
# torch.cuda.is_available() checks and returns a Boolean True if a GPU is available, else it'll return False
is_cuda = torch.cuda.is_available()
# If we have a GPU available, we'll set our device to GPU. We'll use this device variable later in our code.
if is_cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
print (is_cuda)
#print (file)
x = re.findall(r"^\.\/dataset_seed1\\([A-Za-z0-9\_]+)$", filepath)
#print (x)
benchname=x[0].split('_')[0]
input_dim=DF*5+5+2+1
n_class=2
comb_data_X = []
data_X = []
data_Y = []
model = MLP(input_dim, n_class)
criterion = nn.CrossEntropyLoss()
model.load_state_dict(torch.load(f"./best_models/8datasets_cweights_simple_best_MLP_model_pytorch_{n_class}.pt"))
if specific_bench in filepath and '_2_' not in filepath and '_3_' not in filepath:
print (filepath)
# testing
q2latchname={}
add_q2latchname(benchname, filepath, q2latchname)
file=filepath+f'\{benchname}_clean_remove_LD.bench'
report_dir=filepath+f'/{benchname}_time_reports_remove_LD/'
print (file)
netlist_graph = ntk_parser(file)
nxG = customgraph2networkx(netlist_graph)
G_fflatch_only = fflatch_graph(nxG)
# remove clk and reset node
all_nodes = list(G_fflatch_only.nodes())
for node in all_nodes:
if node == 'rst':
G_fflatch_only.remove_node(node)
if node == 'reset':
G_fflatch_only.remove_node(node)
if node == 'clk':
G_fflatch_only.remove_node(node)
test_nodes = []
add_dataset(benchname, seq_sig, q2latchname, report_dir, DB, DF, G_fflatch_only, nxG, data_X, comb_data_X, data_Y, test_nodes)
data_X = np.array(data_X)
data_Y = np.array(data_Y)
# normalization
data_X = feature_normalization(data_X)
Y_test = np.argmax(data_Y, axis=1)
test_batch_size=1
test_data = torch.utils.data.TensorDataset(torch.from_numpy(data_X), torch.from_numpy(Y_test))
test_loader = torch.utils.data.DataLoader(test_data, shuffle=False, batch_size=test_batch_size)
test_loss, test_acc, allsoftmax_probs = evaluate(model, test_loader, criterion, all_results, x[0], n_class)
#results = model.evaluate(X_test, Y_test, batch_size=32)
print(f'test Loss:{test_loss :.4f} | test Acc: {test_acc :.4f}%')
all_results[x[0]]=all_results.get(x[0], [])+[test_acc]
fbo=open(f"./2_all_softmaxprobs/{benchname}_softmaxprobs_remove_LD", "w")
for idx, node in enumerate(test_nodes):
fbo.write(f'{node}: {allsoftmax_probs[idx][0]:.4f} {allsoftmax_probs[idx][1]:.4f} {G_fflatch_only.nodes[node]["type"]}\n')
fbo.close()
print (all_results)
#return data_X, data_Y
f = open(f"8datasets_cweights_simple_best_MLP_model_pytorch_{n_class}_softmax.txt", "w")
for key, val in all_results.items():
f.write(f'{key}:{val}\n')
f.write(str(misclassification))
f.close()
seed(1)
benchpath="./dataset_seed1"
all_bench='s'
seq_sig='reg' # signature of sequential logic, only re-elaborate can generate this signature
DB = 1 # depth for backward (towards inputs)
DF = 1 # depth for forward (towards outputs)
generate_dataset(all_bench, benchpath, DB, DF, seq_sig)