-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathutils.py
More file actions
319 lines (276 loc) · 10.4 KB
/
Copy pathutils.py
File metadata and controls
319 lines (276 loc) · 10.4 KB
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
import os
from datasets import load_dataset
from datasets import Dataset
from collections import defaultdict
import random
from transformers import AutoTokenizer, AutoModelForCausalLM
from pt import CODE_GEN_PT_DICT
from src.data import CodeGenDataset, HumanEvalZero_Data, MBPP_Data
from src.data import SynCodeGen_Data
from src.codellm import LocalVLLM, AbstLiteLLM, AbstLLM
SPLIT_SYM = "::::"
RESULTS_DIR = "results"
os.makedirs(RESULTS_DIR, exist_ok=True)
OVERFIT_DIR = os.path.join(RESULTS_DIR, "overfit_dir")
os.makedirs(OVERFIT_DIR, exist_ok=True)
GENERATED_CODE_DIR = os.path.join(RESULTS_DIR, "generated_code")
os.makedirs(GENERATED_CODE_DIR, exist_ok=True)
FINAL_RES = "final_res"
os.makedirs(FINAL_RES, exist_ok=True)
EXE_ENV_DIR = "exe_env"
os.makedirs(EXE_ENV_DIR, exist_ok=True)
NEW_PROMPT_DIR = os.path.join(RESULTS_DIR, "new_prompt")
os.makedirs(NEW_PROMPT_DIR, exist_ok=True)
EXE_RES_DIR = os.path.join(RESULTS_DIR, "exe_res_dir")
os.makedirs(EXE_RES_DIR, exist_ok=True)
PASS_AT_K_DIR = os.path.join(RESULTS_DIR, "pass_at_k")
os.makedirs(PASS_AT_K_DIR, exist_ok=True)
PARTIAL_LIST = [
0, 0.25, 0.5, 0.75, 1.0
]
STOP_TOKENS_DICT = {
"chat": [],
"competition": [
"\n>>>", "\n$", '\nclass',
'\ndef', '\n#', '\nprint',
"\n@", "\nassert", '\nfor',
"\nif __name__ == '__main__':",
'\nif __name__ == "__main__":'
]
}
def model_id2name_cls(model_id: int):
API_NUN = 4
llm_config = {
"tp_size": 1,
"max_model_len": 8192,
"is_lora": False,
"model_type": "chat"
}
if model_id == 0:
model_name = "us.anthropic.claude-3-5-haiku-20241022-v1:0"
model_cls = AbstLiteLLM
provider = "bedrock"
elif model_id == 1:
model_name = "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
model_cls = AbstLiteLLM
provider = "bedrock"
elif model_id == 2:
model_name = "gemini-1.5-flash-002"
model_cls = AbstLiteLLM
provider = "vertex_ai"
elif model_id == 3:
model_name = "gemini-2.0-flash-001"
model_cls = AbstLiteLLM
provider = "vertex_ai"
elif model_id == API_NUN + 0:
model_name = "meta-llama/Llama-3.2-1B"
model_cls = LocalVLLM
provider = "openai"
llm_config["is_lora"] = True
llm_config["model_type"] = "competition"
llm_config['tp_size'] = 8
elif model_id == API_NUN + 1:
model_name = "meta-llama/Llama-3.2-3B"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
llm_config["is_lora"] = True
elif model_id == API_NUN + 2:
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
model_cls = LocalVLLM
provider = "openai"
llm_config["is_lora"] = True
elif model_id == API_NUN + 3:
model_name = "meta-llama/Llama-3.1-8B"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 4:
model_name = "meta-llama/CodeLlama-7b-hf"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 5:
model_name = "meta-llama/CodeLlama-13b-hf"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 6:
model_name = "deepseek-ai/DeepSeek-V2-Lite"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 7:
model_name = "deepseek-ai/DeepSeek-Coder-V2-Lite-Base"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 8:
model_name = "meta-llama/Llama-3.1-8B-Instruct"
model_cls = LocalVLLM
provider = "openai"
elif model_id == API_NUN + 9:
model_name = "Qwen/Qwen2.5-Coder-7B-Instruct"
model_cls = LocalVLLM
provider = "openai"
elif model_id == API_NUN + 10:
model_name = "Qwen/Qwen2.5-Coder-7B"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 11:
model_name = "Qwen/Qwen2.5-7B-Instruct"
model_cls = LocalVLLM
provider = "openai"
elif model_id == API_NUN + 12:
model_name = "Qwen/Qwen2.5-7B"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 13:
model_name = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
model_cls = LocalVLLM
provider = "openai"
llm_config['tp_size'] = 8
llm_config['dtype'] = 'bfloat16'
elif model_id == API_NUN + 14:
model_name = "meta-llama/Llama-4-Scout-17B-16E"
model_cls = LocalVLLM
provider = "openai"
llm_config['tp_size'] = 8
llm_config['dtype'] = 'bfloat16'
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 15:
model_name = "Qwen/Qwen3-0.6B"
model_cls = LocalVLLM
provider = "openai"
elif model_id == API_NUN + 16:
model_name = "Qwen/Qwen3-0.6B-Base"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 17:
model_name = "Qwen/Qwen3-8B"
model_cls = LocalVLLM
provider = "openai"
elif model_id == API_NUN + 18:
model_name = "Qwen/Qwen3-8B-Base"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
elif model_id == API_NUN + 19:
model_name = "Qwen/Qwen3-14B"
model_cls = LocalVLLM
provider = "openai"
llm_config['tp_size'] = 4
elif model_id == API_NUN + 20:
model_name = "Qwen/Qwen3-14B-Base"
model_cls = LocalVLLM
provider = "openai"
llm_config["model_type"] = "competition"
llm_config['tp_size'] = 4
elif model_id == API_NUN + 21:
model_name = "Qwen/Qwen3-30B-A3B"
model_cls = LocalVLLM
provider = "openai"
llm_config['tp_size'] = 4
elif model_id == API_NUN + 22:
model_name = "Qwen/Qwen3-30B-A3B-Base"
model_cls = LocalVLLM
provider = "openai"
llm_config['tp_size'] = 4
llm_config["model_type"] = "competition"
else:
raise ValueError(f"Model ID {model_id} is not valid")
shrink_model_name = model_name.split('/')[-1]
return provider, model_name, model_cls, llm_config, shrink_model_name
def load_finetune_model(model_id: int):
if model_id in [0, 1, 2]:
_, model_name, _, _ = model_id2name_cls(model_id)
else:
raise ValueError(f"Model {model_id} not supported")
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
model.model_name = model_name.split('/')[-1]
tokenizer = AutoTokenizer.from_pretrained(model_name)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return model, tokenizer
def load_benchmark_model(model_id: int) -> AbstLLM:
provider, model_name, model_cls, llm_config, shrink_model_name = model_id2name_cls(model_id)
model = model_cls(provider, model_name, llm_config)
model.model_name = shrink_model_name
return model
def make_task_name(model_name, dataset, partial):
if partial is not None:
task_name = model_name + SPLIT_SYM + dataset.data_name + SPLIT_SYM + str(partial)
else:
assert dataset is None
task_name = model_name
return task_name
def load_unique_dataset(dataset_name, split, group_name='data_id', random_seed=33):
# 1. Load the dataset
dataset = load_dataset(dataset_name, split=split)
data_id_to_samples = defaultdict(list)
for i, sample in enumerate(dataset):
data_id_to_samples[sample[group_name]].append(sample)
# 3. For each data_id, randomly select ONE sample using random_seed
rng = random.Random(random_seed)
unique_samples = []
for data_id, samples in data_id_to_samples.items():
selected = rng.choice(samples)
unique_samples.append(selected)
# 4. Optionally, return as a Hugging Face Dataset object
return Dataset.from_list(unique_samples)
def load_my_dataset(data_id: int, random_seed=33):
group_name = 'data_id'
if data_id == 0:
ds = load_dataset("HeyixInn0/Reorganized-humaneval", split="train")
data_name = 'HumanEvalZero'
dataset = HumanEvalZero_Data(ds, data_name)
elif data_id == 1:
ds = load_dataset("HeyixInn0/Reorganized-mbpp", split="train")
data_name = 'MBPP_sanitized'
dataset = MBPP_Data(ds, data_name)
elif data_id == 2:
raw_data_name = "CM/Dynamic_HumanEvalZero"
split = "Claude3.5_Sonnet"
ds = load_unique_dataset(raw_data_name, split, group_name, random_seed=random_seed)
data_name = "Sonnet_HumanEvalZero"
dataset = HumanEvalZero_Data(ds, data_name)
elif data_id == 3:
raw_data_name = "CM/Dynamic_MBPP_sanitized"
split = "Claude3.5_Sonnet"
ds = load_unique_dataset(raw_data_name, split, group_name, random_seed=random_seed)
data_name = "Sonnet_MBPP_sanitized"
dataset = MBPP_Data(ds, data_name)
elif data_id == 4:
raw_data_name = "CM/Dynamic_HumanEvalZero"
split = "Claude3.5_Haiku"
ds = load_unique_dataset(raw_data_name, split, group_name, random_seed=random_seed)
data_name = "Haiku_HumanEvalZero"
dataset = HumanEvalZero_Data(ds, data_name)
elif data_id == 5:
raw_data_name = "CM/Dynamic_MBPP_sanitized"
split = "Claude3.5_Haiku"
ds = load_unique_dataset(raw_data_name, split, group_name, random_seed=random_seed)
data_name = "Haiku_HumanEvalZero"
dataset = MBPP_Data(ds, data_name)
else:
raise NotImplementedError
return dataset
def load_lora(model_name, dataset, partial):
task_name = make_task_name(model_name, dataset, partial)
output_dir = os.path.join(OVERFIT_DIR, task_name)
if not os.path.exists(output_dir):
return None
sub_dirs = sorted(list(os.listdir(output_dir)))
return os.path.join(output_dir, sub_dirs[-1])
def dataset2ptdict(dataset):
if isinstance(dataset, CodeGenDataset):
return CODE_GEN_PT_DICT
else:
raise NotImplementedError
if __name__ == '__main__':
for data_id in range(6):
data = load_my_dataset(data_id)
print()