Titovs commited on
Commit
f831a6a
1 Parent(s): 16bca66

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +141 -30
README.md CHANGED
@@ -1,30 +1,141 @@
1
- ---
2
- license: apache-2.0
3
- dataset_info:
4
- features:
5
- - name: task_id
6
- dtype: string
7
- - name: prompt
8
- dtype: string
9
- - name: entry_point
10
- dtype: string
11
- - name: test
12
- dtype: string
13
- - name: description
14
- dtype: string
15
- - name: language
16
- dtype: string
17
- - name: canonical_solution
18
- sequence: string
19
- splits:
20
- - name: train
21
- num_bytes: 505355
22
- num_examples: 161
23
- download_size: 174830
24
- dataset_size: 505355
25
- configs:
26
- - config_name: default
27
- data_files:
28
- - split: train
29
- path: data/train-*
30
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ dataset_info:
4
+ features:
5
+ - name: task_id
6
+ dtype: string
7
+ - name: prompt
8
+ dtype: string
9
+ - name: entry_point
10
+ dtype: string
11
+ - name: test
12
+ dtype: string
13
+ - name: description
14
+ dtype: string
15
+ - name: language
16
+ dtype: string
17
+ - name: canonical_solution
18
+ sequence: string
19
+ splits:
20
+ - name: train
21
+ num_bytes: 505355
22
+ num_examples: 161
23
+ download_size: 174830
24
+ dataset_size: 505355
25
+ configs:
26
+ - config_name: default
27
+ data_files:
28
+ - split: train
29
+ path: data/train-*
30
+ ---
31
+
32
+
33
+ # How to use
34
+
35
+ ```python
36
+ import torch
37
+ import jsonlines
38
+ import re
39
+ from tqdm import tqdm
40
+ from transformers import (
41
+ AutoTokenizer,
42
+ AutoModelForCausalLM,
43
+ StoppingCriteria,
44
+ StoppingCriteriaList,
45
+ )
46
+ from mxeval.data import get_data
47
+ from mxeval.evaluation import evaluate_functional_correctness
48
+ from datasets import load_dataset
49
+
50
+ class StoppingCriteriaSub(StoppingCriteria):
51
+
52
+ def __init__(self, stops, tokenizer):
53
+ (StoppingCriteria.__init__(self),)
54
+ self.stops = rf"{stops}"
55
+ self.tokenizer = tokenizer
56
+
57
+ def __call__(
58
+ self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs
59
+ ) -> bool:
60
+ last_three_tokens = [int(x) for x in input_ids.data[0][-3:]]
61
+ decoded_last_three_tokens = self.tokenizer.decode(last_three_tokens)
62
+
63
+ return bool(re.search(self.stops, decoded_last_three_tokens))
64
+
65
+
66
+ def generate(problem):
67
+
68
+
69
+ stopping_criteria = StoppingCriteriaList(
70
+ [
71
+ StoppingCriteriaSub(
72
+ stops= "\n}\n", tokenizer=tokenizer
73
+ )
74
+ ]
75
+ )
76
+
77
+
78
+ problem = tokenizer.encode(problem, return_tensors="pt").to('cuda')
79
+ sample = model.generate(
80
+ problem,
81
+ temperature=0.1,
82
+ max_new_tokens=256,
83
+ min_new_tokens=128,
84
+ pad_token_id=tokenizer.eos_token_id,
85
+ do_sample=False,
86
+ num_beams=1,
87
+ stopping_criteria=stopping_criteria,
88
+ )
89
+
90
+ answer = tokenizer.decode(sample[0], skip_special_tokens=True)
91
+
92
+ return answer
93
+
94
+ def clean_asnwer(code):
95
+
96
+ # Clean comments
97
+ code_without_line_comments = re.sub(r"//.*", "", code)
98
+ code_without_all_comments = re.sub(
99
+ r"/\*.*?\*/", "", code_without_line_comments, flags=re.DOTALL
100
+ )
101
+
102
+ #Clean signatures
103
+ lines = code.split("\n")
104
+ for i, line in enumerate(lines):
105
+ if line.startswith("fun "):
106
+ return "\n".join(lines[i + 1 :])
107
+
108
+ return code
109
+
110
+
111
+ model_name = "JetBrains/CodeLlama-7B-Kexer"
112
+ dataset = load_dataset("jetbrains/Kotlin_HumanEval")['train']
113
+ problem_dict = {problem['task_id']: problem for problem in dataset}
114
+
115
+ model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.bfloat16).to('cuda')
116
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
117
+
118
+
119
+ output = []
120
+ for key in tqdm(list(problem_dict.keys()), leave=False):
121
+ problem = problem_dict[key]["prompt"]
122
+ answer = generate(problem)
123
+ answer = clean_asnwer(answer)
124
+ output.append({"task_id": key, "completion": answer, "language": "kotlin"})
125
+
126
+
127
+ output_file = f"answers"
128
+ with jsonlines.open(output_file, mode="w") as writer:
129
+ for line in output:
130
+ writer.write(line)
131
+
132
+ evaluate_functional_correctness(
133
+ sample_file=output_file,
134
+ k=[1],
135
+ n_workers=16,
136
+ timeout=15,
137
+ problem_file=problem_dict,
138
+ )
139
+
140
+
141
+ ```