-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphrase_creation_utils.py
More file actions
748 lines (694 loc) · 28.5 KB
/
Copy pathphrase_creation_utils.py
File metadata and controls
748 lines (694 loc) · 28.5 KB
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
'''
This module contains methods for creating phrases from
subject-relationship-object graph edges using preset grammars.
'''
from abc import ABC, abstractmethod
import copy
import json
import os
import random
EOS_TOKEN = "<|endoftext|>"
SUBJECT_TOKEN = "SS"
RELATIONSHIP_TOKEN = "RR"
OBJECT_TOKEN = "OO"
Q_TOKEN = "QQ"
A_TOKEN = "AA"
def all_classes():
return [
SimpleInvertedPhraseCreator,
SimpleTemplatePhraseCreator,
SimpleRepeatsPhraseCreator,
SimpleFinetuningPhraseCreator,
SimpleQuestionPhraseCreator,
SimpleNonsenseGrammarPhraseCreator,
SimpleFinetuningRefusalPhraseCreator,
SimpleFinetuningQARefusalPhraseCreator,
]
def get_all_special_tokens(num_tokens, special_tokens={}):
'''
Iterates through all subclasses of PhraseCreator and collects all special
(grammatical) tokens. This is done for all creators, even ones that are not used,
to avoid a situation where a new pharse creator is used during finetuning, which
requires a retokenization.
:param num_tokens: Number of tokens already spoken for, for assigning numbers to new ones.
:param special_tokens: Already recorded custom tokens.
'''
for klass in all_classes():
i = klass(num_tokens, special_tokens)
num_tokens = i.id_range[1]
special_tokens = i.other_special_tokens
return [num_tokens, special_tokens]
def try_to_load_special_tokens(data_path):
'''
For an already created data directory, load the json containing special tokens and the next token ID.
:param data_path: The path of the dataset from which we want to load the tokens.
'''
special_tokens_path = os.path.join(data_path, "metadata", "other_special_tokens.json")
metadata_path = os.path.join(data_path, "metadata", "metadata.json")
if os.path.isfile(special_tokens_path) and os.path.isfile(metadata_path):
with open(special_tokens_path, "r", encoding="utf-8") as f:
special_tokens = json.load(f)
with open(metadata_path, "r", encoding="utf-8") as f:
next_token_id = json.load(f)["vocab_size"] - 1
return special_tokens, next_token_id
else:
return None, None
class PhraseCreator(ABC):
'''
A PhraseCreator is basically a pre-defined grammar for turning tuples of (subject, relationship, object)
into sentences. All PhraseCreators subclass from this base class.
'''
@abstractmethod
def create_phrase(self, subj_ids, rel_ids, obj_ids):
raise NotImplementedError
@abstractmethod
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
raise NotImplementedError
def other_special_tokens(self):
return self.other_special_tokens
def id_range(self):
return self.id_range
class SimpleTemplatePhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# TEMPLATE = "Person {subject} has an ID{relationship} of ID{object}."
if "W0" in self.other_special_tokens:
W0 = self.other_special_tokens["W0"]
W1 = self.other_special_tokens["W1"]
W3 = self.other_special_tokens["W3"]
last_token = next_token
else:
W0, W1, W3 = [str(x).zfill(4) for x in range(next_token, next_token + 3)]
last_token = next_token + 3
self.other_special_tokens["W0"] = W0
self.other_special_tokens["W1"] = W1
self.other_special_tokens["W3"] = W3
self.subj_idx = 1
self.rel_idx = 5
self.obj_idx = 8
self.word_template = [
SUBJECT_TOKEN,
None,
W0,
W1,
RELATIONSHIP_TOKEN,
None,
W3,
OBJECT_TOKEN,
None,
] # Kind of silly hack: all phrases must start with space
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
phrase[self.obj_idx] = obj_phrase
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template[: self.obj_idx])
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
lin_probe_phrase = " ".join(
phrase[: self.subj_idx + 1]
) # Only go until the subject
return [
(
"default",
" " + " ".join(phrase),
" " + obj_phrase,
lin_probe_phrase,
rel_phrase,
)
]
class SimpleQuestionPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# TEMPLATE = "Q: What {relationship} does {subject} have? A: {object}."
if "WQ1" in self.other_special_tokens:
WQ1 = self.other_special_tokens["WQ1"]
WQ2 = self.other_special_tokens["WQ2"]
WQ3 = self.other_special_tokens["WQ3"]
WQ4 = self.other_special_tokens["WQ4"]
last_token = next_token
else:
WQ1, WQ2, WQ3, WQ4 = [
str(x).zfill(4) for x in range(next_token, next_token + 4)
]
last_token = next_token + 4
self.other_special_tokens["WQ1"] = WQ1
self.other_special_tokens["WQ2"] = WQ2
self.other_special_tokens["WQ3"] = WQ3
self.other_special_tokens["WQ4"] = WQ4
last_token = next_token + 4
self.word_template = [
Q_TOKEN,
WQ1,
RELATIONSHIP_TOKEN,
None,
WQ2,
SUBJECT_TOKEN,
None,
WQ3,
WQ4,
A_TOKEN,
OBJECT_TOKEN,
None,
]
self.subj_idx = 6
self.rel_idx = 3
self.obj_idx = 11
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
phrase[self.obj_idx] = obj_phrase
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(
self.word_template[: self.obj_idx - 1]
) # make it output the "OO" as well.
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([OBJECT_TOKEN] + [str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
lin_probe_phrase = " ".join(
phrase[: self.subj_idx + 1]
) # Only go until the subject
return [
(
"default",
" " + " ".join(phrase),
" " + obj_phrase,
lin_probe_phrase,
rel_phrase,
)
]
class SimpleRepeatsPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# For each of subject, relationship, and object, we prepend 1-3 of the same token and append 0-3.
WS, WR, WO = [str(x).zfill(4) for x in range(next_token, next_token + 3)]
self.max_repeats = (3, 3) # on each side
self.min_repeats = (1, 0)
self.id_range = [next_token, next_token + 3]
self.all_combinations = []
for si in range(self.min_repeats[0], self.max_repeats[0] + 1):
for sj in range(self.min_repeats[1], self.max_repeats[1] + 1):
for ri in range(self.min_repeats[0], self.max_repeats[0] + 1):
for rj in range(self.min_repeats[1], self.max_repeats[1] + 1):
for oi in range(self.min_repeats[0], self.max_repeats[0] + 1):
for oj in range(
self.min_repeats[1], self.max_repeats[1] + 1
):
self.all_combinations.append(
(
[WS] * si
+ [SUBJECT_TOKEN]
+ [None]
+ [WS] * sj
+ [WR] * ri
+ [RELATIONSHIP_TOKEN]
+ [None]
+ [WR] * rj
+ [WO] * oi
+ [OBJECT_TOKEN]
+ [None]
+ [WO] * oj,
(
si + 1,
si + sj + ri + 3,
si + sj + ri + rj + oi + 5,
),
(si, sj, ri, rj, oi, oj),
)
)
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase, token_locs, _ = random.choice(self.all_combinations)
phrase[token_locs[0]] = " ".join([str(x).zfill(4) for x in subj_ids])
phrase[token_locs[1]] = " ".join([str(x).zfill(4) for x in rel_ids])
phrase[token_locs[2]] = " ".join([str(x).zfill(4) for x in obj_ids])
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
all_templates = []
for phrase, token_locs, (si, sj, ri, rj, oi, oj) in self.all_combinations:
if oj > 0:
# avoid creating duplicates
continue
phrase = phrase[: token_locs[2]]
phrase[token_locs[0]] = " ".join([str(x).zfill(4) for x in subj_ids])
phrase[token_locs[1]] = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
lin_probe_phrase = phrase[: token_locs[0] + 1] # Only go until the subject
all_templates.append(
(
f"si{si}_sj{sj}_ri{ri}_rj{rj}_oi_{oi}",
" " + " ".join(phrase),
" " + obj_phrase,
lin_probe_phrase,
" ".join([str(x).zfill(4) for x in rel_ids]),
)
)
return all_templates
class SimpleInvertedPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens, inversion_frac=0) -> None:
self.other_special_tokens = other_special_tokens
self.inversion_frac = inversion_frac
# TEMPLATE = "Person {subject} has an ID{relationship} of ID{object}."
if "W0" in self.other_special_tokens:
W0 = self.other_special_tokens["W0"]
W1 = self.other_special_tokens["W1"]
W2 = self.other_special_tokens["W2"]
W3 = self.other_special_tokens["W3"]
W4 = self.other_special_tokens["W4"]
W5 = self.other_special_tokens["W5"]
W6 = self.other_special_tokens["W6"]
W7 = self.other_special_tokens["W7"]
W8 = self.other_special_tokens["W8"]
W9 = self.other_special_tokens["W9"]
W10 = self.other_special_tokens["W10"]
last_token = next_token
else:
W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10 = [
str(x).zfill(4) for x in range(next_token, next_token + 11)
]
self.other_special_tokens["W0"] = W0
self.other_special_tokens["W1"] = W1
self.other_special_tokens["W2"] = W2
self.other_special_tokens["W3"] = W3
self.other_special_tokens["W4"] = W4
self.other_special_tokens["W5"] = W5
self.other_special_tokens["W6"] = W6
self.other_special_tokens["W7"] = W7
self.other_special_tokens["W8"] = W8
self.other_special_tokens["W9"] = W9
self.other_special_tokens["W10"] = W10
last_token = next_token + 11
# TEMPLATE_SRO="Person {subject}'s ID{relationship} is ID{subject}."
self.default_template = {
"template": [
SUBJECT_TOKEN,
None,
W0,
W1,
RELATIONSHIP_TOKEN,
None,
W2,
OBJECT_TOKEN,
None,
],
"subj_idx": 1,
"rel_idx": 5,
"obj_idx": 8,
}
self.alt_templates = []
# TEMPLATE_SOR="Person {subject} has ID{object} for a ID{relationship}."
self.alt_templates.append(
{
"template": [
SUBJECT_TOKEN,
None,
W0,
OBJECT_TOKEN,
None,
W3,
W4,
RELATIONSHIP_TOKEN,
None,
],
"subj_idx": 1,
"rel_idx": 8,
"obj_idx": 4,
}
)
# TEMPLATE_OSR="ID{object} is Person {subject}'s ID{relationship}."
self.alt_templates.append(
{
"template": [
OBJECT_TOKEN,
None,
W5,
SUBJECT_TOKEN,
None,
W8,
RELATIONSHIP_TOKEN,
None,
],
"subj_idx": 4,
"rel_idx": 7,
"obj_idx": 1,
}
)
# TEMPLATE_RSO="The ID{relationship} of Person {subject} is ID{object}"
self.alt_templates.append(
{
"template": [
W9,
RELATIONSHIP_TOKEN,
None,
W7,
SUBJECT_TOKEN,
None,
W5,
OBJECT_TOKEN,
None,
],
"subj_idx": 5,
"rel_idx": 2,
"obj_idx": 8,
}
)
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids, alt_prob=None):
if self.inversion_frac == 0 or random.uniform(0, 1) > self.inversion_frac:
template = self.default_template
else:
template = random.choice(self.alt_templates)
word_template = template["template"]
phrase = copy.deepcopy(word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[template["subj_idx"]] = subj_phrase
phrase[template["rel_idx"]] = rel_phrase
phrase[template["obj_idx"]] = obj_phrase
if None in phrase:
raise ValueError("something is wrong.")
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
all_templates = []
if self.inversion_frac > 0:
templates = [self.default_template, self.alt_templates[-1]]
else:
templates = [self.default_template]
for i, template in enumerate(templates):
if i == 0:
t_name = "sro"
else:
t_name = "rso"
word_template = template["template"]
phrase = copy.deepcopy(word_template[: template["obj_idx"]])
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[template["subj_idx"]] = subj_phrase
phrase[template["rel_idx"]] = rel_phrase
lin_probe_phrase = " ".join(
phrase[: template["subj_idx"] + 1]
) # Only go until the subject
if None in phrase:
raise ValueError("something is wrong.")
all_templates.append(
(
t_name,
" " + " ".join(phrase),
" " + obj_phrase,
lin_probe_phrase,
rel_phrase,
)
)
return all_templates
class SimpleNonsenseGrammarPhraseCreator(PhraseCreator):
def __init__(
self,
next_token,
other_special_tokens,
inversion_frac=0,
num_random_grammar_tokens=1000,
max_tokens_in_spot=5,
p_token=0.4,
) -> None:
self.inversion_frac = inversion_frac
self.max_tokens_in_spot = max_tokens_in_spot
self.p_token = p_token
self.other_special_tokens = other_special_tokens
self.word_tokens = [
v for k, v in self.other_special_tokens.items() if k.startswith("W")
]
last_token = next_token
for i in range(num_random_grammar_tokens):
if f"W{i}" in self.other_special_tokens:
pass # Nothing to do, we have the token already.
else:
new_token = str(last_token).zfill(4)
last_token += 1
self.other_special_tokens[f"W{i}"] = new_token
self.id_range = [next_token, last_token]
def create_random_token_string(self, fixed_length=None):
if fixed_length is not None:
token_string = random.choices(self.word_tokens, k=fixed_length)
else:
token_string = [
random.choice(self.word_tokens)
] # Always have at least one token.
while (
len(token_string) < self.max_tokens_in_spot
and random.uniform(0, 1) < self.p_token
):
token_string.append(random.choice(self.word_tokens))
return " " + " ".join(token_string)
def create_phrase(self, subj_ids, rel_ids, obj_ids, alt_prob=None):
subj_phrase = " SS " + " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " RR " + " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " OO " + " ".join([str(x).zfill(4) for x in obj_ids])
# Generate some random tokens, then the subject phrase, then some more randoms, then
if random.uniform(0, 1) < self.inversion_frac: # SRO ordering
phrase = (
self.create_random_token_string(fixed_length=None)
+ subj_phrase
+ self.create_random_token_string(fixed_length=None)
+ rel_phrase
+ self.create_random_token_string(fixed_length=None)
+ obj_phrase
)
else: # RSO ordering
phrase = (
self.create_random_token_string(fixed_length=None)
+ rel_phrase
+ self.create_random_token_string(fixed_length=None)
+ subj_phrase
+ self.create_random_token_string(fixed_length=None)
+ obj_phrase
)
return phrase + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
subj_phrase = " SS " + " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " RR " + " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
fixed_length = self.max_tokens_in_spot // 2
return [
(
"sro",
self.create_random_token_string(fixed_length=fixed_length)
+ subj_phrase
+ self.create_random_token_string(fixed_length=fixed_length)
+ rel_phrase
+ self.create_random_token_string(fixed_length=fixed_length)
+ " OO",
obj_phrase,
"",
"",
),
(
"rso",
self.create_random_token_string(fixed_length=fixed_length)
+ rel_phrase
+ self.create_random_token_string(fixed_length=fixed_length)
+ subj_phrase
+ self.create_random_token_string(fixed_length=fixed_length)
+ " OO",
obj_phrase,
"",
"",
),
]
# This is a simple finetuning phrase creator, where we present a new format.
class SimpleFinetuningPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# TEMPLATE = "Q: The {relationship} of {subject} is? A: {object}."
FTWQ1, FTWQ2, FTWQ3 = [
str(x).zfill(4) for x in range(next_token, next_token + 3)
]
last_token = next_token + 3
self.word_template = [
Q_TOKEN,
FTWQ1,
RELATIONSHIP_TOKEN,
None,
FTWQ2,
SUBJECT_TOKEN,
None,
FTWQ3,
A_TOKEN,
OBJECT_TOKEN,
None,
]
self.subj_idx = 6
self.rel_idx = 3
self.obj_idx = 10
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
phrase[self.obj_idx] = obj_phrase
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(
self.word_template[: self.obj_idx - 1]
) # make it output the "OO" as well.
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
obj_phrase = " ".join([OBJECT_TOKEN] + [str(x).zfill(4) for x in obj_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
lin_probe_phrase = " ".join(
phrase[: self.subj_idx + 1]
) # Only go until the subject
return [
(
"default",
" " + " ".join(phrase),
" " + obj_phrase,
lin_probe_phrase,
rel_phrase,
)
]
# This is a phrase creator that's roughly meant to simulate refusing to answer a question, e.g., for safety reasons.
# This is probably most effective when the QA format is also taught during pretraining.
class SimpleFinetuningQARefusalPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# TEMPLATE = "Q: What {relationship} does {subject} have? A: I can't answer that."
if "WQ1" in self.other_special_tokens:
WQ1 = self.other_special_tokens["WQ1"]
WQ2 = self.other_special_tokens["WQ2"]
WQ3 = self.other_special_tokens["WQ3"]
WQ4 = self.other_special_tokens["WQ4"]
last_token = next_token
else:
WQ1, WQ2, WQ3, WQ4 = [
str(x).zfill(4) for x in range(next_token, next_token + 4)
]
self.other_special_tokens["WQ1"] = WQ1
self.other_special_tokens["WQ2"] = WQ2
self.other_special_tokens["WQ3"] = WQ3
self.other_special_tokens["WQ4"] = WQ4
last_token = next_token + 4
# Refusal tokens
# R1, R2, R3 = [str(x).zfill(4) for x in range(last_token, last_token + 3)]
# last_token = last_token + 3
self.word_template = [
Q_TOKEN,
WQ1,
RELATIONSHIP_TOKEN,
None,
WQ2,
SUBJECT_TOKEN,
None,
WQ3,
WQ4,
A_TOKEN,
OBJECT_TOKEN,
OBJECT_TOKEN,
OBJECT_TOKEN,
]
self.subj_idx = 6
self.rel_idx = 3
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template[:10])
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
answer_phrase = copy.deepcopy(self.word_template[-3:])
lin_probe_phrase = " ".join(phrase[: self.subj_idx + 1])
return [
(
"qa_refusal",
" " + " ".join(phrase),
" " + " ".join(answer_phrase),
lin_probe_phrase,
rel_phrase,
)
]
# This is a phrase creator that's roughly meant to simulate refusing to fill in a blank, e.g., for safety reasons.
class SimpleFinetuningRefusalPhraseCreator(PhraseCreator):
def __init__(self, next_token, other_special_tokens) -> None:
self.other_special_tokens = other_special_tokens
# TEMPLATE = "Q: What {relationship} does {subject} have? A: I can't answer that."
if "W0" in self.other_special_tokens:
W0 = self.other_special_tokens["W0"]
W1 = self.other_special_tokens["W1"]
W2 = self.other_special_tokens["W2"]
last_token = next_token
else:
W0, W1, W2 = [str(x).zfill(4) for x in range(next_token, next_token + 3)]
self.other_special_tokens["W0"] = W0
self.other_special_tokens["W1"] = W1
self.other_special_tokens["W2"] = W2
last_token = next_token + 3
# Refusal tokens
# R1, R2, R3 = [str(x).zfill(4) for x in range(last_token, last_token + 3)]
# last_token = last_token + 3
self.word_template = [
SUBJECT_TOKEN,
None,
W0,
W1,
RELATIONSHIP_TOKEN,
None,
W2,
OBJECT_TOKEN,
OBJECT_TOKEN,
OBJECT_TOKEN,
]
self.subj_idx = 1
self.rel_idx = 5
self.id_range = [next_token, last_token]
def create_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template)
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
return " " + " ".join(phrase) + "." + EOS_TOKEN
def create_val_phrase(self, subj_ids, rel_ids, obj_ids):
phrase = copy.deepcopy(self.word_template[:-2])
subj_phrase = " ".join([str(x).zfill(4) for x in subj_ids])
rel_phrase = " ".join([str(x).zfill(4) for x in rel_ids])
phrase[self.subj_idx] = subj_phrase
phrase[self.rel_idx] = rel_phrase
answer_phrase = copy.deepcopy(self.word_template[-2:])
lin_probe_phrase = " ".join(phrase[: self.subj_idx + 1])
return [
(
"refusal",
" " + " ".join(phrase),
" " + " ".join(answer_phrase),
lin_probe_phrase,
rel_phrase,
)
]