-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpt2_summarization_rl_finetuning.py
1336 lines (1163 loc) · 71.3 KB
/
gpt2_summarization_rl_finetuning.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 argparse
import glob
import logging
import os
import pickle
import random
import re
import shutil
from os.path import join
import json
from cytoolz import curry
from utils.reward import *
from utils.statistics import RewardStatistics, LagrangianStatistics
import time
from utils.time_log import time_since
#from utils.transformers_io import SummarizationDataset
from utils.io import remove_old_epoch_states, truncate_and_tokenize_sent_list, find_latest_epoch_state, LEN_BINS, ext_frag_density_to_bin, n_gram_novelty_to_bin, fusion_ratio_to_bin
from gpt2_summarization_finetuning import SummarizationDataset, get_control_mode_special_ids_dict
import copy
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset, SequentialSampler, RandomSampler
from torch.utils.data.distributed import DistributedSampler
from utils.masked_softmax import MaskedSoftmax
import torch.nn.functional as F
import nltk
from model.lagrangian import Lagrangian
from rl_pipeline import build_cost_objects, build_reward_object, train_lagrangian_multiplier
from utils.cost import compute_batch_cost
from utils.reward import compute_batch_reward
from utils.report import export_train_and_valid_reward, export_lagrangian_stats
import sys
EPS = 1e-8
try:
from torch.utils.tensorboard import SummaryWriter
except:
from tensorboardX import SummaryWriter
from tqdm import tqdm, trange
from transformers import (WEIGHTS_NAME, AdamW, get_linear_schedule_with_warmup,
BertConfig, BertForMaskedLM, BertTokenizer,
GPT2Config, GPT2LMHeadModel, GPT2Tokenizer,
OpenAIGPTConfig, OpenAIGPTLMHeadModel, OpenAIGPTTokenizer,
RobertaConfig, RobertaForMaskedLM, RobertaTokenizer,
DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer,
CamembertConfig, CamembertForMaskedLM, CamembertTokenizer)
logger = logging.getLogger(__name__)
MODEL_CLASSES = {
'gpt2': (GPT2Config, GPT2LMHeadModel, GPT2Tokenizer),
'openai-gpt': (OpenAIGPTConfig, OpenAIGPTLMHeadModel, OpenAIGPTTokenizer),
'bert': (BertConfig, BertForMaskedLM, BertTokenizer),
'roberta': (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer),
'distilbert': (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer),
'camembert': (CamembertConfig, CamembertForMaskedLM, CamembertTokenizer)
}
def load_and_cache_examples(args, tokenizer, evaluate=False):
dataset = SummarizationDataset(tokenizer, args, data_dir=args.data_dir, split='val' if evaluate else 'train', control_modes=args.control_modes)
return dataset
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
"""
def _rotate_checkpoints(args, checkpoint_prefix, use_mtime=False):
if not args.save_total_limit:
return
if args.save_total_limit <= 0:
return
# Check if we should delete older checkpoint(s)
glob_checkpoints = glob.glob(os.path.join(args.output_dir, '{}-*'.format(checkpoint_prefix)))
if len(glob_checkpoints) <= args.save_total_limit:
return
ordering_and_checkpoint_path = []
for path in glob_checkpoints:
if use_mtime:
ordering_and_checkpoint_path.append((os.path.getmtime(path), path))
else:
regex_match = re.match('.*{}-([0-9]+)'.format(checkpoint_prefix), path)
if regex_match and regex_match.groups():
ordering_and_checkpoint_path.append((int(regex_match.groups()[0]), path))
checkpoints_sorted = sorted(ordering_and_checkpoint_path)
checkpoints_sorted = [checkpoint[1] for checkpoint in checkpoints_sorted]
number_of_checkpoints_to_delete = max(0, len(checkpoints_sorted) - args.save_total_limit)
checkpoints_to_be_deleted = checkpoints_sorted[:number_of_checkpoints_to_delete]
for checkpoint in checkpoints_to_be_deleted:
logger.info("Deleting older checkpoint [{}] due to args.save_total_limit".format(checkpoint))
shutil.rmtree(checkpoint)
"""
def mask_tokens(inputs, tokenizer, args):
""" Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. """
labels = inputs.clone()
# We sample a few tokens in each sequence for masked-LM training (with probability args.mlm_probability defaults to 0.15 in Bert/RoBERTa)
probability_matrix = torch.full(labels.shape, args.mlm_probability)
special_tokens_mask = [tokenizer.get_special_tokens_mask(val, already_has_special_tokens=True) for val in labels.tolist()]
probability_matrix.masked_fill_(torch.tensor(special_tokens_mask, dtype=torch.bool), value=0.0)
masked_indices = torch.bernoulli(probability_matrix).bool()
labels[~masked_indices] = -1 # We only compute loss on masked tokens
# 80% of the time, we replace masked input tokens with tokenizer.mask_token ([MASK])
indices_replaced = torch.bernoulli(torch.full(labels.shape, 0.8)).bool() & masked_indices
inputs[indices_replaced] = tokenizer.convert_tokens_to_ids(tokenizer.mask_token)
# 10% of the time, we replace masked input tokens with random word
indices_random = torch.bernoulli(torch.full(labels.shape, 0.5)).bool() & masked_indices & ~indices_replaced
random_words = torch.randint(len(tokenizer), labels.shape, dtype=torch.long)
inputs[indices_random] = random_words[indices_random]
# The rest of the time (10% of the time) we keep the masked input tokens unchanged
return inputs, labels
@curry
def coll_rl(batch, TLDR_id_list, pad_idx=0, tokenizer=None, control_modes=[], control_mode_special_ids_dict={}, input_trunc_len=512, output_trunc_len=100):
#doc_seq_lens = []
#for doc, _, _, _, _ in batch:
# doc_seq_lens.append(len(doc[:input_trunc_len]))
#max_doc_len = max(doc_seq_lens)
input_lens = []
doc_trunc_ids_all = []
prefix_ids_all = []
doc_word_2d_list = []
summary_sent_2d_list_tokenized = []
doc_sent_2d_list_tokenized = []
summary_word_2d_list = []
len_bin_list = []
abs_bin_list = []
summary_lens = []
masked_questions_ids_2dlist = []
answer_2dlist = []
#answer_id_2dlist = []
#multiple_choices_ids_2dlist = []
reference_entities_list = []
#max_number_length_token_ids = 3
for doc, summary, doc_sent_list, summary_sent_list, controllable_fields in batch:
# handle sentence list
summary_sent_list_tokenized = truncate_and_tokenize_sent_list(summary_sent_list, output_trunc_len)
summary_sent_2d_list_tokenized.append(summary_sent_list_tokenized)
doc_sent_list_tokenized = truncate_and_tokenize_sent_list(doc_sent_list, input_trunc_len)
doc_sent_2d_list_tokenized.append(doc_sent_list_tokenized)
# handle doc word list and trg word list
doc_str = ' '.join(doc_sent_list)
doc_word_list = doc_str.split(' ')[:input_trunc_len]
doc_word_2d_list.append(doc_word_list)
summary_str = ' '.join(summary_sent_list)
summary_word_list = summary_str.split(' ')[:output_trunc_len]
summary_word_2d_list.append(summary_word_list)
summary_len = len(summary_word_list)
# handle tensors
doc_truncated = doc[:input_trunc_len]
# control mode specific operations
special_token_id_list = []
for control_mode in control_modes:
if control_mode == 1:
len_bin = LEN_BINS[summary_len]
special_token_id_list.append(control_mode_special_ids_dict['len_bin'][len_bin])
#print(summary_len)
#print(len_bin)
#print(control_mode_special_ids_dict['len_bin'][len_bin])
len_bin_list.append(len_bin)
elif control_mode == 2:
length_token_ids = tokenizer.convert_tokens_to_ids([str(summary_len)])
special_token_id_list += length_token_ids
summary_lens.append(summary_len)
elif control_mode == 5:
abs_bin = ext_frag_density_to_bin(controllable_fields['ext_frag_density'])
special_token_id_list.append(control_mode_special_ids_dict['abs_bin'][abs_bin])
abs_bin_list.append(abs_bin)
elif control_mode == 4:
abs_bin = n_gram_novelty_to_bin(controllable_fields['two_gram_novelty'])
special_token_id_list.append(control_mode_special_ids_dict['abs_bin'][abs_bin])
abs_bin_list.append(abs_bin)
elif control_mode == 6:
abs_bin = fusion_ratio_to_bin(controllable_fields['avg_fusion_ratio'])
special_token_id_list.append(control_mode_special_ids_dict['abs_bin'][abs_bin])
abs_bin_list.append(abs_bin)
elif control_mode == 7:
#special_token_id_list += controllable_fields['reference_entities_prefix_ids']
doc_truncated = controllable_fields['reference_entities_prefix_ids'] + doc_truncated
masked_questions_ids_2dlist.append(controllable_fields['masked_question_ids'])
answer_2dlist.append(controllable_fields['answer_list'])
#answer_id_2dlist.append(controllable_fields['answer_idx_list'])
#multiple_choices_ids_2dlist.append(controllable_fields['multiple_choices_ids'])
reference_entities_list.append(controllable_fields['reference_entities'])
doc_trunc_ids_all.append(doc_truncated)
prefix_ids = TLDR_id_list[:-1] + special_token_id_list + TLDR_id_list[-1:]
prefix_ids_all.append(prefix_ids)
input_lens.append( len(doc_truncated) + len(prefix_ids) )
max_input_len = max(input_lens)
input_ids_all_padded = []
position_ids_all_padded = []
prompt_ids_list = []
summary_ids_list = []
for doc_trunc_ids, prefix_ids in zip(doc_trunc_ids_all, prefix_ids_all):
doc_len = len(doc_trunc_ids)
padding_length = max_input_len - doc_len - len(prefix_ids)
input_ids = doc_trunc_ids + ([pad_idx] * padding_length) + prefix_ids
position_ids = list(range(doc_len + padding_length)) + list(range(doc_len, doc_len + len(prefix_ids)))
input_ids_all_padded.append(input_ids)
position_ids_all_padded.append(position_ids)
#prompt_ids = doc_truncated + TLDR_id_list[:-1] + special_token_id_list + TLDR_id_list[-1:]
#prompt_ids_list.append(prompt_ids)
#summary_truncated = summary[:output_trunc_len]
#summary_ids_list.append(summary_truncated)
"""
print()
print(tokenizer.decode(input_ids_all_padded[0], clean_up_tokenization_spaces=False))
print(tokenizer.decode(input_ids_all_padded[1], clean_up_tokenization_spaces=False))
print(tokenizer.decode(input_ids_all_padded[2], clean_up_tokenization_spaces=False))
print(tokenizer.decode(input_ids_all_padded[3], clean_up_tokenization_spaces=False))
print(input_ids_all_padded[0])
print(input_ids_all_padded[1])
print(input_ids_all_padded[2])
print(input_ids_all_padded[3])
print()
print(position_ids_all_padded[0])
print(position_ids_all_padded[1])
print(position_ids_all_padded[2])
print(position_ids_all_padded[3])
#print()
#print(reference_entities_list)
exit()
"""
input_ids_tensor = torch.LongTensor(input_ids_all_padded)
position_ids_tensor = torch.LongTensor(position_ids_all_padded)
#batch = {'input_ids': input_ids_tensor, 'position_ids': position_ids_tensor, 'doc_list_tokenized': document_list_tokenized, 'summary_sent_2d_list': summary_sent_2d_list}
batch = {'input_ids': input_ids_tensor, 'position_ids': position_ids_tensor}
batch['prompt_ids_list'] = prompt_ids_list
batch['doc_sent_2d_list_tokenized'] = doc_sent_2d_list_tokenized
batch['summary_sent_2d_list_tokenized'] = summary_sent_2d_list_tokenized
batch['doc_word_2d_list'] = doc_word_2d_list
batch['summary_word_2d_list'] = summary_word_2d_list
batch['len_bins'] = len_bin_list
batch['abs_bins'] = abs_bin_list
batch['summary_lens'] = summary_lens
batch['summary_ids_list'] = summary_ids_list
batch['reference_entities_list'] = reference_entities_list
batch['masked_questions_ids_2dlist'] = masked_questions_ids_2dlist
batch['answer_2dlist'] = answer_2dlist
#batch['answer_id_2dlist'] = answer_id_2dlist
#batch['multiple_choices_ids_2dlist'] = multiple_choices_ids_2dlist
return batch
def sample_sequence(model, tokenizer, input_ids, mask, position_ids, max_output_length, eos_idx, is_greedy=False):
batch_size = input_ids.size(0)
past = None
end_flags = [False] * batch_size
log_selected_token_dist = []
#device = input_ids.device()
#print("is greedy: {}".format(is_greedy))
for t in range(max_output_length):
#print()
#print("t: {}".format(t))
#start_time = time.time()
inputs = {'input_ids': input_ids, 'past': past, 'attention_mask': mask, 'position_ids': position_ids}
outputs = model(**inputs)
#print("input_ids size: {}".format(input_ids.size()))
#print("attention_mask size: {}".format(mask.size()))
#print("position_ids size: {}".format(position_ids.size()))
#print("forward time: {}".format(time_since(start_time)))
prediction_scores = outputs[0] # (batch_size, sequence_length, config.vocab_size)
#print("logit size: {}".format(prediction_scores.size()))
next_token_logits = prediction_scores[:, -1, :] # [batch, vocab_size]
past = outputs[1] # a list of torch.FloatTensor
#print("past_size: {}".format(past[0].size(3)))
#print("prediction_score_size: {}".format(prediction_scores.size()))
#with torch.no_grad():
# next_token_distribution = F.softmax(next_token_logits, dim=-1) # [batch, vocab_size]
#next_token_log_distribution = torch.log(next_token_distribution + EPS) # [batch, vocab_size]
#start_time = time.time()
next_token_log_distribution = F.log_softmax(next_token_logits, dim=-1) # [batch, vocab_size]
with torch.no_grad():
next_token_distribution = torch.exp(next_token_log_distribution)
#print("softmax time: {}".format(time_since(start_time)))
#start_time = time.time()
if is_greedy:
next_token = torch.argmax(next_token_distribution, dim=-1).unsqueeze(-1) # [batch, 1]
else:
next_token = torch.multinomial(next_token_distribution, num_samples=1) # [batch, 1]
log_selected_token_dist.append(next_token_log_distribution.gather(1, next_token))
#print("gather time: {}".format(time_since(start_time)))
if t == 0:
generated = next_token
#log_distributions_all = next_token_log_distribution.unsqueeze(1) # [batch, 1, vocab_size]
else:
generated = torch.cat((generated, next_token), dim=1)
#log_distributions_all = torch.cat((log_distributions_all, next_token_log_distribution.unsqueeze(1)), dim=1) # [batch, seq_len, vocab_size]
for i in range(batch_size):
if next_token[i, 0].item() == eos_idx:
end_flags[i] = True
if all(end_flags):
break
input_ids = next_token # [batch, 1]
next_position_id = position_ids[:, -1] + 1 # [batch_size]
position_ids = next_position_id.unsqueeze(1) # [batch, 1]
# position_ids = torch.cat([position_ids, next_position_id.unsqueeze(1)], dim=1)
next_attention_mask = torch.FloatTensor([1] * batch_size).to(mask.device) # [batch_size]
mask = torch.cat([mask, next_attention_mask.unsqueeze(1)], dim=1)
if not is_greedy:
log_selected_token_dist = torch.cat(log_selected_token_dist, dim=1) # [batch, T]
assert log_selected_token_dist.size() == torch.Size([batch_size, t+1])
#start_time = time.time()
outputs = generated.tolist()
output_str_list = []
output_ids_list = []
unfinished_mask = torch.ones_like(generated).float()
for i, out_ids in enumerate(outputs):
eos_positions = [position for position, word_id in enumerate(out_ids) if word_id == eos_idx]
if len(eos_positions) > 0:
end_position = eos_positions[0]
if end_position < len(out_ids) - 1:
unfinished_mask[i, end_position+1:] = 0.0
out_ids = out_ids[:end_position]
output_ids_list.append(out_ids)
out_text = tokenizer.decode(out_ids, clean_up_tokenization_spaces=False)
output_str_list.append(out_text)
#print("decode time: {}".format(time_since(start_time)))
#exit()
return output_str_list, log_selected_token_dist, unfinished_mask, output_ids_list
def tokenize_and_sentence_tokenize_str_list(out_str_list):
pred_word_2d_list = []
pred_word_2d_list_sent_tokenized = []
for output_str in out_str_list:
pred_word_2d_list.append(output_str.split(' '))
output_sent_list = nltk.tokenize.sent_tokenize(output_str)
output_sent_list = [output_sent.strip().split(' ') for output_sent in output_sent_list]
pred_word_2d_list_sent_tokenized.append(output_sent_list)
return pred_word_2d_list, pred_word_2d_list_sent_tokenized
def compute_ml_loss(model, prompt_ids_list, target_ids_list, pad_idx, eos_idx, device):
ml_input_ids_list = []
ml_label_ids_list = []
ml_input_ids_lens = []
for prompt_ids, target_ids in zip(prompt_ids_list, target_ids_list):
ml_input_ids = prompt_ids + target_ids + [eos_idx]
ml_input_ids_list.append(ml_input_ids)
ml_label_ids = [-100] * len(prompt_ids) + target_ids + [eos_idx]
ml_label_ids_list.append(ml_label_ids)
ml_input_ids_lens.append(len(ml_input_ids))
# padding
max_ml_input_ids_len = max(ml_input_ids_lens)
ml_input_ids_list_padded = []
ml_label_ids_list_padded = []
for ml_input_ids, ml_label_ids in zip(ml_input_ids_list, ml_label_ids_list):
ml_padding_length = max_ml_input_ids_len - len(ml_input_ids)
ml_input_ids_list_padded.append(ml_input_ids + [pad_idx] * ml_padding_length)
ml_label_ids_list_padded.append(ml_label_ids + [-100] * ml_padding_length)
#print("input_ids sizes: ")
#[print(len(ml_input_ids))for ml_input_ids in ml_input_ids_list_padded]
#[print(len(ml_label_ids)) for ml_label_ids in ml_label_ids_list_padded]
#[print(ml_input_ids) for ml_input_ids in ml_input_ids_list_padded]
#[print(ml_label_ids) for ml_label_ids in ml_label_ids_list_padded]
#print("target_ids sizes: ")
#[print(len(ml_target_ids)) for ml_target_ids in target_ids_list]
#exit()
ml_input_ids_tensor = torch.LongTensor(ml_input_ids_list_padded).to(device)
ml_label_ids_tensor = torch.LongTensor(ml_label_ids_list_padded).to(device)
ml_attn_mask = torch.ne(ml_input_ids_tensor, pad_idx).float()
ml_outputs = model(input_ids=ml_input_ids_tensor, attention_mask=ml_attn_mask, labels=ml_label_ids_tensor)
ml_loss = ml_outputs[0]
return ml_loss
def train_rl(args, train_dataset, model, tokenizer, lagrangian_model, epoch_state_dict, control_mode_special_ids_dict):
""" Train the model """
if args.local_rank in [-1, 0]:
tb_writer = SummaryWriter()
if args.continue_training:
report_train_reward_statistics = epoch_state_dict['report_train_reward_statistics']
report_train_reward = epoch_state_dict['report_train_reward']
report_valid_reward = epoch_state_dict['report_valid_reward']
best_valid_reward = epoch_state_dict['best_valid_reward']
previous_valid_reward = epoch_state_dict['previous_valid_reward']
num_stop_increasing = epoch_state_dict['num_stop_increasing']
print("Previous valid rewards: {}".format(report_valid_reward))
else:
report_train_reward_statistics = RewardStatistics()
report_train_reward = []
report_valid_reward = []
best_valid_reward = float('-inf')
previous_valid_reward = float('-inf')
num_stop_increasing = 0
if args.continue_training:
global_step = epoch_state_dict['global_step']
epoch = epoch_state_dict['epoch']
else:
global_step = 0
epoch = 0
args.train_batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu)
# TLDR_id_list = tokenizer.convert_tokens_to_ids(tokenizer.tokenize("<control><summarize>:"))
TLDR_id_list = tokenizer.convert_tokens_to_ids(tokenizer.tokenize("TL;DR:"))
train_sampler = RandomSampler(train_dataset) if args.local_rank == -1 else DistributedSampler(train_dataset)
pad_idx = tokenizer.convert_tokens_to_ids(['<pad>'])[0]
eos_idx = tokenizer.eos_token_id
train_dataloader = DataLoader(train_dataset, sampler=train_sampler, batch_size=args.train_batch_size,
collate_fn=coll_rl(TLDR_id_list=TLDR_id_list, pad_idx=pad_idx, tokenizer=tokenizer, control_modes=args.control_modes,
control_mode_special_ids_dict=control_mode_special_ids_dict,
input_trunc_len=args.input_trunc_length, output_trunc_len=args.output_trunc_length))
if args.max_steps > 0:
t_total = args.max_steps
args.num_train_epochs = args.max_steps // (len(train_dataloader) // args.gradient_accumulation_steps) + 1
else:
t_total = len(train_dataloader) // args.gradient_accumulation_steps * args.num_train_epochs
# Prepare optimizer and schedule (linear warmup and decay)
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
'weight_decay': args.weight_decay},
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
#scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps,
# num_training_steps=t_total)
if args.continue_training:
optimizer.load_state_dict(epoch_state_dict['optimizer'])
#if not args.do_not_reload_scheduler:
# scheduler.load_state_dict(epoch_state_dict['scheduler'])
reward_obj = build_reward_object(args.reward_type, args.device)
if args.constrained_mdp:
optimizer_lagrangian = torch.optim.Adam(params=filter(lambda p: p.requires_grad, lagrangian_model.parameters()), lr=args.learning_rate_multiplier)
pretrained_model_args = {}
pretrained_model_args['pad_idx'] = pad_idx
pretrained_model_args['eos_idx'] = eos_idx
pretrained_model_args['TLDR_ids_list'] = TLDR_id_list
cost_objs = build_cost_objects(args.cost_types, args.device, args.train_batch_size, args.cost_thresholds, model, pretrained_model_args)
if args.continue_training:
optimizer_lagrangian.load_state_dict(epoch_state_dict['optimizer_lagrangian'])
report_train_lagrangian_statistics = epoch_state_dict['report_train_lagrangian_statistics']
report_lagrangian_loss = epoch_state_dict['report_lagrangian_loss']
report_lagrangian_multipliers = epoch_state_dict['report_lagrangian_multipliers']
report_violate_amounts = epoch_state_dict['report_violate_amounts']
report_lagrangian_grad_norms = epoch_state_dict['report_lagrangian_grad_norms']
else:
report_train_lagrangian_statistics = LagrangianStatistics()
report_lagrangian_loss = []
report_lagrangian_multipliers = []
report_violate_amounts = []
report_lagrangian_grad_norms = []
if args.fp16:
try:
from apex import amp
except ImportError:
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level)
# multi-gpu training (should be after apex fp16 initialization)
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
if args.constrained_mdp:
lagrangian_model = torch.nn.DataParallel(lagrangian_model)
# Distributed training (should be after apex fp16 initialization)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True,
broadcast_buffers=False)
if args.constrained_mdp:
lagrangian_model = torch.nn.parallel.DistributedDataParallel(lagrangian_model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True,
broadcast_buffers=False)
# Train!
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_dataset))
logger.info(" Num Epochs = %d", args.num_train_epochs)
logger.info(" Instantaneous batch size per GPU = %d", args.per_gpu_train_batch_size)
logger.info(" Total train batch size (w. parallel, distributed & accumulation) = %d",
args.train_batch_size * args.gradient_accumulation_steps * (
torch.distributed.get_world_size() if args.local_rank != -1 else 1))
logger.info(" Gradient Accumulation steps = %d", args.gradient_accumulation_steps)
logger.info(" Total optimization steps = %d", t_total)
logger.info("logging_steps = %d", args.logging_steps)
tr_loss, logging_loss = 0.0, 0.0
model_to_resize = model.module if hasattr(model, 'module') else model # Take care of distributed/parallel training
model_to_resize.resize_token_embeddings(len(tokenizer))
model.zero_grad()
train_iterator = trange(int(args.num_train_epochs), desc="Epoch", disable=args.local_rank not in [-1, 0])
set_seed(args) # Added here for reproducibility (even between python 2 and 3)
for _ in train_iterator:
epoch += 1
epoch_iterator = tqdm(train_dataloader, desc="Iteration", disable=args.local_rank not in [-1, 0])
for step, batch in enumerate(epoch_iterator):
context = batch['input_ids']
position_ids = batch['position_ids']
src_sent_2d_list_tokenized = batch['doc_sent_2d_list_tokenized']
trg_sent_2d_list_tokenized = batch['summary_sent_2d_list_tokenized']
src_word_2d_list = batch['doc_word_2d_list']
trg_word_2d_list = batch['summary_word_2d_list']
prompt_ids_list = batch['prompt_ids_list']
trg_summary_ids_list = batch['summary_ids_list']
mask = torch.ne(context, pad_idx).float()
batch_size = context.size(0)
context = context.to(args.device)
position_ids = position_ids.to(args.device)
mask = mask.to(args.device)
input_ids = context
control_variables = {}
"""
if 1 in args.control_modes:
control_variables['len_bins'] = batch['len_bins']
if 2 in args.control_modes:
control_variables['exact_lens'] = batch['exact_lens']
if 3 in args.control_modes or 4 in args.control_modes or 5 in args.control_modes or 6 in args.control_modes:
control_variables['abs_bins'] = batch['abs_bins']
if 6 in args.control_modes:
# tokenize the each src sentence list in the batch and put it in the control variable.
control_variables['src_word_2d_list_sent_tokenized'] = src_sent_2d_list_tokenized
# control_variables['src_word_2d_list'] = batch['src_list_tokenized']
# if 10 in opt.cost_types or 11 in opt.cost_types or 18 in opt.cost_types or 19 in opt.cost_types:
"""
for control_mode in args.control_modes:
if control_mode == 1:
control_variables['len_bins'] = batch['len_bins']
elif control_mode == 2:
control_variables['exact_lens'] = batch['summary_lens']
elif control_mode == 5 or control_mode == 4 or control_mode == 6:
control_variables['abs_bins'] = batch['abs_bins']
elif control_mode == 7:
control_variables['reference_entities_list'] = batch['reference_entities_list']
control_variables['masked_questions_ids_2dlist'] = batch['masked_questions_ids_2dlist']
control_variables['answer_2dlist'] = batch['answer_2dlist']
#control_variables['answer_id_2dlist'] = batch['answer_id_2dlist']
#control_variables['multiple_choices_ids_2dlist'] = batch['multiple_choices_ids_2dlist']
control_variables['src_word_2d_list'] = src_word_2d_list
control_variables['src_word_2d_list_sent_tokenized'] = src_sent_2d_list_tokenized
# sample sequence
model.train()
start_time = time.time()
sample_output_str_list, log_distributions_all, output_mask, sample_output_ids_list = \
sample_sequence(model, tokenizer, input_ids, mask, position_ids, args.max_output_length, eos_idx, is_greedy=False)
#input_sequence_len = context.size(1)
#print("input len {}".format(input_sequence_len))
#outputs_tensor = model.generate(input_ids=input_ids, max_length=input_sequence_len+output_trunc_len, do_sample=True, num_beams=1, pad_token_id=pad_idx, eos_token_ids=[eos_idx], position_ids=position_ids, attn_mask=mask)
sample_time = time_since(start_time)
#print("sample_time: {}".format(sample_time))
# log_distributions_all: [batch, max_seq_len], output_mask: [batch, max_seq_len]
# sum, apply masking and pass it to the control variables
# prompt ids
log_distributions_all_sum = (log_distributions_all * output_mask).sum(dim=1) # [batch]
#print("log distribution size: ")
#print(log_distributions_all.size())
control_variables['pred_log_probs_sum'] = log_distributions_all_sum
control_variables['pred_ids_list'] = sample_output_ids_list
control_variables['prompt_ids_list'] = prompt_ids_list
# greedy sequence
start_time = time.time()
with torch.no_grad():
model.eval()
greedy_output_str_list, _, _, _ = \
sample_sequence(model, tokenizer, input_ids, mask, position_ids, args.max_output_length, eos_idx, is_greedy=True)
#print("greedy_time: {}".format(time_since(start_time)))
sample_word_2d_list, sample_sent_2d_list_tokenized = tokenize_and_sentence_tokenize_str_list(sample_output_str_list)
greedy_word_2d_list, greedy_sent_2d_list_tokenized = tokenize_and_sentence_tokenize_str_list(greedy_output_str_list)
max_sample_seq_len = log_distributions_all.size(1)
start_time = time.time()
with torch.no_grad():
cumulative_reward = compute_batch_reward(sample_word_2d_list, sample_sent_2d_list_tokenized, trg_word_2d_list,
trg_sent_2d_list_tokenized, batch_size, reward_obj,
control_variables=control_variables)
# store the sum of cumulative reward (before baseline) for the experiment log
cumulative_reward_sum = cumulative_reward.detach().sum(0).item()
baseline = compute_batch_reward(greedy_word_2d_list, greedy_sent_2d_list_tokenized, trg_word_2d_list,
trg_sent_2d_list_tokenized, batch_size, reward_obj,
control_variables=control_variables)
if args.constrained_mdp:
cumulative_cost = compute_batch_cost(sample_word_2d_list, sample_sent_2d_list_tokenized, trg_word_2d_list,
trg_sent_2d_list_tokenized, batch_size, cost_objs,
control_variables) # [sample_batch_size, num_cost_types]
cumulative_cost_mean = cumulative_cost.mean(0) # [num_cost_types]
# cumulative_cost: [sample_batch_size, len(cost_types)]
# subtract the regularization term: \lambda \dot C_t
lagrangian_model_to_compute = lagrangian_model.module if hasattr(lagrangian_model, 'module') else lagrangian_model
constraint_regularization = lagrangian_model_to_compute.compute_regularization(
cumulative_cost) # [sample_batch_size]
cumulative_reward -= constraint_regularization
#exit()
#print(cumulative_cost.detach().cpu().numpy())
#print(constraint_regularization.detach().cpu().numpy())
# Subtract the cumulative reward by a baseline if needed
if args.baseline != 'none':
cumulative_reward = cumulative_reward - baseline # [sample_batch_size]
# q value estimation for each time step equals to the (baselined) cumulative reward
q_value_estimate = cumulative_reward.unsqueeze(1).repeat(1, max_sample_seq_len) # [sample_batch_size, max_pred_seq_len]
#print(log_distributions_all.size())
#print(q_value_estimate.size())
#print(cumulative_reward.detach().cpu().numpy())
#print(baseline.detach().cpu().numpy())
#print(cumulative_reward_sum)
# compute loss for model
q_estimate_compute_time = time_since(start_time)
#print("q_estimate_compute_time: {}".format(q_estimate_compute_time))
q_value_estimate.requires_grad_(True)
# compute the policy gradient objective
#print(log_distributions_all.size())
#print(output_mask.size())
#print(q_value_estimate.size())
start_time = time.time()
pg_loss = compute_pg_loss(log_distributions_all, output_mask, q_value_estimate)
pg_loss_normalized = pg_loss.div(batch_size)
#print("pg_loss compute time: {}".format(time_since(start_time)))
start_time = time.time()
#if args.gradient_accumulation_steps > 1:
# pg_loss_normalized = pg_loss_normalized / args.gradient_accumulation_steps
if args.ml_loss_coefficient > 0:
model.train()
ml_loss = compute_ml_loss(model, prompt_ids_list, trg_summary_ids_list, pad_idx, eos_idx, args.device)
if args.n_gpu > 1:
ml_loss = ml_loss.mean() # mean() to average on multi-gpu parallel training
#if args.gradient_accumulation_steps > 1:
# ml_loss = ml_loss / args.gradient_accumulation_steps
total_loss = (1 - args.ml_loss_coefficient) * pg_loss_normalized + args.ml_loss_coefficient * ml_loss
else:
total_loss = pg_loss_normalized
if args.gradient_accumulation_steps > 1:
total_loss = total_loss / args.gradient_accumulation_steps
if args.fp16:
with amp.scale_loss(total_loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
total_loss.backward()
backward_time = time_since(start_time)
#print("backward time: {}".format(backward_time))
#exit()
if args.local_rank in [-1, 0]:
batch_reward_stat = RewardStatistics(cumulative_reward_sum, pg_loss.item(), batch_size, sample_time,
q_estimate_compute_time, backward_time)
report_train_reward_statistics.update(batch_reward_stat)
tr_loss += total_loss.item()
# compute loss for lagrangian model
if args.constrained_mdp:
lagrangian_loss, violate_amount = lagrangian_model(cumulative_cost)
if args.n_gpu > 1:
lagrangian_loss = lagrangian_loss.mean()
violate_amount = violate_amount.sum()
lagrangian_loss_normalized = lagrangian_loss.div(batch_size)
if args.gradient_accumulation_steps > 1:
lagrangian_loss_normalized = lagrangian_loss_normalized / args.gradient_accumulation_steps
lagrangian_loss_normalized.backward()
if args.local_rank in [-1, 0]:
lagrangian_grad_norm = lagrangian_model_to_compute.lagrangian_multiplier.grad.detach().sum().item()
batch_lagrangian_stat = LagrangianStatistics(lagrangian_loss=lagrangian_loss.item(), n_batch=batch_size,
lagrangian_grad_norm=lagrangian_grad_norm,
violate_amount=violate_amount.item())
report_train_lagrangian_statistics.update(batch_lagrangian_stat)
if (step + 1) % args.gradient_accumulation_steps == 0:
# take a gradient step on model
if args.max_grad_norm > 0:
if args.fp16:
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
else:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
#grad_norm_before_clipping = torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
#scheduler.step()
model.zero_grad()
# take a gradient step on model on lagrangian model
if args.constrained_mdp:
optimizer_lagrangian.step()
lagrangian_model_to_compute.clamp_lagrangian_multiplier()
lagrangian_model.zero_grad()
# increment global step
global_step += 1
#if global_step == 10:
# stat = torch.cuda.memory_stats("cuda:0")
# print()
# print()
# print(stat['reserved_bytes.all.peak'])
# print(stat['allocated_bytes.all.peak'])
# exit()
# log each loss to tensorboard
if args.local_rank in [-1, 0] and args.logging_steps > 0 and global_step % args.logging_steps == 0:
# tensorboard
#if args.ml_loss_coefficient > 0:
# tb_writer.add_scalar('ml_loss', ml_loss.item(), global_step)
#tb_writer.add_scalar('pg_loss', pg_loss_normalized.item(), global_step)
#tb_writer.add_scalar('lr', scheduler.get_lr()[0], global_step)
tb_writer.add_scalar('loss', tr_loss - logging_loss, global_step)
logging_loss = tr_loss
if args.constrained_mdp:
lambda_tensor = lagrangian_model_to_compute.get_lagrangian_multiplier()
for cost_i, cost_type in enumerate(args.cost_types):
tb_writer.add_scalar('cost_{}'.format(cost_type),
cumulative_cost_mean[cost_i].detach().item(), global_step)
tb_writer.add_scalar('lambda_{}'.format(cost_type), lambda_tensor[cost_i].item(),
global_step)
# statistics objects
current_train_reward = report_train_reward_statistics.reward()
current_train_pg_loss = report_train_reward_statistics.loss()
report_train_reward.append(current_train_reward)
logging.info('Epoch: %d; batch idx: %d; total batches: %d' % (epoch, step, global_step))
logging.info(
'avg training reward: %.5f; avg training loss: %.5f' % (
current_train_reward, current_train_pg_loss))
if args.constrained_mdp:
current_lagrangian_loss = report_train_lagrangian_statistics.loss()
current_lagrangian_grad_norm = report_train_lagrangian_statistics.grad_norm()
current_violate_amount = report_train_lagrangian_statistics.violate_amt()
report_lagrangian_loss.append(current_lagrangian_loss)
report_violate_amounts.append(current_violate_amount)
report_lagrangian_grad_norms.append(current_lagrangian_grad_norm)
lagrangian_multipliers_array = lagrangian_model_to_compute.get_lagrangian_multiplier_array()
report_lagrangian_multipliers.append(lagrangian_multipliers_array)
logging.info("Lagrangian_loss: %.5f; grad_norm: %.5f" % (
current_lagrangian_loss, current_lagrangian_grad_norm))
logging.info("Value of lagrangian_multipliers: {}".format(lagrangian_multipliers_array))
report_train_reward_statistics.clear()
if args.constrained_mdp:
report_train_lagrangian_statistics.clear()
# check point
"""
if args.local_rank in [-1, 0] and args.save_steps > 0 and global_step % args.save_steps == 0:
checkpoint_prefix = "checkpoint"
# Save model checkpoint
output_dir = os.path.join(args.output_dir, "{}-{}".format(checkpoint_prefix, global_step))
os.makedirs(output_dir, exist_ok=True)
model_to_save = (
model.module if hasattr(model, "module") else model
) # Take care of distributed/parallel training
model_to_save.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
torch.save(args, os.path.join(output_dir, "training_args.bin"))
lagrangian_model_to_save = lagrangian_model.module if hasattr(lagrangian_model,
'module') else lagrangian_model
if args.constrained_mdp:
torch.save(lagrangian_model_to_save.state_dict(), os.path.join(output_dir, "lagrangian_model.bin"))
logger.info("Saving model checkpoint to %s", output_dir)
_rotate_checkpoints(args, checkpoint_prefix)
torch.save(optimizer.state_dict(), os.path.join(output_dir, "optimizer.pt"))
#torch.save(scheduler.state_dict(), os.path.join(output_dir, "scheduler.pt"))
logger.info("Saving optimizer and scheduler states to %s", output_dir)
"""
if args.max_steps > 0 and global_step > args.max_steps:
epoch_iterator.close()
break
if args.local_rank in [-1, 0]:
logging.info("Finished epoch {}".format(epoch))
# save epoch state for continue training
if epoch % 1 == 0:
current_epoch_state_dir = os.path.join(args.output_dir, 'epoch_states', '{}-epoch'.format(epoch))
if not os.path.exists(current_epoch_state_dir):
os.makedirs(current_epoch_state_dir)
# save model
model_to_save = model.module if hasattr(model,
'module') else model # Take care of distributed/parallel training
model_to_save.save_pretrained(current_epoch_state_dir)
# save epoch state dict
lagrangian_model_to_save = lagrangian_model.module if hasattr(lagrangian_model,
'module') else lagrangian_model
current_epoch_state = {
'epoch': epoch,
# 'total_batch': total_batch,
# 'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
#'scheduler': scheduler.state_dict(),
'lagrangian_model': lagrangian_model_to_save.state_dict() if args.constrained_mdp else None,
'optimizer_lagrangian': optimizer_lagrangian.state_dict() if args.constrained_mdp else None,
'best_valid_reward': best_valid_reward,
'previous_valid_reward': previous_valid_reward,
'num_stop_increasing': num_stop_increasing,
'report_train_reward_statistics': report_train_reward_statistics,
'report_train_reward': report_train_reward,
'report_valid_reward': report_valid_reward,
'report_train_lagrangian_statistics': report_train_lagrangian_statistics if args.constrained_mdp else None,
'report_lagrangian_loss': report_lagrangian_loss if args.constrained_mdp else None,
'report_lagrangian_multipliers': report_lagrangian_multipliers if args.constrained_mdp else None,
'report_violate_amounts': report_violate_amounts if args.constrained_mdp else None,
'report_lagrangian_grad_norms': report_lagrangian_grad_norms if args.constrained_mdp else None,
'global_step': global_step
}
epoch_state_dict_path = os.path.join(current_epoch_state_dir, 'epoch_state_dict.pt')
torch.save( # save epoch states
current_epoch_state,
open(epoch_state_dict_path, 'wb')
)
logging.info("saved epoch state.")
# run validation and save validation stat for every epoch
valid_reward_stat = evaluate(args, model, tokenizer, reward_obj, control_mode_special_ids_dict=control_mode_special_ids_dict)
current_valid_reward = valid_reward_stat.reward()
report_valid_reward.append(current_valid_reward)
# print out valid reward
logging.info(
'avg validation reward: %.5f; best validation reward: %.5f' % (
current_valid_reward, best_valid_reward))
lagrangian_multipliers_array = lagrangian_model_to_compute.get_lagrangian_multiplier_array()
logging.info('Value of lagrangian_multipliers: %s' % (lagrangian_multipliers_array.tostring()))
if current_valid_reward > previous_valid_reward: # update the best valid reward and save the model parameters
logging.info("Valid reward increases")
if current_valid_reward > best_valid_reward:
best_valid_reward = current_valid_reward
num_stop_increasing = 0
else:
logging.info("Valid reward does not increases")
num_stop_increasing += 1
previous_valid_reward = current_valid_reward
if args.max_steps > 0 and global_step > args.max_steps:
train_iterator.close()
break
if args.max_epochs > 0 and epoch >= args.max_epochs:
train_iterator.close()
break
if args.local_rank in [-1, 0]:
tb_writer.close()
# export the training curve
train_valid_curve_path = join(args.output_dir, 'train_valid_curve')
export_train_and_valid_reward(report_train_reward, report_valid_reward, args.logging_steps,
train_valid_curve_path)
if args.constrained_mdp:
#print()
#print(report_lagrangian_multipliers)
export_lagrangian_stats(report_lagrangian_loss, report_lagrangian_multipliers, report_lagrangian_grad_norms,
report_violate_amounts, args.logging_steps, train_valid_curve_path)
# log best reward
logging.info("final_best_valid_reward: %.3f" % best_valid_reward)
return global_step, tr_loss / global_step
def evaluate(args, model, tokenizer, reward_obj, prefix="", control_mode_special_ids_dict={}):
# Loop to handle MNLI double evaluation (matched, mis-matched)
eval_output_dir = args.output_dir
eval_dataset = load_and_cache_examples(args, tokenizer, evaluate=True)
if not os.path.exists(eval_output_dir) and args.local_rank in [-1, 0]:
os.makedirs(eval_output_dir)
#args.eval_batch_size = args.per_gpu_eval_batch_size * max(1, args.n_gpu)
args.eval_batch_size = args.per_gpu_eval_batch_size
# Note that DistributedSampler samples randomly
eval_sampler = SequentialSampler(eval_dataset)
#TLDR_id_list = tokenizer.convert_tokens_to_ids(tokenizer.tokenize("<control><summarize>:"))
TLDR_id_list = tokenizer.convert_tokens_to_ids(tokenizer.tokenize("TL;DR:"))
pad_idx = tokenizer.convert_tokens_to_ids(['<pad>'])[0]
eos_idx = tokenizer.eos_token_id
eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler, batch_size=args.eval_batch_size, collate_fn=coll_rl(TLDR_id_list=TLDR_id_list, pad_idx=pad_idx, tokenizer=tokenizer, control_modes=args.control_modes,
control_mode_special_ids_dict=control_mode_special_ids_dict,
input_trunc_len=args.input_trunc_length, output_trunc_len=args.output_trunc_length))
# multi-gpu evaluate
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
# Eval!
logger.info("***** Running evaluation {} *****".format(prefix))
logger.info(" Num examples = %d", len(eval_dataset))
logger.info(" Batch size = %d", args.eval_batch_size)
eval_loss = 0.0
nb_eval_steps = 0
model.eval()
valid_reward_sum = 0.0
n_batch = 0
sample_time_total = 0.0
for batch in tqdm(eval_dataloader, desc="Evaluating"):
context = batch['input_ids']
position_ids = batch['position_ids']
src_word_2d_list = batch['doc_word_2d_list']
trg_word_2d_list = batch['summary_word_2d_list']
src_sent_2d_list_tokenized = batch['doc_sent_2d_list_tokenized']
trg_sent_2d_list_tokenized = batch['summary_sent_2d_list_tokenized']
mask = torch.ne(context, pad_idx).float()
batch_size = context.size(0)
# construct label, set all the pad_idx to -1 so that it will not be computed in the loss
#labels = inputs.masked_fill(inputs == pad_idx, -1)
context = context.to(args.device)
position_ids = position_ids.to(args.device)
mask = mask.to(args.device)
input_ids = context
n_batch += batch_size
control_variables = {}
control_variables['src_word_2d_list'] = src_word_2d_list
for control_mode in args.control_modes:
if control_mode == 1:
control_variables['len_bins'] = batch['len_bins']
elif control_mode == 2:
control_variables['exact_lens'] = batch['summary_lens']
elif control_mode == 5 or control_mode == 4 or control_mode == 6:
control_variables['abs_bins'] = batch['abs_bins']
elif control_mode == 7:
control_variables['reference_entities_list'] = batch['reference_entities_list']
control_variables['masked_questions_ids_2dlist'] = batch['masked_questions_ids_2dlist']
control_variables['answer_2dlist'] = batch['answer_2dlist']
# control_variables['answer_id_2dlist'] = batch['answer_id_2dlist']
# control_variables['multiple_choices_ids_2dlist'] = batch['multiple_choices_ids_2dlist']
# greedy sequence
start_time = time.time()
with torch.no_grad():
greedy_output_str_list, _, _, _ = \
sample_sequence(model, tokenizer, input_ids, mask, position_ids, args.max_output_length, eos_idx, is_greedy=True)
sample_time = time_since(start_time)
sample_time_total += sample_time
greedy_word_2d_list, greedy_sent_2d_list_tokenized = tokenize_and_sentence_tokenize_str_list(
greedy_output_str_list)
with torch.no_grad():
valid_reward = compute_batch_reward(greedy_word_2d_list, greedy_sent_2d_list_tokenized, trg_word_2d_list,
trg_sent_2d_list_tokenized, batch_size, reward_obj,