-
Notifications
You must be signed in to change notification settings - Fork 7
/
task_helper.py
328 lines (278 loc) · 10.4 KB
/
task_helper.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
from utils import *
import os
def load_train_test_set(args):
if os.path.exists("data/{}_{}.json".format(args.task, "train")):
train_data = read_json("data/{}_{}.json".format(args.task, "train"))
else:
train_data = []
eval_split = args.eval_split
dev_data = read_json("data/{}_{}.json".format(args.task, eval_split))
if args.num_train == -1:
args.num_train = len(train_data)
if args.num_dev == -1:
args.num_dev = len(dev_data)
train_data = train_data[args.slice_train:args.slice_train+args.num_train]
dev_data = dev_data[args.slice_dev:args.slice_dev+args.num_dev]
return train_data, dev_data
class TaskHelper:
style_to_completion_length = {}
style_to_train_sep = {}
def __init__(self, style):
self.style = style
@classmethod
def from_taskname(cls, taskname, style):
if taskname == "gsm":
return GSMTaskHelper(style)
elif taskname == "clutrr":
return CLUTRRTaskHelper(style)
elif taskname == "proofd5":
return ProofD5TaskHelper(style)
elif taskname == "arlsat":
return ArLSATTaskHelper(style)
elif taskname == "boardmaindp1":
return Boardmaindp1TaskHelper(style)
elif taskname == "boardmaindp2":
return Boardmaindp2TaskHelper(style)
elif taskname == "boardmaindp3":
return Boardmaindp3TaskHelper(style)
else:
raise RuntimeError("Not Implemented Yet")
def prompt_func(self, test_ex, shots):
raise RuntimeError("Not Implemented Yet")
def get_completion_length(self):
return self.style_to_completion_length[self.style]
def get_train_sep(self):
return self.style_to_train_sep[self.style]
class GSMTaskHelper(TaskHelper):
style_to_completion_length = {
"std": 32,
"cot": 160,
"proglm": 320,
"satlm": 320,
"satcotsolver": 576,
}
style_to_train_sep = {
"std": "\n\n",
"cot": "\n\n",
"proglm": "\n\n\n\n\n\n",
"satlm": "\n\n\n\n\n\n",
"satcotsolver": "\n\n\n\n\n\n",
}
def prompt_func(self, test_ex, shots):
if self.style == "std" or self.style == "cot":
return self.cot_prompt(test_ex, shots)
elif self.style == "proglm":
return self.proglm_prompt(test_ex, shots)
elif self.style == "satlm":
return self.satlm_prompt(test_ex, shots)
elif self.style == "satcotsolver":
return self.satlm_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def cot_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = "Q: {}\nA:".format(test_ex["question"])
return test_example
def proglm_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = "Q: {}\n\n# solution in Python:\n\n\n".format(test_ex["question"])
return test_example
def satlm_prompt(self, test_ex, shots):
return self.proglm_prompt(test_ex, shots)
class ProofWriterTaskHelper(TaskHelper):
style_to_completion_length = {
"std": 16,
"cot": 512,
"satlm": 768,
"proglm": 512,
}
style_to_train_sep = {
"std": "\n\n\n",
"cot": "\n\n\n",
"satlm": "\n\n\n\n\n",
"proglm": "\n\n\n\n\n",
}
def prompt_func(self, test_ex, shots):
if self.style == "cot":
return self.cot_prompt(test_ex, shots)
elif self.style == "std":
return self.std_prompt(test_ex, shots)
elif self.style == "satlm":
return self.satlm_prompt(test_ex, shots)
elif self.style == "proglm":
return self.satlm_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def cot_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = (
'Here are some facts and rules:\n' +
'\n'.join(test_ex["context"]) +
'Does it imply that the statement "{}" is True?\n'.format(test_ex["question"].rstrip('.')) +
'Reasoning:\n'
)
return test_example
def std_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = (
'Here are some facts and rules:\n' +
'\n'.join(test_ex["context"]) +
'Does it imply that the statement "{}" is True?\n'.format(test_ex["question"].rstrip('.')) +
'Answer:'
)
return test_example
def satlm_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = (
'"""\n' +
'Here are some facts and rules:\n' +
'\n'.join(test_ex["context"]) +
'\nQuestion: The statement "{}" is True or False?\n'.format(test_ex["question"].rstrip('.')) +
'"""\n' +
'# solution in Python:\n' +
'def solution():\n'
)
return test_example
class ProofD5TaskHelper(ProofWriterTaskHelper):
pass
class CLUTRRTaskHelper(TaskHelper):
style_to_completion_length = {
"proglm": 512,
"satlm": 512,
"satcotsolver": 768,
}
style_to_train_sep = {
"proglm": "\n\n",
"satlm": "\n\n\n\n\n",
"satcotsolver": "\n\n\n\n\n",
}
def prompt_func(self, test_ex, shots):
if self.style == "proglm":
return self.proglm_prompt(test_ex, shots)
elif self.style == "satlm":
return self.satlm_prompt(test_ex, shots)
elif self.style == "satcotsolver":
return self.satlm_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def proglm_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = ("# Context: {}\n# Question: How is [{}] related to [{}]?\n"
+ "# To answer this question, we write a program to answer the following subquestions:\n").format(
test_ex["context"], test_ex["query"][1], test_ex["query"][0]
)
return test_example
def satlm_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = '"""\n{}\nQuestion: How is [{}] related to [{}]?\n"""\n'.format(
test_ex["context"], test_ex["query"][1], test_ex["query"][0]
)
return test_example
class LongContextMCQAHelper(TaskHelper):
style_to_completion_length = {
"std": 16,
"cot": 512,
"satlm": 1024,
}
style_to_train_sep = {
"std": "\n\n",
"cot": "\n\n\n\n",
"satlm": "\n\n\n\n",
}
CHOICE_IDX = ["(A)", "(B)", "(C)", "(D)", "(E)"]
CODE_HEADER = "### write python code to answer the question"
CODE_BLOCK_COMMENT = '"""'
def prompt_func(self, test_ex, shots):
if self.style == "std":
return self.std_prompt(test_ex, shots)
elif self.style == "cot":
return self.cot_prompt(test_ex, shots)
elif self.style == "satlm":
return self.satlm_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def std_prompt(self, test_ex, shots):
def _single_ex_func(ex, is_train):
choice_str = "\n".join([self.CHOICE_IDX[i] + " " + x for (i, x) in enumerate(ex["choices"])])
p_ex = "{}\nQuestion: {}\nChoices:\n{}\nAnswer:".format(ex["context"], ex["question"], choice_str)
if is_train:
p_ex = p_ex + " The answer is {}.".format(self.CHOICE_IDX[ex["label"]])
return p_ex
showcase_examples = [
_single_ex_func(s, True) for s in shots
]
test_example = [_single_ex_func(test_ex, False)]
return self.get_train_sep().join(showcase_examples + test_example)
def cot_prompt(self, test_ex, shots):
def _single_ex_func(ex, is_train):
assert not is_train
choice_str = "\n".join([self.CHOICE_IDX[i] + " " + x for (i, x) in enumerate(ex["choices"])])
p_ex = "{}\nQuestion: {}\nChoices:\n{}\nAnswer:".format(ex["context"], ex["question"], choice_str)
return p_ex
showcase_examples = [
_single_ex_func(s, True) for s in shots
]
test_example = [_single_ex_func(test_ex, False)]
return self.get_train_sep().join(showcase_examples + test_example)
def satlm_prompt(self, test_ex, shots):
def _single_ex_func(ex, is_train):
assert not is_train
choice_str = "\n".join([self.CHOICE_IDX[i] + " " + x for (i, x) in enumerate(ex["choices"])])
p_ex = "{}\n{}\n{}\nQuestion: {}\nChoices:\n{}\n{}\n".format(
self.CODE_HEADER,
self.CODE_BLOCK_COMMENT,
ex["context"], ex["question"], choice_str,
self.CODE_BLOCK_COMMENT)
return p_ex
showcase_examples = [
_single_ex_func(s, True) for s in shots
]
test_example = [_single_ex_func(test_ex, False)]
return self.get_train_sep().join(showcase_examples + test_example)
class ArLSATTaskHelper(LongContextMCQAHelper):
pass
class BoardgameQATaskHelper(TaskHelper):
style_to_completion_length = {
"cot": 512,
"satlm": 768,
}
style_to_train_sep = {
"cot": "\n\n",
"satlm": "\n\n\n\n\n",
}
def prompt_func(self, test_ex, shots):
if self.style == "cot":
return self.cot_prompt(test_ex, shots)
elif self.style == "satlm":
return self.satlm_prompt(test_ex, shots)
else:
raise RuntimeError("Not Implemented Yet")
def cot_prompt(self, test_ex, shots):
assert len(shots) == 0
# return test_example
return f"Q: {test_ex['question']}\nA:"
def satlm_prompt(self, test_ex, shots):
assert len(shots) == 0
test_example = (
'"""\n'
f'{test_ex["question"]}\n'
'"""\n' +
'# solution in Python:\n' +
'def solution():\n'
)
return test_example
class Boardmaindp1TaskHelper(BoardgameQATaskHelper):
style_to_completion_length = {
"cot": 512,
"satlm": 768,
}
class Boardmaindp2TaskHelper(BoardgameQATaskHelper):
style_to_completion_length = {
"cot": 512,
"satlm": 1536,
}
class Boardmaindp3TaskHelper(BoardgameQATaskHelper):
style_to_completion_length = {
"cot": 768,
"satlm": 1536,
}