-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_sub_aug.py
More file actions
149 lines (138 loc) · 5.41 KB
/
03_sub_aug.py
File metadata and controls
149 lines (138 loc) · 5.41 KB
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
import os
import numpy as np
import random
import glob
import shutil
import utils.aiger_utils as aiger_utils
import utils.circuit_utils as circuit_utils
from utils.utils import hash_arr, run_command
import argparse
import time
gate_to_index = {'PI': 0, 'AND': 1, 'NOT': 2}
aig_dir = './data/raw_aig'
abccrc_path = '/Users/zhengyuanshi/opt/abc/abc.rc'
save_aig_path = './data/sub_aug_aig'
save_bench_path = './data/sub_bench'
NO_TRANS = 10
def parse_aig_head(aig_path):
f = open(aig_path, 'r', encoding='ISO-8859-1')
lines = f.readlines()
f.close()
for line in lines:
header = line.replace('\n', '').split(' ')
if header[0] == 'aig' or header[0] == 'aag':
if len(header) == 7:
# “M”, “I”, “L”, “O”, “A” separated by spaces.
n_variables = eval(header[1])
n_inputs = eval(header[2])
n_latch = eval(header[3])
unknown = eval(header[4])
n_and = eval(header[5])
n_outputs = eval(header[6])
elif len(header) == 6:
n_variables = eval(header[1])
n_inputs = eval(header[2])
n_outputs = eval(header[4])
n_and = eval(header[5])
n_latch = eval(header[3])
else:
n_variables, n_inputs, n_latch, n_outputs, n_and = 0, 0, 0, 0, 0
return n_variables, n_inputs, n_latch, n_outputs, n_and
return 0, 0, 0, 0, 0
def get_syn_command(no_syn):
res = ''
for _ in range(no_syn):
action = random.randint(0, 6)
if action == 0:
res += 'balance'
if random.randint(0, 1) == 0:
res += ' -l'
if random.randint(0, 1) == 0:
res += ' -d'
if random.randint(0, 1) == 0:
res += ' -s'
if random.randint(0, 1) == 0:
res += ' -x'
res += ';'
elif action == 1:
res += 'rewrite'
if random.randint(0, 1) == 0:
res += ' -l'
if random.randint(0, 1) == 0:
res += ' -z'
res += ';'
elif action == 2:
res += 'resub'
if random.randint(0, 1) == 0:
res += ' -l'
if random.randint(0, 1) == 0:
res += ' -z'
attr_K = random.randint(4, 16)
res += ' -K {}'.format(attr_K)
attr_N = random.randint(0, 3)
res += ' -N {}'.format(attr_N)
res += ';'
elif action == 3:
res += 'refactor'
if random.randint(0, 1) == 0:
res += ' -l'
if random.randint(0, 1) == 0:
res += ' -z'
attr_N = random.randint(4, 15)
res += ' -N {}'.format(attr_N)
res += ';'
elif action == 4:
res += 'retime; strash; '
elif action == 5:
res += 'lcorr; '
elif action == 6:
res += 'scleanup; '
return res
if __name__ == '__main__':
# Convert to AIG
no_sub_circuits = 0
if not os.path.exists(save_aig_path):
os.mkdir(save_aig_path)
tot_time = 0
tot_sub_ckt = 0
bench_list = glob.glob(os.path.join(save_bench_path, '*/*.bench'))
for bench_idx, bench_path in enumerate(bench_list):
start_time = time.time()
no_sub_circuits += 1
arr = bench_path.replace('.bench', '').split('/')
design_name = arr[-2]
module_name = arr[-1]
output_dir = os.path.join(save_aig_path, design_name)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
print('Parse: {} ({:} / {:}), Time: {:.2f}s, ETA: {:.2f}s, Tot: {:}'.format(
module_name, bench_idx, len(bench_list),
tot_time, tot_time / ((bench_idx + 1) / len(bench_list)) - tot_time,
tot_sub_ckt
))
output_aig_path = os.path.join(output_dir, '{}_0.aig').format(module_name)
abc_cmd = 'abc -c "read_bench {}; source {}; st; resyn2; write_aiger {}"'.format(bench_path, abccrc_path, output_aig_path)
stdout, _ = run_command(abc_cmd)
if not os.path.exists(output_aig_path):
continue
ori_n_variables, ori_n_inputs, ori_n_latch, ori_n_outputs, ori_n_and = parse_aig_head(output_aig_path)
if ori_n_variables == 0:
continue
for trans_time in range(1, NO_TRANS+1):
tmp_aig_path = './tmp/{}_{}.aig'.format(design_name, module_name)
abc_cmd = 'abc -c "read_bench {}; source {}; st; {}; write_aiger {}"'.format(
bench_path, abccrc_path, get_syn_command(random.randint(2, 5)), tmp_aig_path
)
stdout, _ = run_command(abc_cmd)
if not os.path.exists(tmp_aig_path):
continue
n_variables, n_inputs, n_latch, n_outputs, n_and = parse_aig_head(tmp_aig_path)
diff = abs(n_variables - ori_n_variables) + abs(n_and - ori_n_and)
if diff < 10:
os.remove(tmp_aig_path)
continue
output_aig_path = os.path.join(output_dir, '{}_{}.aig').format(module_name, trans_time)
shutil.move(tmp_aig_path, output_aig_path)
tot_sub_ckt += 1
tot_time += time.time() - start_time
print('[INFO] Total number of subcircuits: {}'.format(tot_sub_ckt))