-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
395 lines (350 loc) · 16.7 KB
/
decoder.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
"""BERT finetuning runner."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import os
import sys
import json
import logging
import argparse
import math
from tqdm import tqdm, trange
import numpy as np
import torch
import random
import pickle
import sys
sys.path.append("./SCRIPT")
from transformers import BertTokenizer, RobertaTokenizer
from s2s_ft.modeling_decoding import BertForSeq2SeqDecoder, BertConfig
from transformers.tokenization_bert import whitespace_tokenize
import s2s_ft.s2s_loader as seq2seq_loader
from s2s_ft.utils import load_and_cache_examples
from transformers import \
BertTokenizer, RobertaTokenizer
from s2s_ft.tokenization_unilm import UnilmTokenizer
from s2s_ft.tokenization_minilm import MinilmTokenizer
import tweetqa_eval
TOKENIZER_CLASSES = {
'bert': BertTokenizer,
'minilm': MinilmTokenizer,
'roberta': RobertaTokenizer,
'unilm': UnilmTokenizer,
}
class WhitespaceTokenizer(object):
def tokenize(self, text):
return whitespace_tokenize(text)
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger(__name__)
def detokenize(tk_list):
r_list = []
for tk in tk_list:
if tk.startswith('##') and len(r_list) > 0:
r_list[-1] = r_list[-1] + tk[2:]
else:
r_list.append(tk)
return r_list
def ascii_print(text):
text = text.encode("ascii", "ignore")
print(text)
def main():
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument("--model_type", default=None, type=str, required=True,
help="Model type selected in the list: " + ", ".join(TOKENIZER_CLASSES.keys()))
parser.add_argument("--model_path", default=None, type=str, required=True,
help="Path to the model checkpoint.")
parser.add_argument("--config_path", default=None, type=str,
help="Path to config.json for the model.")
# tokenizer_name
parser.add_argument("--tokenizer_name", default=None, type=str, required=True,
help="tokenizer name")
parser.add_argument("--max_seq_length", default=512, type=int,
help="The maximum total input sequence length after WordPiece tokenization. \n"
"Sequences longer than this will be truncated, and sequences shorter \n"
"than this will be padded.")
# decoding parameters
parser.add_argument('--fp16', action='store_true',
help="Whether to use 16-bit float precision instead of 32-bit")
parser.add_argument('--amp', action='store_true',
help="Whether to use amp for fp16")
parser.add_argument("--input_file", type=str, help="Input file")
parser.add_argument("--dev_file", type=str, help="dev file")
parser.add_argument('--subset', type=int, default=0,
help="Decode a subset of the input dataset.")
parser.add_argument("--output_file", type=str, help="output file")
parser.add_argument("--split", type=str, default="",
help="Data split (train/val/test).")
parser.add_argument('--tokenized_input', action='store_true',
help="Whether the input is tokenized.")
parser.add_argument('--seed', type=int, default=123,
help="random seed for initialization")
parser.add_argument("--do_lower_case", action='store_true',
help="Set this flag if you are using an uncased model.")
parser.add_argument('--batch_size', type=int, default=4,
help="Batch size for decoding.")
parser.add_argument('--beam_size', type=int, default=1,
help="Beam size for searching")
parser.add_argument('--length_penalty', type=float, default=0,
help="Length penalty for beam search")
parser.add_argument('--forbid_duplicate_ngrams', action='store_true')
parser.add_argument('--forbid_ignore_word', type=str, default=None,
help="Forbid the word during forbid_duplicate_ngrams")
parser.add_argument("--min_len", default=1, type=int)
parser.add_argument('--need_score_traces', action='store_true')
parser.add_argument('--ngram_size', type=int, default=3)
parser.add_argument('--mode', default="s2s",
choices=["s2s", "l2r", "both"])
parser.add_argument('--max_tgt_length', type=int, default=128,
help="maximum length of target sequence")
parser.add_argument('--s2s_special_token', action='store_true',
help="New special tokens ([S2S_SEP]/[S2S_CLS]) of S2S.")
parser.add_argument('--s2s_add_segment', action='store_true',
help="Additional segmental for the encoder of S2S.")
parser.add_argument('--s2s_share_segment', action='store_true',
help="Sharing segment embeddings for the encoder of S2S (used with --s2s_add_segment).")
parser.add_argument('--pos_shift', action='store_true',
help="Using position shift for fine-tuning.")
parser.add_argument("--cache_dir", default=None, type=str,
help="Where do you want to store the pre-trained models downloaded from s3")
parser.add_argument("--do_rule", action='store_true',
help="whether to do rule.")
parser.add_argument("--do_normalize", action='store_true',
help="whether to do_normalize.")
args = parser.parse_args()
if args.need_score_traces and args.beam_size <= 1:
raise ValueError(
"Score trace is only available for beam search with beam size > 1.")
if args.max_tgt_length >= args.max_seq_length - 2:
raise ValueError("Maximum tgt length exceeds max seq length - 2.")
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
if args.seed > 0:
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
else:
random_seed = random.randint(0, 10000)
logger.info("Set random seed as: {}".format(random_seed))
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
if n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
tokenizer = TOKENIZER_CLASSES[args.model_type].from_pretrained(
args.tokenizer_name, do_lower_case=args.do_lower_case,
cache_dir=args.cache_dir if args.cache_dir else None)
if args.model_type == "roberta":
vocab = tokenizer.encoder
else:
vocab = tokenizer.vocab
tokenizer.max_len = args.max_seq_length
config_file = args.config_path if args.config_path else os.path.join(args.model_path, "config.json")
logger.info("Read decoding config from: %s" % config_file)
config = BertConfig.from_json_file(config_file)
bi_uni_pipeline = []
bi_uni_pipeline.append(seq2seq_loader.Preprocess4Seq2seqDecoder(
list(vocab.keys()), tokenizer.convert_tokens_to_ids, args.max_seq_length,
max_tgt_length=args.max_tgt_length, pos_shift=args.pos_shift,
source_type_id=config.source_type_id, target_type_id=config.target_type_id,
cls_token=tokenizer.cls_token, sep_token=tokenizer.sep_token, pad_token=tokenizer.pad_token))
mask_word_id, eos_word_ids, sos_word_id = tokenizer.convert_tokens_to_ids(
[tokenizer.mask_token, tokenizer.sep_token, tokenizer.sep_token])
forbid_ignore_set = None
if args.forbid_ignore_word:
w_list = []
for w in args.forbid_ignore_word.split('|'):
if w.startswith('[') and w.endswith(']'):
w_list.append(w.upper())
else:
w_list.append(w)
forbid_ignore_set = set(tokenizer.convert_tokens_to_ids(w_list))
print(args.model_path)
found_checkpoint_flag = False
for model_recover_path in [args.model_path.strip()]:
logger.info("***** Recover model: %s *****", model_recover_path)
found_checkpoint_flag = True
model = BertForSeq2SeqDecoder.from_pretrained(
model_recover_path, config=config, mask_word_id=mask_word_id, search_beam_size=args.beam_size,
length_penalty=args.length_penalty, eos_id=eos_word_ids, sos_id=sos_word_id,
forbid_duplicate_ngrams=args.forbid_duplicate_ngrams, forbid_ignore_set=forbid_ignore_set,
ngram_size=args.ngram_size, min_len=args.min_len, mode=args.mode,
max_position_embeddings=args.max_seq_length, pos_shift=args.pos_shift,
)
if args.fp16:
model.half()
model.to(device)
if n_gpu > 1:
model = torch.nn.DataParallel(model)
torch.cuda.empty_cache()
model.eval()
next_i = 0
max_src_length = args.max_seq_length - 2 - args.max_tgt_length
to_pred = load_and_cache_examples(
args.input_file, tokenizer, local_rank=-1,
cached_features_file=None, shuffle=False)
input_lines = []
for line in to_pred:
input_lines.append(tokenizer.convert_ids_to_tokens(line["source_ids"])[:max_src_length])
if args.subset > 0:
logger.info("Decoding subset: %d", args.subset)
input_lines = input_lines[:args.subset]
input_lines = sorted(list(enumerate(input_lines)),
key=lambda x: -len(x[1]))
output_lines = [""] * len(input_lines)
output_scores = [""] * len(input_lines)
score_trace_list = [None] * len(input_lines)
total_batch = math.ceil(len(input_lines) / args.batch_size)
with open(args.input_file, "r", encoding="utf-8") as reader:
examples = []
passages = []
for line in reader:
examples.append(json.loads(line))
for example in examples:
passages.append(example["src"])
with tqdm(total=total_batch) as pbar:
batch_count = 0
first_batch = True
while next_i < len(input_lines):
_chunk = input_lines[next_i:next_i + args.batch_size]
buf_id = [x[0] for x in _chunk]
buf = [x[1] for x in _chunk]
next_i += args.batch_size
batch_count += 1
max_a_len = max([len(x) for x in buf])
instances = []
for instance in [(x, max_a_len) for x in buf]:
for proc in bi_uni_pipeline:
instances.append(proc(instance))
with torch.no_grad():
batch = seq2seq_loader.batch_list_to_batch_tensors(
instances)
batch = [
t.to(device) if t is not None else None for t in batch]
input_ids, token_type_ids, position_ids, input_mask, mask_qkv, task_idx = batch
traces, logits = model(input_ids, token_type_ids,
position_ids, input_mask, task_idx=task_idx, mask_qkv=mask_qkv)
if args.beam_size > 1:
traces = {k: v.tolist() for k, v in traces.items()}
output_ids = traces['pred_seq']
else:
output_ids = traces.tolist()
output_logits = logits.tolist()
for i in range(len(buf)):
w_ids = output_ids[i]
w_logit = output_logits[i]
output_buf = tokenizer.convert_ids_to_tokens(w_ids)
output_tokens = []
output_score = []
for t, s in zip(output_buf, w_logit):
if t in (tokenizer.sep_token, tokenizer.pad_token):
break
output_tokens.append(t)
output_score.append(s)
if args.model_type == "roberta":
output_sequence = tokenizer.convert_tokens_to_string(output_tokens)
else:
output_sequence = ' '.join(detokenize(output_tokens))
if '\n' in output_sequence:
output_sequence = " [X_SEP] ".join(output_sequence.split('\n'))
output_lines[buf_id[i]] = output_sequence
if len(output_score):
score = 1
for s in output_score:
score *= s
if args.do_normalize:
score = pow(score, 1/len(output_score))
output_scores[buf_id[i]] = score
# output_scores[buf_id[i]] = pow(score, 1/len(output_score))
else:
output_scores[buf_id[i]] = 0
if first_batch or batch_count % 50 == 0:
logger.info("{} = {}".format(buf_id[i], output_sequence))
if args.need_score_traces:
score_trace_list[buf_id[i]] = {
'scores': traces['scores'][i], 'wids': traces['wids'][i], 'ptrs': traces['ptrs'][i]}
pbar.update(1)
first_batch = False
if args.output_file:
fn_out = args.output_file
else:
fn_out = model_recover_path+'.'+args.split
# with open(ori_path, "r") as f:
# ori_tweets_hashtag_userid = {}
# lst = json.load(f)
# for dct in lst:
# ori_tweets_hashtag_userid[dct["qid"]] = \
# [token if '@' in token or '#' in token for token in dct["Tweet"].split()]
with open(fn_out, "w", encoding="utf-8") as writer, \
open(args.input_file, "r", encoding="utf-8") as reader:
examples = []
qids = []
for line in reader:
examples.append(json.loads(line))
for example in examples:
qids.append(example["qid"])
out = []
for x in range(len(output_lines)):
example = {}
qid = qids[x]
example["qid"] = qid
example["Answer"] = output_lines[x]
example["Logit"] = output_scores[x]
if args.do_rule:
# ori_tweet_hashtag_userid = ori_tweets_hashtag_userid[qid]
example["Answer"] = backprocess(example["Answer"])
out.append(example)
json.dump(out, writer, ensure_ascii=False, sort_keys=False, indent=4, separators=(', ', ': '))
writer.close()
if args.need_score_traces:
with open(fn_out + ".trace.pickle", "wb") as fout_trace:
pickle.dump(
{"version": 0.0, "num_samples": len(input_lines)}, fout_trace)
for x in score_trace_list:
pickle.dump(x, fout_trace)
tweetqa_eval.evaluate(args.dev_file, fn_out)
if not found_checkpoint_flag:
logger.info("Not found the model checkpoint file!")
def backprocess(ans):
ans = ans.strip()
ans = ans.replace(" .", ".")
if ans[ans.find(".")+3:] in ('.', ' '):
ans = ans.replace(". ", ".")
ans = ans.replace("@ ", "")
ans = ans.replace("# ", "")
ans = ans.replace("i ' m", "i'm")
ans = ans.replace("you ' re", "you're")
ans = ans.replace("it ' s", "it's")
ans = ans.replace(" ' s", "'s")
ans = ans.replace(" ,", ",")
if ans[ans.find(",")+2:ans.find(",")+3] in '0123456789':
ans = ans.replace(", ", ",")
ans = ans.replace(" _ ", "_")
ans = ans.replace(" - ", "-")
ans = ans.replace(" - ", "-")
ans = ans.replace(" : ", ":")
ans = ans.replace(" / ", "/")
ans = ans.replace(" ’ s ", "'s")
ans = ans.replace("$ ", "$")
ans = ans.replace(" ' t", "'t")
ans = ans.replace("we ' re", "we're")
ans = ans.replace("f * * *", "f***")
ans = ans.replace("they ' re", "they're")
try:
if ans.find(". ") != -1 and ans[ans.find(". ") + 2] in "0123456789":
ans = ans.replace(". ", ".")
except:
pass
ans = ans.replace(" ’ t", "'t")
ans = ans.replace(" ' ll", "'ll")
ans = ans.replace(" ' ed", "'ed")
ans = ans.replace("r. r.", "r.r.")
return ans
if __name__ == "__main__":
main()