-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepro_std.py
executable file
·299 lines (266 loc) · 13.5 KB
/
prepro_std.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
# coding=utf-8
# Copyright (c) Microsoft. All rights reserved.
import yaml
import os
import numpy as np
import argparse
import json
import sys
from data_utils import load_data
from data_utils.task_def import TaskType, DataFormat
from data_utils.log_wrapper import create_logger
from experiments.exp_def import TaskDefs, EncoderModelType
from experiments.squad import squad_utils
from pretrained_models import *
DEBUG_MODE = False
MAX_SEQ_LEN = 512
DOC_STRIDE = 180
MAX_QUERY_LEN = 64
MRC_MAX_SEQ_LEN = 384
logger = create_logger(
__name__,
to_disk=True,
log_file='mt_dnn_data_proc_{}.log'.format(MAX_SEQ_LEN))
def feature_extractor(tokenizer, text_a, text_b=None, max_length=512, model_type=None, enable_padding=False, pad_on_left=False,
pad_token=0,
pad_token_segment_id=0,
mask_padding_with_zero=False): # set mask_padding_with_zero default value as False to keep consistent with original setting
inputs = tokenizer.encode_plus(
text_a,
text_b,
add_special_tokens=True,
max_length=max_length,
)
input_ids, token_type_ids = inputs["input_ids"], inputs["token_type_ids"]
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
attention_mask = [1 if mask_padding_with_zero else 0] * len(input_ids)
# Zero-pad up to the sequence length.
padding_length = max_length - len(input_ids)
if enable_padding:
if pad_on_left:
input_ids = ([pad_token] * padding_length) + input_ids
attention_mask = ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
token_type_ids = ([pad_token_segment_id] * padding_length) + token_type_ids
else:
input_ids = input_ids + ([pad_token] * padding_length)
attention_mask = attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)
token_type_ids = token_type_ids + ([pad_token_segment_id] * padding_length)
assert len(input_ids) == max_length, "Error with input length {} vs {}".format(len(input_ids), max_length)
assert len(attention_mask) == max_length, "Error with input length {} vs {}".format(len(attention_mask), max_length)
assert len(token_type_ids) == max_length, "Error with input length {} vs {}".format(len(token_type_ids), max_length)
if model_type.lower() in ['bert', 'roberta']:
attention_mask = None
if model_type.lower() not in ['distilbert','bert', 'xlnet'] :
token_type_ids = [0] * len(token_type_ids)
return input_ids,attention_mask, token_type_ids # input_ids, input_mask, segment_id
def build_data(data, dump_path, tokenizer, data_format=DataFormat.PremiseOnly,
max_seq_len=MAX_SEQ_LEN, encoderModelType=EncoderModelType.BERT, lab_dict=None):
def build_data_premise_only(
data, dump_path, max_seq_len=MAX_SEQ_LEN, tokenizer=None, encoderModelType=EncoderModelType.BERT):
"""Build data of single sentence tasks
"""
with open(dump_path, 'w', encoding='utf-8') as writer:
for idx, sample in enumerate(data):
ids = sample['uid']
premise = sample['premise']
label = sample['label']
if len(premise) > max_seq_len - 2:
premise = premise[:max_seq_len - 2]
input_ids, input_mask, type_ids = feature_extractor(tokenizer, premise, max_length=max_seq_len, model_type=encoderModelType.name)
features = {
'uid': ids,
'label': label,
'token_id': input_ids,
'type_id': type_ids}
writer.write('{}\n'.format(json.dumps(features)))
def build_data_premise_and_one_hypo(
data, dump_path, max_seq_len=MAX_SEQ_LEN, tokenizer=None, encoderModelType=EncoderModelType.BERT):
"""Build data of sentence pair tasks
"""
with open(dump_path, 'w', encoding='utf-8') as writer:
for idx, sample in enumerate(data):
ids = sample['uid']
premise = sample['premise']
hypothesis = sample['hypothesis']
label = sample['label']
input_ids, input_mask, type_ids = feature_extractor(tokenizer, premise, text_b=hypothesis, max_length=max_seq_len,
model_type=encoderModelType.name)
features = {
'uid': ids,
'label': label,
'token_id': input_ids,
'type_id': type_ids}
writer.write('{}\n'.format(json.dumps(features)))
def build_data_premise_and_multi_hypo(
data, dump_path, max_seq_len=MAX_SEQ_LEN, tokenizer=None, encoderModelType=EncoderModelType.BERT):
"""Build QNLI as a pair-wise ranking task
"""
with open(dump_path, 'w', encoding='utf-8') as writer:
for idx, sample in enumerate(data):
ids = sample['uid']
premise = sample['premise']
hypothesis_list = sample['hypothesis']
label = sample['label']
input_ids_list = []
type_ids_list = []
for hypothesis in hypothesis_list:
input_ids, mask, type_ids = feature_extractor(tokenizer,
premise, hypothesis, max_length=max_seq_len,
model_type=encoderModelType.name)
input_ids_list.append(input_ids)
type_ids_list.append(type_ids)
features = {
'uid': ids,
'label': label,
'token_id': input_ids_list,
'type_id': type_ids_list,
'ruid': sample['ruid'],
'olabel': sample['olabel']}
writer.write('{}\n'.format(json.dumps(features)))
def build_data_sequence(data, dump_path, max_seq_len=MAX_SEQ_LEN, tokenizer=None, encoderModelType=EncoderModelType.BERT, label_mapper=None):
with open(dump_path, 'w', encoding='utf-8') as writer:
for idx, sample in enumerate(data):
ids = sample['uid']
premise = sample['premise']
tokens = []
labels = []
for i, word in enumerate(premise):
subwords = tokenizer.tokenize(word)
tokens.extend(subwords)
for j in range(len(subwords)):
if j == 0:
labels.append(sample['label'][i])
else:
labels.append(label_mapper['X'])
if len(premise) > max_seq_len - 2:
tokens = tokens[:max_seq_len - 2]
labels = labels[:max_seq_len - 2]
label = [label_mapper['CLS']] + labels + [label_mapper['SEP']]
input_ids = tokenizer.convert_tokens_to_ids([tokenizer.cls_token] + tokens + [tokenizer.sep_token])
assert len(label) == len(input_ids)
type_ids = [0] * len(input_ids)
features = {'uid': ids, 'label': label, 'token_id': input_ids, 'type_id': type_ids}
writer.write('{}\n'.format(json.dumps(features)))
def build_data_mrc(data, dump_path, max_seq_len=MRC_MAX_SEQ_LEN, tokenizer=None, label_mapper=None, is_training=True):
with open(dump_path, 'w', encoding='utf-8') as writer:
unique_id = 1000000000 # TODO: this is from BERT, needed to remove it...
for example_index, sample in enumerate(data):
ids = sample['uid']
doc = sample['premise']
query = sample['hypothesis']
label = sample['label']
doc_tokens, cw_map = squad_utils.token_doc(doc)
answer_start, answer_end, answer, is_impossible = squad_utils.parse_squad_label(label)
answer_start_adjusted, answer_end_adjusted = squad_utils.recompute_span(answer, answer_start, cw_map)
is_valid = squad_utils.is_valid_answer(doc_tokens, answer_start_adjusted, answer_end_adjusted, answer)
if not is_valid: continue
"""
TODO --xiaodl: support RoBERTa
"""
feature_list = squad_utils.mrc_feature(tokenizer,
unique_id,
example_index,
query,
doc_tokens,
answer_start_adjusted,
answer_end_adjusted,
is_impossible,
max_seq_len,
MAX_QUERY_LEN,
DOC_STRIDE,
answer_text=answer,
is_training=True)
unique_id += len(feature_list)
for feature in feature_list:
so = json.dumps({'uid': ids,
'token_id' : feature.input_ids,
'mask': feature.input_mask,
'type_id': feature.segment_ids,
'example_index': feature.example_index,
'doc_span_index':feature.doc_span_index,
'tokens': feature.tokens,
'token_to_orig_map': feature.token_to_orig_map,
'token_is_max_context': feature.token_is_max_context,
'start_position': feature.start_position,
'end_position': feature.end_position,
'label': feature.is_impossible,
'doc': doc,
'doc_offset': feature.doc_offset,
'answer': [answer]})
writer.write('{}\n'.format(so))
if data_format == DataFormat.PremiseOnly:
build_data_premise_only(
data,
dump_path,
max_seq_len,
tokenizer,
encoderModelType)
elif data_format == DataFormat.PremiseAndOneHypothesis:
build_data_premise_and_one_hypo(
data, dump_path, max_seq_len, tokenizer, encoderModelType)
elif data_format == DataFormat.PremiseAndMultiHypothesis:
build_data_premise_and_multi_hypo(
data, dump_path, max_seq_len, tokenizer, encoderModelType)
elif data_format == DataFormat.Seqence:
build_data_sequence(data, dump_path, max_seq_len, tokenizer, encoderModelType, lab_dict)
elif data_format == DataFormat.MRC:
build_data_mrc(data, dump_path, max_seq_len, tokenizer, encoderModelType)
else:
raise ValueError(data_format)
def parse_args():
parser = argparse.ArgumentParser(
description='Preprocessing GLUE/SNLI/SciTail dataset.')
parser.add_argument('--model', type=str, default='bert-base-uncased',
help='support all BERT, XLNET and ROBERTA family supported by HuggingFace Transformers')
parser.add_argument('--do_lower_case', action='store_true')
parser.add_argument('--root_dir', type=str, default='data/canonical_data')
parser.add_argument('--task_def', type=str, default="experiments/glue/glue_task_def.yml")
args = parser.parse_args()
return args
def main(args):
# hyper param
do_lower_case = args.do_lower_case
root = args.root_dir
assert os.path.exists(root)
literal_model_type = args.model.split('-')[0].upper()
encoder_model = EncoderModelType[literal_model_type]
literal_model_type = literal_model_type.lower()
mt_dnn_suffix = literal_model_type
if 'base' in args.model:
mt_dnn_suffix += "_base"
elif 'large' in args.model:
mt_dnn_suffix += "_large"
config_class, model_class, tokenizer_class = MODEL_CLASSES[literal_model_type]
tokenizer = tokenizer_class.from_pretrained(args.model, do_lower_case=do_lower_case)
if 'uncased' in args.model:
mt_dnn_suffix = '{}_uncased'.format(mt_dnn_suffix)
else:
mt_dnn_suffix = '{}_cased'.format(mt_dnn_suffix)
if do_lower_case:
mt_dnn_suffix = '{}_lower'.format(mt_dnn_suffix)
mt_dnn_root = os.path.join(root, mt_dnn_suffix)
if not os.path.isdir(mt_dnn_root):
os.mkdir(mt_dnn_root)
task_defs = TaskDefs(args.task_def)
for task in task_defs.get_task_names():
task_def = task_defs.get_task_def(task)
logger.info("Task %s" % task)
for split_name in task_def.split_names:
file_path = os.path.join(root, "%s_%s.tsv" % (task, split_name))
if not os.path.exists(file_path):
logger.warning("File %s doesnot exit")
sys.exit(1)
rows = load_data(file_path, task_def)
dump_path = os.path.join(mt_dnn_root, "%s_%s.json" % (task, split_name))
logger.info(dump_path)
build_data(
rows,
dump_path,
tokenizer,
task_def.data_type,
encoderModelType=encoder_model,
lab_dict=task_def.label_vocab)
if __name__ == '__main__':
args = parse_args()
main(args)