-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_refinements_codegen_finetuned.py
233 lines (202 loc) · 7.33 KB
/
generate_refinements_codegen_finetuned.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
from tqdm import tqdm
from datasets import Dataset, load_dataset, concatenate_datasets
from jaxformer.hf.codegen import modeling_codegen
from jaxformer.hf import sample
import torch
import pprint
import os
import logging
import json
import csv
import argparse
import re
def load_jsonl(filepath):
data = [json.loads(line) for line in open(filepath).readlines()]
fields = data[0].keys()
data_dict = {k: [x[k] for x in data] for k in fields}
ds = Dataset.from_dict(data_dict)
return ds
def load_csv(filepath):
data = list(csv.DictReader(open(filepath)))
fields = data[0].keys()
data_dict = {k: [x[k] for x in data] for k in fields}
ds = Dataset.from_dict(data_dict)
return ds
def load_feedback(feedback_path):
extension = "csv" if feedback_path.endswith("csv") else "json"
if extension == "json":
d = load_jsonl(feedback_path)
else:
d = load_csv(feedback_path)
d = d.map(
lambda _, idx: {"row_id": idx},
with_indices=True,
)
d = d.filter(
lambda example: example["Refinement"] is not None and example["Refinement"]
)
return d
def sample_code_from_codegen(args, prompt, model, tokenizer):
device = "cuda:0"
completions = []
print(f"Tokenizing input: {prompt}")
input_ids = tokenizer(
prompt, truncation=True, max_length=1024, return_tensors="pt"
).input_ids.cuda()
if args.temperature == 0.0:
args.num_samples = 1
for i in range(args.num_samples):
try:
# Note: max_length is max length of input IDs, and max_length_sample is max length for completion (not including input IDs)
if args.temperature > 0:
tokens = model.generate(
input_ids,
do_sample=True,
num_return_sequences=1,
max_length=input_ids.shape[1] + 1024,
temperature=args.temperature,
use_cache=True,
)
else:
tokens = model.generate(
input_ids,
num_return_sequences=1,
max_length=input_ids.shape[1] + 1024,
use_cache=True,
)
text = tokenizer.decode(tokens[0])
if "<|endoftext|>" in text:
text = text[: text.find("<|endoftext|>")]
completions.append(text)
except RuntimeError as e:
logging.error(f"Could not sample from model: {e}")
return completions
def truncate(ex, tokenizer, max_length):
return tokenizer.decode(
tokenizer(ex, max_length=max_length, truncation=True).input_ids
)
def format_mbpp_prompt(mbpp, task_id):
idx = mbpp["task_id"].index(task_id)
text = mbpp["text"][idx]
tests = mbpp["test_list"][idx]
sample_code = mbpp["code"][idx]
# Create prompt from scratch
prompt = f'"""\n{text}\n\n'
# Add the first unit test as an input-output example
example = tests[0].split("assert ")[-1].replace("==", "=")
prompt += f">>> Example: {example}\n"
# Add code prefix
fn_name = tests[0].split("assert ")[-1].split("(")[0]
fn_search = re.search(f"def {fn_name}\(.*\):", sample_code)
if fn_search is None:
raise ValueError(
f"Could not find 'def {fn_name}\(.*\):' in code for task {task_id}."
)
code_prefix = sample_code[: fn_search.end()]
prompt = f'{prompt}"""\n\n{code_prefix}\n'
return prompt
def gen_refinement_prompt(args, example, tokenizer, mbpp):
prompt = (
f"OLD CODE:\n{truncate(example[args.completion_column], tokenizer, 512)}"
f"\n\nFEEDBACK:\n{example['Feedback']}\n\n"
f"REFINEMENT:\n{format_mbpp_prompt(mbpp, example['task_id'])}"
)
return prompt
def gen_code(args, data, model, tokenizer):
mbpp = load_dataset("mbpp")
mbpp = concatenate_datasets([mbpp[k] for k in mbpp.keys()])
output = data.map(
lambda ex: {"input_str": gen_refinement_prompt(args, ex, tokenizer, mbpp)}
)
output = output.map(
lambda ex: {
"output_strs": sample_code_from_codegen(
args, ex["input_str"], model, tokenizer
)
},
desc="Sampling code from codegen...",
)
return output
def generate_code_for_problems(args):
data = load_feedback(args.feedback_file)
if args.model_path is None:
model = modeling_codegen.CodeGenForCausalLM.from_pretrained(
f"{args.codegen_model_dir}/{args.arch}-mono",
revision="float16",
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
).cuda()
else:
model = modeling_codegen.CodeGenForCausalLM.from_pretrained(
args.model_path, low_cpu_mem_usage=True, torch_dtype=torch.float32
).cuda()
tokenizer = sample.create_custom_gpt2_tokenizer()
tokenizer.pad_token = 50256
val = gen_code(args, data, model, tokenizer)
output = []
for row in tqdm(val):
for completion in row["output_strs"]:
output.append(
{
"task_id": row["task_id"],
"prompt": row["input_str"],
"feedback": row["Feedback"],
"old_completion": row[args.completion_column],
"completion": completion,
}
)
return output
def write_jsonl(data, output_filepath):
with open(output_filepath, "w") as f:
for row in data:
f.write(json.dumps(row) + "\n")
def parse_args():
parser = argparse.ArgumentParser(
description="Run a trained model to generate Python code for the MBPP benchmark."
)
parser.add_argument(
"--arch", default="codegen-6B", choices=["codegen-16B", "codegen-6B"]
)
parser.add_argument(
"--codegen-model-dir",
default="checkpoints",
help="Directory where pre-trained CodeGen model checkpoints are saved.",
)
parser.add_argument(
"--model-path",
default=None,
required=True,
help="Directory to load model checkpoint from. If None, will load a pre-trained "
"CodeGen model using the --arch argument instead.",
)
parser.add_argument("--num-samples", default=1, type=int)
parser.add_argument("-d", "--debug", action="store_true")
parser.add_argument("--output-dir", type=str)
parser.add_argument("--output-file-suffix", type=str, default="")
parser.add_argument("--temperature", default=0.8, type=float)
parser.add_argument(
"--feedback-file",
default=None,
required=True,
help="CSV file containing feedback and past completions.",
)
parser.add_argument("--completion-column", default="completion")
args = parser.parse_args()
return args
def main(args):
argsdict = vars(args)
print(pprint.pformat(argsdict))
completions = generate_code_for_problems(args)
if args.model_path is None:
output_filepath = os.path.join(
args.output_dir,
f"refinements_{args.arch}_temp{args.temperature}_{args.output_file_suffix}.jsonl",
)
else:
output_filepath = os.path.join(
args.model_path,
f"refinements_{args.arch}_temp{args.temperature}_{args.output_file_suffix}.jsonl",
)
write_jsonl(completions, output_filepath)
if __name__ == "__main__":
main(parse_args())