This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdata_utils.py
503 lines (368 loc) · 15.1 KB
/
data_utils.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# Copyright 2023 The Distilling-step-by-step authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import re
import json
import numpy as np
from datasets import Dataset, DatasetDict, load_dataset
DATASET_ROOT = 'datasets'
class DatasetLoader(object):
def __init__(self, dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=None):
self.data_root = DATASET_ROOT
self.dataset_name = dataset_name
self.source_dataset_name = source_dataset_name
self.dataset_version = dataset_version
self.has_valid = has_valid
self.split_map = split_map
self.batch_size = batch_size
self.train_batch_idxs = train_batch_idxs
self.test_batch_idxs = test_batch_idxs
self.valid_batch_idxs = valid_batch_idxs
assert self.split_map is not None
def load_from_source(self):
if self.source_dataset_name is None:
self.source_dataset_name = self.dataset_name
if self.dataset_version is None:
datasets = load_dataset(self.source_dataset_name)
else:
datasets = load_dataset(self.source_dataset_name, self.dataset_version)
return datasets
def to_json(self, datasets):
for k, v in self.split_map.items():
datasets[v].to_json(f'{self.data_root}/{self.dataset_name}/{self.dataset_name}_{k}.json')
def load_from_json(self):
data_files = {
'train': f'{self.data_root}/{self.dataset_name}/{self.dataset_name}_train.json',
'test': f'{self.data_root}/{self.dataset_name}/{self.dataset_name}_test.json',
}
if self.has_valid:
data_files.update({'valid': f'{self.data_root}/{self.dataset_name}/{self.dataset_name}_valid.json',})
datasets = load_dataset('json', data_files=data_files)
datasets = self._post_process(datasets)
# subsample training dataset if needed
num_train = len(datasets['train'])
idxs = list()
for idx in self.train_batch_idxs:
idxs += range(idx*self.batch_size, (idx+1)*self.batch_size)
datasets['train'] = Dataset.from_dict(datasets['train'][[idx for idx in idxs if idx < num_train]])
return datasets
def load_llm_preds(self, split):
labels = list()
rationales = list()
for idx in getattr(self, f'{split}_batch_idxs'):
with open(f'{self.data_root}/{self.dataset_name}/llm/{split}_CoT_{idx}.json') as f:
outputs = json.load(f)
for output in outputs:
rationale, label = self._parse_llm_output(output)
rationales.append(rationale)
labels.append(label)
return rationales, labels
def load_gpt_preds(self, split):
labels = list()
rationales = list()
with open(f'{self.data_root}/gpt-neox/{self.dataset_name}/{split}.json') as f:
outputs = json.load(f)
for output in outputs:
rationale, label = self._parse_gpt_output(output)
rationales.append(rationale)
labels.append(label)
return rationales, labels
def _post_process(self, datasets):
raise NotImplementedError
def _parse_llm_output(self, output):
raise NotImplementedError
def _parse_gpt_output(self, output):
raise NotImplementedError
class CQADatasetLoader(DatasetLoader):
def __init__(self):
dataset_name = 'cqa'
source_dataset_name = 'cos_e'
dataset_version = 'v1.11'
has_valid = False
split_map = {
'train': 'train',
'test': 'validation',
}
batch_size = 1000
train_batch_idxs = range(10)
test_batch_idxs = range(2)
super().__init__(dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=None)
def _post_process(self, datasets):
def prepare_input(example):
question = example['question']
c_0 = example['choices'][0]
c_1 = example['choices'][1]
c_2 = example['choices'][2]
c_3 = example['choices'][3]
c_4 = example['choices'][4]
input = f'{question}\nAnswer Choices:\n(a) {c_0}\n(b) {c_1}\n(c) {c_2}\n(d) {c_3}\n(e) {c_4}'
example['input'] = input
example['label'] = example['answer']
return example
datasets = datasets.map(prepare_input)
datasets = datasets.remove_columns(['id', 'question', 'choices', 'answer', 'abstractive_explanation', 'extractive_explanation'])
return datasets
def _parse_llm_output(self, output):
rationale_label = output.split('Q:')[0]
rationale_label = rationale_label.rstrip()
rationale, label = rationale_label.split('So the answer is')
rationale = rationale.rstrip()
try:
label = re.split(r'\(.\)', label)[1].strip()
label = label if label[-1]!='.' else label[:-1]
except:
label = ' '
return rationale, label
def _parse_gpt_output(self, output):
rationale_label = output.split('Q:')[0]
rationale_label = rationale_label.rstrip().lstrip()
try:
rationale, label = rationale_label.split('So the answer is')
rationale = rationale.rstrip()
except:
rationale = ' '
label = ' '
return rationale, label
try:
label = re.split(r'\(.\)', label)[1].strip()
label = label if label[-1]!='.' else label[:-1]
except:
label = ' '
return rationale, label
class SVAMPDatasetLoader(DatasetLoader):
def __init__(self):
dataset_name = 'svamp'
source_dataset_name = 'svamp'
dataset_version = None
has_valid = False
split_map = {
'train': 'train',
'test': 'test',
}
batch_size = 500
train_batch_idxs = range(2)
test_batch_idxs = range(1)
super().__init__(dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=None)
def load_from_source(self):
with open(f'{self.data_root}/{self.dataset_name}/SVAMP.json') as f:
original_dataset = json.load(f)
dataset = list()
for data in original_dataset:
input = f'{data["Body"]}\n{data["Question"]}'
equation = data["Equation"]
dataset.append({
'input': input,
'label': equation,
})
idxs = np.random.RandomState(seed=0).permutation(len(dataset))
train_idxs = idxs[:800]
test_idxs = idxs[800:]
train_dataset = Dataset.from_list(np.array(dataset)[train_idxs].tolist())
test_dataset = Dataset.from_list(np.array(dataset)[test_idxs].tolist())
datasets = DatasetDict({
'train': train_dataset,
'test': test_dataset
})
return datasets
def _post_process(self, datasets):
return datasets
def _parse_llm_output(self, output):
rationale_label = output.split('Q:')[0]
rationale_label = rationale_label.rstrip()
try:
rationale, label = rationale_label.split('The answer is')
except:
rationale = ' '
label = ' '
return rationale, label
rationale = rationale.rstrip()
try:
label = re.search(r'\(.*\)', label).group(0)
except:
label = ' '
return rationale, label
def _parse_gpt_output(self, output):
rationale_label = output.split('Q:')[0]
rationale_label = rationale_label.rstrip().lstrip()
try:
rationale, label = rationale_label.split('The answer is')
except:
rationale = ' '
label = ' '
return rationale, label
rationale = rationale.rstrip()
try:
label = re.search(r'\(.*\)', label).group(0)
except:
label = ' '
return rationale, label
class ASDivDatasetLoader(DatasetLoader):
def __init__(self):
dataset_name = 'asdiv'
dataset_version = None
has_valid = False
split_map = {
'train': 'train',
'test': 'test',
}
batch_size = 1000
train_batch_idxs = range(3)
test_batch_idxs = range(1)
super().__init__(dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=None)
def load_from_source(self):
raise NotImplementedError
def _post_process(self, datasets):
def prepare_input(example):
example['input'] = example['Body'] + '\n' + example['Question']
answer = example['Answer'].split(' ')[0]
example['label'] = answer
return example
datasets = datasets.map(prepare_input)
datasets = datasets.remove_columns(['Body', 'Question', 'Formula', 'Answer'])
return datasets
def _parse_llm_output(self, output):
rationale_label = output.split('Q:')[0]
rationale_label = rationale_label.rstrip()
try:
rationale, label = rationale_label.split('The answer is')
except:
rationale = ' '
label = ' '
return rationale, label
rationale = rationale.rstrip()
try:
label = re.search(r'\(.*\)', label).group(0)
except:
label = ' '
return rationale, label
def _parse_gpt_output(self, output):
raise NotImplementedError
class ESNLIDatasetLoader(DatasetLoader):
def __init__(self, subset='full'):
dataset_name = 'esnli'
source_dataset_name = 'esnli'
dataset_version = None
has_valid = True
split_map = {
'train': 'train',
'valid': 'validation',
'test': 'test',
}
batch_size = 5500
if subset == 'full':
train_batch_idxs = range(100)
elif subset == 'small':
train_batch_idxs = range(10)
else:
raise ValueError
test_batch_idxs = range(2)
valid_batch_idxs = range(2)
super().__init__(dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=valid_batch_idxs)
def _post_process(self, datasets):
def prepare_input(example):
if example['label'] == 0:
example['label'] = 'entailment'
elif example['label'] == 1:
example['label'] = 'neutral'
elif example['label'] == 2:
example['label'] = 'contradiction'
return example
datasets = datasets.map(prepare_input)
datasets = datasets.remove_columns(['explanation_1', 'explanation_2', 'explanation_3'])
return datasets
def _parse_llm_output(self, output):
rationale = output.split("Answer:")[0].rstrip()
try:
label = output.split("Answer: ")[1].split("Premise")[0].rstrip()
except:
label = ' '
return rationale, label
def _parse_gpt_output(self, output):
rationale = output.split("Answer:")[0].rstrip().lstrip()
try:
label = output.split("Answer: ")[1].split("Premise")[0].rstrip()
except:
label = ' '
return rationale, label
class ANLIDatasetLoader(DatasetLoader):
def __init__(self, dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs):
super().__init__(dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=valid_batch_idxs)
def _post_process(self, datasets):
def label_idx2text(example):
if example['label'] == 0:
example['label'] = 'entailment'
elif example['label'] == 1:
example['label'] = 'neutral'
elif example['label'] == 2:
example['label'] = 'contradiction'
return example
datasets = datasets.map(label_idx2text)
datasets = datasets.remove_columns(['uid', 'reason'])
return datasets
def _parse_llm_output(self, output):
try:
rationale, label = output.split("Premise:")[0].rstrip().split("So the answer is")
except:
rationale = ''
label = ''
rationale = rationale.rstrip()
label = label.lstrip()[:-1]
return rationale, label
def _parse_gpt_output(self, output):
try:
rationale, label = output.split("Premise:")[0].rstrip().lstrip().split("So the answer is")
except:
try:
rationale, label = output.split("Premise:")[0].rstrip().lstrip().split("The answer is")
except:
rationale = ''
label = ''
rationale = rationale.rstrip()
label = label.lstrip()[:-1]
return rationale, label
class ANLI1DatasetLoader(ANLIDatasetLoader):
def __init__(self):
dataset_name = 'anli1'
source_dataset_name = 'anli'
dataset_version = None
has_valid = True
split_map = {
'train': 'train_r1',
'valid': 'dev_r1',
'test': 'test_r1',
}
batch_size = 5000
train_batch_idxs = range(4)
test_batch_idxs = range(1)
valid_batch_idxs = range(1)
super().__init__(dataset_name, source_dataset_name, dataset_version, has_valid, split_map,
batch_size, train_batch_idxs, test_batch_idxs, valid_batch_idxs=valid_batch_idxs)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True)
args = parser.parse_args()
if args.dataset == 'cqa':
dataset_loader = CQADatasetLoader()
elif args.dataset == 'svamp':
dataset_loader = SVAMPDatasetLoader()
elif args.dataset == 'esnli':
dataset_loader = ESNLIDatasetLoader()
elif args.dataset == 'anli1':
dataset_loader = ANLI1DatasetLoader()
datasets = dataset_loader.load_from_source()
dataset_loader.to_json(datasets)