-
Notifications
You must be signed in to change notification settings - Fork 531
/
utils.py
501 lines (453 loc) · 17 KB
/
utils.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
import re
import json
import gzip
import torch
import numpy
import random
import logging
import itertools
import numpy as np
from typing import *
LANGUAGE_TAG = {
"c" : "// language: C",
"c++" : "// language: C++",
"cpp" : "// language: C++",
"c#" : "// language: C#",
"csharp" : "// language: C#",
"c-sharp" : "// language: C#",
"css" : "/* language: CSS */",
"cuda" : "// language: Cuda",
"dart" : "// language: Dart",
"lua" : "// language: Lua",
"objectivec" : "// language: Objective-C",
"objective-c" : "// language: Objective-C",
"objective-c++": "// language: Objective-C++",
"python" : "# language: Python",
"perl" : "# language: Perl",
"prolog" : f"% language: Prolog",
"swift" : "// language: swift",
"lisp" : "; language: Lisp",
"java" : "// language: Java",
"scala" : "// language: Scala",
"tex" : f"% language: TeX",
"vue" : "<!--language: Vue-->",
"markdown" : "<!--language: Markdown-->",
"html" : "<!--language: HTML-->",
"php" : "// language: PHP",
"js" : "// language: JavaScript",
"javascript" : "// language: JavaScript",
"typescript" : "// language: TypeScript",
"go" : "// language: Go",
"shell" : "# language: Shell",
"rust" : "// language: Rust",
"sql" : "-- language: SQL",
"kotlin" : "// language: Kotlin",
"vb" : "' language: Visual Basic",
"ruby" : "# language: Ruby",
"pascal" : "// language: Pascal",
"r" : "# language: R",
"fortran" : "!language: Fortran",
"lean" : "-- language: Lean",
"matlab" : f"% language: Matlab",
"delphi" : "{language: Delphi}",
"scheme" : "; language: Scheme",
"basic" : "' language: Basic",
"assembly" : "; language: Assembly",
"groovy" : "// language: Groovy",
"abap" : "* language: Abap",
"gdscript" : "# language: GDScript",
"haskell" : "-- language: Haskell",
"julia" : "# language: Julia",
"elixir" : "# language: Elixir",
"excel" : "' language: Excel",
"clojure" : "; language: Clojure",
"actionscript" : "// language: ActionScript",
"solidity" : "// language: Solidity",
"powershell" : "# language: PowerShell",
"erlang" : f"% language: Erlang",
"cobol" : "// language: Cobol",
"alloy" : "/* language: Alloy */",
"awk" : "// language: AWK",
"thrift" : "/* language: Thrift */",
"sparql" : "# language: SPARQL",
"augeas" : "// language: Augeas",
"cmake" : "# language: CMake",
"f-sharp" : "// language: F#",
"stan" : "// language: Stan",
"isabelle" : "(*language: Isabelle*)",
"dockerfile" : "# language: Dockerfile",
"rmarkdown" : "# language: RMarkdown",
"literate-agda": "-- language: Literate Agda",
"tcl" : "// language: Augeas",
"glsl" : "// language: GLSL",
"antlr" : "// language: ANTLR",
"verilog" : "// language: Verilog",
"racket" : "; language: Racket",
"standard-ml" : "(*language:Standard ML*)",
"elm" : "-- language: Elm",
"yaml" : "# language: YAML",
"smalltalk" : "'' language: Smalltalk",
"ocaml" : "(*language: OCaml*)",
"idris" : "-- language: Idris",
"visual-basic" : "' language: Visual Basic",
"protocol-buffer": "// language: Protocol Buffer",
"bluespec" : "// language: Bluespec",
"applescript" : "-- language: AppleScript",
"makefile" : "# language: Makefile",
"tcsh" : "# language: TCSH",
"maple" : "# language: Maple",
"systemverilog": "// language: SystemVerilog",
"literate-coffeescript": "# language: Literate CoffeeScript",
"vhdl" : "-- language: VHDL",
"restructuredtext": ".. language: reStructuredText",
"sas" : "* language: SAS",
"literate-haskell": "> language: Literate Haskell",
"java-server-pages": "// language: Java Server Pages",
"coffeescript" : "# language: CoffeeScript",
"emacs-lisp" : "; language: Emacs Lisp",
"mathematica" : "// language: Mathematica",
"xslt" : "<!--language: XSLT-->",
"zig" : "// language: Zig",
"common-lisp" : "; language: Common Lisp",
"stata" : "* language: Stata",
"agda" : "-- language: Agda",
"ada" : "-- language: Ada",
}
LANGUAGE_COMMENT_SIGN = {}
for lang in LANGUAGE_TAG:
LANGUAGE_COMMENT_SIGN[lang] = LANGUAGE_TAG[lang].split("language:")[0].strip()
IMPORT_HELPER = {
"python": [
"import math",
"import re",
"import sys",
"import copy",
"import datetime",
"import itertools",
"import collections",
"import heapq",
"import statistics",
"import functools",
"import hashlib",
"import numpy",
"import numpy as np",
"import string",
"from typing import *",
"from collections import *",
],
"go" : [
"math",
"strings",
"fmt",
"strconv",
"time",
"bytes",
"regexp",
"sort",
"math/rand",
"crypto/md5",
],
"cpp" : [
"#include<stdlib.h>",
"#include<algorithm>",
"#include<math.h>",
"#include<stdio.h>",
"#include<vector>",
"#include<string>",
"#include<climits>",
"#include<cstring>",
"#include<iostream>",
],
}
def set_random_seed(seed):
"""Set random seed for reproducability."""
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)
def stream_jsonl(filename: str) -> Iterable[Dict]:
"""
Parses each jsonl line and yields it as a dictionary
"""
if filename.endswith(".gz"):
with open(filename, "rb") as gzfp:
with gzip.open(gzfp, "rt") as fp:
for line in fp:
if any(not x.isspace() for x in line):
yield json.loads(line)
else:
with open(filename, "r") as fp:
for line in fp:
if any(not x.isspace() for x in line):
yield json.loads(line)
def stream_jsonl_all(filename: str) -> Iterable[Dict]:
results = []
if filename.endswith(".gz"):
fp = gzip.open(open(filename, "rb"), "rt")
else:
fp = open(filename, "r")
for line in fp:
if any(not x.isspace() for x in line):
results.append(json.loads(line))
fp.close()
return results
def read_dataset(
data_file: str = None,
dataset_type: str = "humanevalx",
) -> Dict:
if "humanevalx" in dataset_type.lower():
dataset = {task["task_id"]: task for task in stream_jsonl(data_file)}
elif "mbpp" in dataset_type.lower():
problems = {task["task_id"]: task for task in stream_jsonl(data_file)}
task_ids = sorted(problems.keys())[10:510]
dataset = {}
for task_id in task_ids:
sample = problems[task_id]
description = sample["text"]
test_example = sample["test_list"][0]
prompt = f'"""\n{description}\n{test_example}\n"""\n'
sample["prompt"] = prompt
dataset[task_id] = sample
elif "ds1000" in dataset_type.lower():
# install ds1000 from https://github.com/HKUNLP/DS-1000
from ds1000 import DS1000Dataset
ds1000 = DS1000Dataset(source_dir=data_file, libs="all", mode="Completion")
for lib in ds1000.libs:
for problem_id in range(len(ds1000[lib])):
prefix = ""
suffix = ""
insert_flag = False
first_line_flag = True
# extract prefix and suffix of the prompt
for line in ds1000[lib][problem_id]["prompt"].split("\n"):
if "[insert]" in line:
insert_flag = True
continue
if first_line_flag:
first_line_flag = False
else:
line = "\n" + line
if not insert_flag:
prefix += line
else:
suffix += line
else:
raise f"Dataset: {dataset_type} not supported."
return dataset
def read_translation_dataset(
data_file_src: str = None,
data_file_tgt: str = None,
lang_src: str = None,
lang_tgt: str = None,
dataset_type: str = "humanevalx",
) -> Dict:
if "humanevalx" in dataset_type.lower():
dataset_src = {task["task_id"]: task for task in stream_jsonl(data_file_src)}
dataset_tgt = {task["task_id"].split("/")[-1]: task for task in stream_jsonl(data_file_tgt)}
for k, sample in dataset_src.items():
prompt = "code translation\n"
if lang_src == "cpp":
prompt += "C++:\n"
elif lang_src == "js":
prompt += "JavaScript:\n"
else:
prompt += f"{lang_src}:\n".capitalize()
prompt += dataset_src[k]["declaration"] + "\n" + dataset_src[k]["canonical_solution"].rstrip() + "\n"
if lang_tgt == "cpp":
prompt += "C++:\n"
elif lang_tgt == "js":
prompt += "JavaScript:\n"
else:
prompt += f"{lang_tgt}:\n".capitalize()
prompt += dataset_tgt[k.split("/")[-1]]["declaration"]
dataset_src[k]["prompt"] = prompt
else:
raise f"Dataset: {dataset_type} not supported."
return dataset_src
def process_extra_prompt(
prompt: str,
language_type: str = "python",
dataset_type: str = None,
generation_mode: str = "completion",
) -> str:
"""
Processes the extra prompt.
"""
language = language_type.lower()
if dataset_type == "humanevalx":
extra_prompt = ""
# extra_prompt = LANGUAGE_TAG[language] + "\n"
prompt = prompt.strip()
if generation_mode == "instruction":
return "问:" + extra_prompt + prompt + "\n答:"
return extra_prompt + prompt
elif dataset_type == "mbpp":
extra_prompt = ""
prompt = prompt.strip()
return extra_prompt + prompt
else:
return prompt
def is_code_generation_finished(
code: str,
dataset_type: str = None,
language_type: str = None,
):
"""
Checks whether the generated code is finished.
"""
if dataset_type == "mbpp":
end_words = ["\ndef", "\nassert"]
for w in end_words:
if w == "\ndef":
if code.count(w) > 1:
return True
else:
if w in code:
return True
else:
if language_type.lower() == "python":
for line in code.split("\n"):
if len(line.strip()) > 0 and line[0] != ' ' and line[0] != '\t':
return True
end_words = ["\ndef", "\nclass", "\nif", "\n#", "\nprint"]
for w in end_words:
if w in code:
return True
elif language_type.lower() == "java":
if code.count("{") + 1 == code.count("}"):
return True
elif language_type.lower() == "go":
if "\nfunc main(" in code:
return True
if code.count("{") + 1 == code.count("}"):
return True
elif language_type.lower() == "js":
if code.count("{") + 1 == code.count("}"):
return True
elif language_type.lower() == "cpp":
if "\nint main()" in code:
return True
if code.count("{") + 1 == code.count("}"):
return True
elif language_type.lower() == "rust":
if "\nfn main()" in code:
return True
if code.count("{") + 1 == code.count("}"):
return True
return False
# Modified from https://github.com/bigcode-project/bigcode-evaluation-harness/blob/main/lm_eval/tasks/mbpp.py
stop_words=["\nclass", "\nassert", '\n"""', "\nprint", "\nif"]
def first_block(string, stop_words):
"""Split off first block of code by scanning for class, def etc. on newlines."""
return re.split("|".join(stop_words), string)[0].rstrip()
def cleanup_code(
code: str,
dataset_type: str = None,
language_type: str = None,
):
"""
Cleans up the generated code.
"""
if dataset_type == "mbpp":
end_words = ["\nassert", "\ndef"]
for w in end_words:
if w == "\ndef":
if code.count(w) > 1:
code = code[:code.rfind(w)]
else:
code = code[:code.rfind(w)]
code = first_block(code, stop_words)
elif dataset_type == "humanevalx":
if language_type.lower() == "python":
code_splits = code.split("\n")
is_empty_line = False
ind_empty_line = None
for i, line in enumerate(code_splits):
if len(line.strip()) > 0 and line[0] != ' ' and line[0] != '\t':
is_empty_line = True
ind_empty_line = i
break
if is_empty_line:
code = "\n".join(code_splits[:ind_empty_line])
else:
end_words = ["\ndef", "\nclass", "\n#", "\nassert", '\n"""', "\nprint", "\nif", "\n\n\n"]
for w in end_words:
if w in code:
code = code[:code.rfind(w)]
elif language_type.lower() == "java":
main_pos = code.find("public static void main")
if main_pos != -1:
code = code[:main_pos] + '}'
if '}' in code:
code = code[:code.rfind('}')] + '}'
if code.count('{') + 1 == code.count('}'):
code += "\n}"
elif language_type.lower() == "go":
if "\nfunc main(" in code:
code = code[:code.rfind("func main(")]
if '}' in code:
code = code[:code.rfind('}')] + '}'
elif language_type.lower() == "cpp":
if "\nint main()" in code:
code = code[:code.rfind("int main()")]
if '}' in code:
code = code[:code.rfind('}')] + '}'
elif language_type.lower() == "js":
if '}' in code:
code = code[:code.rfind('}')] + '}'
elif language_type.lower() == "rust":
if '}' in code:
code = code[:code.rfind('}')] + '}'
return code
def estimate_pass_at_k(
num_samples: Union[int, List[int], np.ndarray],
num_correct: Union[List[int], np.ndarray],
k: int
) -> np.ndarray:
"""
Estimates pass@k of each problem and returns them in an array.
"""
def estimator(n: int, c: int, k: int) -> float:
"""
Calculates 1 - comb(n - c, k) / comb(n, k).
"""
if n - c < k:
return 1.0
return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1))
if isinstance(num_samples, int):
num_samples_it = itertools.repeat(num_samples, len(num_correct))
else:
assert len(num_samples) == len(num_correct)
num_samples_it = iter(num_samples)
return np.array([estimator(int(n), int(c), k) for n, c in zip(num_samples_it, num_correct)])
class Logger:
def __init__(self, name, log_level=logging.INFO, log_file=None, log_mode="both", disable_formatter=False):
self.logger = logging.getLogger(name)
self.logger.setLevel(log_level)
self.formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# Log to console
if log_mode == "both" or log_mode == "terminal":
console_handler = logging.StreamHandler()
if not disable_formatter:
console_handler.setFormatter(self.formatter)
self.logger.addHandler(console_handler)
# Log to file
if log_file is not None:
if log_mode == "both" or log_mode == "file":
file_handler = logging.FileHandler(log_file, mode='w')
if not disable_formatter:
file_handler.setFormatter(self.formatter)
self.logger.addHandler(file_handler)
def add_file_handler(self, file_name):
file_handler = logging.FileHandler(file_name, mode='w')
file_handler.setFormatter(self.formatter)
self.logger.addHandler(file_handler)
def debug(self, message):
self.logger.debug(message)
def info(self, message):
self.logger.info(message)
def warning(self, message):
self.logger.warning(message)
def error(self, message):
self.logger.error(message)
def critical(self, message):
self.logger.critical(message)