Repository navigation
Expand file tree
/
Copy pathgenerate_code_share8.py
More file actions
83 lines (72 loc) · 4.1 KB
/
Copy pathgenerate_code_share8.py
File metadata and controls
83 lines (72 loc) · 4.1 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
"""Generate model responses for the pinned EvalPlus datasets."""
import argparse
import gzip
import json
from pathlib import Path
import torch
from vllm import LLM, SamplingParams
def instruction(name, prompt):
if name == "humanevalplus":
return ("Implement the following Python function. Return the complete implementation "
"inside one Python code block.\n\n" + prompt)
return ("Solve the following Python programming task. Return the complete implementation "
"inside one Python code block.\n\n" + prompt)
def lcb_instruction(row):
message = row["question_content"]
starter = row["starter_code"]
if starter:
return f"{message}\n\nComplete this starter code and return the full solution in a Python code block.\n```python\n{starter}\n```"
return (f"{message}\n\nWrite a complete Python program. Read from standard input, "
"write to standard output, and return the program in a Python code block.")
def benchmarks(name, data):
if name == "livecodebench":
benchmark = "livecodebench-v6-tail"
with (data / (benchmark + "-prompts.jsonl")).open() as source:
rows = [json.loads(line) for line in source]
if len(rows) != 175:
raise ValueError((benchmark, len(rows), 175))
yield benchmark, [(row["question_id"], lcb_instruction(row)) for row in rows]
return
fixtures = [("humanevalplus", "HumanEvalPlus-v0.1.10", 164),
("mbppplus", "MbppPlus-v0.2.0", 378)]
for dataset, fixture, expected in fixtures:
with gzip.open(data / f"{fixture}.jsonl.gz", "rt") as source:
rows = [json.loads(line) for line in source]
if len(rows) != expected:
raise ValueError((dataset, len(rows), expected))
yield dataset, [(row["task_id"], instruction(dataset, row["prompt"])) for row in rows]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--model", required=True)
parser.add_argument("--family", choices=["qwen", "intern", "glm", "gpt-oss"], default="qwen")
parser.add_argument("--data", type=Path, required=True)
parser.add_argument("--out", type=Path, required=True)
parser.add_argument("--benchmark", choices=["evalplus", "livecodebench"], default="evalplus")
args = parser.parse_args()
if not torch.cuda.is_available() or torch.cuda.get_device_capability() != (9, 0):
raise RuntimeError("This baseline requires an NVIDIA Hopper GPU")
model = LLM(model=args.model, tensor_parallel_size=2, dtype="bfloat16", max_model_len=10240,
trust_remote_code=args.family == "intern",
gpu_memory_utilization=0.85, limit_mm_per_prompt={"image": 0, "video": 0},
generation_config="vllm", additional_config={"gdn_prefill_backend": "triton"})
config = SamplingParams(n=1, temperature=0.7, top_p=0.95, max_tokens=8192, seed=427,
skip_special_tokens=args.family != "gpt-oss")
template = {"reasoning_effort": "medium"} if args.family == "gpt-oss" else {"enable_thinking": True}
args.out.mkdir(parents=True, exist_ok=True)
for name, items in benchmarks(args.benchmark, args.data):
prompts = [[{"role": "user", "content": prompt}] for _, prompt in items]
result = model.chat(prompts, sampling_params=config, chat_template_kwargs=template)
temporary = args.out / f"{name}.jsonl.tmp"
with temporary.open("w") as stream:
for (task_id, _), answer in zip(items, result, strict=True):
sample = answer.outputs[0]
row = {"task_id": task_id, "completion": sample.text, "finish_reason": sample.finish_reason}
if args.family == "gpt-oss":
row["raw_completion"] = sample.text
final = sample.text.partition("<|channel|>final<|message|>")[2]
row["completion"] = final.partition("<|")[0]
stream.write(json.dumps(row) + "\n")
temporary.replace(args.out / f"{name}.jsonl")
print(f"{name}: generated {len(items)} responses", flush=True)
if __name__ == "__main__":
main()