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

Upload eval_gsm8k_vllm.py with huggingface_hub

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