-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (111 loc) · 3.91 KB
/
main.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
import numpy as np
import torch
import random
import os
from tqdm import tqdm
import sys
import argparse
from datasets import load_dataset
from dataclasses import dataclass, asdict
import json
from pprint import pprint
import itertools
import src
from src import tasks_repo as tasks_mapping
from src import Evaluator
from src.fuzzycopy import generator
from src.utils import load_json, Timer, time_str
# Check if CUDA is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = "cpu"
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--tasks", type=str, default="a_level,a_level_symbol")
parser.add_argument("--model_id", type=str, default="meta-llama/Llama-2-7b-hf")
parser.add_argument("--batch_size", type=int, default=3)
parser.add_argument("--num_fewshot", type=int, default=10)
parser.add_argument("--description_dict_path", default="./data/description.json")
parser.add_argument(
"--limit",
type=float,
default=100,
help="Limit the number of examples per task. "
"If <1, limit is a percentage of the total number of examples.",
)
parser.add_argument("--output_base_path", type=str, default=None)
return parser.parse_args()
def inference(
task_name,
evaluator,
n_shot,
description,
limit,
output_base_path,
):
print(f"inference for {task_name}...")
timer = Timer()
task = tasks_mapping[task_name]()
# print(task.dataset)
dataset = task.dataset
train_examples = task.training_docs()
test_examples = task.validation_docs()
### sample ids
rnd = random.Random()
rnd.seed(3407)
test_examples = list(test_examples)
rnd_test_shuffle = random.Random()
rnd_test_shuffle.seed(3407)
rnd_test_shuffle.shuffle(test_examples) # TODO: tem zhuoyan
kwangs_fewshot_context = {}
if limit is not None:
limit = int(len(test_examples) * limit) if limit < 1.0 else int(limit)
accs = 0
results = []
for doc_id, doc in enumerate(itertools.islice(test_examples, 0, limit)):
if task_name == "ab_level":
kwangs_fewshot_context.update({"doc_id": doc_id})
if (doc_id+1) % 10 == 0:
print(f"===== {task_name} =========== {doc_id+1}")
qn = task.fewshot_context(doc, n_shot , rnd, description=description, **kwangs_fewshot_context)
prompt = qn
answer = task.doc_to_target(doc)
# print(qn)
# print(answer)
result = evaluator.eval(prompt, answer, seq_len = 20)
accs += result.accuracy
results.append(asdict(result))
results = [{"acc": accs / min(len(test_examples),limit)}] + results
if output_base_path:
file_path = output_base_path
else:
file_path = f"./output/debug"
file_name = f"{task_name}"
if not os.path.exists(file_path):
os.makedirs(file_path)
print(f"save to {file_path}/{file_name}")
with open(
f"{file_path}/{file_name}.json",
"w",
) as f:
json.dump(results, f, indent = 4)
print(f"{task_name} | {evaluator.get_model_id()} | use time {time_str(timer.end())}")
def main():
args = parse_args()
task_names = args.tasks.split(",")
print("this run comtain tasks: ", task_names)
description_dict = load_json(args.description_dict_path)
timer = Timer()
evaluator = Evaluator(args.model_id, device)
for task_name in task_names:
description = description_dict.get(task_name) or ""
inference(
task_name = task_name,
evaluator = evaluator,
n_shot = args.num_fewshot,
description = description,
limit = args.limit,
output_base_path = args.output_base_path
)
print(f"{args.model_id} all done | use time {time_str(timer.end())}")
if __name__ == '__main__':
main()