oExz commited on
Commit
7ea035c
·
verified ·
1 Parent(s): 841334b

Upload eval_gsm8k_baseline_fair.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. eval_gsm8k_baseline_fair.py +113 -0
eval_gsm8k_baseline_fair.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = [
3
+ # "vllm",
4
+ # "transformers>=4.40.0",
5
+ # "datasets>=2.14",
6
+ # "pyarrow<19.0",
7
+ # "torch>=2.0.0",
8
+ # "huggingface-hub",
9
+ # "tqdm>=4.65",
10
+ # ]
11
+ # ///
12
+
13
+ import os
14
+ import re
15
+ from datasets import load_dataset
16
+ from vllm import LLM, SamplingParams
17
+ from transformers import AutoTokenizer
18
+
19
+ hf_token = os.getenv("HF_TOKEN")
20
+ if hf_token:
21
+ os.environ.setdefault("HF_HUB_TOKEN", hf_token)
22
+
23
+ MODEL = os.getenv("MODEL", "Qwen/Qwen3.5-4B")
24
+ MAX_SAMPLES = int(os.getenv("MAX_SAMPLES", "250"))
25
+
26
+ def extract_answer(text):
27
+ text = text.strip()
28
+ m = re.search(r"####\s*(-?\d+(?:\.\d+)?)", text)
29
+ if m:
30
+ return m.group(1).strip()
31
+ m = re.search(r"ANSWER:\s*(-?\d+(?:\.\d+)?)", text, re.IGNORECASE)
32
+ if m:
33
+ return m.group(1).strip()
34
+ nums = re.findall(r"-?\d+(?:\.\d+)?", text)
35
+ if nums:
36
+ return nums[-1].strip()
37
+ return ""
38
+
39
+ def build_prompt(question):
40
+ messages = [
41
+ {"role": "system", "content": "You are a helpful assistant that thinks step by step."},
42
+ {"role": "user", "content": question},
43
+ ]
44
+ return messages
45
+
46
+ if __name__ == '__main__':
47
+ print(f"Loading GSM8K dataset (max {MAX_SAMPLES} samples)...")
48
+ ds = load_dataset("gsm8k", "main", split="test")
49
+ if MAX_SAMPLES:
50
+ ds = ds.select(range(min(MAX_SAMPLES, len(ds))))
51
+
52
+ print(f"Loading model {MODEL} with vLLM...")
53
+ tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
54
+ llm = LLM(
55
+ model=MODEL,
56
+ trust_remote_code=True,
57
+ dtype="half",
58
+ gpu_memory_utilization=0.9,
59
+ )
60
+
61
+ questions = []
62
+ ground_truth = []
63
+ for ex in ds:
64
+ msgs = build_prompt(ex["question"])
65
+ formatted = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
66
+ questions.append(formatted)
67
+ ground_truth.append(extract_answer(ex["answer"]))
68
+
69
+ # SAME sampling params as tuned eval (temperature=0.7, do_sample)
70
+ sampling_params = SamplingParams(
71
+ temperature=0.7,
72
+ top_p=0.9,
73
+ max_tokens=512,
74
+ stop=["<|im_end|>", "<|endoftext|>"],
75
+ )
76
+
77
+ print(f"Generating answers for {len(questions)} questions...")
78
+ outputs = llm.generate(questions, sampling_params)
79
+
80
+ correct = 0
81
+ results = []
82
+ for i, out in enumerate(outputs):
83
+ pred_text = out.outputs[0].text
84
+ pred_answer = extract_answer(pred_text)
85
+ gt = ground_truth[i]
86
+ is_correct = pred_answer == gt
87
+ if is_correct:
88
+ correct += 1
89
+ results.append({
90
+ "question": ds[i]["question"],
91
+ "ground_truth": gt,
92
+ "predicted": pred_answer,
93
+ "correct": is_correct,
94
+ "raw_output": pred_text,
95
+ })
96
+ if i < 5:
97
+ print(f"Q{i+1}: GT={gt} Pred={pred_answer} OK={is_correct}")
98
+
99
+ accuracy = correct / len(ds) * 100
100
+ print(f"\n=== GSM8K BASELINE (FAIR) ===")
101
+ print(f"Correct: {correct}/{len(ds)}")
102
+ print(f"Accuracy: {accuracy:.2f}%")
103
+
104
+ import json
105
+ with open("/tmp/gsm8k_baseline_fair_results.json", "w") as f:
106
+ json.dump({
107
+ "model": MODEL,
108
+ "accuracy": accuracy,
109
+ "correct": correct,
110
+ "total": len(ds),
111
+ "results": results,
112
+ }, f, indent=2)
113
+ print("Results saved to /tmp/gsm8k_baseline_fair_results.json")