-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_pretrain.py
477 lines (346 loc) · 13 KB
/
edit_pretrain.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
import sys, os, torch, json, time, random, ast, utils, argparse
import numpy as np
import matplotlib.pyplot as plt
from utils import device
from copy import deepcopy, copy
from tqdm import tqdm
import train_utils as tru
from edit_models import EditNet
VERBOSE = False
def make_prog_pairs(domain, set_name, num_pairs):
ex = domain.executor
args = domain.args
batch_size = args.batch_size
data = []
pbar = tqdm(total=num_pairs)
while len(data) < num_pairs:
synth_progs, synth_vdata = ex.det_prog_random_sample(batch_size, use_pbar=False, ret_data=True)
inp_vdata = torch.stack(synth_vdata,dim=0)
samples = domain.os_net.eval_batch_sample_prog(inp_vdata)
for i, tar_tokens in enumerate(synth_progs):
if i not in samples or samples[i] is None:
continue
try:
if 'END' not in samples[i][-1]:
assert domain.name == 'layout'
corr_tokens = samples[i] + ['END']
else:
corr_tokens = samples[i]
PNF, EI, edit_ops = domain.prog_diff.get_edit_info(corr_tokens, tar_tokens)
except Exception as e:
print(f"Failed get info with {e}")
continue
data.append((
corr_tokens,
tar_tokens,
(PNF, EI, edit_ops),
'edit'
))
pbar.update(1)
return data
def convert_to_eval_data(
domain, eval_prog_pairs
):
eval_data = []
ex = domain.executor
print("Loading eval data")
with torch.no_grad():
for start_tokens, tar_tokens, _, etype in tqdm(eval_prog_pairs):
tar_vdata = ex.execute(' '.join(tar_tokens))
eval_data.append({
'start_tokens': start_tokens,
'tar_vdata': tar_vdata,
'edit_type': etype,
})
return eval_data
def convert_all_prog_pairs_to_data(domain, prog_pairs):
pair_data = []
ex = domain.executor
print("Loading all pair data")
start_time = time.time()
errors = 0
num_pairs = 0
for start_tokens, tar_tokens, (PNF, EI, edit_ops), etype in tqdm(prog_pairs):
try:
conv_data = domain.prog_diff.conv_to_data(
'super_ap',
start_tokens, tar_tokens, PNF, EI, edit_ops
)
except Exception as e:
errors += 1
continue
pair_data.append({
'start_tokens': start_tokens,
'tar_tokens': tar_tokens,
'conv_data': conv_data,
'edit_type': etype,
})
num_pairs += len(conv_data)
utils.log_print(f"Sampled {num_pairs} data pairs in {round(time.time() - start_time)} seconds with {errors} errors", domain.args)
return pair_data
class SynthDataset:
def __init__(
self, domain, set_name,
):
args = domain.args
self.os_net = domain.os_net
self.mode = 'train'
self.args = domain.args
self.domain = domain
self.ex = domain.executor
self.device= domain.device
self.set_name = set_name
self.batch_size = args.batch_size
self.eval_batch_size = args.eval_batch_size
self.iter_num = 0
self.inds = []
self.data = []
self.eval_data = []
with torch.no_grad():
self.make_data_pairs()
self.size = len(self.data)
self.eval_size = len(self.eval_data)
print(f"Set {set_name} : Train {self.size} | Eval {self.eval_size}")
def make_data_pairs(self):
assert len(self.data) == 0
cache_path = self.args.synth_pair_cache_path
print(f"Loading cache from {cache_path}")
args = self.args
CACHE = torch.load(cache_path)
if self.set_name == 'train':
size = args.train_size
elif self.set_name == 'val':
size = args.eval_size
else:
assert False
prog_pairs = CACHE[f'{self.set_name}_prog_pairs']
self.data_conv_mode = args.data_conv_mode
assert len(self.eval_data) == 0
self.eval_data = convert_to_eval_data(
self.domain, prog_pairs[:args.eval_size]
)
self.pair_data = convert_all_prog_pairs_to_data(
self.domain, prog_pairs[:size]
)
self.pair_data_q = []
with torch.no_grad():
self.load_next_pair_data()
self.inds = list(range(len(self.data)))
random.shuffle(self.inds)
def load_next_pair_data(self):
print("Loading next pair data in all pair mode")
self.data = []
ex = self.domain.executor
pred_mode = self.domain.args.pred_mode
if len(self.pair_data_q) == 0:
self.pair_data_q = torch.randperm(len(self.pair_data)).tolist()
errors = 0
count = 0
pbar = tqdm(total = min(len(self.pair_data_q), 10000))
while len(self.pair_data_q) > 0 and count <= 10000:
count += 1
pdata = self.pair_data[self.pair_data_q.pop(0)]
tar_tokens = pdata['tar_tokens']
try:
tar_vdata = ex.execute(' '.join(tar_tokens)).cpu()
except Exception as e:
if VERBOSE:
print(f"unexpectedly couldn't execute tar with {e}")
errors += 1
continue
for pcd in pdata['conv_data']:
corr_tokens = pcd['corr_tokens']
try:
corr_vdata = ex.execute(' '.join(corr_tokens)).cpu()
except Exception as e:
if VERBOSE:
print(f"unexpectedly couldn't execute corr with {e}")
errors += 1
continue
assert pred_mode == 'edit'
eoi = random.choice(pcd['eoi'])
eot, eol, eos = eoi
self.data.append({
'tar_vdata': tar_vdata,
'corr_tokens': corr_tokens,
'corr_vdata': corr_vdata,
'edit_ps_info': eoi,
'edit_tl_info': (eot, eol)
})
pbar.update(1)
pbar.close()
print(f"Added {len(self.data)} pairs to data with {errors} errors" )
def __iter__(self):
if self.mode == 'train':
yield from self.train_static_iter()
elif self.mode == 'eval':
yield from self.eval_iter()
else:
assert False, f'bad mode {self.mode}'
def train_static_iter(self):
if len(self.inds) == 0:
if self.set_name == 'train':
with torch.no_grad():
self.load_next_pair_data()
self.inds = list(range(len(self.data)))
random.shuffle(self.inds)
while len(self.inds) > 0:
binds = self.inds[:self.batch_size]
self.inds = self.inds[self.batch_size:]
bdata = [self.data[bi] for bi in binds]
with torch.no_grad():
batch = self.ex.make_batch(bdata, self.args)
g_batch = {
k: v.to(self.device) for k,v in
batch.items()
}
yield g_batch
def eval_iter(self):
inds = torch.arange(len(self.data[:self.eval_size]))
assert self.args.eval_batch_size == 1
for start in range(
0, inds.shape[0], self.args.eval_batch_size
):
bind = inds[start]
bdata = self.eval_data[bind]
args = self.args
if start < self.num_write:
name = f'{args.outpath}/{args.exp_name}/vis/{self.set_name}_shape_{bind}_itn_{self.iter_num}_{bdata["edit_type"]}'
else:
name = None
g_batch = {
'vdata': bdata['tar_vdata'].to(self.device),
'name': name,
'os_net': self.os_net
}
yield g_batch
def get_synth_datasets(domain):
train_loader = SynthDataset(
domain,
'train',
)
val_loader = SynthDataset(
domain,
'val',
)
eval_size = min(
[
v for v in
(domain.args.eval_size, train_loader.eval_size, val_loader.eval_size)
if v is not None
]
)
train_loader.eval_size = eval_size
val_loader.eval_size = eval_size
train_loader.num_write = min(eval_size-1, domain.args.num_write)
val_loader.num_write = min(eval_size-1, domain.args.num_write)
return train_loader, val_loader
def get_edit_net(
domain, model_path=None
):
net = EditNet(domain)
net.acc_count = 0
net.acc_period = domain.args.acc_period
net.log_period = domain.args.log_period
if model_path is not None:
print(f"Loading from {model_path}")
net.load_state_dict(
torch.load(model_path)
)
net.to(device)
return net
def pretrain(domain):
args = domain.get_pt_args()
domain.load_prog_diff()
domain.load_oneshot_net()
cache_path = args.synth_pair_cache_path
assert cache_path is not None
if cache_path.split('/')[-1] not in os.listdir('/'.join(cache_path.split('/')[:-1])):
utils.log_print(f"Cache does not exist. Saving cache at {cache_path}", args)
with torch.no_grad():
train_prog_pairs = make_prog_pairs(domain, 'train', args.train_size)
val_prog_pairs = make_prog_pairs(domain, 'val', args.eval_size)
torch.save({
'train_prog_pairs': train_prog_pairs,
'val_prog_pairs': val_prog_pairs
}, cache_path)
utils.log_print(f"Cache saved at {cache_path}. Please re-run script to begin edit network pretraining", args)
return
train_loader, val_loader = get_synth_datasets(
domain,
)
net = get_edit_net(domain, args.load_model_path)
net.prog_diff = domain.prog_diff
if args.load_res_path is not None:
res = json.load(open(args.load_res_path))
try:
starting_iter = int(res['eval_iters'][-1])
except:
starting_iter = 0
else:
res = {
'train_plots': {'train':{'iters':[]}, 'val':{'iters':[]}},
'eval_plots': {'val':{}},
'eval_iters': []
}
starting_iter = 0
train_loader.iter_num = starting_iter
last_print = starting_iter
last_eval = starting_iter
last_save = starting_iter
if args.save_per is None:
args.save_per = args.eval_per
opt = torch.optim.Adam(
net.parameters(),
lr = args.lr,
eps = 1e-6
)
save_model_count = 0
eval_data = [
('val', val_loader),
]
print("Starting Training")
pbar = None
while True:
if pbar is None:
pbar = tqdm(total=args.print_per)
itn = train_loader.iter_num
if itn > args.max_iters:
break
if itn - last_print >= args.print_per:
do_print = True
last_print = itn
pbar.close()
pbar = None
else:
do_print = False
tru.run_train_epoch(
args,
res,
net,
opt,
train_loader,
val_loader,
domain.TRAIN_LOG_INFO,
do_print,
)
if pbar is not None:
pbar.update(train_loader.iter_num-itn)
if itn - last_eval >= args.eval_per:
last_eval = itn
val_loader.iter_num = itn
tru.run_eval_epoch(
args,
res,
net,
eval_data,
domain.EVAL_LOG_INFO,
itn,
)
if itn - last_save >= args.save_per:
last_save = itn
utils.save_model(
net.state_dict(),
f"{args.outpath}/{args.exp_name}/models/net_CKPT_{save_model_count}.pt"
)
save_model_count += 1