-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting.py
165 lines (149 loc) · 5.27 KB
/
voting.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
import json
import numpy as np
import pandas as pd
from yaml import safe_load
import itertools
import statistics
import tqdm
global result
script = """
{function}
global result
result = sol()
"""
def loadGSM(path):
dataset = safe_load(open(path))
return [
{
"statement": problem["text"],
"solution": float(problem["response"].replace(",",""))
}
for problem in dataset
]
def loadSVAMP(svamp_path):
dataset = json.load(open(svamp_path))
return [
{
"statement": problem["Body"] + ". " + problem["Question"],
"solution": problem["Answer"],
}
for problem in dataset
]
def _eval_file(func, folder, i, k, solution, dataset="svamp"):
filename = folder + f"/{dataset}-{i:04d}-{k:02d}.py"
compiles = True
if func == "":
return False, False
if " while" in func:
return False, False
if "input(" in func:
return False, False
if " for" in func:
return False, False
if " if " in func:
return False, False
try:
exec(
script.format(function=func),
globals(),
globals(),
)
except:
print("EXCEPTION IN ", filename)
compiles = False
result = globals().get("result")
if result is None:
result = float('Inf')
return result, compiles
def eval_algorithm(folder, svamp, df, model, temp, flavor="svamp"):
results_k = np.empty((len(svamp), 8))
model_indx = df.model == model
temp_indx = df.temperature == temp
shots_indx = df.nshots==1
func_indx = model_indx & temp_indx & shots_indx
func_df = df[func_indx]
for i in range(len(svamp)):
problem = svamp[i]
results = []
sample_indx = func_df["sample"] == i
for k in range(10):
gen_indx = func_df.generation == k
response = func_df.loc[sample_indx & gen_indx].response.values[0]
results.append(_eval_file(response, folder, i, k, problem["solution"], flavor)[0])
results = list(map(lambda x: float(x) if (isinstance(x, (int, float) )) else float("-Inf"), results))
results_k[i, :] = [int(statistics.mode(results[:k]) == problem["solution"]) for k in range(3, 11)]
return {f"accuracy_{k}": f"{v:.3f}" for k, v in list(zip(range(3, 11), list(results_k[:i].mean(axis=0))))}
def main(result_root="experiments/output", svamp_path="SVAMP.json"):
svamp = loadSVAMP(svamp_path)
temperatures = ["0.1", "0.3", "0.5"]
nshots = [1]
models = [
"nvidia/OpenMath-Mistral-7B-v0.1-hf",
"meta-llama/Meta-Llama-3-8B",
"codellama/CodeLlama-34b-Python-hf",
"mistralai/Mistral-7B-v0.1",
"codellama/CodeLlama-13b-Python-hf",
"bigcode/starcoder2-7b",
"codellama/CodeLlama-7b-Python-hf",
"GPT-3/davinci-codex",
"bigcode/starcoder2-3b",
"GPT-3/cushman-codex",
"THUDM/codegeex2-6b",
"Salesforce/codegen25-7b-mono",
"Salesforce/codegen-16B-mono",
"Salesforce/codegen-6B-mono",
"Salesforce/codegen-2B-mono",
"EleutherAI/gpt-j-6B",
"facebook/incoder-6B",
"Salesforce/codegen-350M-mono",
"facebook/incoder-1B",
]
df = []
processed_svamp = pd.read_json("SVAMP-processed.json")
result_root = "C:/Users/pablo/outputs/output"
for model, temp, nshots in tqdm.tqdm(itertools.product(models, temperatures, nshots ),total=len(models)*len(temperatures)):
result_path = "{}/{}-shot/temp={}/{}".format(result_root, nshots, temp, model)
df.append({
"temperature": temp,
"nshots": nshots,
"model": model,
**eval_algorithm(result_path, svamp, processed_svamp, model, float(temp))
})
pd.DataFrame(df).to_csv("SVAMP-res-voteing.csv")
def gsm8k(result_root="experiments/output/", gsm8k_path="GSM-8K.yaml"):
dataset = loadGSM(gsm8k_path)
models = [
"nvidia/OpenMath-Mistral-7B-v0.1-hf",
"meta-llama/Meta-Llama-3-8B",
"codellama/CodeLlama-34b-Python-hf",
"mistralai/Mistral-7B-v0.1",
"codellama/CodeLlama-13b-Python-hf",
"bigcode/starcoder2-7b",
"codellama/CodeLlama-7b-Python-hf",
"bigcode/starcoder2-3b",
"THUDM/codegeex2-6b",
"Salesforce/codegen25-7b-mono",
"Salesforce/codegen-16B-mono",
"Salesforce/codegen-6B-mono",
"Salesforce/codegen-2B-mono",
"EleutherAI/gpt-j-6b",
"facebook/incoder-6B",
"Salesforce/codegen-350M-mono",
"facebook/incoder-1B",
]
temperatures = (0.1 , 0.3, 0.5)
nshots = (1,)
df = []
processed_gsm = pd.read_json("GSM-8K-processed.json")
for model, temp, nshots in tqdm.tqdm(itertools.product(models, temperatures, nshots ), total=len(models)*len(temperatures)):
result_path = "{}/{}-shot/temp={}/{}".format(result_root, nshots, temp, model)
df.append({
"temperature": temp,
"nshots": nshots,
"model": model,
**eval_algorithm(result_path, dataset, processed_gsm, model, float(temp), flavor="gsm-8k")
})
pd.DataFrame(df).to_csv("GSM-res-voting.csv")
if __name__ == "__main__":
main()
gsm8k()