-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cosmosqa.py
343 lines (274 loc) · 11.2 KB
/
data_cosmosqa.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
import torch
from torch.utils.data.dataset import Dataset
from typing import List, Dict, Optional
from transformers import PreTrainedTokenizer
from filelock import FileLock
import logging
from enum import Enum
from dataclasses import dataclass
import os
import jsonlines
from tqdm import tqdm
logger = logging.getLogger(__name__)
class Split(Enum):
train = "train"
dev = "eval"
test = "test"
@dataclass(frozen=True)
class InputFeatures:
question_id: int
input_ids: List[List[int]]
input_mask: Optional[List[int]]
segment_ids: Optional[List[int]]
label: Optional[int]
class CosmosQAData(Dataset):
features: List[InputFeatures]
def __init__(
self,
task: str,
data_dir: str,
evid_dir: str,
tokenizer: PreTrainedTokenizer,
max_seq_length: Optional[int],
mode: Split = Split.train,
overwrite_cache=False,
demo=False
):
self.evid_dir = evid_dir
self.mode = mode
if mode == Split.dev:
self.evids = torch.load(os.path.join(self.evid_dir, "dev_evid_feats.pt"))
elif mode == Split.test:
self.evids = torch.load(os.path.join(self.evid_dir, "test_evid_feats.pt"))
processor = processors[task]()
cached_features_file = os.path.join(
data_dir,
"cached_data",
"cached_{}_{}_{}_{}".format(
mode.value,
tokenizer.__class__.__name__,
str(max_seq_length),
task,
)
)
if demo: cached_features_file += "_demo"
lock_path = cached_features_file + ".lock"
with FileLock(lock_path):
if os.path.exists(cached_features_file) and not overwrite_cache:
logger.info(f"Loading features from cached file {cached_features_file}")
self.features = torch.load(cached_features_file)
else:
logger.info(f"Creating features from dataset file at {data_dir}")
label_list = processor.get_labels()
if mode == Split.dev:
if demo:
examples = processor.get_dev_demo(data_dir)
else:
examples = processor.get_dev_examples(data_dir)
elif mode == Split.test:
examples = processor.get_test_examples(data_dir)
elif mode == Split.train:
if demo:
examples = processor.get_train_demo(data_dir)
else:
examples = processor.get_train_examples(data_dir)
else:
print('mode: {}'.format(mode))
raise Exception()
logger.info("Training examples: %s", len(examples))
self.features = convert_examples_to_features(
examples, tokenizer, max_seq_length
)
logger.info("Saving features into cached file %s", cached_features_file)
torch.save(self.features, cached_features_file)
def __len__(self):
return len(self.features)
def __getitem__(self, i):
item = self.features[i]
qid = item.question_id
# if self.mode == "train":
if self.mode == Split.train:
try:
evid = torch.load(os.path.join(self.evid_dir,
'train_evid_feats',
'{}_evid_feats.pt'.format(qid)
))
evid = evid[0]
except:
print('evid_dir: {} is not available'.format(
os.path.join(self.evid_dir, 'train_evid_feats', '{}_evid_feats.pt'.format(qid))
))
assert 1 == 0
else:
evid = self.evids[qid][0]
if len(evid.shape) == 3: evid = evid[0]
# print('getitem: {}'.format(evid.shape))
return item.input_ids, item.input_mask, item.segment_ids, item.label, evid, None, None
class CosmosQAExample(object):
def __init__(self,
question_id,
evidence,
context_sentence,
start_ending,
endings,
label=None):
self.question_id = question_id
self.evidence = evidence
self.context_sentence = context_sentence
self.start_ending = start_ending
self.endings = endings
self.label = label
def __str__(self):
return self.__repr__()
def __repr__(self):
l = [
f"question_id: {self.question_id}",
f"evidence: {self.evidence}",
f"context_sentence: {self.context_sentence}",
f"start_ending: {self.start_ending}",
f"endings: {self.endings}",
]
if self.label is not None:
l.append(f"label: {self.label}")
return ", ".join(l)
class DataProcessor(object):
"""Base class for data converters for sequence classification data sets."""
def get_train_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the train set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
@classmethod
def _read_json(cls, input_file):
reader = jsonlines.Reader(open(input_file, "r"))
lines = [each for each in reader]
return lines
class CosmosQAProcessor(DataProcessor):
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_json(os.path.join(data_dir, "train.jsonl")))
def get_train_demo(self, data_dir):
return self._create_examples(
self._read_json(os.path.join(data_dir, "train_10.jsonl")))
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_json(os.path.join(data_dir, "dev.jsonl")))
def get_dev_demo(self, data_dir):
return self._create_examples(
self._read_json(os.path.join(data_dir, "dev_10.jsonl")))
def get_test_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_json(os.path.join(data_dir, "test.jsonl")))
def get_examples_from_file(self, input_file):
return self._create_examples(
self._read_json(input_file))
def get_labels(self):
"""See base class."""
return ["0", "1", "2", "4"]
def _create_examples(self, records):
examples = []
for (i, record) in enumerate(records):
question_id = record["id"]
context = record["context"]
evidence = record["evidence"]
question = record["question"]
answers = record["choices"]
answer_0 = answers[0]["text"]
answer_1 = answers[1]["text"]
answer_2 = answers[2]["text"]
answer_3 = answers[3]["text"]
label = record["label"]
if label is not None: label = int(label)
examples.append(
CosmosQAExample(
question_id=question_id,
evidence=evidence,
context_sentence=context,
start_ending=question,
endings = [answer_0, answer_1, answer_2, answer_3],
label=label)
)
return examples
def label_field(self):
return "AnswerRightEnding"
def convert_examples_to_features(examples, tokenizer, max_seq_length):
"""Loads a data file into a list of `InputBatch`s."""
features = []
for example_index, example in tqdm(enumerate(examples), desc="converting examples to features"):
CLS = tokenizer.cls_token
SEP = tokenizer.sep_token
PAD_id = tokenizer.pad_token_id
context_tokens = tokenizer.tokenize(example.context_sentence)
start_ending_tokens = tokenizer.tokenize(example.start_ending)
choices_inputs = []
for ending_index, ending in enumerate(example.endings):
context_tokens_choice = context_tokens[:]
ending_tokens = tokenizer.tokenize(ending)
option_len = len(ending_tokens)
ques_len = len(start_ending_tokens)
ending_tokens = start_ending_tokens + ending_tokens
_truncate_seq_pair(context_tokens_choice, ending_tokens, max_seq_length - 3)
doc_len = len(context_tokens_choice)
tokens = [CLS] + context_tokens_choice + [SEP] + ending_tokens + [SEP]
segment_ids = [0] * (len(context_tokens_choice) + 2) + [1] * (len(ending_tokens) + 1)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_mask = [1] * len(input_ids)
padding = [0] * (max_seq_length - len(input_ids))
padding_ids = [PAD_id] * (max_seq_length - len(input_ids))
input_ids += padding_ids
input_mask += padding
segment_ids += padding
assert len(input_ids) == len(input_mask) == len(segment_ids) == max_seq_length
assert (doc_len + ques_len + option_len) <= max_seq_length
# choices_features.append(())
inputs = {}
inputs["input_ids"] = input_ids
inputs["attention_mask"] = input_mask
inputs["token_type_ids"] = segment_ids
choices_inputs.append(inputs)
input_ids = [x["input_ids"] for x in choices_inputs]
attention_mask = [x["attention_mask"] for x in choices_inputs]
token_type_ids = [x["token_type_ids"] for x in choices_inputs]
label = example.label
question_id = example.question_id
features.append(
InputFeatures(
question_id=question_id,
label=label,
input_ids=input_ids,
input_mask=attention_mask,
segment_ids=token_type_ids,
)
)
for f in features[:2]:
logger.info("*** Example ***")
logger.info("feature: %s" % f)
return features
def _truncate_seq_pair(tokens_a, tokens_b, max_length):
"""Truncates a sequence pair in place to the maximum length."""
# This is a simple heuristic which will always truncate the longer sequence
# one token at a time. This makes more sense than truncating an equal percent
# of tokens from each, since if one sequence is very short then each token
# that's truncated likely contains more information than a longer sequence.
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_length:
break
if len(tokens_a) > len(tokens_b):
tokens_a.pop()
else:
tokens_b.pop()
def _truncate_sequences(max_length, inputs):
idx = 0
for ta, tb in zip(inputs[0], inputs[1]):
_truncate_seq_pair(ta, tb, max_length)
processors = {
"cosmosqa": CosmosQAProcessor
}