oExz commited on
Commit
491e16d
·
verified ·
1 Parent(s): 1e646bc

Upload eval_gsm8k_vllm.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. eval_gsm8k_vllm.py +66 -0
eval_gsm8k_vllm.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "datasets",
5
+ # "vllm>=0.4.0",
6
+ # "torch>=2.0.0",
7
+ # "transformers>=4.40.0",
8
+ # "huggingface-hub",
9
+ # ]
10
+ # ///
11
+
12
+ import os
13
+ import re
14
+ from datasets import load_dataset
15
+
16
+ os.environ.setdefault("HF_HUB_TOKEN", os.getenv("HF_TOKEN", ""))
17
+ MODEL = os.getenv("MODEL", "Qwen/Qwen3.5-4B")
18
+ LIMIT = int(os.getenv("LIMIT", "250"))
19
+
20
+ print(f"Loading GSM8K dataset (limit={LIMIT})...")
21
+ ds = load_dataset("openai/gsm8k", "main", split="test").select(range(min(LIMIT, 1319)))
22
+
23
+ print(f"Loading model {MODEL} with vLLM...")
24
+ from vllm import LLM, SamplingParams
25
+ from transformers import AutoTokenizer
26
+
27
+ tok = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
28
+ llm = LLM(model=MODEL, trust_remote_code=True, dtype="half", gpu_memory_utilization=0.9)
29
+
30
+ sys_msg = "You are a helpful assistant that solves math problems step by step."
31
+
32
+ problems = []
33
+ answers = []
34
+ for ex in ds:
35
+ msgs = [
36
+ {"role": "system", "content": sys_msg},
37
+ {"role": "user", "content": f"Solve this math problem step by step. End your answer with 'ANSWER: <number>'\n\n{ex['question']}"},
38
+ ]
39
+ formatted = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
40
+ problems.append(formatted)
41
+ gt_match = re.search(r"####\s*(-?\d[\d,]*\.?\d*)", ex["answer"])
42
+ gt = gt_match.group(1).replace(",", "") if gt_match else ex["answer"].strip().split()[-1]
43
+ answers.append(gt)
44
+
45
+ print(f"Generating answers for {len(problems)} problems...")
46
+ sampling_params = SamplingParams(temperature=0.0, max_tokens=512, stop=["<|im_end|>", "<|endoftext|>"])
47
+ outputs = llm.generate(problems, sampling_params)
48
+
49
+ correct = 0
50
+ total = len(outputs)
51
+ for i, output in enumerate(outputs):
52
+ gen = output.outputs[0].text.strip()
53
+ pred_match = re.search(r"ANSWER:\s*(-?\d[\d,]*\.?\d*)", gen)
54
+ if pred_match:
55
+ pred = pred_match.group(1).replace(",", "")
56
+ else:
57
+ nums = re.findall(r"-?\d[\d,]*\.?\d*", gen)
58
+ pred = nums[-1].replace(",", "") if nums else ""
59
+
60
+ if pred == answers[i]:
61
+ correct += 1
62
+ if i < 5 or i % 50 == 0:
63
+ print(f"Q{i+1}: GT={answers[i]} Pred={pred} OK={pred==answers[i]}")
64
+
65
+ accuracy = correct / total * 100
66
+ print(f"\nGSM8K: {correct}/{total} = {accuracy:.2f}%")