oExz commited on
Commit
97fea6f
·
verified ·
1 Parent(s): 02c4d82

Upload analyze_baseline_small.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. analyze_baseline_small.py +72 -0
analyze_baseline_small.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = [
3
+ # "transformers>=4.40.0",
4
+ # "datasets>=2.14",
5
+ # "pyarrow<19.0",
6
+ # "torch>=2.0.0",
7
+ # "huggingface-hub",
8
+ # "accelerate",
9
+ # "tqdm>=4.65",
10
+ # ]
11
+ # ///
12
+
13
+ import os
14
+ import re
15
+ from datasets import load_dataset
16
+ from transformers import AutoModelForCausalLM, AutoTokenizer
17
+ import torch
18
+
19
+ hf_token = os.getenv("HF_TOKEN")
20
+ if hf_token:
21
+ os.environ.setdefault("HF_HUB_TOKEN", hf_token)
22
+
23
+ MODEL = "Qwen/Qwen3.5-4B"
24
+ N = 10
25
+
26
+ def extract_answer(text):
27
+ text = text.strip()
28
+ m = re.search(r"####\s*(-?\d+(?:\.\d+)?)", text)
29
+ if m: return m.group(1).strip()
30
+ m = re.search(r"ANSWER:\s*(-?\d+(?:\.\d+)?)", text, re.IGNORECASE)
31
+ if m: return m.group(1).strip()
32
+ nums = re.findall(r"-?\d+(?:\.\d+)?", text)
33
+ if nums: return nums[-1].strip()
34
+ return ""
35
+
36
+ print("Loading dataset...")
37
+ ds = load_dataset("gsm8k", "main", split="test").select(range(N))
38
+
39
+ print(f"Loading {MODEL}...")
40
+ tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
41
+ model = AutoModelForCausalLM.from_pretrained(
42
+ MODEL, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto",
43
+ )
44
+ model.eval()
45
+
46
+ print("\n" + "=" * 70)
47
+ print("BASELINE MODEL — FULL OUTPUTS")
48
+ print("=" * 70)
49
+
50
+ for i, ex in enumerate(ds):
51
+ msgs = [
52
+ {"role": "system", "content": "You are a helpful assistant that thinks step by step."},
53
+ {"role": "user", "content": ex["question"]},
54
+ ]
55
+ text = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
56
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
57
+
58
+ with torch.no_grad():
59
+ outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9)
60
+
61
+ raw = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
62
+ pred = extract_answer(raw)
63
+ gt_match = re.search(r"####\s*(-?\d[\d,]*\.?\d*)", ex["answer"])
64
+ gt = gt_match.group(1).replace(",", "") if gt_match else ""
65
+
66
+ print(f"\n{'─' * 70}")
67
+ print(f"QUESTION {i+1} | GT: {gt} | Pred: {pred} | Match: {pred == gt}")
68
+ print(f"{'─' * 70}")
69
+ print(raw[:1000])
70
+ if len(raw) > 1000:
71
+ print("... [truncated]")
72
+ print()