-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
345 lines (285 loc) · 12.8 KB
/
eval.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
#!/usr/bin/python3
# coding=utf-8
import argparse
import functools
import logging
import os
import random
import sys
import re
import time
import numpy as np
import torch
from torch.utils.data import DataLoader, SequentialSampler
from pytorch_transformers import XLNetTokenizer, XLNetLMHeadModel
from eval_utils import CNNDailyMailDataset, \
encode_for_summarization, build_attention_mask, build_perm_mask, build_target_mapping
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
def set_seed(args):
"""Function set seeds for replication purposes
Args:
args: dictionary containing seed passed to by user
"""
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
# ------------
# Load dataset
# ------------
def load_and_cache_examples(data_dir, tokenizer):
"""Loads paths to data to instance of CNNDailyMailDataset
Args:
data_dir (str): path to directory containing data
tokenizer: instance of XLNetTokenizer loaded from XLNet-Base model
Returns:
dataset: instance of CNNDailyMailDataset, holding paths to every story in set in property stories_path
"""
dataset = CNNDailyMailDataset(tokenizer, data_dir=data_dir)
return dataset
def collate(data, tokenizer, args):
"""Function used to transform a batch to model input
Transforms story and summary to input_ids, generates permutation and attention masks and target mapping.
Args:
data (list): contains tuples (story name, story and summary), length of list=batch_size
tokenizer: instance of XLNetTokenizer loaded from XLNet-Base model, needed to encode sequences
args: ditionary containing arguments passed by user, including the maximal sequence length (max_seqlen) and
length of generated summary (sum_len)
Returns:
input_ids: tensor containing input sequence (args.batch_size x args.max_seqlen)
attention_masks: tensor containing attention mask (args.batch_size x args.max_seqlen)
perm_masks: tensor containing permutation mask (args.batch_size x args.max_seqlen x args.max_seqlen)
target_mappings: tensor containing target mapping (args.batch_size x 1 x args.max_seqlen)
storynames: list with story names of batch
"""
# remove the files with empty an story/summary
data = filter(lambda x: not (len(x[1]) == 0 or len(x[2]) == 0), data)
story_names, stories, summaries = zip(*list(data))
# create input_ids of shape (batch_size, seq_len)
input_ids, input_lens = \
zip(*list([encode_for_summarization(story_lines=story, tokenizer=tokenizer, max_seqlen=args.max_seqlen,
sum_len=args.sum_len, prompt=args.prompt)
for (_, story, _) in zip(story_names, stories, summaries)]))
# create perm_masks of shape (batch_size, seq_len, seq_len)
perm_masks = \
torch.cat([build_perm_mask(sum_len=args.sum_len, seq_len=args.max_seqlen, prompt=args.prompt)
for _ in input_ids], dim=0)
# create target_mappings (batch_size, num_predict, seq_len) for first position to be predicted
# num_predict=1 for evaluation (one token predicted at a time)
target_mappings = torch.cat([build_target_mapping(args.max_seqlen, prompt=args.prompt) for _ in input_ids], dim=0)
# create attention_masks (batch_size, seq_len)
attention_masks = torch.cat([build_attention_mask(input_len, args.max_seqlen)
for input_len in input_lens], dim=0)
input_ids = torch.cat(input_ids, dim=0)
return (
input_ids,
attention_masks,
perm_masks,
target_mappings,
story_names
)
# ------------
# Evaluate
# ------------
def evaluate(args, model, tokenizer):
"""Loads data in batches, performs evaluation for each batch and saves generated summaries
For a batch, evaluation is performed by looping over the provided summary length and generating one token at a time.
The generated token replaces the <mask> token in the input_ids and then prediction for next token is performed.
Args:
args: ditionary containing arguments passed by user, including the path to the data folder (args. data_dir),
batch size (args.batch_size), the device (args.device) and more
model: instance of XLNetLMHeadModel, loaded from pretrained XLNet-Base
tokenizer: instance of XLNetTokenizer loaded from XLNet-Base model, needed to encode sequences
"""
set_seed(args)
eval_dataset = load_and_cache_examples(tokenizer=tokenizer, data_dir=args.data_dir)
eval_sampler = SequentialSampler(eval_dataset)
model_collate_fn = functools.partial(collate, tokenizer=tokenizer, args=args)
eval_dataloader = DataLoader(
eval_dataset,
sampler=eval_sampler,
batch_size=args.batch_size,
collate_fn=model_collate_fn,
num_workers=args.num_workers
)
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_dataset))
logger.info(" Batch size = %d", args.batch_size)
model.eval().to(args.device)
for step, batch in enumerate(eval_dataloader):
tic = time.perf_counter()
print("Batch {}".format(step))
input_ids, attention_mask, perm_mask, target_mapping, article_names = batch
# To keep track of the generated sequence
predicted_sequence = torch.zeros((input_ids.shape[0], args.sum_len), dtype=torch.int32)
input_ids = input_ids.to(args.device)
perm_mask = perm_mask.to(args.device)
attention_mask = attention_mask.to(args.device)
for predict_pos in range(args.sum_len):
print("Predicting position {}".format(predict_pos))
if predict_pos > 0:
# for each position a new target_mapping has to be created
target_mapping = torch.cat([build_target_mapping(seq_len=input_ids.shape[1], predict_pos=predict_pos,
prompt=args.prompt)
for _ in range(input_ids.shape[0])], dim=0)
target_mapping = target_mapping.to(args.device)
with torch.no_grad():
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
perm_mask=perm_mask,
target_mapping=target_mapping,
)
# Output has shape [batch_size, num_predict, config.vocab_size],
# num_predict is number of tokens to be predicted, for evaluation: num_predict=1
next_token_logits = outputs[0]
# slightly modified multiplicative repetition penalty from CTRL paper (https://arxiv.org/abs/1909.05858)
if args.repetition_penalty != 1.0:
for i in range(input_ids.shape[0]):
# loop through previously generated tokens
# generated tokens replace <mask> token in input
if args.prompt:
generated_tokens = set(input_ids[i].tolist()[args.prompt:args.prompt + predict_pos])
else:
generated_tokens = set(input_ids[i].tolist()[:predict_pos])
for previous_tokens in generated_tokens:
# if score < 0,
# then repetition penalty has to be multiplied to reduce the previous token probability
if next_token_logits[i, 0, previous_tokens] < 0:
next_token_logits[i, 0, previous_tokens] *= args.repetition_penalty
else:
next_token_logits[i, 0, previous_tokens] /= args.repetition_penalty
# alternative additive penalty
#if args.repetition_penalty != 0.0:
#for i in range(input_ids.shape[0]):
#for previous_tokens in set(input_ids[i].tolist()[:predict_pos]):
#next_token_logits[i, 0, previous_tokens] -= args.repetition_penalty
# choosing candidate token (no beam search)
_, predicted_indices = torch.max(next_token_logits.view(input_ids.shape[0], -1), dim=1, keepdim=True)
for i in range(predicted_indices.shape[0]):
# keep track of prediction
predicted_sequence[i, predict_pos] = int(predicted_indices[i].item())
# replace prediction in input
if args.prompt:
input_ids[i, args.prompt + predict_pos] = predicted_indices[i].item()
else:
input_ids[i, predict_pos] = predicted_indices[i].item()
# saving predicted sequence to file
for i, seq in enumerate(predicted_sequence.tolist()):
if args.prompt:
pref = input_ids.tolist()[i][:args.prompt]
pref_decoded = tokenizer.decode(pref, clean_up_tokenization_spaces=True, skip_special_tokens=True)
seq_decoded = tokenizer.decode(seq, clean_up_tokenization_spaces=True, skip_special_tokens=True)
logger.info("***** Writing prediction for article {}".format(article_names[i]))
chunk = re.match(r".*_data_(.*)", args.data_dir).group(1)
path = os.path.join("test_summaries_{}_wo_penalty".format(chunk), "{}_generated.txt".format(article_names[i]))
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as summary_file:
if args.prompt:
summary_file.write(pref_decoded)
summary_file.write(seq_decoded)
toc = time.perf_counter()
print("Prediction of batch {} took {} seconds".format(step, (toc-tic)))
def main():
"""Main routine, loading model, setting up logging and calling evaluate function"""
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument(
"--data_dir",
default=None,
type=str,
required=True,
help="The input training data file (a text file).",
)
# Optional parameters
parser.add_argument(
"--do_evaluate",
type=bool,
default=False,
help="Run model evaluation on out-of-sample data.",
)
parser.add_argument(
"--model_name_or_path",
default="xlnet-base-cased",
type=str,
help="The model checkpoint to initialize the encoder and decoder's weights with.",
)
parser.add_argument(
"--num_layers",
default=12,
type=int,
help="Number of layers of model to use",
)
parser.add_argument(
"--num_epochs",
default=1,
type=int,
help="Total number of training epochs to perform.",
)
parser.add_argument(
"--batch_size",
default=1,
type=int,
help="Batch size per GPU/CPU for training.",
)
parser.add_argument(
"--num_workers",
default=0,
type=int,
help="Number of workers for data loading",
)
parser.add_argument(
"--max_seqlen",
type=int,
help="Maximal sequence length, longer sentences are truncated",
)
parser.add_argument(
"--sum_len",
type=int,
help="Only for eval mode: length of summary to be generated",
)
parser.add_argument("--seed", default=42, type=int)
parser.add_argument(
"--repetition_penalty",
type=float,
default=1.0,
help="The parameter for repetition penalty. Between 1.0 and + infinity. 1.0 means no penalty. Default to 1.",
)
parser.add_argument(
"--prompt",
type=int,
help="Number of tokens to use as prompt",
)
parser.add_argument("--is_cpu", type=bool, help="Set training to cpu")
parser.add_argument("--is_cuda", type=bool, help="Set training to gpu")
args = parser.parse_args()
# Set up training device
if args.is_cuda:
if torch.cuda.is_available():
args.device = torch.device("cuda")
else:
args.device = torch.device("cpu")
# Load pretrained model and tokenizer
tokenizer = XLNetTokenizer.from_pretrained(args.model_name_or_path)
model = XLNetLMHeadModel.from_pretrained(args.model_name_or_path, n_layer=args.num_layers)
# Setup logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger.warning(
"Device: %s, ",
args.device
)
logger.info("Training/evaluation parameters %s", args)
# Evaluate the model
if args.do_evaluate:
# baseline
logger.info("***** Running Evaluation *****")
if args.model_name_or_path == "xlnet-base-cased":
logger.info("***** Running baseline *****")
evaluate(args, model, tokenizer)
logger.info("***** Evaluation finished *****")
if __name__ == "__main__":
main()