-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
409 lines (370 loc) · 14.1 KB
/
dataset.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
import os
import pandas as pd
import pytorch_lightning as pl
import torch.utils.data
from pytorch_lightning.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from transformers import AutoTokenizer, PreTrainedTokenizerFast
from model.thought_expander import ConstObject, NumberObject, ThoughtExpander
class DataModule(pl.LightningDataModule):
def __init__(
self,
tokenizer,
datasets,
batch_size,
test_on_validation,
num_token=None,
collate_raw=False,
):
super(DataModule, self).__init__()
self.batch_size = batch_size
if len(datasets) == 3:
self.train_dataset, self.dev_dataset, self.test_dataset = datasets
self.predict_dataset = None
elif len(datasets) == 4:
(
self.train_dataset,
self.dev_dataset,
self.test_dataset,
self.predict_dataset,
) = datasets
self.batch_processor = BatchProcessor(f".language-models/{tokenizer}", num_token=num_token)
self.test_on_validation = test_on_validation
if collate_raw:
self.collate = self.collate_raw
else:
self.collate = self.collate_tensor
@staticmethod
def as_tensor(k, v):
if isinstance(v, list) and v:
if k in ("input_ids", "attention_mask", "label_final"):
return torch.as_tensor(v)
elif k in ("label_dd", "label_dd_indices"):
return [[torch.as_tensor(se) for se in e] for e in v]
return v
def as_tensor_batch(self, batch):
return {k: self.as_tensor(k, v) for k, v in batch.items()}
def collate_raw(self, batch):
batch = self.batch_processor(batch)
return batch
def collate_tensor(self, batch):
batch = self.batch_processor(batch)
return self.as_tensor_batch(batch)
def train_dataloader(self) -> TRAIN_DATALOADERS:
return torch.utils.data.DataLoader(
self.train_dataset, collate_fn=self.collate, batch_size=self.batch_size, shuffle=True
)
def test_dataloader(self) -> EVAL_DATALOADERS:
return torch.utils.data.DataLoader(
self.test_dataset, collate_fn=self.collate, batch_size=self.batch_size
)
def val_dataloader(self) -> EVAL_DATALOADERS:
if self.test_on_validation:
return [
torch.utils.data.DataLoader(
self.dev_dataset, collate_fn=self.collate, batch_size=self.batch_size
),
torch.utils.data.DataLoader(
self.test_dataset, collate_fn=self.collate, batch_size=self.batch_size
),
]
else:
return torch.utils.data.DataLoader(
self.dev_dataset, collate_fn=self.collate, batch_size=self.batch_size
)
def predict_dataloader(self) -> EVAL_DATALOADERS:
return torch.utils.data.DataLoader(
self.predict_dataset, collate_fn=self.collate, batch_size=self.batch_size
)
class BatchProcessor:
def __init__(self, tokenizer_name, num_token):
self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
assert isinstance(
self.tokenizer, PreTrainedTokenizerFast
), f"{type(self.tokenizer).__name__} is not a fast tokenizer. Slow version of tokenizer is not compatible."
self.num_token = num_token if num_token is not None else self.tokenizer.mask_token
self.num_token_id = self.tokenizer.vocab[self.num_token]
def __call__(self, batch):
problems = [item["problem"] for item in batch]
questions = [item["question"] for item in batch]
tokenized = self.tokenizer(
problems, padding="longest", truncation=True, return_offsets_mapping=True
)
offsets = tokenized.pop("offset_mapping")
tokenized["problem"] = problems
tokenized["question"] = questions
tokenized["question_idx"] = [
next(i for i, (s, e) in enumerate(o) if s <= qe < e)
for qe, o in zip(
((p.index(q) + len(q) - 1) for p, q in zip(problems, questions)), offsets
)
]
tokenized["question_indices"] = [
[i for i, (s, e) in enumerate(o) if qs <= e > 0 and s < qs + qn]
for (qs, qn), o in zip(
((p.index(q), len(q)) for p, q in zip(problems, questions)), offsets
)
]
tokenized["num_indices"] = [
[i for i, token_id in enumerate(ids) if token_id == self.num_token_id]
for ids in tokenized["input_ids"]
]
return tokenized | {
key: [item[key] for item in batch] for key in batch[0] if key not in tokenized
}
class MathDataset(torch.utils.data.Dataset):
def __init__(
self,
file_path,
tokenizer_name,
limit_depth,
compress_num,
constants,
ignore_over_depth,
multi,
label=False,
replacer=None,
num_token=None,
):
data = pd.read_csv(file_path, keep_default_na=False)
print(f"read {file_path} : n={len(data)}")
tokenizer = AutoTokenizer.from_pretrained(f".language-models/{tokenizer_name}")
self.num_token = num_token if num_token is not None else tokenizer.mask_token
self.multi = multi
self.compress_num = compress_num
if constants is None:
self._extract_constants(data)
else:
self.constants = constants
self.replacer = replacer
self._data = []
n_ignore, n_unknown_consts = 0, 0
max_depth = 0
for problem, question, equation, answer, numbers in zip(
data["Problem"], data["Question"], data["Equation"], data["Answer"], data["Numbers"]
):
equation = self._fix_equation(equation)
initial_thoughts = self._generate_initial_thoughts(numbers)
results = None
try:
if multi:
results = self._generate_labels(
initial_thoughts,
equation,
limit_depth=limit_depth if ignore_over_depth else -1,
)
if results is None:
target = [eval(equation, {b.expr: b for b in initial_thoughts})]
for e in target:
target += e.children
results = self._generate_labels(
initial_thoughts,
equation,
limit_depth=limit_depth if ignore_over_depth else -1,
target=target,
)
except NameError:
if label:
n_unknown_consts += 1
except LookupError as e:
if ignore_over_depth:
n_ignore += 1
raise e
if results is None:
continue
else:
(
target_thought,
label_thoughts,
label_indices,
label_dds,
label_dd_indices,
n_thoughts,
label_final,
n_dds,
depth,
) = results
if depth > max_depth:
max_depth = depth
if label:
example = {
"problem": self._process_text(problem),
"question": self._process_text(question),
"equation": equation,
"answer": answer,
"initial_thoughts": initial_thoughts,
"target_thought": target_thought,
"label_thoughts": label_thoughts,
"label_thought_indices": label_indices,
"label_dd": label_dds,
"label_dd_indices": label_dd_indices,
"label_final": label_final,
"n_thoughts": n_thoughts,
"n_dds": n_dds,
}
else:
example = {
"problem": self._process_text(problem),
"question": self._process_text(question),
"equation": equation,
"answer": answer,
"initial_thoughts": initial_thoughts,
}
self._data.append(example)
if ignore_over_depth and n_ignore:
print(" - ignored examples :", n_ignore)
if n_unknown_consts:
print(" - unknown constants :", n_unknown_consts)
self.max_depth = max_depth
def _process_text(self, text):
if self.num_token != "[NUM]":
text = text.replace("[NUM]", self.num_token)
return text
def _extract_constants(self, raw):
if "Constants" not in raw:
print("This dataset does not contain constant values.")
self.constants, self.replacer = False, False
return
collected_consts = [
float(c) for c in set(sum((str(c).split() for c in raw["Constants"]), []))
]
replacer = {c: 1 / c for c in collected_consts if c < 1 and (1 / c) in collected_consts}
self.constants = [ConstObject(c) for c in collected_consts if c not in replacer]
self.replacer = {
ConstObject(k).expr: f"( {(1 / ConstObject(v))} )" for k, v in replacer.items()
}
print("Extracted constants :", [c.value for c in self.constants])
def _fix_equation(self, equation):
if self.replacer:
return " ".join(
(t if t not in self.replacer else self.replacer[t]) for t in equation.split()
)
return equation
@staticmethod
def parse_number(number):
if "%" in number:
return float(number[:-1]) / 100
elif "/" in number:
f1, f2 = number[1:-1].split("/")
return float(f1) / float(f2)
return float(number)
def _generate_initial_thoughts(self, numbers):
number_thoughts = [
NumberObject(i, self.parse_number(n)) for i, n in enumerate(numbers.split())
]
if self.compress_num:
initial_thoughts = []
initial_thoughts += (
n for n in number_thoughts if not any(n.value == b.value for b in initial_thoughts)
)
else:
initial_thoughts = number_thoughts
if self.constants:
initial_thoughts += self.constants
return initial_thoughts
def _generate_labels(self, initial_thoughts, equation, *, limit_depth, target=None):
if target is None:
target_thought = target = eval(equation, {b.expr: b for b in initial_thoughts})
else:
target_thought = target[0] if isinstance(target, list) else target
label_thoughts, label_indices, label_dds, label_dd_indices = [], [], [], []
n_dds = 0
expander = ThoughtExpander(initial_thoughts, limit_depth)
for expanded_thoughts, expanded_indices in expander:
label_dd = [float(e in target) for e in expanded_thoughts]
label_dd_ind = [i for i, s in enumerate(label_dd) if s]
label_thoughts.append(expanded_thoughts)
label_indices.append(expanded_indices)
label_dds.append(label_dd)
label_dd_indices.append(label_dd_ind)
n_dds += len(expanded_thoughts)
expander.collect([expanded_thoughts[i] for i in label_dd_ind])
final_thought = next((e for e in expanded_thoughts if e == target_thought), None)
if final_thought is not None:
label_final = [expander.thoughts.index(final_thought)]
break
if len(label_dd_ind) > 30:
return
else:
raise LookupError(
f"{target_thought!r} is not in expanded thoughts. (equation={equation}, depth={expander.depth - 1})"
)
return (
final_thought,
label_thoughts,
label_indices,
label_dds,
label_dd_indices,
len(expander.thoughts),
label_final,
n_dds,
expander.depth,
)
def __getitem__(self, idx):
return self._data[idx]
def __len__(self):
return len(self._data)
def is_cv(data_root, dataset_name):
return any(
not os.path.exists(p)
for p in (
os.path.join(data_root, dataset_name, "train.csv"),
os.path.join(data_root, dataset_name, "dev.csv"),
os.path.join(data_root, dataset_name, "test.csv"),
)
)
def load_datasets(
data_root,
dataset_name,
limit_depth,
compress_num,
ignore_over_depth,
tokenizer=None,
multi=False,
label=False,
num_token=None,
):
print("Start loading dataset :", dataset_name)
train_path = os.path.join(data_root, dataset_name, "train.csv")
dev_path = os.path.join(data_root, dataset_name, "dev.csv")
test_path = os.path.join(data_root, dataset_name, "test.csv")
train_dataset = MathDataset(
train_path,
tokenizer,
limit_depth,
compress_num,
constants=None,
ignore_over_depth=ignore_over_depth,
multi=multi,
label=True,
num_token=num_token,
)
dev_dataset = MathDataset(
dev_path,
tokenizer,
limit_depth,
compress_num,
constants=train_dataset.constants,
replacer=train_dataset.replacer,
ignore_over_depth=ignore_over_depth,
multi=multi,
label=label,
num_token=num_token,
)
test_dataset = MathDataset(
test_path,
tokenizer,
limit_depth,
compress_num,
constants=train_dataset.constants,
replacer=train_dataset.replacer,
ignore_over_depth=ignore_over_depth,
multi=multi,
label=label,
num_token=num_token,
)
print("maximum test depth :", test_dataset.max_depth)
return (
(train_dataset, dev_dataset, test_dataset),
train_dataset.constants,
test_dataset.max_depth,
)