-
Notifications
You must be signed in to change notification settings - Fork 25
/
predict.py
358 lines (312 loc) · 11.2 KB
/
predict.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
import argparse
import glob
import json
import os
import numpy as np
import torch
from transformers import (
AutoTokenizer,
)
from vllm.lora.request import LoRARequest
import arclib.messagers
from arclib.arc import (
make_submission,
read_tasks_from_single_file,
to_list,
to_tuple,
)
import arclib.augmenters # noqa: F401 to prevent removal by black
from arclib.eval import evaluate
from arclib.messagers import GPTTextMessageRepresenterV2, GPTTextMessageRepresenterForBarc
from arclib.representers import (
DiffExampleRepresenter,
PythonListGridRepresenter,
TextExampleRepresenter,
TextTaskRepresenter,
WordGridRepresenter,
)
from arclib.voting import vote
from inference.engine import get_sampling_params, initialize_engine, process_requests
from inference.preprocess import get_preprocessed_tasks
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("--seed", type=int, default=0, help="Random seed")
parser.add_argument(
"--data_file",
type=str,
default="/kaggle/input/arc-prize-2024/arc-agi_evaluation_challenges.json",
help="Data file path to evaluate",
)
parser.add_argument(
"--solution_file",
type=str,
default="/kaggle/input/arc-prize-2024/arc-agi_evaluation_solutions.json",
help="Solution file path to evaluate",
)
parser.add_argument(
"--num_examples",
type=int,
default=419,
help="Number of examples to process for limited evaluation.",
)
parser.add_argument(
"--pretrained_checkpoint",
type=str,
default="checkpoints/pretrained/multi_format_model/",
help="path to the pretrained checkpoint",
)
parser.add_argument(
"--lora_checkpoints_folder",
type=str,
default=None,
help="LoRA checkpoints folder, if none then base model is used",
)
parser.add_argument(
"--quantization", type=str, default=None, help="Qusantization type bitsandbytes or none"
)
parser.add_argument("--max_tokens", type=int, default=8192, help="Max tokens")
parser.add_argument("--temperature", type=float, default=0.0, help="Temperature for sampling")
parser.add_argument(
"--n_sample", type=int, default=1, help="Number of samples to generate per input"
)
parser.add_argument(
"--experiment_folder", type=str, default="experiments/tti/new/", help="submission folder"
)
parser.add_argument(
"--formatter",
type=str,
default="arclib.messagers.GPTTextMessageRepresenterV2",
help="formatter for the task, better to be same with the one used for training",
)
parser.add_argument(
"--max_lora_rank",
type=int,
default=64,
help="Max lora rank, should be same with the one used for training",
)
parser.add_argument(
"--include_n",
type=int,
nargs="+",
default=[1],
help="Which leave-n tasks to include, it is generally 1 for test time trained model, 0 for base model",
)
parser.add_argument(
"--permute_n",
type=int,
default=2,
help="Number of permutations to generate for each leave-n task",
)
parser.add_argument(
"--new_format", action="store_true", help="Whether to use the new format or not"
)
parser.add_argument(
"--barc_format", action="store_true", help="Whether to use the new format or not"
)
parser.add_argument(
"--add_diff_format", action="store_true", help="Whether to use the new format or not"
)
parser.add_argument(
"--use_all_lora", action="store_true", help="single trained lora"
)
args = parser.parse_args()
# set seed
np.random.seed(args.seed)
torch.manual_seed(args.seed)
# print args
print("Arguments:")
for arg in vars(args):
print(f"{arg}: {getattr(args, arg)}")
os.makedirs(args.experiment_folder, exist_ok=True)
tasks = read_tasks_from_single_file(args.data_file, solution_file=args.solution_file, test=True)
id_to_lora_path = {}
# get lora paths and filter tasks if necessary
if args.lora_checkpoints_folder is not None:
id_to_lora_path = {}
for lora_path in glob.glob(f"{args.lora_checkpoints_folder}/*/adapter_model.bin"):
lora_id = lora_path.split("/")[-2]
id_to_lora_path[lora_id] = lora_path
lora_dir = os.path.dirname(lora_path)
if args.num_examples is not None:
# shuffle
np.random.seed(args.seed)
np.random.shuffle(tasks)
tasks = tasks[: args.num_examples]
formatters = []
if args.new_format:
messager = GPTTextMessageRepresenterV2(
task_representer=TextTaskRepresenter(
example_representer=TextExampleRepresenter(
io_sep=" -> ",
input_header="",
output_header="",
output_footer="#",
grid_representer=PythonListGridRepresenter(),
)
)
)
formatters.append(messager)
elif args.barc_format:
messages = arclib.messagers.GPTTextMessageRepresenterForBarc(
task_representer=arclib.representers.TextTaskRepresenter(
example_representer=arclib.representers.TextExampleRepresenter(
grid_representer=arclib.representers.WordGridRepresenter(),
input_header="Input:\n",
output_header="\nOutput:\n",
io_sep="\n"
)))
formatters.append(messages)
else:
messager = arclib.messagers.GPTTextMessageRepresenterV2()
formatters.append(messager)
if args.add_diff_format:
diff_formatter = TextTaskRepresenter(
example_representer=DiffExampleRepresenter(
use_output=False,
io_sep=" -> ",
input_header="",
output_header="",
output_footer="#",
grid_representer=PythonListGridRepresenter(),
)
)
input_diff_formatter = GPTTextMessageRepresenterV2(task_representer=diff_formatter)
formatters.append(input_diff_formatter)
tokenizer = AutoTokenizer.from_pretrained(args.pretrained_checkpoint)
task_name_to_processed_data = get_preprocessed_tasks(
tasks,
tokenizer,
formatters,
max_tokens=args.max_tokens,
id_to_lora_path=id_to_lora_path,
include_n=args.include_n,
permute_n=args.permute_n,
)
valid_tasks = [info for key, info in task_name_to_processed_data.items() if info["valid"]]
invalid_tasks = [info for key, info in task_name_to_processed_data.items() if not info["valid"]]
print("Len of valid tasks:", len(valid_tasks))
print("Len of invalid tasks:", len(invalid_tasks))
# for each valid task print the length of queries
for info in valid_tasks:
print(f"{info['task'].name}: Number of Queries: {len(info['queries'])}")
example_task = valid_tasks[0]
example_task_id = example_task["task"].name.split("-")[0]
print("Example Task Information:")
print(f"Task Name: {example_task['task'].name}")
print(f"Number of Queries: {len(example_task['queries'])}")
print("Example Query:" + example_task["queries"][0]["text"])
# lora_path = f"{args.lora_checkpoints_folder}/{example_task_id}/"
# abstract away
inputs_to_the_engine = []
inputs_to_remember = {}
lora_path_idxs = list(id_to_lora_path.keys())
if len(lora_path_idxs) > 0:
# load one adapter_config.json
with open(
id_to_lora_path[lora_path_idxs[0]].replace("adapter_model.bin", "adapter_config.json")
) as f:
lora_adapter_config = json.load(f)
else:
lora_adapter_config = {}
engine = initialize_engine(
model=args.pretrained_checkpoint,
quantization=args.quantization,
max_lora_rank=lora_adapter_config.get("r", args.max_lora_rank),
enable_lora=args.lora_checkpoints_folder is not None,
enforce_eager=False,
lora_target_modules=lora_adapter_config.get("target_modules", None),
)
for i, info in enumerate(valid_tasks):
name = info["task"].name
idx, no = name.split("-")
if args.lora_checkpoints_folder is not None:
lora_path = id_to_lora_path[idx]
lora_path = os.path.dirname(lora_path)
# get the parent folder
if args.use_all_lora:
lora_path = os.path.join(os.path.dirname(lora_path), "all/")
lora_index = lora_path_idxs.index(idx)
lora_request = LoRARequest(idx + no, lora_index, lora_path)
else:
lora_request = None
test_inputs = info["queries"]
for j, test_input in enumerate(test_inputs):
input_token_length = len(tokenizer.encode(test_input["text"])) - 1
sampling_param = get_sampling_params(
tokenizer,
input_token_length,
args.max_tokens,
temperature=args.temperature,
n=args.n_sample,
)
inputs_to_the_engine.append(
(test_input["text"], sampling_param, lora_request, name + "-" + str(j))
)
inputs_to_remember[name + "-" + str(j)] = test_input
print(f"Number of input queries to the engine: {len(inputs_to_the_engine)}")
outputs_by_key = process_requests(engine, inputs_to_the_engine)
for key in list(outputs_by_key.keys()):
inverter = inputs_to_remember[key]["inverter"]
if inverter is not None:
inverter_fn = eval("arclib.augmenters." + inverter)
else:
inverter_fn = np.array
outputs = outputs_by_key[key]
outputs_by_key[key] = []
current_formatter_repr = inputs_to_remember[key]["formatter"]
input = inputs_to_remember[key]["input"]["content"]
current_formatter = eval(current_formatter_repr)
for output in outputs:
output = output.replace("#", "")
output = output.replace(" ", " ")
if "```" in output:
# get things between ``` and ```
output = output.split("```")[1]
output = output.strip()
input = input.split("Here is the input grid for the test example:\nInput:\n")[-1]
input = input.split("\n\n\nDirectly provide")[0]
input = input.strip()
try:
decoded = current_formatter.task_representer.example_representer.decode(
(input, output)
)
except Exception as e:
print(f"Cannot Decode: {e}")
print(f"Input: {input}")
print(f"Output: {output}")
continue
try:
pred = to_tuple(inverter_fn(decoded.output))
except Exception as e:
print(f"Error: {e}")
continue
if decoded is not None:
outputs_by_key[key].append(
{
"output": to_tuple(inverter_fn(decoded.output)),
"inverter": inverter,
"formatter": current_formatter_repr,
}
)
outputs_by_key = {key: outputs for key, outputs in outputs_by_key.items() if len(outputs) > 0}
# save
all_predictions_file = os.path.join(args.experiment_folder, "all_predictions.json")
with open(all_predictions_file, "w") as f:
json.dump(outputs_by_key, f)
outputs = {}
for task in tasks:
name = task.name
to_vote = [out for key, out in outputs_by_key.items() if name in key]
to_vote = [out for sublist in to_vote for out in sublist]
if len(to_vote) == 0:
outputs[name] = [[[0]], [[0]]]
continue
else:
attempt_1, attempt_2 = vote(to_vote)
outputs[name] = [to_list(attempt_1), to_list(attempt_2)]
predictions = [outputs[task.name] for task in tasks]
submission_file = os.path.join(args.experiment_folder, "submission.json")
make_submission(tasks, predictions, submission_file, number_of_attempts=2)
print(f"Submission file is saved to {submission_file}")
# evaluate
if args.solution_file is not None:
evaluate(args.data_file, args.solution_file, submission_file)