forked from annypan/ilm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_SST2.py
132 lines (109 loc) · 4.38 KB
/
generate_SST2.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
MODEL_DIR = "train_SST2/"
MASK_CLS = 'ilm.mask.hierarchical.MaskHierarchical'
import os
import pickle
import ilm.tokenize_util
tokenizer = ilm.tokenize_util.Tokenizer.GPT2
with open(os.path.join(MODEL_DIR, 'additional_ids_to_tokens.pkl'), 'rb') as f:
additional_ids_to_tokens = pickle.load(f)
additional_tokens_to_ids = {v:k for k, v in additional_ids_to_tokens.items()}
try:
ilm.tokenize_util.update_tokenizer(additional_ids_to_tokens, tokenizer)
except ValueError:
print('Already updated')
print(additional_tokens_to_ids)
# Load model
import torch
from transformers import GPT2LMHeadModel
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GPT2LMHeadModel.from_pretrained(MODEL_DIR)
model.eval()
_ = model.to(device)
context = """
. _ funny _ bad behavior .
""".strip()
context_ids = ilm.tokenize_util.encode(context, tokenizer)
print(context_ids)
# Replace blanks with appropriate tokens from left to right
_blank_id = ilm.tokenize_util.encode(' _', tokenizer)[0]
context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_word|>']
context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_word|>']
#context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_ngram|>']
#context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_sentence|>']
#context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_sentence|>']
#context_ids[context_ids.index(_blank_id)] = additional_tokens_to_ids['<|infill_sentence|>']
print(ilm.tokenize_util.decode(context_ids, tokenizer))
from ilm.infer import infill_with_ilm
generated = infill_with_ilm(
model,
additional_tokens_to_ids,
context_ids,
num_infills=10)
for g in generated:
print('-' * 80)
print(ilm.tokenize_util.decode(g, tokenizer))
blankCandidates = []
with open(f"/u/scr/mhahn/PRETRAINED/GLUE/glue_data/SST-2/dev_alternatives_c_sentBreak_new_finetuned_large.tsv", "r") as inFile:
for line in inFile:
if line.startswith("####"):
next(inFile)
tokenized = next(inFile).strip().split(" ")
print("TOK", tokenized)
line = next(inFile)
if len(line) < 3:
continue
try:
mask, sampled = line.strip().split("\t")
except ValueError:
continue
sampled = sampled.strip().split(" ")
mask = mask.strip()
assert len(sampled) == len(mask), (sampled, mask)
masked = [sampled[i] if mask[i] == "0" else "[MASK]" for i in range(len(mask))]
# print(mask)
# print(masked)
# print(tokenized)
masked = "".join(masked).replace("▁", " ").replace("[MASK]", " _ ").replace(" ", " ").replace("</s>", "").strip()
# print(masked)
blankCandidates.append((" ".join(tokenized), mask, masked))
# if len(blankCandidates) > 1000:
# quit()
queue = []
processed = set()
with open(f"/u/scr/mhahn/PRETRAINED/GLUE/glue_data/SST-2/dev_alternatives_ILM.tsv", "w") as outFile:
for tokenized, mask, masked in blankCandidates:
if (tokenized, mask, masked) in processed:
continue
processed.add((tokenized, mask, masked))
# print(masked)
context = masked
if context[0] == "_":
context = " "+context
_blank_id = ilm.tokenize_util.encode(' _', tokenizer)[0]
context_ids = ilm.tokenize_util.encode(context, tokenizer)
# print(context_ids)
i = 0
while i < len(context_ids):
if context_ids[i] == _blank_id:
print(i)
for j in range(i, len(context_ids)):
if context_ids[j] != _blank_id:
break
#print(j)
if j - i < 2:
context_ids[i] = additional_tokens_to_ids['<|infill_word|>']
else:
context_ids[i] = additional_tokens_to_ids['<|infill_ngram|>']
context_ids = context_ids[:i+1] + context_ids[j:]
i+=1
print(ilm.tokenize_util.decode(context_ids, tokenizer))
from ilm.infer import infill_with_ilm
generated = infill_with_ilm(
model,
additional_tokens_to_ids,
context_ids,
num_infills=10)
for g in generated:
decoded = ilm.tokenize_util.decode(g, tokenizer)
print(mask, decoded)
print(mask, "\t", tokenized, "\t", decoded, file=outFile)