-
Notifications
You must be signed in to change notification settings - Fork 1
/
runold.py
1530 lines (1478 loc) · 71.9 KB
/
runold.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
from torch import optim
from Dataset import SumDataset,rs_collate_fn,Graph,ChunkedRandomSampler,rs_collate_fn1,pad_seq2
import os
from tqdm import tqdm
from Model import *
import numpy as np
from copy import deepcopy
import pickle
import sys
from ScheduledOptim import *
from scipy import sparse
import json
from stringfycode import stringfyNode
from transformers import AutoModel, AutoTokenizer
onelist = ['argument_list', 'formal_parameters', 'block', 'array_initializer', 'switch_block', 'type_arguments', "method_declaration", "modifiers"]
#tokenizer = AutoTokenizer.from_pretrained('roberta-base')
from torch import multiprocessing as mp
import ssl
from torch.cuda import amp as torch_amp
import argparse
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
import random
ssl._create_default_https_context = ssl._create_unverified_context
from tensorboardX import SummaryWriter
#sys.setrecursionlimit(500000000)
sys.setrecursionlimit(500000000)
#from pythonBottom.run import finetune
#from pythonBottom.run import pre
#wandb.init("sql")
class dotdict(dict):
def __getattr__(self, name):
return self[name]
args = dotdict({
'NlLen':1024,
'CodeLen':150,
'batch_size':144,
'TableLen':100,
'embedding_size':512,
'WoLen':15,
'Vocsize':100,
'Nl_Vocsize':100,
'max_step':3,
'margin':0.5,
'poolsize':50,
'Code_Vocsize':100,
'num_steps':50,
'rulenum':10,
'seed':19970316,
'edgelen':0,
'cnum':407,
'mask_id':0,
'bertnum':0,
"gradient_accumulation_steps":10,
"patience":5,
"max_num_trials":10,
"max_rel_pos":10,
'par': True,
'use_apex':False,
'max_grad_norm':1.0,
'use_torch_amp':False,
"pretrain_name":"grammart5-small"
})
onelist = ['argument_list', 'formal_parameters', 'block', 'array_initializer', 'switch_block', 'type_arguments', "method_declaration", "modifiers", 'annotation_argument_list', 'variable_declarator', 'throws', 'element_value_array_initializer', 'annotation_argument_list', 'switch_block_statement_group', 'class_body', 'catch_type', 'assert_statement', 'try_statement', 'local_variable_declaration', 'try_statement', 'constructor_body', 'type_parameters', 'resource_specification', 'inferred_parameters', 'try_with_resources_statement', 'inits', 'updates', 'conditions']
identifiers = ['identifier', 'type_identifier', 'null_literal', 'decimal_integer_literal', 'character_literal', 'decimal_floating_point_literal', 'hex_integer_literal', 'string_literal']
#os.environ["CUDA_VISIBLE_DEVICES"]="3, 0, 1, 2, 4, 5, 6, 7"
#os.environ['CUDA_LAUNCH_BLOCKING']="1"
def save_model(model, dirs='checkpointSearch/', optimizer=None, amp=None):
if not os.path.exists(dirs):
os.makedirs(dirs)
if optimizer is not None:
checkpoint = {
'model':model.state_dict(),
'optimizer':optimizer.state_dict(),
'amp':amp.state_dict()
}
torch.save(checkpoint, dirs + 'best_model.ckpt')
else:
torch.save(model.state_dict(), dirs + 'best_model.ckpt')
def load_model(model, dirs = 'checkpointSearch/'):
assert os.path.exists(dirs + 'best_model.ckpt'), 'Weights for saved model not found'
model.load_state_dict(torch.load(dirs + 'best_model.ckpt', map_location='cpu'))
use_cuda = True#torch.cuda.is_available()
def gVar(data):
tensor = data
if isinstance(data, np.ndarray):
tensor = torch.from_numpy(data)
else:
assert isinstance(tensor, torch.Tensor)
if use_cuda:
tensor = tensor.cuda()
return tensor
def getAntiMask(size):
ans = np.zeros([size, size])
for i in range(size):
for j in range(0, i + 1):
ans[i, j] = 1.0
return ans
def getAdMask(size):
ans = np.zeros([size, size])
for i in range(size - 1):
ans[i, i + 1] = 1.0
return ans
def getRulePkl(vds):
inputruleparent = []
inputrulechild = []
for i in range(args.cnum):
rule = vds.rrdict[i].strip().lower().split()
if len(rule) < 2:
continue
inputrulechild.append(vds.pad_seq(vds.Get_Em(rule[2:], vds.Code_Voc), vds.Char_Len))
inputruleparent.append(vds.Code_Voc[rule[0].lower()])
return np.array(inputruleparent), np.array(inputrulechild)
def getAstPkl(vds):
rrdict = {}
for x in vds.Code_Voc:
rrdict[vds.Code_Voc[x]] = x
inputchar = []
for i in range(len(vds.Code_Voc)):
rule = rrdict[i].strip().lower()
inputchar.append(vds.pad_seq(vds.Get_Char_Em([rule])[0], vds.Char_Len))
return np.array(inputchar)
def display(tab=None):
experiment = comet_ml.get_global_experiment()
experiment.display(tab=tab)
from Grape import Grape
import wandb
def splitbyCard(tmp, r, c, idx, cardnum=7):
assert args.batch_size % cardnum == 0
newbatchsize = args.batch_size // cardnum
ans = []
for i in range(7):
tmpidx = tmp[0][idx[i][0]:idx[i][1], :]
print(tmpidx)
tmpidx[:, 0] = tmpidx[:,0] - i * newbatchsize
tmpv = tmp[1][idx[i][0]:idx[i][1]]
print(tmpidx.shape, tmpv.shape)
print(tmpidx)
tmpad = torch.sparse_coo_tensor(tmpidx.t(), tmpv, torch.Size([newbatchsize, r, c]))
ans.append(tmpad)
return ans
def train():
#g = Grape(args, 'pythonrule.pkl')
parser = argparse.ArgumentParser()
parser.add_argument("--local_rank", default=-1)
argc = parser.parse_args()
local_rank = argc.local_rank
torch.cuda.set_device('cuda:' + local_rank)
dist.init_process_group(backend='nccl')
device = torch.device("cuda", int(local_rank))
torch.manual_seed(args.seed)
np.random.seed(args.seed)
train_set = SumDataset(args, None, "train")
args.cnum = len(train_set.ruledict)
#print(len(train_set.edgedict))
#print(args.bertnum)
if dist.get_rank() == 0:
wandb.init(project="code-gen")
dev_set = SumDataset(args, None, "test")
args.Nl_Vocsize = len(train_set.Nl_Voc)
args.Vocsize = len(train_set.Char_Voc)
args.rulenum = len(train_set.ruledict)
#dev_set = SumDataset(args, "val")
args.edgelen = len(train_set.edgedict)
model = Decoder(args)
model.encoder.model.resize_token_embeddings(args.rulenum)
#load_model(model, 'checkModelNUM-304/')
base_params = list(map(id, model.encoder.model.parameters()))
logits_params = filter(lambda p: id(p) not in base_params, model.parameters())
params = [
{"params": logits_params, "lr": 1e-4, 'notchange':False},
{"params": model.encoder.model.parameters(), "lr": 5e-5, 'notchange':True},
]
optimizer = optim.AdamW(params, eps=1e-8)
#scheduler_poly_lr_decay = PolynomialLRDecay(optimizer, max_decay_steps=100000, init_lr0=3e-4, init_lr2=5e-5, end_learning_rate=0.000, power=1.0)
pathnames = []
num_trial = patience = 0
isBetter = False
#optimizer = ScheduledOptim(optimizer, d_model=args.embedding_size, max_steps=50000)
maxAcc= 0
maxC = 0
minloss = 1e10
if use_cuda:
print('using GPU')
#os.environ["CUDA_VISIBLE_DEVICES"] = "3"
model = model.to(device)
torch.cuda.manual_seed_all(args.seed)
model = DDP(model, device_ids=[int(local_rank)], output_device='cuda:' + local_rank, find_unused_parameters=True)
#from blcDP import BalancedDataParallel
#model = nn.DataParallel(model, device_ids=[0, 1, 2, 3, 4, 5, 6])
#BalancedDataParallel(0, model)#nn.DataParallel(model, device_ids=[0, 1])
#model.to()
global_step = 0
model_filename = 'model.onnx'
hasgraph = False
train_sampler = torch.utils.data.distributed.DistributedSampler(train_set)
train_size = args.batch_size // 8
for epoch in range(100000):
j = 0
train_sampler.set_epoch(epoch)
data_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=train_size, drop_last=True, num_workers=4,collate_fn=rs_collate_fn, sampler=train_sampler, pin_memory=True)
for dBatch in tqdm(data_loader):
isBetter = False
if j % 400 == 0 and dist.get_rank() == 0:
devloader = torch.utils.data.DataLoader(dataset=dev_set, batch_size=args.batch_size,
shuffle=False, drop_last=True, num_workers=4,collate_fn=rs_collate_fn)
model = model.eval()
accs = []
tcard = []
tmp = []
for devBatch in tqdm(devloader):
for i in range(len(devBatch)):
if i == 3:
tmpad = torch.sparse_coo_tensor(devBatch[i][0].t(), devBatch[i][1], torch.Size([devBatch[0].size(0), devBatch[1].size(1), devBatch[1].size(1)])).to_dense()
else:
devBatch[i] = gVar(devBatch[i])
antimask = gVar(getAntiMask(devBatch[1].size(1)))
antimask2 = antimask.unsqueeze(0).repeat(args.batch_size, 1, 1).unsqueeze(1)
with torch.no_grad():
bsize = devBatch[0].size(0)
antimask2 = antimask2[:bsize]
_, pre = model(devBatch[0], devBatch[1], devBatch[4], antimask2, devBatch[2])
if False:
torch.onnx.export(model.module, (devBatch[0], devBatch[1], devBatch[4], antimask2, devBatch[2]), model_filename, opset_version=11)
writer.add_onnx_graph(model_filename)
hasgraph = True
display("assets")
#print(devBatch[2].size())
pred = pre.argmax(dim=-1)
resmask = torch.ne(devBatch[2], args.mask_id)
acc = (torch.eq(pred, devBatch[2]) * resmask).float()#.mean(dim=-1)
predres = (1 - acc) * pred.float() * resmask.float()
accsum = torch.sum(acc, dim=-1)
resTruelen = torch.sum(resmask, dim=-1).float()
cnum = (torch.eq(accsum, resTruelen)).sum().float()
acc = acc.sum(dim=-1) / resTruelen
accs.append(acc.mean().item())
tcard.append(cnum.item())
#print(devBatch[5])
#print(predres)
tnum = np.sum(tcard)
acc = np.mean(accs)
wandb.log({"acc": acc, "tnum": tnum})
print(str(acc), str(tnum), str(maxC))
#print(tmp)
if maxC < tnum:
maxC = tnum
save_model(model.module, 'checkModelNUM/')
isBetter = True
print('find better accuracy %f'%tnum)
#save_model(model)
if maxAcc < acc:
isBetter = True
maxAcc = acc
if isBetter:
patience = 0
print('save model to [%s]' % 'checkModel/', file=sys.stderr)
#save_model(model.module, 'checkModel%d-%d/'%(epoch, j))
save_model(model.module, 'checkModel/')
torch.save(optimizer.state_dict(), 'checkModel/optim.bin')
elif patience < args.patience:
patience += 1
print('hit patience %d' % patience, file=sys.stderr)
if patience == args.patience:
num_trial += 1
print('hit #%d trial' % num_trial, file=sys.stderr)
if num_trial == args.max_num_trials:
print('early stop!', file=sys.stderr)
exit(0)
lr = optimizer.param_groups[0]['lr'] * 0.5
model.module.load_state_dict(torch.load('checkModel/best_model.ckpt'))
model = model.cuda()
optimizer.load_state_dict(torch.load('checkModel/optim.bin'))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
patience = 0
else:
patience += 1
model = model.train()
for i in range(len(dBatch)):
if i == 3:
spad = torch.sparse_coo_tensor(dBatch[i][0].t(), dBatch[i][1], torch.Size([dBatch[0].size(0), dBatch[1].size(1), dBatch[1].size(1)])).to_dense()
else:
dBatch[i] = gVar(dBatch[i])
antimask = gVar(getAntiMask(dBatch[1].size(1)))
antimask2 = antimask.unsqueeze(0).repeat(train_size, 1, 1).unsqueeze(1)
loss, _ = model(dBatch[0], dBatch[1], dBatch[4], antimask2, dBatch[2])
'''if loss.item() == np.inf:
rrdict = {}
for x in dev_set.Nl_Voc:
rrdict[dev_set.Nl_Voc[x]] = x
ans = ""
for x in dBatch[0][0].data.cpu().numpy():
ans += rrdict[x]
print(ans)
#for i in range(len(dBatch)):
#print(dBatch[i][16].data.cpu().numpy())
exit(0)'''
resmask = torch.ne(dBatch[2], args.mask_id)
#print(torch.sum(resmask), torch.sum(loss))
loss = torch.sum(loss) / torch.sum(resmask)
if loss.sum().item() == np.inf:
print('inf')
exit(0)
if loss.item() == np.inf:
print(j)
assert(0)
if args.gradient_accumulation_steps > 1:
loss /= args.gradient_accumulation_steps
loss.backward()
if j % args.gradient_accumulation_steps == 0:
optimizer.step()#_and_update_lr()
optimizer.zero_grad()
if dist.get_rank() == 0:
wandb.log({"loss": loss.item()})
#wandb.log({"loss": loss.item()})
j += 1
global_step += 1
#display("metrics")
#display("assets")
def pretrain():
if args.par:
parser = argparse.ArgumentParser()
parser.add_argument("--local_rank", default=-1)
argc = parser.parse_args()
local_rank = argc.local_rank
torch.cuda.set_device('cuda:' + local_rank)
dist.init_process_group(backend='nccl')
if dist.get_rank() == 0:
wandb.init(project="grammar-t5")
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if args.par:
train_set = SumDataset(args, None, "train", idx=int(dist.get_rank()))
device = torch.device("cuda", int(local_rank))
else:
train_set = SumDataset(args, None, "train", idx=0)
device = torch.device("cuda", 0)
args.Nl_Vocsize = len(train_set.Nl_Voc)
args.rulenum = train_set.rulenum
model = Decoder(args)
model.encoder.model.resize_token_embeddings(args.rulenum)
base_params = list(map(id, model.encoder.model.parameters()))
logits_params = filter(lambda p: id(p) not in base_params, model.parameters())
params = [
{"params": logits_params, "lr": 2e-4, 'notchange':False},
{"params": model.encoder.model.parameters(), "lr": 5e-5, 'notchange':True},
]
optimizer = optim.AdamW(params, eps=1e-8)
scheduler_poly_lr_decay = PolynomialLRDecay(optimizer, max_decay_steps=100000, init_lr0=5e-5, init_lr2=2e-4, end_learning_rate=0.000, power=1.0)
pathnames = []
num_trial = patience = 0
isBetter = False
#optimizer = ScheduledOptim(optimizer, d_model=args.embedding_size, max_steps=50000)
maxAcc= 0
maxC = 0
minloss = 1e10
if use_cuda:
model = model.cuda()
print('using GPU')
if args.use_apex:
model, optimizer = amp.initialize(model, optimizer, opt_level="O1")
torch.cuda.manual_seed_all(args.seed)
if args.par:
model = DDP(model, device_ids=[int(local_rank)], output_device='cuda:' + local_rank, find_unused_parameters=True)
else:
model = model#nn.DataParallel(model)
#from blcDP import BalancedDataParallel
#model = nn.DataParallel(model, device_ids=[0, 1, 2, 3, 4, 5, 6])
#BalancedDataParallel(0, model)#nn.DataParallel(model, device_ids=[0, 1])
#model.to()
global_step = 0
train_size = args.batch_size // 8
if args.use_torch_amp:
scaler = torch_amp.GradScaler()
for epoch in range(100000):
j = 0
sampler = ChunkedRandomSampler(train_set, train_size)
data_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=train_size, drop_last=True, num_workers=10,collate_fn=rs_collate_fn, sampler=sampler, pin_memory=True)
for dBatch in tqdm(data_loader):
if j % 10000 == 1000 and dist.get_rank() == 0:
if len(pathnames) < 10:
save_model(model.module, 'checkpointEpchLR%dIter%d/'%(epoch, j))#print(loss.item())
pathnames.append('checkpointEpchLR%dIter%d/'%(epoch, j))
else:
os.system('rm -r %s' % pathnames[0])
pathnames.pop(0)
save_model(model.module, 'checkpointEpchLR%dIter%d/'%(epoch, j))#print(loss.item())
pathnames.append('checkpointEpchLR%dIter%d/'%(epoch, j))
model = model.train()
for x in dBatch:
for k in dBatch[x]:
dBatch[x][k] = gVar(dBatch[x][k])
#dBatch[x] = gVar(dBatch[x])
tdBatch = dBatch
#iden
dBatch = tdBatch['iden']
antimask = gVar(getAntiMask(dBatch['res'].size(1) - 1))
antimask2 = antimask.unsqueeze(0).repeat(train_size, 1, 1).unsqueeze(1)
if args.use_torch_amp:
with torch_amp.autocast():
loss, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=True)
else:
loss, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=True)
resmask = torch.ne(dBatch['res'][:,1:], args.mask_id)
loss = torch.sum(loss) / torch.sum(resmask)
dBatch = tdBatch['rule']
antimask = gVar(getAntiMask(dBatch['res'].size(1) - 1))
antimask2 = antimask.unsqueeze(0).repeat(train_size, 1, 1).unsqueeze(1)
if args.use_torch_amp:
with torch_amp.autocast():
loss2, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=True)
loss2, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=True)
resmask = torch.ne(dBatch['res'][:,1:], args.mask_id)
loss2 = torch.sum(loss2) / torch.sum(resmask)
loss3 = 0.5 * loss + 0.5 * loss2
if loss.item() == np.inf:
print(j)
assert(0)
if args.gradient_accumulation_steps > 1:
loss = loss3 / args.gradient_accumulation_steps
if args.use_apex:
with amp.scale_loss(loss,optimizer) as scaled_loss:
scaled_loss.backward()
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
if j % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
elif args.use_torch_amp:
scaler.scale(loss).backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
if j % args.gradient_accumulation_steps == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
else:
loss.backward()
if j % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
if args.par and dist.get_rank() == 0:
wandb.log({"loss": loss.item() * 10})
#wandb.log({"loss": loss.item()})
j += 1
global_step += 1
#display("metrics")
#display("assets")
def pretrain2():
if args.par:
parser = argparse.ArgumentParser()
parser.add_argument("--local_rank", default=-1)
argc = parser.parse_args()
local_rank = argc.local_rank
torch.cuda.set_device('cuda:' + local_rank)
dist.init_process_group(backend='nccl')
if dist.get_rank() == 0:
wandb.init(project="grammar-t5")
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if args.par:
train_set = SumDataset(args, None, "train", idx=int(dist.get_rank()), mode='gen')
device = torch.device("cuda", int(local_rank))
else:
train_set = SumDataset(args, None, "train", idx=0, mode='gen')
device = torch.device("cuda", 0)
args.Nl_Vocsize = len(train_set.Nl_Voc)
args.rulenum = train_set.rulenum
model = Decoder(args)
model.encoder.model.resize_token_embeddings(args.rulenum)
base_params = list(map(id, model.encoder.model.parameters()))
logits_params = filter(lambda p: id(p) not in base_params, model.parameters())
params = [
{"params": logits_params, "lr": 2e-4, 'notchange':False},
{"params": model.encoder.model.parameters(), "lr": 5e-5, 'notchange':True},
]
optimizer = optim.AdamW(params, eps=1e-8)
scheduler_poly_lr_decay = PolynomialLRDecay(optimizer, max_decay_steps=100000, init_lr0=5e-5, init_lr2=2e-4, end_learning_rate=0.000, power=1.0)
pathnames = []
num_trial = patience = 0
isBetter = False
#optimizer = ScheduledOptim(optimizer, d_model=args.embedding_size, max_steps=50000)
maxAcc= 0
maxC = 0
minloss = 1e10
if use_cuda:
model = model.cuda()
print('using GPU')
if args.use_apex:
model, optimizer = amp.initialize(model, optimizer, opt_level="O1")
torch.cuda.manual_seed_all(args.seed)
load_model(model, 'checkpointEpchLR8Iter11000/')
if args.par:
model = apex.parallel.DistributedDataParallel(model)#DDP(model, device_ids=[int(local_rank)], output_device='cuda:' + local_rank, find_unused_parameters=True)
else:
model = model#nn.DataParallel(model)
#from blcDP import BalancedDataParallel
#model = nn.DataParallel(model, device_ids=[0, 1, 2, 3, 4, 5, 6])
#BalancedDataParallel(0, model)#nn.DataParallel(model, device_ids=[0, 1])
#model.to()
global_step = 0
train_size = args.batch_size // 8
if args.use_torch_amp:
scaler = torch_amp.GradScaler()
for epoch in range(100000):
j = 0
sampler = ChunkedRandomSampler(train_set, train_size)
data_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=train_size, drop_last=True, num_workers=10,collate_fn=rs_collate_fn1, sampler=sampler, pin_memory=True)
for dBatch in tqdm(data_loader):
if j % 10000 == 1000 and dist.get_rank() == 0:
if len(pathnames) < 10:
save_model(model.module, 'checkpointEpch%dIter%d/'%(epoch, j))#print(loss.item())
pathnames.append('checkpointEpch%dIter%d/'%(epoch, j))
else:
os.system('rm -r %s' % pathnames[0])
pathnames.pop(0)
save_model(model.module, 'checkpointEpch%dIter%d/'%(epoch, j))#print(loss.item())
pathnames.append('checkpointEpch%dIter%d/'%(epoch, j))
model = model.train()
for x in dBatch:
dBatch[x] = gVar(dBatch[x])
#dBatch[x] = gVar(dBatch[x])
antimask = gVar(getAntiMask(dBatch['res'].size(1) - 1))
antimask2 = antimask.unsqueeze(0).repeat(train_size, 1, 1).unsqueeze(1)
if args.use_torch_amp:
with torch_amp.autocast():
loss, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=False)
else:
loss, _ = model(dBatch['nl'], dBatch['res'], dBatch['parent'], antimask2, lefttree=False)
resmask = torch.ne(dBatch['res'][:,1:], args.mask_id)
loss = torch.sum(loss) / torch.sum(resmask)
loss3 = loss
if loss.item() == np.inf:
print(j)
assert(0)
if args.gradient_accumulation_steps > 1:
loss = loss3 / args.gradient_accumulation_steps
if args.use_apex:
with amp.scale_loss(loss,optimizer) as scaled_loss:
scaled_loss.backward()
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
if j % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
elif args.use_torch_amp:
scaler.scale(loss).backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
if j % args.gradient_accumulation_steps == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
else:
loss.backward()
if j % args.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
scheduler_poly_lr_decay.step()
if args.par and dist.get_rank() == 0:
wandb.log({"loss": loss.item() * 10})
#wandb.log({"loss": loss.item()})
j += 1
global_step += 1
#display("metrics")
#display("assets")
def mergeIdentifier(root):
if root.name in identifiers:
if False:
pass
else:
oname = ""
for x in root.child:
oname += x.name[:-4]
oname += "_ter"
nnode = Node(oname, root.depth)
nnode.father = root
root.child = [nnode]
for x in root.child:
mergeIdentifier(x)
return
class Node:
def __init__(self, name, d):
self.name = name
self.depth = d
self.father = None
self.child = []
self.sibiling = None
self.expanded = False
self.fatherlistID = 0
self.id = -1
def getiden(root):
if len(root.child) == 0:
return root.name[:-4]
ans = ""
for x in root.child:
ans += getiden(x)
return ans
class SearchNode:
def __init__(self, ds, nl, name='', mode='gen', idenids=None):
if mode == 'iden':
self.currIdenIdx = 0
self.idenids = idenids
self.state = [ds.ruledict['<extra_id_0>']]
self.newidens = []
else:
self.state = [ds.ruledict["start -> java"]]
self.prob = 0
self.aprob = 0
self.bprob = 0
self.finish = False
if mode == 'iden':
self.root = Node(name, 0)
else:
self.root = Node("java", 2)
self.parent = [0]
#self.parent[args.NlLen]
self.expanded = None
#self.ruledict = ds.rrdict
self.expandedname = []
self.child = {}
for x in ds.ruledict:
self.expandedname.append(x.strip().split()[0])
self.expandedname.extend(onelist)
def selcetNode(self, root):
if not root.expanded and (root.name in self.expandedname) and root.name not in onelist and root.name not in identifiers:
return root
else:
for x in root.child:
ans = self.selcetNode(x)
if ans:
return ans
if (root.name in onelist or root.name in identifiers) and root.expanded == False:
return root
return None
def selectExpandedNode(self):
self.expanded = self.selcetNode(self.root)
def getRuleEmbedding(self, ds):
inputrule = ds.pad_seq(self.state, ds.Code_Len)
return inputrule
def getTreePath(self, ds):
tmppath = [self.expanded.name.lower()]
node = self.expanded.father
while node:
tmppath.append(node.name.lower())
node = node.father
tmp = ds.pad_seq(ds.Get_Em(tmppath, ds.Code_Voc), 10)
self.everTreepath.append(tmp)
return ds.pad_list(self.everTreepath, ds.Code_Len, 10)
def copynode(self, newnode, original):
for x in original.child:
nnode = Node(x.name, 0)
nnode.father = newnode
nnode.expanded = True
newnode.child.append(nnode)
self.copynode(nnode, x)
return
def checkapply(self, rule, ds):
rules = ds.rrdict[rule]
lst = rules.strip().split()
if "->" not in rules or lst[0] == '->':
if lst[0] == '->' and self.expanded.name != 'string_literal':
return False
else:
if self.expanded.name not in identifiers:
return False
else:
rules = ds.rrdict[rule]
if rules.strip().split()[0].lower() != self.expanded.name.lower():
return False
return True
def applyrule(self, rule, ds, mode='gen'):
#print(rule)
#print(self.state)
#print(self.printTree(self.root))
rules = ds.rrdict[rule]
lst = rules.strip().split()
if "->" not in rules or lst[0] == '->':
if rules == ' -> String_ter ':
nnode = Node("srini_string_ter", 0)
else:
nnode = Node(lst[0] + '_ter', 0)
nnode.father = self.expanded
nnode.fatherlistID = len(self.state)
self.expanded.child.append(nnode)
else:
rules = ds.rrdict[rule]
#print(rules)
if rules.strip().split()[0].lower() != self.expanded.name.lower():
assert(0)
return False
#assert(rules.strip().split()[0] == self.expanded.name)
if rules == self.expanded.name + " -> End ":
self.expanded.expanded = True
else:
for x in rules.strip().split()[2:]:
if self.expanded.depth + 1 >= 40:
nnode = Node(x, 39)
else:
nnode = Node(x, self.expanded.depth + 1)
#nnode = Node(x, self.expanded.depth + 1)
self.expanded.child.append(nnode)
nnode.father = self.expanded
nnode.fatherlistID = len(self.state)
self.expanded.id = len(self.state)
if mode == 'iden':
if 'extra_id' in rules:
self.parent.append(0)
else:
self.parent.append(self.idenids[self.currIdenIdx])
else:
self.parent.append(self.expanded.fatherlistID)
#self.graph.addEdge(len(self.state), self.expanded.fatherlistID, 1)
#if self.expanded.father:
# if len(self.expanded.child) >= 2:
# self.graph.addEdge(len(self.state), self.expanded.child[-2].id, 1)
assert(self.expanded.fatherlistID != -1)
if rule >= len(ds.ruledict):
assert(0)
self.parentrow.append(args.NlLen + args.TableLen + len(self.depth))
if rule - len(ds.ruledict) >= args.CodeLen:
self.parentcol.append(rule - len(ds.ruledict) - args.CodeLen + args.NlLen)
else:
self.parentcol.append(rule - len(ds.ruledict) + args.NlLen + args.TableLen)
self.parentdata.append(1)
#self.parent[args.NlLen + args.TableLen + len(self.depth), rule - len(ds.ruledict)] = 1
if rule >= len(ds.ruledict):
assert(0)
else:
self.state.append(rule)
if self.expanded.name not in onelist:
self.expanded.expanded = True
if self.expanded.name in identifiers: #self.expanded.name in ['qualifier', 'member', 'name', 'value', 'flag']:
if 'Ġ' in rules:
self.expanded.child.reverse()
self.expanded.expanded = True
if self.root.name == 'identifier':
self.newidens.append(getiden(self.root))
self.currIdenIdx += 1
if self.currIdenIdx < len(self.idenids):
self.root = Node('identifier', 0)
else:
self.expanded.expanded = False
return True
def printTree(self, r):
s = r.name + " "#print(r.name)
if len(r.child) == 0:
s += "^ "
return s
#r.child = sorted(r.child, key=lambda x:x.name)
for c in r.child:
s += self.printTree(c)
s += "^ "#print(r.name + "^")
return s
def getTreestr(self):
return self.printTree(self.root)
def BeamSearch(inputnl, vds, model, beamsize, batch_size, k, queue=None, name='', mode='gen'):
#print(inputnl[0].shape)
batch_size = gVar(inputnl[0]).size(0)
args.NlLen = inputnl[0].shape[1]
#print('------------------1')
#print(inputnl[3][0])
with torch.no_grad():
beams = {}
for i in range(batch_size):
if mode == 'iden':
beams[i] = [SearchNode(vds, [], name=name, mode=mode, idenids=inputnl[1])]
else:
beams[i] = [SearchNode(vds, [])]
nlencode, nlmask = model.nl_encode(gVar(torch.tensor(inputnl[0])))
index = 0
endnum = {}
continueSet = {}
codelen = 250
showtqdm = tqdm(range(codelen))
while True:
#print(index)
showtqdm.update(1)
args.CodeLen = min(index + 2, codelen)
vds.Code_Len = min(index + 2, codelen)#index + 1
tmpbeam = {}
ansV = {}
if len(endnum) == batch_size:
break
#if index > 10:
# assert(0)
if index >= codelen:
break
for p in range(beamsize):
tmprule = []
tmprulechild = []
tmpruleparent = []
tmptreepath = []
tmpAd = []
validnum = []
tmpdepth = []
for i in range(batch_size):
if p >= len(beams[i]):
continue
x = beams[i][p]
#print(x.printTree(x.root))
if not x.finish:
x.selectExpandedNode()
if x.expanded == None or len(x.state) >= args.CodeLen:
x.finish = True
ansV.setdefault(i, []).append(x)
else:
validnum.append(i)
a = x.getRuleEmbedding(vds)
tmprule.append(a)
#tmpAd.append(inputnl[3][0][:args.CodeLen, :args.CodeLen])
tmpAd.append(pad_seq2(x.parent, args.CodeLen))
#print("--------------------------")
if len(tmprule) == 0:
continue
batch_sizess = len(tmprule)
antimask2 = antimask.unsqueeze(0).repeat(batch_sizess, 1, 1).unsqueeze(1)
tmprule = np.array(tmprule)
tmpAd = np.array(tmpAd)
bsize = batch_sizess
antimask2 = antimask2[:bsize]
#print('------------------2')
#print(tmpAd[0])
if mode == 'iden':
result = model.test_foward(nlencode[validnum], nlmask[validnum], gVar(tmprule))
else:
result = model.test_foward(nlencode[validnum], nlmask[validnum], gVar(tmprule))
results = result#indexs = torch.argsort(result, descending=True)#results = result.data.cpu().numpy()
currIndex = 0
for j in range(batch_size):
if j not in validnum:
continue
x = beams[j][p]
tmpbeamsize = 0
result = results[currIndex, index]#np.negative(results[currIndex, index])
currIndex += 1
cresult = result#np.negative(result)
indexs = torch.argsort(result, descending=True)
for i in range(len(indexs)):
if tmpbeamsize >= 20 or i > 150:
break
#copynode = pickle.loads(pickle.dumps(x))#deepcopy(x)
#if indexs[i] >= len(vds.rrdict):
#print(cresult[indexs[i]])
#print('-', indexs[i])
c = x.checkapply(indexs[i].item(), vds)
#c = copynode.applyrule(indexs[i], vds.nl[args.batch_size * k + j], vds.tabless[args.batch_size * k + j], vds)
if not c:
tmpbeamsize += 1
continue
prob = x.prob + np.log(cresult[indexs[i]].item())#copynode.prob = copynode.prob + np.log(cresult[indexs[i]])
tmpbeam.setdefault(j, []).append([prob, indexs[i].item(), x])#tmpbeam.setdefault(j, []).append(copynode)
for i in range(batch_size):
if i in ansV:
if len(ansV[i]) == beamsize:
endnum[i] = 1
for j in range(args.batch_size):
if j in tmpbeam:
if j in ansV:
for x in ansV[j]:
tmpbeam[j].append([x.prob, -1, x])
tmp = sorted(tmpbeam[j], key=lambda x: x[0], reverse=True)[:beamsize]
beams[j] = []
for x in tmp:
if x[1] != -1:
copynode = pickle.loads(pickle.dumps(x[2]))
copynode.applyrule(x[1], vds, mode)
copynode.prob = x[0]
beams[j].append(copynode)
else:
beams[j].append(x[2])
index += 1
return beams
for i in range(len(beams)):
mans = -1000000
lst = beams[i]
tmpans = 0
for y in lst:
#f.write("\t".join(vds.nl[args.batch_size * k + i]) + "\n")
#f.write(y.getTreestr() + "\n")
#f.flush()
mergeIdentifier(y.root)
#print(y.getTreestr())
#print(stringfy(y.root))
if y.prob > mans:
mans = y.prob
tmpans = y
beams[i] = tmpans
if queue is not None:
queue.put({'id':index, 'ans':beams})
else:
return beams
#return beams
def stringfy(node):
ans = ""
if len(node.child) == 0:
if node.name[0] == 'Ġ':
ans += node.name[1:-4]
else:
ans = node.name[:-4]
else:
for x in node.child:
ans += stringfy(x) + " "
return ans
def testdis():
#pre()
#torch.manual_seed(args.seed)
#np.random.seed(args.seed)
dev_set = SumDataset(args, "test")
#rulead = gVar(pickle.load(open("rulead.pkl", "rb"))).float()
#tmpast = getAstPkl(dev_set)
args.cnum = len(dev_set.ruledict)
#print(len(train_set.edgedict))
print(args.bertnum)
#a, b = getRulePkl(dev_set)
#tmpf = gVar(a).unsqueeze(0).repeat(args.batch_size, 1).long()
#tmpc = gVar(b).unsqueeze(0).repeat(args.batch_size, 1, 1).long()
#tmpindex = gVar(np.arange(args.bertnum, len(dev_set.ruledict))).unsqueeze(0).repeat(args.batch_size, 1).long() - args.bertnum
#tmpchar = gVar(tmpast).unsqueeze(0).repeat(args.batch_size, 1, 1).long()
#tmpindex2 = gVar(np.arange(len(dev_set.Code_Voc))).unsqueeze(0).repeat(args.batch_size, 1).long()
args.Code_Vocsize = len(dev_set.Code_Voc)
args.Nl_Vocsize = len(dev_set.Nl_Voc)
args.Vocsize = len(dev_set.Char_Voc)
args.rulenum = len(dev_set.ruledict)
#dev_set = SumDataset(args, "val")
args.edgelen = len(dev_set.edgedict)
rdic = {}
for x in dev_set.Nl_Voc:
rdic[dev_set.Nl_Voc[x]] = x
#print(dev_set.Nl_Voc)
model = Decoder(args)
load_model(model, 'checkModel/')
if use_cuda:#torch.cuda.is_available():
print('using GPU')
#os.environ["CUDA_VISIBLE_DEVICES"] = "3"
#model = model.cuda()
model.to(0)
#model = nn.DataParallel(model, device_ids=[0, 1])
#model = model.module
model.share_memory()
ctx = mp.get_context('spawn')
'''for n, x in model.named_parameters():
print(n, x.numel())
total_params3 = sum(x.numel() for x in model.parameters())
total_params = sum(p.numel() for p in model.encoder.parameters())
total_params1 = sum(p.numel() for p in model.encodeTransformerBlock.parameters())
total_params2 = sum(p.numel() for p in model.decodeTransformerBlocksP.parameters())
print(f'{total_params:,} total parameters.')
print(f'{total_params1:,} total parameters in encoder.')
print(f'{total_params2:,} total parameters in decoder.')
print(f'{total_params3:,} total parameters in model.')
assert(0)'''
args.batch_size = 1
devloader = torch.utils.data.DataLoader(dataset=dev_set, batch_size=args.batch_size,
shuffle=False, drop_last=False, num_workers=0, collate_fn=rs_collate_fn)
model = model.eval()
#load_model(model)
index = 0
#antimask = gVar(getAntiMask(args.CodeLen))
#antimask2 = antimask.unsqueeze(0).repeat(1, 1, 1).unsqueeze(1)
#rulead = gVar(pickle.load(open("rulead.pkl", "rb"))).float().unsqueeze(0).repeat(2, 1, 1)
#a, b = getRulePkl(dev_set)
#tmpf = gVar(a).unsqueeze(0).repeat(2, 1).long()
#tmpc = gVar(b).unsqueeze(0).repeat(2, 1, 1).long()
#tmpindex = gVar(np.arange(len(dev_set.ruledict))).unsqueeze(0).repeat(2, 1).long()
res = ctx.SimpleQueue()
process = []
for x in tqdm(devloader):
'''pre = model(gVar(x[0]), gVar(x[1]), gVar(x[2]), gVar(x[3]), gVar(x[4]), gVar(x[6]), gVar(x[7]), gVar(x[8]), gVar(x[9]), gVar(x[10]), gVar(x[11]), tmpindex, tmpf, tmpc, rulead, antimask2, None, 'test')
#print(pre[0,3,4020], pre[0,3,317])
pred = pre.argmax(dim=-1)