-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQGData.py
2243 lines (2057 loc) · 148 KB
/
QGData.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
import json
import gzip
import re
import pickle as pkl
import string
import numpy as np
from tqdm import tqdm
from collections import Counter, defaultdict
import torch
from torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler, SequentialSampler
from QAData import QAData, AmbigQAData
from DataLoader import MySimpleQADataset, MySimpleQGDataset, MyDataLoader, MySimpleQGDynamicDataset, MySimpleQGDynamicWeightedLossDataset, MySimpleQGWeightedLossDataset
from util import decode_span_batch
# for evaluation
from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer
from ambigqa_evaluate_script import normalize_answer, get_exact_match, get_f1, get_qg_metrics
from pycocoevalcap.bleu.bleu import Bleu
PUNCTUATIONS = ["''", "'", "``", "`", "-LRB-", "-RRB-", "-LCB-", "-RCB-", \
".", "?", "!", ",", ":", "-", "--", "...", ";"]
PUNCT_WORDS = set(string.punctuation)
IGNORE_WORDS = {'in', 'the', 'is', 'at', 'which', 'on', 'what', 'who', 'where', 'when', 'how', 'with', 'a', 'about', 'an', 'are', 'as', 'at', 'be',
'by', 'for', 'from', 'how', 'in', 'is', 'it', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'will', 'with', 'do', 'does', 'did',
'i', 'me', 'we', 'our', 'ours', 'he', 'his', 'her', 'she', 'they', 'their', 'mine', 'theirs', 'ours', 'how', 'is', 'are', 'were', 'was',
'will', 'would', 'by', 'in', 'on', 'under', 'above', }
VALID_POS = ['ADJ', 'NOUN', 'NUM', 'PROPN', 'SYM', 'VERB']
class QGData(QAData):
def __init__(self, logger, args, data_path, is_training, passages=None):
super(QGData, self).__init__(logger, args, data_path, is_training, passages)
self.metric = "Bleu"
if args.do_train or args.task == "qg_mask":
import spacy
self.qg_tokenizer = spacy.load("en_core_web_sm")
else:
self.qg_tokenizer = PTBTokenizer()
def load_dpr_data(self):
dpr_retrieval_path = os.path.join(self.args.dpr_data_dir, "{}_predictions.json".format(
self.data_type + "_20200201" if self.args.wiki_2020 else self.data_type)).replace('train_for_inference', 'train')
postfix = self.tokenizer.__class__.__name__.replace("zer", "zed")
dpr_tokenized_path = os.path.join(self.args.reader_data_dir, self.args.predict_file.split("/")[-2],
"{}{}_predictions.json".format(self.data_type, "-reos" if self.args.t5_no_intermediate_eos else "", ))
dpr_tokenized_path = dpr_tokenized_path.replace(".json", "_{}_qg.json".format(postfix))
if "Bart" in postfix:
if not self.args.filter_not_found_answer_passages:
return self.load_dpr_data_bart(dpr_retrieval_path, dpr_tokenized_path)
else:
raise NotImplementedError
return self.load_dpr_data_bart_filter_nfa_psgs(dpr_retrieval_path, dpr_tokenized_path)
else:
raise NotImplementedError
def load_dpr_data_bart_old(self, dpr_retrieval_path, dpr_tokenized_path):
self.logger.info("{}\n{}".format(dpr_retrieval_path, dpr_tokenized_path))
if os.path.exists(dpr_tokenized_path):
self.logger.info("Loading DPR data from {}".format(dpr_tokenized_path))
with open(dpr_tokenized_path, "r") as f:
self.tokenized_data = json.load(f)
else:
self.logger.info("Start processing DPR data")
if self.passages.tokenized_data is None:
self.passages.load_tokenized_data("bart", all=True)
# if "train_for_inference" not in dpr_retrieval_path:
# dpr_retrieval_path = dpr_retrieval_path.replace("train", "train_for_inference")
with open(dpr_retrieval_path, "r") as f:
dpr_passages = json.load(f)
assert len(dpr_passages)==len(self)
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata = self.tokenized_data
assert len(dpr_passages) == len(input_ids) == len(attention_mask) == len(metadata)
bos_token_id = self.tokenizer.bos_token_id
def _included(tokens, curr_input_ids, end_of_answer):
is_answer_exist = []
for _curr_input_ids in curr_input_ids:
is_exist = False
for jdx in range(end_of_answer, len(_curr_input_ids)-len(tokens)+1):
if _curr_input_ids[jdx:jdx+len(tokens)]==tokens:
is_exist = True
is_answer_exist.append(is_exist)
return is_answer_exist
new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list = [], [], [], [], []
for idx, (curr_input_ids, curr_attention_mask, curr_metadata, dpr_ids) in enumerate(zip(
input_ids, attention_mask, metadata, dpr_passages)):
dpr_input_ids = [self.passages.tokenized_data["input_ids"][_id] for _id in dpr_ids]
dpr_attention_mask = [self.passages.tokenized_data["attention_mask"][_id] for _id in dpr_ids]
# create multiple inputs
answer_input_ids_list, answer_attention_mask_list, is_valid_list = [], [], []
for answer_idx in range(*curr_metadata):
end_of_answer = decoder_input_ids[answer_idx].index(self.tokenizer.eos_token_id) + 1
answer_input_ids = decoder_input_ids[answer_idx][:end_of_answer]
answer_attention_mask = decoder_attention_mask[answer_idx][:end_of_answer]
ap_input_ids, ap_attention_mask = [], []
for jdx, (_dpr_input_ids, _dpr_attention_mask) in enumerate(zip(dpr_input_ids, dpr_attention_mask)):
assert _dpr_input_ids[0] == bos_token_id
answer_input_ids_jdx = answer_input_ids + _dpr_input_ids[1:]
answer_attention_mask_jdx = answer_attention_mask + _dpr_attention_mask[1:]
assert len(answer_input_ids_jdx) == len(answer_attention_mask_jdx)
answer_input_ids_jdx += [self.tokenizer.pad_token_id for _ in range(32 + 128 - len(answer_input_ids_jdx))]
answer_attention_mask_jdx += [0 for _ in range(32 + 128 - len(answer_attention_mask_jdx))]
ap_input_ids.append(answer_input_ids_jdx)
ap_attention_mask.append(answer_attention_mask_jdx)
assert len(ap_input_ids[jdx]) == len(ap_attention_mask[jdx]) == 160 # here we use 32+128
assert len(ap_input_ids) == len(ap_attention_mask) == 100
answer_input_ids_list.append(ap_input_ids)
answer_attention_mask_list.append(ap_attention_mask)
is_valid_list.append(_included(decoder_input_ids[answer_idx][1:end_of_answer - 1], ap_input_ids, end_of_answer))
assert len(answer_input_ids_list) == len(answer_attention_mask_list) == len(is_valid_list) == curr_metadata[1] - curr_metadata[0]
new_decoder_input_ids.append(curr_input_ids)
new_decoder_attention_mask.append(curr_attention_mask)
new_input_ids.append(answer_input_ids_list)
new_attention_mask.append(answer_attention_mask_list)
new_is_valid_list.append(is_valid_list)
assert len(new_decoder_input_ids) == len(new_decoder_attention_mask) == len(new_input_ids) == len(new_attention_mask) == len(new_is_valid_list) == len(self)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list]
with open(dpr_tokenized_path, "w") as f:
json.dump(self.tokenized_data, f)
self.logger.info("Finish saving tokenized DPR data at {}".format(dpr_tokenized_path))
raw_input_ids, raw_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, raw_is_valid_list = self.tokenized_data
if self.args.use_reranker:
assert self.args.psg_sel_dir is not None
psg_sel_fn = os.path.join(self.args.psg_sel_dir,
"{}{}_psg_sel.json".format(
self.data_type.replace("train", "train_for_inference"),
"_20200201" if self.args.wiki_2020 else ""))
self.logger.info("Loading passage selection from DPR reader: {}".format(psg_sel_fn))
with open(psg_sel_fn, "r") as f:
fg_passages = json.load(f)
assert len(fg_passages) == len(raw_input_ids)
ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list = [], [], []
for idx, fg_psgs in enumerate(fg_passages):
ranked_raw_input_ids.append([[x[i] for i in fg_psgs][:self.args.top_k_passages] for x in raw_input_ids[idx]])
ranked_raw_attention_mask.append([[x[i] for i in fg_psgs][:self.args.top_k_passages] for x in raw_attention_mask[idx]])
ranked_raw_is_valid_list.append([[x[i] for i in fg_psgs][:self.args.top_k_passages] for x in raw_is_valid_list[idx]])
else:
ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list = [], [], []
for idx in range(len(raw_input_ids)):
ranked_raw_input_ids.append([x[:self.args.top_k_passages] for x in raw_input_ids[idx]])
ranked_raw_attention_mask.append([x[:self.args.top_k_passages] for x in raw_attention_mask[idx]])
ranked_raw_is_valid_list.append([x[:self.args.top_k_passages] for x in raw_is_valid_list[idx]])
# prepare for customized training and inference dataset
new_input_ids, new_attention_mask, new_metadata = [], [], []
has_valid = []
if not self.is_training:
old_decoder_input_ids, old_decoder_attention_mask, = new_decoder_input_ids, new_decoder_attention_mask,
new_decoder_input_ids, new_decoder_attention_mask, = [], []
for idx, (rr_input_ids, rr_attention_mask, rr_is_valid_list) in enumerate(zip(ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list)):
topk_is_valid_list = [any(rr_is_valid) for rr_is_valid in rr_is_valid_list]
has_valid.append(any(topk_is_valid_list))
if self.is_training:
if self.args.discard_not_found_answers:
if not any(topk_is_valid_list):
topk_is_valid_list = [True for _ in topk_is_valid_list]
new_metadata.append((len(new_input_ids), len(new_input_ids) + sum(topk_is_valid_list)))
new_input_ids += [answer_input_ids for answer_input_ids, is_valid in zip(rr_input_ids, topk_is_valid_list) if is_valid]
new_attention_mask += [answer_attention_mask for answer_attention_mask, is_valid in zip(rr_attention_mask, topk_is_valid_list) if is_valid]
else:
offset = len(new_input_ids)
new_input_ids.extend(rr_input_ids)
new_attention_mask.extend(rr_attention_mask)
new_metadata.append((offset, len(new_input_ids)))
else:
# Yifan: original code only evaluates when gold sample is found, but here we evaluate all qa pairs
# we generate all questions conditioned on all answers, and get the best if the question have multiple answer candidates
offset = len(new_input_ids)
new_input_ids.extend(rr_input_ids)
new_attention_mask.extend(rr_attention_mask)
for i in range(offset, len(new_input_ids)):
new_metadata.append((i,i+1))
new_decoder_input_ids.append(old_decoder_input_ids[idx])
new_decoder_attention_mask.append(old_decoder_attention_mask[idx])
assert len(new_input_ids) == len(new_attention_mask) == new_metadata[-1][-1]
if not self.is_training:
assert len(new_input_ids) == len(new_attention_mask) == len(new_metadata) == len(new_decoder_input_ids) == len(new_decoder_attention_mask)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_metadata]
self.logger.info("%.2f%% questions have at least one answer mentioned in passages" % (100*np.mean(has_valid)))
def load_dpr_data_bart(self, dpr_retrieval_path, dpr_tokenized_path):
self.logger.info("{}\n{}".format(dpr_retrieval_path, dpr_tokenized_path))
if os.path.exists(dpr_tokenized_path):
self.logger.info("Loading DPR data from {}".format(dpr_tokenized_path))
with open(dpr_tokenized_path, "r") as f:
self.tokenized_data = json.load(f)
else:
self.logger.info("Start processing DPR data")
if self.passages.tokenized_data is None:
self.passages.load_tokenized_data("bart", all=True)
with open(dpr_retrieval_path, "r") as f:
dpr_passages = json.load(f)
assert len(dpr_passages)==len(self)
if self.args.use_reranker:
assert self.args.psg_sel_dir is not None
psg_sel_fn = os.path.join(self.args.psg_sel_dir,
"{}{}_psg_sel.json".format(
self.data_type.replace("train", "train_for_inference"),
"_20200201" if self.args.wiki_2020 else ""))
self.logger.info("Loading passage selection from DPR reader: {}".format(psg_sel_fn))
with open(psg_sel_fn, "r") as f:
fg_passages = json.load(f)
assert len(fg_passages) == len(dpr_passages)
dpr_passages = [[psgs[i] for i in fg_psgs][:100] for psgs, fg_psgs in zip(dpr_passages, fg_passages)]
else:
raise NotImplementedError
bos_token_id = self.tokenizer.bos_token_id
eos_token_id = self.tokenizer.eos_token_id
pad_token_id = self.tokenizer.pad_token_id
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata, = self.tokenized_data
assert len(dpr_passages) == len(input_ids) == len(attention_mask) == len(metadata)
assert len(decoder_input_ids) == len(decoder_attention_mask) == metadata[-1][-1]
def _included(tokens, curr_input_ids, end_of_answer_prompt):
is_answer_exist = []
for _curr_input_ids in curr_input_ids:
is_exist = False
for jdx in range(end_of_answer_prompt, len(_curr_input_ids)-len(tokens)+1):
if _curr_input_ids[jdx:jdx+len(tokens)]==tokens:
is_exist = True
is_answer_exist.append(is_exist)
return is_answer_exist
new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list, = [], [], [], [], []
for idx, (curr_input_ids, curr_attention_mask, curr_metadata, dpr_ids, ) \
in enumerate(zip(tqdm(input_ids), attention_mask, metadata, dpr_passages,)):
dpr_input_ids = [self.passages.tokenized_data["input_ids"][_id] for _id in dpr_ids]
dpr_attention_mask = [self.passages.tokenized_data["attention_mask"][_id] for _id in dpr_ids]
# create multiple inputs
for answer_idx in range(*curr_metadata):
curr_answer_ids = decoder_input_ids[answer_idx]
end_of_answer = curr_answer_ids.index(eos_token_id) if eos_token_id in curr_answer_ids else len(curr_answer_ids)
answer_input_ids = curr_answer_ids[:end_of_answer] + [eos_token_id]
answer_attention_mask = [1] * len(answer_input_ids)
end_of_answer_prompt = len(answer_input_ids)
ap_input_ids, ap_attention_mask = [], []
for jdx, (_dpr_input_ids, _dpr_attention_mask) in enumerate(zip(dpr_input_ids, dpr_attention_mask)):
end_of_dpr_input = _dpr_attention_mask.index(0) if 0 in _dpr_attention_mask else len(_dpr_attention_mask)
_dpr_input_ids = _dpr_input_ids[:end_of_dpr_input]
_dpr_attention_mask = _dpr_attention_mask[:end_of_dpr_input]
assert _dpr_input_ids[0] == bos_token_id
answer_input_ids_jdx = answer_input_ids + _dpr_input_ids[1:]
answer_attention_mask_jdx = answer_attention_mask + _dpr_attention_mask[1:]
assert len(answer_input_ids_jdx) == len(answer_attention_mask_jdx)
if len(answer_input_ids_jdx) > 160:
answer_input_ids_jdx = answer_input_ids_jdx[:159] + [eos_token_id]
answer_attention_mask_jdx = answer_attention_mask_jdx[:160]
else:
answer_input_ids_jdx += [pad_token_id for _ in range(32 + 128 - len(answer_input_ids_jdx))]
answer_attention_mask_jdx += [0 for _ in range(32 + 128 - len(answer_attention_mask_jdx))]
ap_input_ids.append(answer_input_ids_jdx)
ap_attention_mask.append(answer_attention_mask_jdx)
assert len(ap_input_ids[jdx]) == len(ap_attention_mask[jdx]) == 160 # here we use 32+128
assert len(ap_input_ids) == len(ap_attention_mask) == 100
curr_is_valid_list = _included(curr_answer_ids[1:end_of_answer], ap_input_ids, end_of_answer_prompt)
new_input_ids.append(ap_input_ids)
new_attention_mask.append(ap_attention_mask)
new_is_valid_list.append(curr_is_valid_list)
new_decoder_input_ids.append(curr_input_ids)
new_decoder_attention_mask.append(curr_attention_mask)
assert len(new_decoder_input_ids) == len(new_decoder_attention_mask) == len(metadata)
assert metadata[-1][-1] == len(new_input_ids) == len(new_attention_mask) == len(new_is_valid_list)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, metadata, new_is_valid_list]
with open(dpr_tokenized_path, "w") as f:
json.dump(self.tokenized_data, f)
self.logger.info("Finish saving tokenized DPR data at {}".format(dpr_tokenized_path))
# if self.is_training:
# exit()
aq_psgs_input_ids, aq_psgs_attention_mask, _, _, _, aq_psgs_discard = self.tokenized_data
self.tokenized_data[0] = [_aq_psgs_input_ids[:self.args.top_k_passages] for _aq_psgs_input_ids in aq_psgs_input_ids]
self.tokenized_data[1] = [_aq_psgs_attention_mask[:self.args.top_k_passages] for _aq_psgs_attention_mask in aq_psgs_attention_mask]
self.tokenized_data[5] = [_aq_psgs_discard[:self.args.top_k_passages] for _aq_psgs_discard in aq_psgs_discard]
if self.is_training and self.args.discard_not_found_answers:
raise NotImplementedError
self.tokenized_data = self.tokenized_data[:5]
def load_dpr_data_bart_filter_nfa_psgs(self, dpr_retrieval_path, dpr_tokenized_path):
self.logger.info("{}\n{}".format(dpr_retrieval_path, dpr_tokenized_path))
if os.path.exists(dpr_tokenized_path):
self.logger.info("Loading DPR data from {}".format(dpr_tokenized_path))
with open(dpr_tokenized_path, "r") as f:
self.tokenized_data = json.load(f)
else:
self.logger.info("Start processing DPR data")
if self.passages.tokenized_data is None:
self.passages.load_tokenized_data("bart", all=True)
# if "train_for_inference" not in dpr_retrieval_path:
# dpr_retrieval_path = dpr_retrieval_path.replace("train", "train_for_inference")
with open(dpr_retrieval_path, "r") as f:
dpr_passages = json.load(f)
assert len(dpr_passages)==len(self)
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata = self.tokenized_data
assert len(dpr_passages) == len(input_ids) == len(attention_mask) == len(metadata)
bos_token_id = self.tokenizer.bos_token_id
def _included(tokens, curr_input_ids, end_of_answer):
is_answer_exist = []
for _curr_input_ids in curr_input_ids:
is_exist = False
for jdx in range(end_of_answer, len(_curr_input_ids)-len(tokens)+1):
if _curr_input_ids[jdx:jdx+len(tokens)]==tokens:
is_exist = True
is_answer_exist.append(is_exist)
return is_answer_exist
new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list = [], [], [], [], []
for idx, (curr_input_ids, curr_attention_mask, curr_metadata, dpr_ids) in enumerate(zip(
input_ids, attention_mask, metadata, dpr_passages)):
dpr_input_ids = [self.passages.tokenized_data["input_ids"][_id] for _id in dpr_ids]
dpr_attention_mask = [self.passages.tokenized_data["attention_mask"][_id] for _id in dpr_ids]
# create multiple inputs
answer_input_ids_list, answer_attention_mask_list, is_valid_list = [], [], []
for answer_idx in range(*curr_metadata):
end_of_answer = decoder_input_ids[answer_idx].index(self.tokenizer.eos_token_id) + 1
answer_input_ids = decoder_input_ids[answer_idx][:end_of_answer]
answer_attention_mask = decoder_attention_mask[answer_idx][:end_of_answer]
ap_input_ids, ap_attention_mask = [], []
for jdx, (_dpr_input_ids, _dpr_attention_mask) in enumerate(zip(dpr_input_ids, dpr_attention_mask)):
assert _dpr_input_ids[0] == bos_token_id
answer_input_ids_jdx = answer_input_ids + _dpr_input_ids[1:]
answer_attention_mask_jdx = answer_attention_mask + _dpr_attention_mask[1:]
assert len(answer_input_ids_jdx) == len(answer_attention_mask_jdx)
answer_input_ids_jdx += [self.tokenizer.pad_token_id for _ in range(32 + 128 - len(answer_input_ids_jdx))]
answer_attention_mask_jdx += [0 for _ in range(32 + 128 - len(answer_attention_mask_jdx))]
ap_input_ids.append(answer_input_ids_jdx)
ap_attention_mask.append(answer_attention_mask_jdx)
assert len(ap_input_ids[jdx]) == len(ap_attention_mask[jdx]) == 160 # here we use 32+128
assert len(ap_input_ids) == len(ap_attention_mask) == 100
answer_input_ids_list.append(ap_input_ids)
answer_attention_mask_list.append(ap_attention_mask)
is_valid_list.append(_included(decoder_input_ids[answer_idx][1:end_of_answer - 1], ap_input_ids, end_of_answer))
assert len(answer_input_ids_list) == len(answer_attention_mask_list) == len(is_valid_list) == curr_metadata[1] - curr_metadata[0]
new_decoder_input_ids.append(curr_input_ids)
new_decoder_attention_mask.append(curr_attention_mask)
new_input_ids.append(answer_input_ids_list)
new_attention_mask.append(answer_attention_mask_list)
new_is_valid_list.append(is_valid_list)
assert len(new_decoder_input_ids) == len(new_decoder_attention_mask) == len(new_input_ids) == len(new_attention_mask) == len(new_is_valid_list) == len(self)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list]
with open(dpr_tokenized_path, "w") as f:
json.dump(self.tokenized_data, f)
self.logger.info("Finish saving tokenized DPR data at {}".format(dpr_tokenized_path))
raw_input_ids, raw_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, raw_is_valid_list = self.tokenized_data
if self.args.use_reranker:
assert self.args.psg_sel_dir is not None
psg_sel_fn = os.path.join(self.args.psg_sel_dir,
"{}{}_psg_sel.json".format(
self.data_type.replace("train", "train_for_inference"),
"_20200201" if self.args.wiki_2020 else ""))
self.logger.info("Loading passage selection from DPR reader: {}".format(psg_sel_fn))
with open(psg_sel_fn, "r") as f:
fg_passages = json.load(f)
assert len(fg_passages) == len(raw_input_ids)
ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list = [], [], []
for idx, fg_psgs in enumerate(fg_passages):
# re-order fg_psgs according to containing answers or not (raw_is_valid)
ranked_raw_input_ids_i, ranked_raw_attention_mask_i, ranked_raw_is_valid_list_i = [], [], []
for rid, ratt, rvalid in zip(raw_input_ids[idx], raw_attention_mask[idx], raw_is_valid_list[idx]):
new_fg_psgs_ext, new_fg_psgs_abs = [], []
for i in fg_psgs:
if rvalid[i]:
new_fg_psgs_ext.append(i)
else:
new_fg_psgs_abs.append(i)
new_fg_psgs = new_fg_psgs_ext + new_fg_psgs_abs
ranked_raw_input_ids_i.append([rid[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_attention_mask_i.append([ratt[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_is_valid_list_i.append([rvalid[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_input_ids.append(ranked_raw_input_ids_i)
ranked_raw_attention_mask.append(ranked_raw_attention_mask_i)
ranked_raw_is_valid_list.append(ranked_raw_is_valid_list_i)
else:
ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list = [], [], []
for idx in range(len(raw_input_ids)):
ranked_raw_input_ids_i, ranked_raw_attention_mask_i, ranked_raw_is_valid_list_i = [], [], []
fg_psgs = list(range(len(raw_input_ids[0][0])))
for rid, ratt, rvalid in zip(raw_input_ids[idx], raw_attention_mask[idx], raw_is_valid_list[idx]):
new_fg_psgs_ext, new_fg_psgs_abs = [], []
for i in fg_psgs:
if rvalid[i]:
new_fg_psgs_ext.append(i)
else:
new_fg_psgs_abs.append(i)
new_fg_psgs = new_fg_psgs_ext + new_fg_psgs_abs
ranked_raw_input_ids_i.append([rid[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_attention_mask_i.append([ratt[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_is_valid_list_i.append([rvalid[i] for i in new_fg_psgs][:self.args.top_k_passages])
ranked_raw_input_ids.append(ranked_raw_input_ids_i)
ranked_raw_attention_mask.append(ranked_raw_attention_mask_i)
ranked_raw_is_valid_list.append(ranked_raw_is_valid_list_i)
# prepare for customized training and inference dataset
new_input_ids, new_attention_mask, new_metadata = [], [], []
has_valid = []
if not self.is_training:
old_decoder_input_ids, old_decoder_attention_mask, = new_decoder_input_ids, new_decoder_attention_mask,
new_decoder_input_ids, new_decoder_attention_mask, = [], []
for idx, (rr_input_ids, rr_attention_mask, rr_is_valid_list) in enumerate(zip(ranked_raw_input_ids, ranked_raw_attention_mask, ranked_raw_is_valid_list)):
topk_is_valid_list = [any(rr_is_valid) for rr_is_valid in rr_is_valid_list]
has_valid.append(any(topk_is_valid_list))
if self.is_training:
if self.args.discard_not_found_answers:
if not any(topk_is_valid_list):
topk_is_valid_list = [True for _ in topk_is_valid_list]
new_metadata.append((len(new_input_ids), len(new_input_ids) + sum(topk_is_valid_list)))
new_input_ids += [answer_input_ids for answer_input_ids, is_valid in zip(rr_input_ids, topk_is_valid_list) if is_valid]
new_attention_mask += [answer_attention_mask for answer_attention_mask, is_valid in zip(rr_attention_mask, topk_is_valid_list) if is_valid]
else:
offset = len(new_input_ids)
new_input_ids.extend(rr_input_ids)
new_attention_mask.extend(rr_attention_mask)
new_metadata.append((offset, len(new_input_ids)))
else:
# Yifan: original code only evaluates when gold sample is found, but here we evaluate all qa pairs
# we generate all questions conditioned on all answers, and get the best if the question have multiple answer candidates
offset = len(new_input_ids)
new_input_ids.extend(rr_input_ids)
new_attention_mask.extend(rr_attention_mask)
for i in range(offset, len(new_input_ids)):
new_metadata.append((i,i+1))
new_decoder_input_ids.append(old_decoder_input_ids[idx])
new_decoder_attention_mask.append(old_decoder_attention_mask[idx])
assert len(new_input_ids) == len(new_attention_mask) == new_metadata[-1][-1]
if not self.is_training:
assert len(new_input_ids) == len(new_attention_mask) == len(new_metadata) == len(new_decoder_input_ids) == len(new_decoder_attention_mask)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_metadata]
self.logger.info("%.2f%% questions have at least one answer mentioned in passages" % (100*np.mean(has_valid)))
def load_dataset(self, tokenizer, do_return=False):
if self.tokenized_data is None:
self.load_tokenized_data(tokenizer)
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata = self.tokenized_data
self.dataset = MySimpleQADataset(input_ids,
attention_mask,
decoder_input_ids if self.is_training else None,
decoder_attention_mask if self.is_training else None,
in_metadata=metadata if self.is_training else None,
out_metadata=None,
is_training=self.is_training)
self.logger.info("Loaded {} examples from {} data".format(len(self.dataset), self.data_type))
if do_return:
return self.dataset
def load_dataloader(self, do_return=False, **kwargs):
self.dataloader = MyDataLoader(self.args, self.dataset, is_training=self.is_training, **kwargs)
if do_return:
return self.dataloader
def evaluate(self, predictions, n_paragraphs=None):
# create a reference set
references = []
for i in range(len(self.data)):
ans = self.data[i]["answer"]
ques = self.data[i]["question"]
for _ in ans:
references.append(ques)
assert len(predictions) == len(references), (len(predictions), len(references))
# first, tokenize
data_to_tokenize = {}
for i, (ref, pred, ) in enumerate(zip(references, predictions,)):
data_to_tokenize["ref.{}".format(i)] = [{"caption": ref}]
data_to_tokenize["gen.{}".format(i)] = [{"caption": pred if type(pred) == str else pred[0]}]
if self.args.do_train or self.args.task == 'qg_mask':
all_tokens = {}
for k, v in data_to_tokenize.items():
doc = self.qg_tokenizer(v[0]['caption'])
tkied = [tk.text for tk in doc if tk.text not in PUNCTUATIONS]
all_tokens[k] = [' '.join(tkied)]
else:
all_tokens = self.qg_tokenizer.tokenize(data_to_tokenize)
def _get(key):
return {'sent': [normalize_answer(value) for value in all_tokens[key]]}
bleu_flatten = []
for i in range(len(references)):
reference = {"sent": [normalize_answer(text) for text in all_tokens["ref.{}".format(i)]]}
generated = {"sent": [normalize_answer(text) for text in all_tokens["gen.{}".format(i)]]}
bleu_flatten.append(Bleu(4).compute_score(reference, generated)[0][-1])
bleu, = [],
metadata = self.tokenized_data[-1]
assert metadata[-1][-1] == len(bleu_flatten)
for idx, m in enumerate(metadata):
start, end = m
bleu.append(np.mean(bleu_flatten[start:end]))
assert len(bleu) == len(self.data)
self.logger.info("BLEU=%.2f" % (100 * np.mean(bleu)))
results = {
'BLEU': np.mean(bleu) * 100,
}
return results['BLEU'], results
def save_predictions(self, predictions, mode=''):
# assert len(predictions)==len(self), (len(predictions), len(self))
save_path = os.path.join(self.args.output_dir, "{}{}{}{}_predictions.json".format(
self.data_type if self.args.prefix is None else self.args.prefix,
"_20200201" if self.args.wiki_2020 else "",
"_aq" if self.args.ambigqa else "",
mode,
))
with open(save_path, "w") as f:
json.dump(predictions, f)
self.logger.info("Saved prediction in {}".format(save_path))
class QGMaskedData(QGData):
def __init__(self, logger, args, data_path, is_training, passages=None):
super(QGMaskedData, self).__init__(logger, args, data_path, is_training, passages)
self.SEP = "<SEP>"
self.metric = "EDIT-F1"
self.masked_span_length = [1,2,3,4,5]
self.masked_data_path = data_path.replace('.json', '_mask.json')
if os.path.exists(self.masked_data_path):
with open(self.masked_data_path, "r") as f:
self.data = json.load(f)
else:
for d in self.data:
question = d["question"] if d["question"].endswith("?") else d["question"] + "?"
# generate masked questions
span_length = np.random.choice(self.masked_span_length)
question_spacy = self.qg_tokenizer(question)
question_t = [tk.text_with_ws for tk in question_spacy]
valid_token_idx = [1 if tk.pos_ in VALID_POS else 0 for tk in question_spacy]
valid_start_positions = []
for idx in range(len(valid_token_idx)):
if idx <= len(valid_token_idx) - span_length:
is_valid = False
for jdx in range(idx, idx + span_length):
if valid_token_idx[jdx] == 1:
is_valid = True
break
if is_valid:
valid_start_positions.append(idx)
if len(valid_start_positions) == 0:
span_start = np.random.choice(len(question_t) - span_length - 1) # -1 for not masking the last question mark ?
else:
span_start = np.random.choice(valid_start_positions) # -1 for not masking the last question mark ?
question_t_masked = question_t[:span_start] + question_t[span_start + span_length:]
assert len(question_t_masked) + span_length == len(question_t)
d["question"] = "".join(question_t)
d["prompt"] = "".join(question_t_masked)
with open(self.masked_data_path, "w") as f:
json.dump(self.data, f)
def load_tokenized_data(self, tokenizer):
self.tokenizer = tokenizer
postfix = tokenizer.__class__.__name__.replace("zer", "zed")
assert "Bart" in postfix or "Bert" in postfix or "Albert" in postfix or 'T5' in postfix
preprocessed_path = os.path.join(
"/".join(self.data_path.split("/")[:-1]),
self.data_path.split("/")[-1].replace(
".tsv" if self.data_path.endswith(".tsv") else ".json",
"{}{}{}-masked-{}.json".format(
"-uncased" if self.args.do_lowercase else "",
"-xbos" if self.args.append_another_bos else "",
"-reos" if self.args.t5_no_intermediate_eos else "",
postfix)))
if self.load and os.path.exists(preprocessed_path):
self.logger.info("Loading pre-tokenized data from {}".format(preprocessed_path))
with open(preprocessed_path, "r") as f:
tokenized_data = json.load(f)
else:
print ("Start tokenizing...")
questions = [d["question"] for d in self.data]
prompts = [d["prompt"] for d in self.data]
answers = [d["answer"] for d in self.data]
answers, metadata = self.flatten(answers)
if self.args.bert_name.startswith("t5"):
if self.args.t5_no_intermediate_eos:
questions = ["question: " + question for question in questions]
else:
questions = ["question: " + question + " </s>" for question in questions]
answers = [answer + " </s>" for answer in answers]
if self.args.do_lowercase:
questions = [question.lower() for question in questions]
prompts = [prompt.lower() for prompt in prompts]
answers = [answer.lower() for answer in answers]
question_input = tokenizer.batch_encode_plus(questions,
pad_to_max_length=True,
max_length=32)
question_masked_input = tokenizer.batch_encode_plus(prompts,
pad_to_max_length=True,
max_length=32)
answer_input = tokenizer.batch_encode_plus(answers,
pad_to_max_length="Bart" in postfix or "T5" in postfix,
max_length=20)
input_ids, attention_mask = question_input["input_ids"], question_input["attention_mask"]
input_masked_ids, attention_masked_mask = question_masked_input["input_ids"], question_masked_input["attention_mask"]
decoder_input_ids, decoder_attention_mask = answer_input["input_ids"], answer_input["attention_mask"]
tokenized_data = [input_ids, attention_mask,
decoder_input_ids, decoder_attention_mask, metadata,
input_masked_ids, attention_masked_mask]
if self.load:
with open(preprocessed_path, "w") as f:
json.dump(tokenized_data, f)
self.tokenized_data = tokenized_data
if not self.args.dpr:
self.load_dpr_data()
def load_dpr_data(self):
dpr_retrieval_path = os.path.join(self.args.dpr_data_dir, "{}_predictions.json".format(
self.data_type + "_20200201" if self.args.wiki_2020 else self.data_type)).replace('train_for_inference', 'train')
postfix = self.tokenizer.__class__.__name__.replace("zer", "zed")
dpr_tokenized_path = os.path.join(self.args.reader_data_dir, self.args.predict_file.split("/")[-2],
"{}{}_predictions.json".format(self.data_type, "-reos" if self.args.t5_no_intermediate_eos else "", ))
dpr_tokenized_path = dpr_tokenized_path.replace(".json", "_{}_qg_masked.json".format(postfix))
if "Bart" in postfix:
if not self.args.filter_not_found_answer_passages:
return self.load_dpr_data_bart(dpr_retrieval_path, dpr_tokenized_path)
else:
return self.load_dpr_data_bart_filter_nfa_psgs(dpr_retrieval_path, dpr_tokenized_path)
else:
raise NotImplementedError
def load_dpr_data_bart(self, dpr_retrieval_path, dpr_tokenized_path):
self.logger.info("{}\n{}".format(dpr_retrieval_path, dpr_tokenized_path))
if os.path.exists(dpr_tokenized_path):
self.logger.info("Loading DPR data from {}".format(dpr_tokenized_path))
with open(dpr_tokenized_path, "r") as f:
self.tokenized_data = json.load(f)
else:
self.logger.info("Start processing DPR data")
if self.passages.tokenized_data is None:
self.passages.load_tokenized_data("bart", all=True)
with open(dpr_retrieval_path, "r") as f:
dpr_passages = json.load(f)
assert len(dpr_passages)==len(self)
if self.args.use_reranker:
assert self.args.psg_sel_dir is not None
psg_sel_fn = os.path.join(self.args.psg_sel_dir,
"{}{}_psg_sel.json".format(
self.data_type.replace("train", "train_for_inference"),
"_20200201" if self.args.wiki_2020 else ""))
self.logger.info("Loading passage selection from DPR reader: {}".format(psg_sel_fn))
with open(psg_sel_fn, "r") as f:
fg_passages = json.load(f)
assert len(fg_passages) == len(dpr_passages)
dpr_passages = [[psgs[i] for i in fg_psgs][:100] for psgs, fg_psgs in zip(dpr_passages, fg_passages)]
else:
raise NotImplementedError
bos_token_id = self.tokenizer.bos_token_id
eos_token_id = self.tokenizer.eos_token_id
pad_token_id = self.tokenizer.pad_token_id
sep_token_id = self.tokenizer.convert_tokens_to_ids(self.SEP)
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata, input_masked_ids, attention_masked_mask = self.tokenized_data
assert len(dpr_passages) == len(input_ids) == len(attention_mask) == len(metadata) == len(input_masked_ids) == len(attention_masked_mask)
assert len(decoder_input_ids) == len(decoder_attention_mask) == metadata[-1][-1]
def _included(tokens, curr_input_ids, end_of_answer_prompt):
is_answer_exist = []
for _curr_input_ids in curr_input_ids:
is_exist = False
for jdx in range(end_of_answer_prompt, len(_curr_input_ids)-len(tokens)+1):
if _curr_input_ids[jdx:jdx+len(tokens)]==tokens:
is_exist = True
is_answer_exist.append(is_exist)
return is_answer_exist
new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, new_is_valid_list, = [], [], [], [], []
for idx, (curr_input_ids, curr_attention_mask, curr_metadata, dpr_ids, curr_input_masked_ids, curr_attention_masked_mask) \
in enumerate(zip(tqdm(input_ids), attention_mask, metadata, dpr_passages, input_masked_ids, attention_masked_mask)):
dpr_input_ids = [self.passages.tokenized_data["input_ids"][_id] for _id in dpr_ids]
dpr_attention_mask = [self.passages.tokenized_data["attention_mask"][_id] for _id in dpr_ids]
end_of_masked_question = curr_input_masked_ids.index(eos_token_id) + 1 if eos_token_id in curr_input_masked_ids else len(curr_input_masked_ids)
curr_input_masked_ids = curr_input_masked_ids[:end_of_masked_question]
# create multiple inputs
for answer_idx in range(*curr_metadata):
curr_answer_ids = decoder_input_ids[answer_idx]
end_of_answer = curr_answer_ids.index(eos_token_id) if eos_token_id in curr_answer_ids else len(curr_answer_ids)
answer_input_ids = curr_answer_ids[:end_of_answer] + [sep_token_id] + curr_input_masked_ids[1:]
answer_attention_mask = [1] * len(answer_input_ids)
end_of_answer_prompt = len(answer_input_ids)
ap_input_ids, ap_attention_mask = [], []
for jdx, (_dpr_input_ids, _dpr_attention_mask) in enumerate(zip(dpr_input_ids, dpr_attention_mask)):
end_of_dpr_input = _dpr_attention_mask.index(0) if 0 in _dpr_attention_mask else len(_dpr_attention_mask)
_dpr_input_ids = _dpr_input_ids[:end_of_dpr_input]
_dpr_attention_mask = _dpr_attention_mask[:end_of_dpr_input]
assert _dpr_input_ids[0] == bos_token_id
answer_input_ids_jdx = answer_input_ids + _dpr_input_ids[1:]
answer_attention_mask_jdx = answer_attention_mask + _dpr_attention_mask[1:]
assert len(answer_input_ids_jdx) == len(answer_attention_mask_jdx)
if len(answer_input_ids_jdx) > 160:
answer_input_ids_jdx = answer_input_ids_jdx[:159] + [eos_token_id]
answer_attention_mask_jdx = answer_attention_mask_jdx[:160]
else:
answer_input_ids_jdx += [pad_token_id for _ in range(32 + 128 - len(answer_input_ids_jdx))]
answer_attention_mask_jdx += [0 for _ in range(32 + 128 - len(answer_attention_mask_jdx))]
ap_input_ids.append(answer_input_ids_jdx)
ap_attention_mask.append(answer_attention_mask_jdx)
assert len(ap_input_ids[jdx]) == len(ap_attention_mask[jdx]) == 160 # here we use 32+128
assert len(ap_input_ids) == len(ap_attention_mask) == 100
curr_is_valid_list = _included(curr_answer_ids[1:end_of_answer], ap_input_ids, end_of_answer_prompt)
new_input_ids.append(ap_input_ids)
new_attention_mask.append(ap_attention_mask)
new_is_valid_list.append(curr_is_valid_list)
new_decoder_input_ids.append(curr_input_ids)
new_decoder_attention_mask.append(curr_attention_mask)
assert len(new_decoder_input_ids) == len(new_decoder_attention_mask) == len(metadata)
assert metadata[-1][-1] == len(new_input_ids) == len(new_attention_mask) == len(new_is_valid_list)
self.tokenized_data = [new_input_ids, new_attention_mask, new_decoder_input_ids, new_decoder_attention_mask, metadata, new_is_valid_list]
with open(dpr_tokenized_path, "w") as f:
json.dump(self.tokenized_data, f)
self.logger.info("Finish saving tokenized DPR data at {}".format(dpr_tokenized_path))
# if self.is_training:
# exit()
aq_psgs_input_ids, aq_psgs_attention_mask, _, _, _, aq_psgs_discard = self.tokenized_data
self.tokenized_data[0] = [_aq_psgs_input_ids[:self.args.top_k_passages] for _aq_psgs_input_ids in aq_psgs_input_ids]
self.tokenized_data[1] = [_aq_psgs_attention_mask[:self.args.top_k_passages] for _aq_psgs_attention_mask in aq_psgs_attention_mask]
self.tokenized_data[5] = [_aq_psgs_discard[:self.args.top_k_passages] for _aq_psgs_discard in aq_psgs_discard]
if self.is_training and self.args.discard_not_found_answers:
raise NotImplementedError
self.tokenized_data = self.tokenized_data[:5]
def evaluate(self, predictions, n_paragraphs=None):
# create a reference set
references = []
prompts = []
for i in range(len(self.data)):
ans = self.data[i]["answer"]
ques = self.data[i]["question"]
pmt = self.data[i]["prompt"]
for _ in ans:
references.append(ques)
prompts.append(pmt)
assert len(predictions) == len(references) == len(prompts), (len(predictions), len(references))
# first, tokenize
data_to_tokenize = {}
for i, (ref, pred, pmt) in enumerate(zip(references, predictions, prompts)):
data_to_tokenize["ref.{}".format(i)] = [{"caption": ref}]
data_to_tokenize["pmt.{}".format(i)] = [{"caption": pmt}]
data_to_tokenize["gen.{}".format(i)] = [{"caption": pred if type(pred)==str else pred[0]}]
if self.args.do_train or self.args.task == 'qg_mask':
all_tokens = {}
for k, v in data_to_tokenize.items():
doc = self.qg_tokenizer(v[0]['caption'])
tkied = [tk.text for tk in doc if tk.text not in PUNCTUATIONS]
all_tokens[k] = [' '.join(tkied)]
else:
all_tokens = self.qg_tokenizer.tokenize(data_to_tokenize)
def _get(key):
return {'sent': [normalize_answer(value) for value in all_tokens[key]]}
bleu_flatten, f1s_flatten = [], []
for i in range(len(references)):
e = get_qg_metrics(_get("gen.{}".format(i)),
_get("ref.{}".format(i)),
_get("pmt.{}".format(i)),
metrics=["bleu4", "edit-f1"])
bleu_flatten.append(e["bleu4"])
f1s_flatten.append(e["edit-f1"])
bleu, f1s = [], []
metadata = self.tokenized_data[-1]
assert metadata[-1][-1] == len(bleu_flatten)
for idx, m in enumerate(metadata):
start, end = m
bleu.append(np.mean(bleu_flatten[start:end]))
f1s.append(np.mean(f1s_flatten[start:end]))
assert len(bleu) == len(self.data) == len(f1s)
self.logger.info("BLEU=%.2f, EDIT-F1=%.2f" % (100 * np.mean(bleu), 100 * np.mean(f1s)))
results = {
'BLEU': np.mean(bleu) * 100,
'EDIT-F1': np.mean(f1s) * 100,
}
return results['EDIT-F1'], results
class AmbigQGData(AmbigQAData, QAData):
def __init__(self, logger, args, data_path, is_training, passages=None):
super(AmbigQGData, self).__init__(logger, args, data_path, is_training, passages)
self.ref_questions = []
self.ref_answers = []
if args.ambigqa_editqg:
import spacy
nlp = spacy.load("en_core_web_sm")
# we will only consider questions with multiple answers
for i, d in enumerate(self.data):
if not all([ann["type"]=="multipleQAs" for ann in d["annotations"]]):
self.ref_questions.append(None)
self.ref_answers.append(None)
continue
questions, answers = [], []
for annotation in d["annotations"]:
curr_questions = []
for pair in annotation["qaPairs"]:
curr_q = [q.strip() for q in pair["question"].split("|")]
if is_training:
curr_questions.append([curr_q[0]])
else:
curr_questions.append(curr_q)
if args.ambigqa_editqg:
curr_questions_inserted = []
prompt = nlp(d['question'].lower().strip())
prompt_tkd = [token.text for token in prompt]
for curr_q in curr_questions:
curr_q_inserted = []
for curr_q_i in curr_q:
noamb_q = nlp(curr_q_i.lower().strip())
noamb_q_tkd = [token.text for token in noamb_q]
deletion, insertion = self._get_edits(prompt_tkd, noamb_q_tkd)
if len(insertion) == 0:
if len(deletion) != 0:
curr_q_inserted.append(" ".join(deletion))
else:
curr_q_inserted.append(curr_q_i.lower().strip())
logger.info("DQ == Prompt!")
else:
curr_q_inserted.append(" ".join(insertion))
assert len(curr_q_inserted) == len(curr_q)
curr_questions_inserted.append(curr_q_inserted)
assert len(curr_questions) == len(curr_questions_inserted)
questions.append(curr_questions_inserted)
else:
questions.append(curr_questions)
answers.append([list(set(pair["answer"])) for pair in annotation["qaPairs"]])
assert type(answers)==list and \
all([type(answer)==list for answer in answers]) and \
all([type(_a)==str for answer in answers+questions for _answer in answer for _a in _answer])
self.ref_questions.append(questions)
self.ref_answers.append(answers)
self.SEP = "<SEP>"
self.metric = "EDIT-F1"
if args.do_train or args.is_sagemaker:
import spacy
self.qg_tokenizer = spacy.load("en_core_web_sm")
else:
self.qg_tokenizer = PTBTokenizer()
def _get_edits(self, tokens1, tokens2):
allCommon = []
while True:
commons = list(set(tokens1) & set(tokens2))
if len(commons) == 0:
break
allCommon += commons
for c in commons:
ind1, ind2 = tokens1.index(c), tokens2.index(c)
tokens1 = tokens1[:ind1] + tokens1[ind1 + 1:]
tokens2 = tokens2[:ind2] + tokens2[ind2 + 1:]
original_tokens2 = tokens2
while len(tokens2) > 0 and (tokens2[0] in PUNCT_WORDS or tokens2[0] in IGNORE_WORDS):
tokens2 = tokens2[1:]
while len(tokens2) > 0 and (tokens2[-1] in PUNCT_WORDS or tokens2[-1] in IGNORE_WORDS):
tokens2 = tokens2[:-1]
if len(tokens2) > 0:
return tokens1, tokens2
else:
return tokens1, original_tokens2
# override
def load_dpr_data(self):
dpr_retrieval_path = os.path.join(self.args.dpr_data_dir, "{}{}_predictions.json".format(
self.data_type + "_20200201" if self.args.wiki_2020 else self.data_type,
"_aq" if self.args.ambigqa else "")).replace('train_for_inference', 'train')
postfix = self.tokenizer.__class__.__name__.replace("zer", "zed")
dpr_tokenized_path = os.path.join(self.args.reader_data_dir, self.args.predict_file.split("/")[-2],
"{}{}_predictions.json".format(self.data_type, "-reos" if self.args.t5_no_intermediate_eos else "", ))
dpr_tokenized_path = dpr_tokenized_path.replace(".json", "{}_{}_qg{}.json".format("_20200201" if self.args.wiki_2020 else "", postfix, "_edit" if self.args.ambigqa_editqg else ""))
if "Bart" in postfix:
self.load_dpr_data_bart(dpr_retrieval_path, dpr_tokenized_path)
else:
raise NotImplementedError
# v0: answer [SEP] promptQ </s> passage
def load_dpr_data_bart(self, dpr_retrieval_path, dpr_tokenized_path):
if os.path.exists(dpr_tokenized_path):
self.logger.info("Loading DPR data from {}".format(dpr_tokenized_path))
with open(dpr_tokenized_path, "r") as f:
self.tokenized_data = json.load(f)
else:
self.logger.info("Start processing DPR data from {}".format(dpr_retrieval_path))
if self.passages.tokenized_data is None:
self.passages.load_tokenized_data("bart", all=True)
with open(dpr_retrieval_path, "r") as f:
dpr_passages = json.load(f)
if self.args.use_reranker:
assert self.args.psg_sel_dir is not None
psg_sel_fn = os.path.join(self.args.psg_sel_dir,
"{}{}{}_psg_sel.json".format(self.data_type.replace("train", "train_for_inference"),
"_20200201" if self.args.wiki_2020 else "",
"_aq" if self.args.ambigqa else ""))
self.logger.info("Loading passage selection from DPR reader: {}".format(psg_sel_fn))
with open(psg_sel_fn, "r") as f:
fg_passages = json.load(f)
assert len(fg_passages)==len(dpr_passages)
dpr_passages = [[psgs[i] for i in fg_psgs][:100] for psgs, fg_psgs in zip(dpr_passages, fg_passages)]
else:
raise NotImplementedError
assert len(dpr_passages)==len(self)
input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, metadata = self.tokenized_data
assert len(dpr_passages)==len(input_ids)==len(attention_mask)==len(metadata)
bos_token_id = self.tokenizer.bos_token_id
eos_token_id = self.tokenizer.eos_token_id
pad_token_id = self.tokenizer.pad_token_id
sep_token_id = self.tokenizer.convert_tokens_to_ids(self.SEP)
assert type(bos_token_id)==type(eos_token_id)==type(sep_token_id)==int
def _get_tokenized_answer(idx, append_another_bos=False):
tokens = decoder_input_ids[idx]
# remove padded token
if 0 in decoder_attention_mask[idx]:
tokens = tokens[:decoder_attention_mask[idx].index(0)]
if append_another_bos:
assert tokens[0] == tokens[1] == bos_token_id and tokens[-1] == self.tokenizer.eos_token_id
return tokens[2:-1]
else:
assert tokens[0] == bos_token_id and tokens[-1] == eos_token_id
return tokens[1:-1]
def _included(tokens, psg_input_ids):
is_token_included = []
for _psg_input_ids in psg_input_ids:
is_token_icl = False
for jdx in range(len(_psg_input_ids) - len(tokens) + 1):
if _psg_input_ids[jdx:jdx + len(tokens)] == tokens:
is_token_icl = True
break
is_token_included.append(is_token_icl)
return is_token_included
new_input_ids, new_attention_mask, new_output, new_metadata = [], [], [], []
if self.is_training:
discard_not_found_answers = []
for idx, (curr_input_ids, curr_attention_mask, curr_ref_questions, curr_ref_answers, curr_metadata, dpr_ids) \
in enumerate(zip(tqdm(input_ids), attention_mask, self.ref_questions, self.ref_answers, metadata, dpr_passages)):
if curr_ref_questions is None: