metadata
license: apache-2.0
dataset_info:
features:
- name: task_id
dtype: string
- name: prompt
dtype: string
- name: entry_point
dtype: string
- name: test
dtype: string
- name: description
dtype: string
- name: language
dtype: string
- name: canonical_solution
sequence: string
splits:
- name: train
num_bytes: 505355
num_examples: 161
download_size: 174830
dataset_size: 505355
configs:
- config_name: default
data_files:
- split: train
path: data/train-*
How to use
import torch
import jsonlines
import re
from tqdm import tqdm
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
StoppingCriteria,
StoppingCriteriaList,
)
from mxeval.data import get_data
from mxeval.evaluation import evaluate_functional_correctness
from datasets import load_dataset
class StoppingCriteriaSub(StoppingCriteria):
def __init__(self, stops, tokenizer):
(StoppingCriteria.__init__(self),)
self.stops = rf"{stops}"
self.tokenizer = tokenizer
def __call__(
self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs
) -> bool:
last_three_tokens = [int(x) for x in input_ids.data[0][-3:]]
decoded_last_three_tokens = self.tokenizer.decode(last_three_tokens)
return bool(re.search(self.stops, decoded_last_three_tokens))
def generate(problem):
stopping_criteria = StoppingCriteriaList(
[
StoppingCriteriaSub(
stops= "\n}\n", tokenizer=tokenizer
)
]
)
problem = tokenizer.encode(problem, return_tensors="pt").to('cuda')
sample = model.generate(
problem,
temperature=0.1,
max_new_tokens=256,
min_new_tokens=128,
pad_token_id=tokenizer.eos_token_id,
do_sample=False,
num_beams=1,
stopping_criteria=stopping_criteria,
)
answer = tokenizer.decode(sample[0], skip_special_tokens=True)
return answer
def clean_asnwer(code):
# Clean comments
code_without_line_comments = re.sub(r"//.*", "", code)
code_without_all_comments = re.sub(
r"/\*.*?\*/", "", code_without_line_comments, flags=re.DOTALL
)
#Clean signatures
lines = code.split("\n")
for i, line in enumerate(lines):
if line.startswith("fun "):
return "\n".join(lines[i + 1 :])
return code
model_name = "JetBrains/CodeLlama-7B-Kexer"
dataset = load_dataset("jetbrains/Kotlin_HumanEval")['train']
problem_dict = {problem['task_id']: problem for problem in dataset}
model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.bfloat16).to('cuda')
tokenizer = AutoTokenizer.from_pretrained(model_name)
output = []
for key in tqdm(list(problem_dict.keys()), leave=False):
problem = problem_dict[key]["prompt"]
answer = generate(problem)
answer = clean_asnwer(answer)
output.append({"task_id": key, "completion": answer, "language": "kotlin"})
output_file = f"answers"
with jsonlines.open(output_file, mode="w") as writer:
for line in output:
writer.write(line)
evaluate_functional_correctness(
sample_file=output_file,
k=[1],
n_workers=16,
timeout=15,
problem_file=problem_dict,
)