/
MATH.py
425 lines (330 loc) · 14.6 KB
/
MATH.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
"""
Load MATH Data for training.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import json
import glob
import logging
import io
import random
import numpy as np
from dataset.util import last_boxed_only, _clean_numbers, last_boxed_only_string, only_until_first_boxed_from_tokens
from multiprocessing import Manager
from torch.multiprocessing import Pool
from dataset.base_math_dataset import BaseMathDataset
class MATHDataset(BaseMathDataset):
"""Configurable Math Dataset.
"""
def __len__(self):
return int(len(self.samples) * self.len_multiplier)
def initialize(self):
"""
Set up self.samples by loading from the dataroot
"""
all_filenames = glob.glob(self.dataroot)
samples_raw = []
for fname in all_filenames:
with open(fname, 'r') as fp:
try:
problem_data = json.load(fp)
except Exception as e:
print(f"Error loading JSON from {fname}", e)
raise e
curr_sample_raw = (problem_data['problem'], problem_data['solution'], fname)
for e in curr_sample_raw:
assert e
samples_raw.append(curr_sample_raw)
manager = Manager()
samples_raw = manager.list(samples_raw)
self.samples = samples_raw
del samples_raw
print(f"{self.__class__.__name__}: Loaded {len(self.samples)} samples.")
def clean_filter_sample_gpt(self, sample):
"""
Does the actual tokenization. Should be parallelized because it can be a bit slow.
"""
if sample == None:
return None
if self.mode_answe == 'peeking_only':
return self.clean_filter_sample_peeking_gpt(sample)
if self.mode_answer == 'mixed_full_and_peeking':
if random.random() < 0.5:
return self.clean_filter_sample_peeking_gpt(sample)
else:
_mode_answer = 'full'
elif self.mode_answer == 'mixed_full_and_nopack_padding':
if random.random() < 0.5:
return self.clean_filter_sample_nopackpadding_gpt(sample)
else:
_mode_answer = 'full'
elif self.mode_answer == 'mixed_final_boxed_and_full':
if random.random() < 0.5:
_mode_answer = 'full'
else:
_mode_answer = 'final_boxed'
elif self.mode_answer == 'full':
_mode_answer = 'full'
elif self.mode_answer == 'final_boxed':
_mode_answer = 'final_boxed'
else:
raise NotImplementedError(f"self.mode_answer = {self.mode_answer} not recognized.")
if _mode_answer == 'full':
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question, verbose=False))
sep_ids_2 = torch.LongTensor(self.tokenizer.encode("\nFULL SOLUTION:\n", verbose=False))
answer_ids = self.tokenizer.encode(answer, verbose=False)
answer_ids.append(self.tokenizer.eos_token_id)
answer_ids = torch.LongTensor(answer_ids)
input_ids = torch.cat([
question_ids,
sep_ids_2,
answer_ids
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
torch.ones_like(question_ids) * -100,
torch.ones_like(sep_ids_2) * -100,
answer_ids.clone()
], dim=0)
elif _mode_answer == 'final_boxed':
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
if not answer_final:
print("ERROR FROM", question, answer)
return None
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question, verbose=False))
sep_ids_1 = torch.LongTensor(self.tokenizer.encode("\nFINAL ANSWER:\n", verbose=False))
answer_final_ids = self.tokenizer.encode(answer_final, verbose=False)
answer_final_ids.append(self.tokenizer.eos_token_id)
answer_final_ids = torch.LongTensor(answer_final_ids)
input_ids = torch.cat([
question_ids,
sep_ids_1,
answer_final_ids,
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
torch.ones_like(question_ids) * -100,
torch.ones_like(sep_ids_1) * -100,
answer_final_ids.clone(),
], dim=0)
else:
raise NotImplementedError()
# Stop early if this Q,A pair is too long
if input_ids.shape[0] > self.max_tokens:
# Print reason for skipping
# print(f"Skipping due to input_ids being too big. input_ids.shape[0] = {input_ids.shape[0]}.")
return None
input_ids = input_ids.tolist()
label_ids = label_ids.tolist()
return {
'input_ids_list' : input_ids,
'label_ids_list' : label_ids
}
def clean_filter_sample_nopackpadding_gpt(self, sample):
if sample == None:
return None
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question, verbose=False))
sep_ids = torch.LongTensor(self.tokenizer.encode("\nFINAL ANSWER:\n", verbose=False))
final_answer_ids = torch.LongTensor(self.tokenizer.encode(answer_final, verbose=False))
# Stop early if this Q,A pair is too long
num_to_pad = 32
padding_tensor = torch.ones((num_to_pad)) * 220 # 220 is the token for space in the case of GPT2 models
input_ids = torch.cat([
question_ids,
padding_tensor,
sep_ids,
final_answer_ids
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
torch.ones_like(question_ids) * -100,
torch.ones_like(padding_tensor) * -100,
torch.ones_like(sep_ids) * -100,
final_answer_ids.clone()
], dim=0)
input_ids = input_ids.tolist()
label_ids = label_ids.tolist()
return {
'input_ids_list' : input_ids,
'label_ids_list' : label_ids
}
def clean_filter_sample_nopackpadding_gpt_eval(self, sample):
if sample == None:
return None
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question, verbose=False))
sep_ids = torch.LongTensor(self.tokenizer.encode("\nFINAL ANSWER:\n", verbose=False))
final_answer_ids = torch.LongTensor(self.tokenizer.encode(answer_final, verbose=False))
num_to_pad = 32
padding_tensor = torch.ones((num_to_pad)) * 220 # 220 is the token for space in the case of GPT2 models
input_ids = torch.cat([
question_ids,
padding_tensor,
sep_ids,
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
final_answer_ids.clone()
], dim=0)
# Stop early if this Q,A pair is too long
if input_ids.shape[0] + label_ids.shape[0] > self.max_tokens:
# Print reason for skipping
# print(f"Skipping due to input_ids being too big. input_ids.shape[0] = {input_ids.shape[0]}.")
return None
input_ids = input_ids.tolist()
label_ids = label_ids.tolist()
return {
'input_ids_list' : input_ids,
'label_ids_list' : label_ids
}
def clean_filter_sample_peeking_gpt(self, sample):
"""
Does the actual tokenization. Should be parallelized because it can be a bit slow.
"""
if sample == None:
return None
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question + "\nFULL SOLUTION:\n", verbose=False))
answer_ids = self.tokenizer.tokenize(answer)
answer_ids = only_until_first_boxed_from_tokens(answer, answer_ids)
answer_ids = torch.LongTensor(self.tokenizer.encode(answer_ids, verbose=False))
# Take a fraction
if isinstance(self.peek_fraction, tuple):
final_idx = int(len(answer_ids) * random.uniform(*self.peek_fraction))
else:
final_idx = int(len(answer_ids) * self.peek_fraction)
# # Override peeking fraction
# final_idx = int(len(answer_ids) * np.random.choice([0.25, 0.5, 0.75, 1.0], p=[1/6, 1/6, 1/3, 1/3]))
answer_ids = answer_ids[:final_idx]
sep_ids = torch.LongTensor(self.tokenizer.encode("\nFINAL ANSWER:\n", verbose=False))
final_answer_ids = torch.LongTensor(self.tokenizer.encode(answer_ids[final_idx:]))
input_ids = torch.cat([
question_ids,
answer_ids,
sep_ids,
final_answer_ids
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
torch.ones_like(question_ids) * -100,
torch.ones_like(answer_ids) * -100,
torch.ones_like(sep_ids) * -100,
final_answer_ids.clone()
], dim=0)
# Stop early if this Q,A pair is too long
if input_ids.shape[0] > self.max_tokens:
# Print reason for skipping
# print(f"Skipping due to input_ids being too big. input_ids.shape[0] = {input_ids.shape[0]}.")
return None
input_ids = input_ids.tolist()
label_ids = label_ids.tolist()
return {
'input_ids_list' : input_ids,
'label_ids_list' : label_ids
}
def clean_filter_sample_peeking_gpt_eval(self, sample):
"""
Does the actual tokenization. Should be parallelized because it can be a bit slow.
"""
if sample == None:
return None
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question + "\nFULL SOLUTION:\n", verbose=False))
answer_ids = self.tokenizer.tokenize(answer)
answer_ids_full = torch.LongTensor(self.tokenizer.encode(answer))
answer_ids = only_until_first_boxed_from_tokens(answer, answer_ids)
if len(answer_ids) == 0:
return None
answer_ids = torch.LongTensor(self.tokenizer.encode(answer_ids, verbose=False))
# Take a fraction
if isinstance(self.peek_fraction, tuple):
final_idx = int(len(answer_ids) * random.uniform(*self.peek_fraction))
else:
final_idx = int(len(answer_ids) * self.peek_fraction)
answer_ids = answer_ids[:final_idx]
# sep_ids = torch.LongTensor(self.tokenizer.encode("\nFINAL ANSWER\n", verbose=False))
final_answer_ids = answer_ids_full[final_idx:]
print(final_answer_ids)
input_ids = torch.cat([
question_ids,
answer_ids,
# sep_ids,
], dim=0)
# Only answer_ids contribute to the loss
label_ids = torch.cat([
final_answer_ids.clone()
], dim=0)
# Stop early if this Q,A pair is too long
if input_ids.shape[0] + label_ids.shape[0] > self.max_tokens:
# Print reason for skipping
# print(f"Skipping due to input_ids being too big. input_ids.shape[0] = {input_ids.shape[0]}.")
return None
input_ids = input_ids.tolist()
label_ids = label_ids.tolist()
return {
'input_ids_list' : input_ids,
'label_ids_list' : label_ids
}
def clean_filter_sample_gpt_eval(self, sample):
"""
Does tokenization for final model evaluation. This should return
input_ids as the context and labels as the true answer.
"""
if sample == None:
return None
if self.mode_answer == 'eval_peeking':
return self.clean_filter_sample_peeking_gpt_eval(sample)
elif self.mode_answer == 'eval_nopack_padding':
return self.clean_filter_sample_nopackpadding_gpt_eval(sample)
question, answer = sample
if self.clean_numbers:
question = _clean_numbers(question)
answer = _clean_numbers(answer)
answer_final = last_boxed_only_string(answer)
assert not answer.isspace()
question_ids = torch.LongTensor(self.tokenizer.encode("\nQUESTION:\n" + question, verbose=False))
sep_ids = torch.LongTensor(self.tokenizer.encode("\FULL SOLUTION:\n", verbose=False))
answer_final_ids = torch.LongTensor(self.tokenizer.encode(answer_final, verbose=False)) # Loss only counted on these tokens.
input_ids = torch.cat([
question_ids,
sep_ids,
], dim=0)
label_ids = torch.cat([
answer_final_ids.clone()
], dim=0)
# Stop early if this Q,A pair is too long
if input_ids.shape[0] + label_ids.shape[0] > self.max_tokens:
# Print reason for skipping
# print(f"Skipping due to input_ids being too big. input_ids.shape[0] = {input_ids.shape[0]}.")
return None
return {
'input_ids_list' : input_ids.tolist(),
'label_ids_list' : label_ids.tolist()
}