-
Notifications
You must be signed in to change notification settings - Fork 327
/
pvp.py
1422 lines (1197 loc) · 55.8 KB
/
pvp.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
# Licensed 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.
"""
This file contains the pattern-verbalizer pairs (PVPs) for all tasks.
"""
import copy
import math
import random
import string
from abc import ABC, abstractmethod
from collections import defaultdict
from typing import Tuple, List, Union, Dict
import numpy as np
from tasks.data_utils import InputExample, num_special_tokens_to_add, build_input_from_ids, build_sample, \
build_decoder_input, build_decoder_sample
from utils import print_rank_0
FilledPattern = Tuple[List[Union[str, Tuple[str, bool]]], List[Union[str, Tuple[str, bool]]]]
class PVP(ABC):
"""
This class contains functions to apply patterns and verbalizers as required by PET. Each task requires its own
custom implementation of a PVP.
"""
def __init__(self, args, tokenizer, label_list, max_seq_length, pattern_id: int = 0, verbalizer_file: str = None,
seed: int = 42, is_multi_token=False, max_segment_length=0, fast_decode: bool = False, split='train',
num_prompt_tokens=0):
"""
Create a new PVP.
:param args: the args
:param tokenizer: the tokenizer
:param label_list: the list of labels
:param max_seq_length: the maximum length of the sequence
:param pattern_id: the pattern id to use
:param seed: a seed to be used for generating random numbers if necessary
:param is_multi_token: if the verbalizers contain multiple tokens
:param fast_decode: whether to use the fast decode mode for multi-token tasks
:param continuous_prompt: whether to use continuous prompt optimization
"""
self.args = args
self.tokenizer = tokenizer
self.label_list = label_list
self.max_seq_length = max_seq_length
self.pattern_id = pattern_id
self.num_prompt_tokens = num_prompt_tokens
self.rng = random.Random(seed)
self.num_truncated = 0
self.fast_decode = fast_decode
self.split = split
self.max_dec_seq_length = 16
self._is_multi_token = is_multi_token
self.max_segment_length = max_segment_length
self.task_mask = args.task_mask
self.continuous_prompt = args.continuous_prompt
self.prefix_prompt = args.prefix_prompt
if self.continuous_prompt:
print_rank_0(f"Prompt tokens in pvp {self.num_prompt_tokens} spell length {self.spell_length}")
if verbalizer_file:
self.verbalize = PVP._load_verbalizer_from_file(verbalizer_file, self.pattern_id)
@property
def is_multi_token(self):
return self._is_multi_token
@property
def spell_length(self):
return 0
@property
def mask(self) -> int:
"""Return the underlying LM's mask token"""
return self.tokenizer.get_command('MASK').Id
@property
def mask_id(self) -> int:
"""Return the underlying LM's mask id"""
return self.tokenizer.get_command('MASK').Id
@property
def max_num_verbalizers(self) -> int:
"""Return the maximum number of verbalizers across all labels"""
return max(len(self.verbalize(label)) for label in self.label_list)
@staticmethod
def shortenable(s):
"""Return an instance of this string that is marked as shortenable"""
return s, True
@staticmethod
def remove_final_punc(s: Union[str, Tuple[str, bool]]):
"""Remove the final punctuation mark"""
if isinstance(s, tuple):
return PVP.remove_final_punc(s[0]), s[1]
return s.rstrip(string.punctuation)
@staticmethod
def lowercase_first(s: Union[str, Tuple[str, bool]]):
"""Lowercase the first character"""
if isinstance(s, tuple):
return PVP.lowercase_first(s[0]), s[1]
return s[0].lower() + s[1:]
@staticmethod
def uppercase_first(s: Union[str, Tuple[str, bool]]):
"""Lowercase the first character"""
if isinstance(s, tuple):
return PVP.uppercase_first(s[0]), s[1]
return s[0].upper() + s[1:]
@staticmethod
def available_patterns():
return [0]
def replace_prompt_tokens(self, parts_a, parts_b):
if not self.continuous_prompt:
parts_a = [part for part in parts_a if part is not None]
parts_b = [part for part in parts_b if part is not None]
return parts_a, parts_b
num_prompt_tokens = self.num_prompt_tokens
num_pos = 0
for parts in (parts_a, parts_b):
for part in parts:
if part is None:
num_pos += 1
avg_prompt_tokens = math.ceil(num_prompt_tokens / num_pos)
new_parts_a, new_parts_b = [], []
for part in parts_a:
if part is None:
if num_prompt_tokens > 0:
if num_prompt_tokens >= avg_prompt_tokens:
new_parts_a.append(avg_prompt_tokens)
num_prompt_tokens -= avg_prompt_tokens
else:
new_parts_a.append(num_prompt_tokens)
num_prompt_tokens = 0
else:
new_parts_a.append(part)
for part in parts_b:
if part is None:
if num_prompt_tokens > 0:
if num_prompt_tokens >= avg_prompt_tokens:
new_parts_b.append(avg_prompt_tokens)
num_prompt_tokens -= avg_prompt_tokens
else:
new_parts_b.append(num_prompt_tokens)
num_prompt_tokens = 0
else:
new_parts_b.append(part)
return new_parts_a, new_parts_b
def encode(self, example: InputExample, priming: bool = False, labeled: bool = False):
"""
Encode an input example using this pattern-verbalizer pair.
:param example: the input example to encode
:param priming: whether to use this example for priming
:param labeled: if ``priming=True``, whether the label should be appended to this example
:return: A tuple, consisting of a list of input ids and a list of token type ids
"""
if not priming:
assert not labeled, "'labeled' can only be set to true if 'priming' is also set to true"
tokenizer = self.tokenizer
raw_parts_a, raw_parts_b = self.get_parts(example)
raw_parts_a = [x if isinstance(x, tuple) else (x, False) for x in raw_parts_a]
prompt_id = tokenizer.num_tokens
def encode_input(raw_parts):
parts = []
for x, s in raw_parts:
if isinstance(x, str):
x = tokenizer.EncodeAsIds(x)
elif isinstance(x, int):
x = [prompt_id] * x
else:
pass
parts.append((x, s))
return parts
parts_a = encode_input(raw_parts_a)
if self.prefix_prompt > 0:
parts_a = [([prompt_id] * self.prefix_prompt, False)] + parts_a
parts_b = None
if raw_parts_b:
raw_parts_b = [x if isinstance(x, tuple) else (x, False) for x in raw_parts_b]
parts_b = encode_input(raw_parts_b)
if self.is_multi_token:
answers = self.get_answers(example)
if example.label is not None:
label = self.label_list.index(example.label)
else:
label = 0
if not self.fast_decode:
ids_list, positions_list, sep_list, mask_list, target_list, prompt_list = [], [], [], [], [], []
segment_id_list = []
if priming:
answer = answers[label]
answer_ids = get_verbalization_ids(answer, tokenizer, force_single_token=False)
self.num_truncated += self.truncate(parts_a, parts_b, answer_ids, max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in parts_a for token_id in part]
tokens_b = [token_id for part, _ in parts_b for token_id in part] if parts_b else None
input_ids = tokens_a
if tokens_b:
input_ids += tokens_b
if labeled:
mask_idx = input_ids.index(self.mask_id)
input_ids = input_ids[:mask_idx] + answer_ids + input_ids[mask_idx + 1:]
return input_ids
else:
for idx, answer in enumerate(answers):
this_parts_a, this_parts_b = copy.deepcopy(parts_a), copy.deepcopy(parts_b)
answer_ids = get_verbalization_ids(answer, tokenizer, force_single_token=False)
answer_ids = answer_ids + [tokenizer.get_command('eop').Id]
self.num_truncated += self.truncate(this_parts_a, this_parts_b, answer_ids,
max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in this_parts_a for token_id in part]
tokens_b = [token_id for part, _ in this_parts_b for token_id in part] if parts_b else None
if self.max_segment_length > 0:
num_segments = (len(answer_ids) - 1) // self.max_segment_length + 1
segments = [
answer_ids[index * self.max_segment_length: (index + 1) * self.max_segment_length]
for
index in range(num_segments)]
segment_id_list += [idx] * len(segments)
else:
segments = [answer_ids]
for segment in segments:
data = build_input_from_ids(tokens_a, tokens_b, segment, self.max_seq_length,
self.tokenizer,
args=self.args, add_cls=True, add_sep=False, add_piece=True,
mask_id=self.mask_id)
ids, types, paddings, position_ids, sep, target_ids, loss_masks = data
prompt_pos = [idx for idx, token in enumerate(ids) if token == prompt_id]
ids = [idx if idx != prompt_id else 0 for idx in ids]
prompt_list.append(prompt_pos)
ids_list.append(ids)
positions_list.append(position_ids)
sep_list.append(sep)
target_list.append(target_ids)
mask_list.append(loss_masks)
if self.mask in tokens_a:
mask_pos = tokens_a.index(self.mask)
tokens_a = tokens_a[:mask_pos] + segment + tokens_a[mask_pos:]
else:
mask_pos = tokens_b.index(self.mask)
tokens_b = tokens_b[:mask_pos] + segment + tokens_b[mask_pos:]
segment_id_list = segment_id_list if segment_id_list else None
sample = build_sample(ids_list, positions=positions_list, masks=sep_list, label=label,
logit_mask=mask_list, target=target_list,
unique_id=example.guid, segment_ids=segment_id_list, prompt_ids=prompt_list)
return sample
else:
this_parts_a, this_parts_b = copy.deepcopy(parts_a), copy.deepcopy(parts_b)
self.num_truncated += self.truncate(this_parts_a, this_parts_b, None, max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in this_parts_a for token_id in part]
tokens_b = [token_id for part, _ in this_parts_b for token_id in part] if parts_b else None
data = build_input_from_ids(tokens_a, tokens_b, None, self.max_seq_length, self.tokenizer,
args=self.args, add_cls=True, add_sep=False, add_piece=False)
ids, types, paddings, position_ids, sep, target_ids, loss_masks = data
sample = build_sample(ids, positions=position_ids, masks=sep, label=label, unique_id=example.guid)
ids_list, positions_list, mask_list, target_list, logit_mask_list = [], [], [], [], []
for answer in answers:
answer_ids = get_verbalization_ids(answer, tokenizer, force_single_token=False)
answer_ids = answer_ids + [tokenizer.get_command('eop').Id]
answer_ids = answer_ids[:self.max_dec_seq_length]
data = build_decoder_input(ids, answer_ids, self.max_seq_length, self.max_dec_seq_length, tokenizer)
dec_ids, _, _, dec_position_ids, _, dec_target_ids, dec_loss_masks = data
ids_list.append(dec_ids)
positions_list.append(dec_position_ids)
mask_list.append(sep)
target_list.append(dec_target_ids)
logit_mask_list.append(dec_loss_masks)
sample = build_decoder_sample(sample, ids_list, positions_list, mask_list, target_list, logit_mask_list)
return sample
else:
self.num_truncated += self.truncate(parts_a, parts_b, [], max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in parts_a for token_id in part]
tokens_b = [token_id for part, _ in parts_b for token_id in part] if parts_b else None
if priming:
input_ids = tokens_a
if tokens_b:
input_ids += tokens_b
if labeled:
mask_idx = input_ids.index(self.mask_id)
verbalizer = self.verbalize(example.label)
assert len(verbalizer) == 1, 'priming only supports one verbalization per label'
verbalizer = verbalizer[0]
verbalizer_id = get_verbalization_ids(verbalizer, self.tokenizer, force_single_token=True)
input_ids[mask_idx] = verbalizer_id
return input_ids
data = build_input_from_ids(tokens_a, tokens_b, None, self.max_seq_length, self.tokenizer, args=self.args,
add_cls=True, add_sep=False, add_piece=True)
ids, types, paddings, position_ids, sep, target_ids, loss_masks = data
prompt_pos = [idx for idx, token in enumerate(ids) if token == prompt_id]
ids = [token if token != prompt_id else 0 for token in ids]
target_ids = self.get_verbalizer_ids()
if example.label is not None:
label = self.label_list.index(example.label)
else:
label = 0
sample = build_sample(ids=ids, positions=position_ids, target=target_ids, masks=sep, logit_mask=loss_masks,
label=label, unique_id=example.guid, prompt_ids=prompt_pos)
return sample
@staticmethod
def _seq_length(parts: List[Tuple[List[int], bool]], only_shortenable: bool = False):
return sum([len(x) for x, shortenable in parts if not only_shortenable or shortenable]) if parts else 0
@staticmethod
def _remove_last(parts: List[Tuple[List[int], bool]]):
last_idx = max(idx for idx, (seq, shortenable) in enumerate(parts) if shortenable and seq)
parts[last_idx] = (parts[last_idx][0][:-1], parts[last_idx][1])
def truncate(self, parts_a: List[Tuple[List[int], bool]], parts_b: List[Tuple[List[int], bool]], answer: List[int],
max_length: int):
"""Truncate two sequences of text to a predefined total maximum length"""
total_len = self._seq_length(parts_a) + self._seq_length(parts_b)
if answer:
total_len += len(answer)
total_len += num_special_tokens_to_add(parts_a, parts_b, answer, add_cls=True, add_sep=False, add_piece=True)
num_tokens_to_remove = total_len - max_length
if num_tokens_to_remove <= 0:
return False
for _ in range(num_tokens_to_remove):
if self._seq_length(parts_a, only_shortenable=True) > self._seq_length(parts_b, only_shortenable=True):
self._remove_last(parts_a)
else:
self._remove_last(parts_b)
return True
@abstractmethod
def get_parts(self, example: InputExample) -> FilledPattern:
"""
Given an input example, apply a pattern to obtain two text sequences (text_a and text_b) containing exactly one
mask token (or one consecutive sequence of mask tokens for PET with multiple masks). If a task requires only a
single sequence of text, the second sequence should be an empty list.
:param example: the input example to process
:return: Two sequences of text. All text segments can optionally be marked as being shortenable.
"""
pass
def get_answers(self, example: InputExample):
return [self.verbalize(label)[0] for label in self.label_list]
def get_verbalizer_ids(self):
target_ids = []
for label in self.label_list:
verbalizer = self.verbalize(label)[0]
verbalizer_id = get_verbalization_ids(verbalizer, self.tokenizer, force_single_token=True)
target_ids.append(verbalizer_id)
return target_ids
@abstractmethod
def verbalize(self, label) -> List[str]:
"""
Return all verbalizations for a given label.
:param label: the label
:return: the list of verbalizations
"""
pass
def get_mask_positions(self, input_ids: List[int]) -> List[int]:
label_idx = input_ids.index(self.mask_id)
labels = [-1] * len(input_ids)
labels[label_idx] = 1
return labels
@staticmethod
def _load_verbalizer_from_file(path: str, pattern_id: int):
verbalizers = defaultdict(dict) # type: Dict[int, Dict[str, List[str]]]
current_pattern_id = None
with open(path, 'r') as fh:
for line in fh.read().splitlines():
if line.isdigit():
current_pattern_id = int(line)
elif line:
label, *realizations = line.split()
verbalizers[current_pattern_id][label] = realizations
print_rank_0("Automatically loaded the following verbalizer: \n {}".format(verbalizers[pattern_id]))
def verbalize(label) -> List[str]:
return verbalizers[pattern_id][label]
return verbalize
class CopaPVP(PVP):
@staticmethod
def available_patterns():
return [0, 1]
@property
def is_multi_token(self):
return True
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
@property
def mask(self) -> str:
"""Return the underlying LM's mask token"""
mask_token = 'MASK'
return self.tokenizer.get_command(mask_token).Id
@property
def mask_id(self) -> int:
"""Return the underlying LM's mask id"""
mask_token = 'MASK'
return self.tokenizer.get_command(mask_token).Id
def get_answers(self, example: InputExample):
choice1 = " " + self.remove_final_punc(self.lowercase_first(example.meta['choice1']))
choice2 = " " + self.remove_final_punc(self.lowercase_first(example.meta['choice2']))
return [choice1, choice2]
def get_parts(self, example: InputExample) -> FilledPattern:
assert self.pattern_id in [0, 1, 2, 3]
premise = self.remove_final_punc(self.shortenable(" " + example.text_a))
choice1 = self.remove_final_punc(self.lowercase_first(example.meta['choice1']))
choice2 = self.remove_final_punc(self.lowercase_first(example.meta['choice2']))
question = example.meta['question']
assert question in ['cause', 'effect']
if question == 'cause':
joiner = ' because'
else:
joiner = ', so'
if self.pattern_id == 0:
parts_a, parts_b = [None, '"', choice1, '" or "', choice2, '"?', None, premise, joiner, None, [self.mask],
'.'], []
elif self.pattern_id == 1:
parts_a, parts_b = [None, choice1, ' or', " " + choice2, '?', None, premise, joiner, None, [self.mask],
'.'], []
elif self.pattern_id == 2:
parts_a, parts_b = [None, '"', choice1, '" or "', choice2, '"', None, premise, joiner, [self.mask], '.',
None], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def verbalize(self, label) -> List[str]:
return []
def encode(self, example: InputExample, priming: bool = False, labeled: bool = False):
"""
Encode an input example using this pattern-verbalizer pair.
:param example: the input example to encode
:param priming: whether to use this example for priming
:param labeled: if ``priming=True``, whether the label should be appended to this example
:return: A tuple, consisting of a list of input ids and a list of token type ids
"""
if self.continuous_prompt or self.pattern_id < 2:
return super().encode(example, priming=priming, labeled=labeled)
if not priming:
assert not labeled, "'labeled' can only be set to true if 'priming' is also set to true"
tokenizer = self.tokenizer
premise = self.remove_final_punc(self.shortenable(example.text_a))
choice1 = " " + self.remove_final_punc(self.lowercase_first(example.meta['choice1']))
choice2 = " " + self.remove_final_punc(self.lowercase_first(example.meta['choice2']))
question = example.meta['question']
assert question in ['cause', 'effect']
answer = " because" if question == 'cause' else " so"
answer_ids = [get_verbalization_ids(answer, tokenizer, force_single_token=True)]
if self.is_multi_token:
answer_ids.append(tokenizer.get_command('eop').Id)
ids_list, positions_list, sep_list, mask_list, target_list = [], [], [], [], []
for choice in [choice1, choice2]:
parts = ['"', choice1[1:], '" or "', choice2[1:], '"?', premise, [self.mask], choice]
parts = [x if isinstance(x, tuple) else (x, False) for x in parts]
parts = [(tokenizer.EncodeAsIds(x).tokenization if isinstance(x, str) else x, s) for x, s in parts if
x]
self.num_truncated += self.truncate(parts, None, answer_ids, max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in parts for token_id in part]
data = build_input_from_ids(tokens_a, None, answer_ids, self.max_seq_length, self.tokenizer, args=self.args,
add_cls=True, add_sep=False, add_piece=True)
ids, types, paddings, position_ids, sep, target_ids, loss_masks = data
ids_list.append(ids)
positions_list.append(position_ids)
sep_list.append(sep)
target_list.append(target_ids)
mask_list.append(loss_masks)
if example.label is not None:
label = self.label_list.index(example.label)
else:
label = 0
sample = build_sample(ids_list, positions=positions_list, masks=sep_list, label=label,
logit_mask=mask_list, target=target_list,
unique_id=example.guid)
return sample
class WscPVP(PVP):
@staticmethod
def available_patterns():
return [0, 1, 2]
@property
def is_multi_token(self):
return True
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
def get_answers(self, example: InputExample):
target = " " + example.meta['span1_text']
answers = [target]
if 'candidates' in example.meta:
candidates = example.meta['candidates']
# if len(candidates) > 10:
# random.shuffle(candidates)
# candidates = candidates[:10]
answers += [" " + cand for cand in candidates]
return answers
def get_parts(self, example: InputExample) -> FilledPattern:
pronoun = example.meta['span2_text']
pronoun_idx = example.meta['span2_index']
words_a = example.text_a.split()
words_a[pronoun_idx] = '*' + words_a[pronoun_idx] + '*'
text_a = ' '.join(words_a)
text_a = self.shortenable(text_a)
if self.pattern_id == 0:
parts_a, parts_b = [None, text_a, None, " The pronoun '*" + pronoun + "*' refers to", None, [self.mask],
'.'], []
elif self.pattern_id == 1:
parts_a, parts_b = [None, text_a, None,
" In the previous sentence, the pronoun '*" + pronoun + "*' refers to", None,
[self.mask], '.'], []
elif self.pattern_id == 2:
parts_a, parts_b = [None, text_a, None,
" Question: In the passage above, what does the pronoun '*" + pronoun + "*' refer to?",
None,
" Answer:", [self.mask], '.'], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def encode(self, example: InputExample, priming: bool = False, labeled: bool = False):
"""
Encode an input example using this pattern-verbalizer pair.
:param example: the input example to encode
:param priming: whether to use this example for priming
:param labeled: if ``priming=True``, whether the label should be appended to this example
:return: A tuple, consisting of a list of input ids and a list of token type ids
"""
if self.args.loss_func in ['generative', 'mix']:
sample = super().encode(example, priming=priming, labeled=labeled)
if self.split == 'train':
sample['label'] = 0
return sample
if not priming:
assert not labeled, "'labeled' can only be set to true if 'priming' is also set to true"
tokenizer = self.tokenizer
prompt_id = tokenizer.num_tokens
raw_parts_a, raw_parts_b = self.get_parts(example)
raw_parts_a = [x if isinstance(x, tuple) else (x, False) for x in raw_parts_a]
def encode_input(raw_parts):
parts = []
for x, s in raw_parts:
if isinstance(x, str):
x = tokenizer.EncodeAsIds(x)
elif isinstance(x, int):
x = [prompt_id] * x
else:
pass
parts.append((x, s))
return parts
parts_a = encode_input(raw_parts_a)
if self.prefix_prompt > 0:
parts_a = [([prompt_id] * self.prefix_prompt, False)] + parts_a
parts_b = None
if raw_parts_b:
raw_parts_b = [x if isinstance(x, tuple) else (x, False) for x in raw_parts_b]
parts_b = encode_input(raw_parts_b)
answer = self.get_answers(example)[0]
answer_ids = get_verbalization_ids(answer, tokenizer, force_single_token=False)
answer_ids = answer_ids + [tokenizer.get_command('eop').Id]
self.num_truncated += self.truncate(parts_a, parts_b, answer_ids, max_length=self.max_seq_length)
tokens_a = [token_id for part, _ in parts_a for token_id in part]
tokens_b = [token_id for part, _ in parts_b for token_id in part] if parts_b else None
data = build_input_from_ids(tokens_a, tokens_b, answer_ids, self.max_seq_length, self.tokenizer, args=self.args,
add_cls=True, add_sep=False, add_piece=True)
ids, types, paddings, position_ids, sep, target_ids, loss_masks = data
prompt_pos = [idx for idx, token in enumerate(ids) if token == prompt_id]
ids = [token if token != prompt_id else 0 for token in ids]
if example.label is not None:
label = self.label_list.index(example.label)
else:
label = 0
return {'text': np.array(ids, dtype=np.int64), 'target': np.array(target_ids, dtype=np.int64),
'attention_mask': np.array(sep, dtype=np.int64), 'loss_mask': np.array(loss_masks, dtype=np.int64),
"position_id": np.array(position_ids, dtype=np.int64),
'prompt_pos': np.array(prompt_pos, dtype=np.int64), 'label': label, 'uid': example.guid}
def verbalize(self, label) -> List[str]:
return []
class RecordPVP(PVP):
@property
def is_multi_token(self):
return True
def get_answers(self, example: InputExample):
choices = example.meta['candidates']
choices = [" " + choice for choice in choices]
return choices
def get_parts(self, example: InputExample) -> FilledPattern:
premise = self.shortenable(example.text_a)
assert '@placeholder' in example.text_b, f'question "{example.text_b}" does not contain a @placeholder token'
question_a, question_b = example.text_b.split('@placeholder')
return [premise, " " + question_a.rstrip(), [self.mask], question_b], []
def verbalize(self, label) -> List[str]:
return []
class RacePVP(PVP):
@property
def is_multi_token(self):
return True
@staticmethod
def available_patterns():
return [0, 1]
def get_answers(self, example: InputExample):
choices = example.meta['choices']
choices = [" " + choice for choice in choices]
return choices
def get_parts(self, example: InputExample) -> FilledPattern:
context = self.shortenable(example.text_a)
question = " " + example.text_b
if "_" in question:
left, right = question.split('_', maxsplit=1)
if self.pattern_id == 0:
return [context], [self.shortenable(left.rstrip()), [self.mask], self.shortenable(right)]
else:
left = left.rstrip()
if left:
left = self.lowercase_first(left)
return [context], [" Based on the previous passage,",
self.shortenable(left), [self.mask],
self.shortenable(right)]
else:
if self.pattern_id == 0:
return [context], [" Question:", self.shortenable(question), " Answer:", [self.mask]]
else:
return [context], [" Based on the previous passage,", self.shortenable(question), [self.mask]]
def verbalize(self, label) -> List[str]:
return []
class RtePVP(PVP):
VERBALIZER = {
"not_entailment": [" No"],
"entailment": [" Yes"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4]
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
def get_parts(self, example: InputExample) -> FilledPattern:
# switch text_a and text_b to get the correct order
text_a = example.text_a
text_b = example.text_b.rstrip(string.punctuation)
if self.pattern_id == 0:
parts_a, parts_b = [None, '"', self.shortenable(text_b), '" ?'], [None, [self.mask], ',', None, ' "',
self.shortenable(text_a), '"']
elif self.pattern_id == 1:
parts_a, parts_b = [None, self.shortenable(text_b), '?'], [None, [self.mask], ',', None,
self.shortenable(" " + text_a)]
elif self.pattern_id == 2:
parts_a, parts_b = [None, '"', self.shortenable(text_b), '" ?'], [None, [self.mask], '. "', None,
self.shortenable(text_a), '"']
elif self.pattern_id == 3:
parts_a, parts_b = [None, self.shortenable(text_b), '?'], [None, [self.mask], '.', None,
self.shortenable(" " + text_a)]
elif self.pattern_id == 4:
parts_a, parts_b = [None, self.shortenable(text_a), None, ' question:', self.shortenable(" " + text_b),
' True or False?', None, ' answer:', [self.mask]], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def verbalize(self, label) -> List[str]:
if self.pattern_id == 4:
return [' true'] if label == 'entailment' else [' false']
return RtePVP.VERBALIZER[label]
class CbPVP(RtePVP):
VERBALIZER = {
"contradiction": [" No"],
"entailment": [" Yes"],
"neutral": [" Maybe"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4]
def get_parts(self, example: InputExample) -> FilledPattern:
if self.pattern_id == 4:
text_a = self.shortenable(example.text_a)
text_b = self.shortenable(" " + example.text_b)
parts_a, parts_b = [None, text_a, None, ' question:', text_b, ' true, false or neither?', None, ' answer:',
[self.mask]], []
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
return super().get_parts(example)
def verbalize(self, label) -> List[str]:
if self.pattern_id == 4:
return [' true'] if label == 'entailment' else [' false'] if label == 'contradiction' else [' neither']
return CbPVP.VERBALIZER[label]
class BoolQPVP(PVP):
VERBALIZER_A = {
"false": [" No"],
"true": [" Yes"]
}
VERBALIZER_B = {
"false": [" false"],
"true": [" true"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4, 5]
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
def get_parts(self, example: InputExample) -> FilledPattern:
passage = example.text_a
question = example.text_b
if self.pattern_id < 2:
parts_a, parts_b = [None, self.shortenable(passage), None, ' Question:', self.shortenable(" " + question),
'? Answer:', None, [self.mask], '.'], []
elif self.pattern_id < 4:
parts_a, parts_b = [None, self.shortenable(passage), ' Based on the previous passage,', None,
self.shortenable(" " + question), '?', None, [self.mask], '.'], []
elif self.pattern_id < 6:
parts_a, parts_b = ['Based on the following passage', None, self.shortenable(" " + question), '?', None,
[self.mask], '.', None, self.shortenable(" " + passage)], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def verbalize(self, label) -> List[str]:
if self.pattern_id == 0 or self.pattern_id == 2 or self.pattern_id == 4:
return BoolQPVP.VERBALIZER_A[label]
else:
return BoolQPVP.VERBALIZER_B[label]
class MultiRcPVP(PVP):
VERBALIZER = {
0: [" No"],
1: [" Yes"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4]
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
def get_parts(self, example: InputExample) -> FilledPattern:
passage = self.remove_final_punc(self.shortenable(example.text_a.rstrip()))
question = self.remove_final_punc(example.text_b.rstrip())
answer = example.meta['answer']
if self.pattern_id == 0:
parts_a, parts_b = [passage, '.', None, ' Question:', " " + question + '?', None, ' Is it', " " + answer,
'?', None, [self.mask], '.'], []
elif self.pattern_id == 1:
parts_a, parts_b = [passage, '.', None, ' Question:', " " + question, '?', None, ' Is the correct answer "',
answer, '"?', None, [self.mask], '.'], []
elif self.pattern_id == 2:
parts_a, parts_b = [passage, '. Based on the previous passage,', None, " " + question, '?', None, ' Is "',
answer, '" a correct answer?', None, [self.mask], '.'], []
elif self.pattern_id == 3:
parts_a, parts_b = [None, passage, None, " " + question, '- [', [self.mask], ']', None, answer], []
elif self.pattern_id == 4:
parts_a, parts_b = [passage, '.', None, ' Question:', " " + question, '?', None, " " + answer, '?', None,
[self.mask], '.'], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def verbalize(self, label) -> List[str]:
if self.pattern_id == 3:
return [' False'] if label == 0 else [' True']
return MultiRcPVP.VERBALIZER[label]
class WicPVP(PVP):
VERBALIZER_A = {
"false": [" No"],
"true": [" Yes"]
}
VERBALIZER_B = {
"false": ["2"],
"true": ["b"]
}
@staticmethod
def available_patterns():
return [0, 1, 2]
@property
def spell_length(self):
return self.num_prompt_tokens + self.prefix_prompt
def get_parts(self, example: InputExample) -> FilledPattern:
text_a = example.text_a
text_b = example.text_b
word = example.meta['word']
if self.pattern_id == 0:
parts_a, parts_b = [None, self.shortenable('"' + text_a + '" / "' + text_b + '"'), None,
' Similar sense of "' + word + '"?', None, [self.mask], '.'], []
elif self.pattern_id == 1:
parts_a, parts_b = [self.shortenable(text_a), None, self.shortenable(" " + text_b), None,
' Does ' + word + ' have the same meaning in both sentences?', None, [self.mask]], []
elif self.pattern_id == 2:
parts_a, parts_b = [None, word, ' .', None, ' Sense (1) (a) "', self.shortenable(text_a), '"', None, ' (',
[self.mask], ') "', text_b, '"'], []
else:
raise NotImplementedError(self.pattern_id)
parts_a, parts_b = self.replace_prompt_tokens(parts_a, parts_b)
return parts_a, parts_b
def verbalize(self, label) -> List[str]:
if self.pattern_id == 2:
return WicPVP.VERBALIZER_B[label]
return WicPVP.VERBALIZER_A[label]
class AgnewsPVP(PVP):
VERBALIZER = {
"1": [" World"],
"2": [" Sports"],
"3": [" Business"],
"4": [" Tech"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4, 5]
def get_parts(self, example: InputExample) -> FilledPattern:
text_a = self.shortenable(example.text_a)
text_b = self.shortenable(example.text_b)
if self.pattern_id == 0:
return [[self.mask], ':', text_a, text_b], []
elif self.pattern_id == 1:
return [[self.mask], ' News:', text_a, text_b], []
elif self.pattern_id == 2:
return [text_a, '(', [self.mask], ')', text_b], []
elif self.pattern_id == 3:
return [text_a, text_b, '(', [self.mask], ')'], []
elif self.pattern_id == 4:
return ['[ Category:', [self.mask], ']', text_a, text_b], []
elif self.pattern_id == 5:
return [[self.mask], '-', text_a, text_b], []
else:
raise ValueError("No pattern implemented for id {}".format(self.pattern_id))
def verbalize(self, label) -> List[str]:
return AgnewsPVP.VERBALIZER[label]
class YahooPVP(PVP):
VERBALIZER = {
"1": [" Society"],
"2": [" Science"],
"3": [" Health"],
"4": [" Education"],
"5": [" Computer"],
"6": [" Sports"],
"7": [" Business"],
"8": [" Entertainment"],
"9": [" Relationship"],
"10": [" Politics"],
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3, 4, 5]
def get_parts(self, example: InputExample) -> FilledPattern:
text_a = self.shortenable(example.text_a)
text_b = self.shortenable(example.text_b)
if self.pattern_id == 0:
return [[self.mask], ':', text_a, text_b], []
elif self.pattern_id == 1:
return [[self.mask], ' Question:', text_a, text_b], []
elif self.pattern_id == 2:
return [text_a, '(', [self.mask], ')', text_b], []
elif self.pattern_id == 3:
return [text_a, text_b, '(', [self.mask], ')'], []
elif self.pattern_id == 4:
return ['[ Category:', [self.mask], ']', text_a, text_b], []
elif self.pattern_id == 5:
return [[self.mask], '-', text_a, text_b], []
else:
raise ValueError("No pattern implemented for id {}".format(self.pattern_id))
def verbalize(self, label) -> List[str]:
return YahooPVP.VERBALIZER[label]
class MnliPVP(PVP):
VERBALIZER_A = {
"contradiction": [" Wrong"],
"entailment": [" Right"],
"neutral": [" Maybe"]
}
VERBALIZER_B = {
"contradiction": [" No"],
"entailment": [" Yes"],
"neutral": [" Maybe"]
}
@staticmethod
def available_patterns():
return [0, 1, 2, 3]