forked from zhao-ht/DensePolicy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelines.py
538 lines (469 loc) · 18 KB
/
pipelines.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import copy
import json
import os.path
from threading import Thread, BoundedSemaphore, Lock
import pandas as pd
from torch.utils.data import Subset
from tqdm import tqdm
from dataset.get_dataset import get_dataset_part
from sup_func.sup_func import pd_concat_ignore2, eval_pd_dataset, flat_row
def answer_eval(response, dataset, data, evaluator=None, model_result_recoder=None):
result = copy.copy(data)
if response["res"] is None:
score = 0
else:
if dataset in [
"baking",
"barman",
"blocks",
"block_medium",
"blockworld",
"doors",
"elevator",
"footwear",
"fridge",
"glibrearrangement",
"gripper",
"hanoi",
"mineraft",
"newspapers",
"spannerlearning",
"strage",
"termes",
"tireworld_test",
"trapnewspapers",
"tyreworld",
]:
score = evaluator(response["res"], data)
elif dataset in [
"alfworld_put",
"alfworld_clean",
"alfworld_heat",
"alfworld_cool",
"alfworld_examine",
"alfworld_puttwo",
"alfworld"
]:
score = evaluator(response["res"], data)
elif dataset in ['m3tooleval']:
score = evaluator(response["res"], data)
elif dataset in ['gsm8k', 'math']:
score = evaluator(response["res"], data)
elif dataset in ['babyai', 'maze', 'wordle', 'sciworld', 'sqlgym', 'textcraft', 'tool', 'webarena', 'webshop']:
score = evaluator(response["res"], data)
else:
print("evaluation of dataset {} not implied yet".format(dataset))
raise ValueError
result["score"] = score
if model_result_recoder is None:
# default response record items
result["res"] = response["res"]
if isinstance(response["gen"], dict):
for key in response["gen"].keys():
result[key] = response["gen"][key]
else:
result["generation"] = response["gen"]
else:
# model specific response record
result = model_result_recoder(response, result)
return score, result
def save_result_jsonl(file_name, result):
with open(file_name, "a") as f:
f.write(json.dumps(result) + "\n")
f.flush()
def save_result_pd(file_name, result, sort_columns=False):
# File index is not used
df = pd.DataFrame([[result[key]
for key in result.keys()]], columns=result.keys())
if os.path.exists(file_name):
result_per_dataset_table_permutated_all = pd_concat_ignore2(
pd.read_csv(file_name, index_col=0), df
)
else:
result_per_dataset_table_permutated_all = df
if sort_columns:
result_per_dataset_table_permutated_all = (
result_per_dataset_table_permutated_all.sort_index(axis=1)
)
if os.path.dirname(file_name) != "" and not os.path.exists(
os.path.dirname(file_name)
):
os.makedirs(os.path.dirname(file_name))
result_per_dataset_table_permutated_all.to_csv(file_name, header=True)
def update_result_pd(file_name, result, replace_id):
df = pd.DataFrame([[result[key]
for key in result.keys()]], columns=result.keys())
if os.path.exists(file_name):
result_df = pd.read_csv(file_name, index_col=0)
result_df.at[replace_id, "usable"] = False # Mark for filter
result_per_dataset_table_permutated_all = pd_concat_ignore2(
result_df, df)
else:
result_per_dataset_table_permutated_all = df
if os.path.dirname(file_name) != "" and not os.path.exists(
os.path.dirname(file_name)
):
os.makedirs(os.path.dirname(file_name))
result_per_dataset_table_permutated_all.to_csv(file_name, header=True)
def resume_result_jsonl(file_name):
lines = open(file_name).readlines()
num_skip_exps = len(lines)
for id, data in enumerate(map(json.loads, lines)):
if "score" not in data:
print(id, data)
scores = [data["score"] for data in map(json.loads, lines)]
return scores, num_skip_exps
def resume_result_pd(file_name, executed_column):
resume = pd.read_csv(file_name, usecols=[executed_column])
if "score" in resume:
scores = resume["score"].values.tolist()
elif "success" in resume:
scores = (resume["success"] > 0).values.tolist()
else:
print("Warnning! No score or success in resumed file. No score resumed")
scores = []
if executed_column in resume:
executed_samples = []
for sample_list in resume[executed_column].values.tolist():
if not pd.isna(sample_list):
try:
samples = eval(sample_list)
if not isinstance(samples, list):
executed_samples.append(str(samples))
else:
for item in eval(sample_list):
executed_samples.append(str(item))
except:
executed_samples.append(str(sample_list))
executed_samples = set(executed_samples)
num_skip_exps = len(executed_samples)
else:
num_skip_exps = len(resume)
executed_samples = set()
return scores, num_skip_exps, executed_samples
def resume_result_data_level(file_name, executed_column, distributed_test, distributed_id, distributed_number):
# specific function for codeact_agent_tree; chaneg each line into an individual df
if distributed_test:
total_rows = 0
for chunk in pd.read_csv(file_name, chunksize=1):
total_rows += len(chunk)
print('total line {}'.format(total_rows))
to_load_ids = get_dataset_part(range(total_rows), distributed_id, distributed_number)
resume = pd.read_csv(file_name,skiprows=range(1, to_load_ids[0]), nrows=len(to_load_ids))
else:
resume = pd.read_csv(file_name)
resume = eval_pd_dataset(resume)
if "score" in resume:
scores = resume["score"].values.tolist()
elif "success" in resume:
scores = (resume["success"] > 0).values.tolist()
else:
print("Warnning! No score or success in resumed file. No score resumed")
scores = []
if executed_column in resume:
executed_samples = []
for sample_list in tqdm(resume[executed_column].values.tolist()):
if not pd.isna(sample_list):
try:
samples = eval(sample_list)
if not isinstance(samples, list):
executed_samples.append(str(samples))
else:
for item in eval(sample_list):
executed_samples.append(str(item))
except:
executed_samples.append(str(sample_list))
executed_samples = set(executed_samples)
num_skip_exps = len(executed_samples)
else:
raise ValueError('executed_column {} not in resume'.format(executed_column))
flat_resume = pd.concat(resume.apply(flat_row, axis=1).tolist(), ignore_index=True)
return scores, num_skip_exps, executed_samples, flat_resume
def test_single_sample(
data, model, args, file_name, evaluator, is_parallel=False, ignore_error=False
):
global scores, f, pbar
if is_parallel or ignore_error: # ignore error to release the process of parallel
try:
response = model(data)
except Exception as e:
print(e)
response = {
"res": None,
"gen": None,
"error": "0_test_single_sample_{}".format(e),
}
else:
response = model(data)
if is_parallel:
lock.acquire()
score, result = answer_eval(
response, args.dataset_name, data, evaluator, model.result_recoder
)
scores.append(score)
pbar.set_description(f"Total Score : {100 * sum(scores) / len(scores)}")
save_result_pd(file_name, result)
if is_parallel:
lock.release()
pool.release()
def learn_single_sample(
data, model, args, file_name, evaluator, is_parallel=False, ignore_error=False
):
global scores, pbar
if is_parallel or ignore_error: # ignore error to release the process of parallel
try:
response_list = model.learn(data, evaluator)
except Exception as e:
print(e)
response_list = [
{
"tool_cases_list": [data],
"error": "0_learn_single_sample_{}".format(e),
"success": 0,
}
]
else:
response_list = model.learn(data, evaluator)
if is_parallel:
lock.acquire()
assert isinstance(response_list, list)
score_recorded = False
for response in response_list:
if "func_id" in response:
func_id, response = response["func_id"], response["response"]
if ("pre_version" not in response) or (
func_id is not None and response["pre_version"] is None
):
response["pre_version"] = func_id
else:
func_id = None
if "usable" not in response:
response["usable"] = response["success"] > 0
if not score_recorded:
score = float(response["success"])
scores.append(float(score > 0))
score_recorded = True
pbar.set_description(f"Total Score : {100 * sum(scores) / len(scores)}")
if func_id is not None:
update_result_pd(file_name, response, func_id)
else:
save_result_pd(file_name, response)
if is_parallel:
lock.release()
pool.release()
pool = BoundedSemaphore(4)
lock = Lock()
def training(dataloader, model, args):
global scores, pbar
train_index_key = dataloader["test_index_key"]
OUTPUT_PATH = (
args.learn_save_path
if args.learn_save_path is not None
else f"learn_results/{args.planning_method}/{args.dataset_name}_{args.split_dataset_num[0]}_split_{args.split_file}.csv"
)
print("Saving training to {}".format(OUTPUT_PATH))
if args.resume and os.path.exists(OUTPUT_PATH):
print("Resuming training from {}".format(OUTPUT_PATH))
scores, num_skip_exps, executed_samples = resume_result_pd(
OUTPUT_PATH, "tool_cases_list"
)
else:
num_skip_exps = 0
scores = []
executed_samples = set()
if os.path.exists(OUTPUT_PATH):
raise ValueError(
"Learned file exists. Cannot start a new learning. Please rename the learned file {} first.".format(
OUTPUT_PATH
)
)
trial = 0
threads = []
pbar = tqdm(dataloader["dataset"]["train"])
print("executed_samples: {}".format(len(executed_samples)))
for data in pbar:
trial += 1
if not args.parallel_learn:
if (
(
train_index_key in data
and str(data[train_index_key]) in executed_samples
)
or (
isinstance(data, list)
and train_index_key in data[0]
and set([str(item[train_index_key]) for item in data]).issubset(
executed_samples
)
)
or (str(data) in executed_samples)
):
print("skip")
continue
if dataloader["data_cleaner"] is not None and (
not dataloader["data_cleaner"](data)
):
print("Dirty Data! Skip")
continue
learn_single_sample(
data,
model,
args,
OUTPUT_PATH,
dataloader["evaluator"],
ignore_error=args.ignore_error,
)
else:
if (
train_index_key in data
and str(data[train_index_key]) in executed_samples
) or (str(data) in executed_samples):
print("skip")
continue
if dataloader["data_cleaner"] is not None and (
not dataloader["data_cleaner"](data)
):
print("Dirty Data! Skip")
continue
pool.acquire()
thread = Thread(
target=learn_single_sample,
args=(
data,
model,
args,
OUTPUT_PATH,
dataloader["evaluator"],
True,
args.ignore_error,
),
)
threads.append(thread)
thread.start()
for thread in threads:
thread.join(300) # 5 min for each task
if thread.is_alive():
print("A job didn't finish within the time limit")
print(f"Total Score : {100 * sum(scores) / len(scores)}")
print("Training finished")
def testing(dataloader, model, args):
global scores, pbar
trial = 0
test_index_key = dataloader["test_index_key"]
OUTPUT_PATH = (
args.eval_save_path
if args.eval_save_path is not None
else f"eval_results/{args.planning_method}.{args.model_name}/{args.dataset_name}.{args.split_dataset_num}_split_{args.split_file}.{args.exp_id}.{args.distributed_id}.csv"
)
print("Saving testing to {}".format(OUTPUT_PATH))
if args.resume_path is not None:
resume_path = args.resume_path
else:
if args.resume_from_merge:
parts = OUTPUT_PATH.split('.')
resume_path = '.'.join(parts[:-2] + ['None'] + [parts[-1]])
else:
resume_path = OUTPUT_PATH
if args.resume and os.path.exists(resume_path):
print("Resuming testing from {}".format(resume_path))
if args.resume_data_level:
assert args.resume_path is not None
print("Resuming testing data-level")
scores, num_skip_exps, executed_samples, previous_result_data_level = resume_result_data_level(
resume_path, test_index_key, args.distributed_test, args.distributed_id, args.distributed_number
)
else:
scores, num_skip_exps, executed_samples = resume_result_pd(
resume_path, test_index_key
)
if args.resume_from_merge or args.resume_path is not None:
if os.path.exists(OUTPUT_PATH):
raise ValueError(
"Eval result file exists. Cannot start a new testing. Please rename the eval result file {} first.".format(
OUTPUT_PATH
)
)
else:
scores = []
num_skip_exps = 0
executed_samples = set()
if os.path.exists(OUTPUT_PATH):
raise ValueError(
"Eval result file exists. Cannot start a new testing. Please rename the eval result file {} first.".format(
OUTPUT_PATH
)
)
print("Executed_samples: {}".format(len(executed_samples)))
dataset = dataloader["dataset"]["test"]
assert test_index_key == 'index'
if not args.resume_data_level:
left_index = [index for index in range(len(dataset)) if str(index) not in executed_samples]
else:
# for data-level resume, only run the data that have been run
left_index = [index for index in range(len(dataset)) if str(index) in executed_samples]
dataset = Subset(dataset, left_index)
if not args.resume_data_level:
if args.distributed_test:
dataset = get_dataset_part(dataset, args.distributed_id, args.distributed_number)
pbar = tqdm(dataset)
threads = []
for data in pbar:
trial += 1
if not args.parallel_test:
# if str(data[test_index_key]) in executed_samples or (
# str(data[test_index_key]).replace(
# "\r\n", "\n") in executed_samples
# ):
# print("skip")
# continue
if dataloader["data_cleaner"] is not None and (
not dataloader["data_cleaner"](data)
):
print("Dirty Data! Skip")
continue
if args.resume_data_level:
previous_result = previous_result_data_level[
previous_result_data_level[test_index_key] == data[test_index_key]]
assert len(previous_result) > 0
data['previous_result'] = previous_result
test_single_sample(
data,
model,
args,
OUTPUT_PATH,
dataloader["evaluator"],
ignore_error=args.ignore_error,
)
else:
# if (
# str(data[test_index_key]) in executed_samples
# or (str(data[test_index_key]).replace("\r\n", "\n")) in executed_samples
# ):
# print("skip")
# continue
if dataloader["data_cleaner"] is not None and (
not dataloader["data_cleaner"](data)
):
print("Dirty Data! Skip")
continue
pool.acquire()
thread = Thread(
target=test_single_sample,
args=(
data,
model,
args,
OUTPUT_PATH,
dataloader["evaluator"],
True,
args.ignore_error,
),
)
threads.append(thread)
thread.start()
for thread in threads:
thread.join(300) # 5 min for each task
if thread.is_alive():
print("A job didn't finish within the time limit")
print(f"Total Score : {100 * sum(scores) / len(scores)}")
print("Testing finished")