-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfer_progs.py
362 lines (269 loc) · 9.85 KB
/
infer_progs.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
import matplotlib.pyplot as plt
import json
import torch
import time
import math
from tqdm import tqdm
import utils
import joint_infer as ji
VERBOSE = False
FT_EVAL_LOG_INFO = [
('Obj', 'mval_best', 'count'),
('Start Obj', 'mval_start', 'count'),
('Error Rate', 'errors', 'count')
]
def os_inference(os_inf_net, args, vinput, ret_extra_info=False):
if args.infer_is_mode == 'beam':
try:
eval_info = os_inf_net.eval_infer_progs(
vinput,
beams = os_inf_net.beams,
)
assert len(eval_info['mval']) == 1, 'more than one'
except Exception as e:
if VERBOSE:
print(f"Failed os infer with {e}")
return None, None
elif args.infer_is_mode == 'sample':
try:
eval_info = os_inf_net.eval_infer_progs_sample(
vinput,
pop_size = args.infer_pop_size ,
rounds = args.infer_rounds,
rei=ret_extra_info
)
assert len(eval_info['mval']) == 1, 'more than one'
assert eval_info['exec'][0] is not None, 'none inference'
except Exception as e:
if VERBOSE:
print(f"Failed os infer with {e}")
return None, None
else:
assert False, f'bad os im {args.infer_is_mode}'
try:
res = {
'mval_best': eval_info['mval'][0],
'count': 1,
}
if ret_extra_info:
info = eval_info['extra']
else:
info = {
'mval': res['mval_best'],
'program': eval_info['info'][0]['expr'].split(),
'tar_exec': vinput[0],
'best_exec': eval_info['exec'][0],
'start_exec': eval_info['exec'][0]
}
except Exception as e:
if VERBOSE:
print(f"Failed os infer with {e}")
return None, None
return res, info
def run_inference(os_inf_net, edit_net, args, vinput, ret_extra_info=False):
if edit_net is None:
return os_inference(os_inf_net, args, vinput, ret_extra_info)
else:
return ji.joint_infer(edit_net, os_inf_net, {'vdata': vinput}, ret_extra_info)
class VisStruct:
def __init__(self, name, itn, args):
self.save_path_base = f'{args.outpath}/{args.exp_name}/vis/{name}'
self.itn = itn
self.num_to_save = args.num_write
self.num_per_render = 5
self.data = []
def add_vis_ex(self, tar, start, best):
if len(self.data) >= self.num_to_save:
return
self.data.append((tar, start, best))
def save_res(self, domain):
i = -1
while len(self.data) > 0:
i += 1
r1 = []
r2 = []
r3 = []
count = 0
while len(self.data) > 0 and count < self.num_per_render:
count += 1
tar, start, best = self.data.pop(0)
r1 += [tar]
r2 += [start]
r3 += [best]
domain.executor.render_group(
r1 + r2 + r3,
f'{self.save_path_base}_grp_{i}_itn_{self.itn}',
rows = 3
)
class EvalVisStruct:
def __init__(self):
self.data = []
self.error_count = 0
def add_res(self, eres, time):
if eres is None:
self.error_count += 1
else:
self.data.append((eres, time))
def get_and_save_info(self, domain):
args = domain.args
ex = domain.executor
R = {}
AT = []
for eres, time in self.data:
AT.append(time)
if 'rounds' not in R:
R['rounds'] = eres['rounds']
assert len(R['rounds']) == len(eres['rounds'])
for i, exc, prg in zip(eres['rounds'],eres['round_best_exec'], eres['round_best_prog']):
rec_mets = domain.pixel_recon_metrics(
exc,
eres['target']
)
rec_mets['prog_len'] = prg.shape[0]
for k,v in rec_mets.items():
if k not in R:
R[k] = {}
if i not in R[k]:
R[k][i] = []
R[k][i].append(v)
def avg(L):
return torch.tensor(L).float().mean().item()
srounds = R.pop('rounds')
srounds.sort()
AR = {'rounds': srounds}
for mn, V in R.items():
AR[mn] = []
for i in AR['rounds']:
ml = V[i]
AR[mn].append(avg(ml))
utils.log_print(
f"Inference time {round(avg(AT), 3)} | Errors : {self.error_count}", args
)
json.dump(AR, open(f'model_output/{args.exp_name}/eval_inf_res.json', 'w'))
rounds = AR.pop('rounds')
for mn, V in AR.items():
best_val = round(max(V), 3)
start_val = round(V[0], 3)
imp_amt = round(best_val - start_val, 3)
utils.log_print(
f" {mn} : {best_val} | Start : {start_val} | Imp {imp_amt}", args
)
out_name = f'model_output/{args.exp_name}/plots/{mn}_over_rounds.png'
plt.clf()
plt.plot(rounds, V)
plt.grid()
plt.savefig(out_name)
plt.close('all')
def save_results(self, domain):
with torch.no_grad():
self.get_and_save_info(domain)
RNDS = [-1, 0, 1, 3, 7, 15, 31, 63]
num_write = domain.args.num_write
for c, (eres, _) in enumerate(self.data[:num_write]):
row_info = {}
for i, exc in zip(eres['rounds'],eres['round_best_exec']):
if i in RNDS:
row_info[i] = exc
if 'max' in RNDS:
row_info['max'] = exc
row_execs = [row_info[i] for i in RNDS if i in row_info] + [eres['target']]
domain.executor.render_group(
row_execs,
f'model_output/{domain.args.exp_name}/vis/inf_eval_{c}',
rows = 1
)
def infer_programs(domain, os_inf_net, edit_net, data, train_pbest, val_pbest):
args = domain.args
path = args.infer_path
os_inf_net.eval()
if edit_net is not None:
edit_net.eval()
else:
os_inf_net.beams = args.os_inf_beams
results = {}
ITER_DATA = [
(data.train_eval_iter, train_pbest, data.get_set_size('train'), 'train'),
(data.val_eval_iter, val_pbest, data.get_set_size('val'), 'val'),
]
for gen, record, num, name in ITER_DATA:
VS = VisStruct(name, os_inf_net.iter_num, args)
eval_res = {
'errors': 0.,
'count': 0.
}
utils.log_print(f"Inferring for {name}", args)
assert args.eval_batch_size == 1
for batch in \
tqdm(gen(), total = math.ceil(num / args.eval_batch_size)):
# Inference network runs beam search on each entry in vinput, and returns the beam with highest metric against the entry
key = batch['bkey']
vinput = batch['vdata']
eres, einfo = run_inference(
os_inf_net,
edit_net,
args,
vinput,
)
if eres is None:
assert einfo is None
eval_res['errors'] += 1
continue
for k,v in eres.items():
if k not in eval_res:
eval_res[k] = 0.
eval_res[k] += v
if record is not None:
record.update(
key,
einfo['program'],
einfo['mval']
)
VS.add_vis_ex(
einfo['tar_exec'],
einfo['start_exec'],
einfo['best_exec'],
)
results[name] = utils.print_results(
FT_EVAL_LOG_INFO,
eval_res,
args,
ret_early=True
)
utils.log_print(f'Eval res {name}:', args)
for k,v in results[name].items():
rv = round(v,3)
utils.log_print(f" {k}: {rv}", args)
VS.save_res(domain)
return results
def infer_for_eval(domain, os_inf_net, edit_net, data):
args = domain.args
path = args.infer_path
os_inf_net.eval()
if edit_net is not None:
edit_net.eval()
else:
os_inf_net.beams = args.os_inf_beams
results = {}
ITER_DATA = [
(data.test_eval_iter, None, data.get_set_size('test'), 'test')
]
count = -1
for gen, record, num, name in ITER_DATA:
EVS = EvalVisStruct()
assert args.eval_batch_size == 1
for batch in \
tqdm(gen(), total = math.ceil(num / args.eval_batch_size)):
# Inference network runs beam search on each entry in vinput, and returns the beam with highest metric against the entry
count += 1
key = batch['bkey']
vinput = batch['vdata']
t = time.time()
eres = ji.test_time_infer(
edit_net,
os_inf_net,
args,
{'vdata': vinput},
key
)
EVS.add_res(eres, time.time()-t)
EVS.save_results(domain)