-
Notifications
You must be signed in to change notification settings - Fork 5
/
inference-lewis.py
163 lines (149 loc) · 5.53 KB
/
inference-lewis.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
import argparse
import torch
from transformers import RobertaForSequenceClassification, RobertaTokenizer
from fairseq.models.bart import BARTModel
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--input_file_path", type=str, metavar="STR")
parser.add_argument("--output_file_path", type=str, default="out_mask.txt")
parser.add_argument(
"--bart-mt-checkpoint-path",
type=str,
help="Path to fine-tuned bart-mt checkpoint",
)
parser.add_argument(
"--bart-mt-data-bin-path",
type=str,
help="Path to fine-tuned bart-mt bin data",
)
parser.add_argument(
"--hf-dump",
type=str,
metavar="STR",
help="Path to RoBERTa-based classifier folder",
)
parser.add_argument(
"--target_label_index",
type=int,
help="Index of the target label (for RoBERTA-classifer-based filtering)",
)
parser.add_argument("--bart_mt_beam_size", default=5, type=int)
parser.add_argument("--bart_mt_top_k", default=5, type=int)
args = parser.parse_args()
if args.bart_mt_beam_size != args.bart_mt_top_k:
raise NotImplementedError
bart = BARTModel.from_pretrained(
"/".join(args.bart_mt_checkpoint_path.split("/")[:-1]),
checkpoint_file=args.bart_mt_checkpoint_path.split("/")[-1],
data_name_or_path=args.bart_mt_data_bin_path,
)
target_idx = args.target_label_index
bart.cuda()
bart.eval()
bart.half()
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
classifier = (
RobertaForSequenceClassification.from_pretrained(
f"{args.hf_dump}/pytorch_model.bin",
config=f"{args.hf_dump}/config.json",
output_attentions=True,
)
.half()
.cuda()
# .eval()
)
count = 1
bsz = 32
with open(args.input_file_path) as source, open(
args.output_file_path, "w"
) as fout:
sline = (
source.readline()
.strip()
.replace("<mask>", " <mask> ")
.replace(" ", " ")
)
slines = [sline]
for sline in source:
if count % bsz == 0:
with torch.no_grad():
hypotheses_batch = bart.sample(
slines,
beam=args.bart_mt_beam_size,
topk=args.bart_mt_top_k,
match_source_len=False,
min_len=3,
)
newb = []
b = tokenizer(
hypotheses_batch, padding=True, return_tensors="pt",
)
total_classifier_output = classifier(
b["input_ids"].cuda(),
attention_mask=b["attention_mask"].cuda(),
)[0]
torch.cuda.empty_cache()
for i in range(
0, len(hypotheses_batch), args.bart_mt_beam_size
):
_bool = False
outputs = hypotheses_batch[i : i + args.bart_mt_beam_size]
classifier_output = total_classifier_output[
i : i + args.bart_mt_beam_size
]
classifier_output = (
classifier_output.argmax(-1).cpu().tolist()
)
for index, label in enumerate(classifier_output):
if label == target_idx:
_bool = True
newb.append(outputs[index])
break
if _bool == False:
newb.append(outputs[0])
for hypothesis in newb:
fout.write(
hypothesis.replace("<mask>", "")
.replace(" ", " ")
.strip()
+ "\n"
)
fout.flush()
slines = []
slines.append(sline.strip())
count += 1
print(count)
if slines != []:
hypotheses_batch = bart.sample(
slines,
beam=args.bart_mt_beam_size,
match_source_len=False,
topk=args.bart_mt_top_k,
min_len=3,
)
newb = []
b = tokenizer(hypotheses_batch, padding=True, return_tensors="pt",)
total_classifier_output = classifier(
b["input_ids"].cuda(), attention_mask=b["attention_mask"].cuda()
)[0]
torch.cuda.empty_cache()
for i in range(0, len(hypotheses_batch), args.bart_mt_beam_size):
_bool = False
outputs = hypotheses_batch[i : i + args.bart_mt_beam_size]
classifier_output = total_classifier_output[
i : i + args.bart_mt_beam_size
]
classifier_output = classifier_output.argmax(-1).cpu().tolist()
for index, label in enumerate(classifier_output):
if label == target_idx:
_bool = True
newb.append(outputs[index])
break
if _bool == False:
newb.append(outputs[0])
for hypothesis in newb:
fout.write(
hypothesis.replace("<mask>", "").replace(" ", " ").strip()
+ "\n"
)
fout.flush()