Upload analyze_baseline.py with huggingface_hub
Browse files- analyze_baseline.py +83 -0
analyze_baseline.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import json
|
| 16 |
+
from datasets import load_dataset
|
| 17 |
+
from vllm import LLM, SamplingParams
|
| 18 |
+
from transformers import AutoTokenizer
|
| 19 |
+
|
| 20 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 21 |
+
if hf_token:
|
| 22 |
+
os.environ.setdefault("HF_HUB_TOKEN", hf_token)
|
| 23 |
+
|
| 24 |
+
MODEL = "Qwen/Qwen3.5-4B"
|
| 25 |
+
N_SAMPLES = 20
|
| 26 |
+
|
| 27 |
+
def extract_answer(text):
|
| 28 |
+
text = text.strip()
|
| 29 |
+
m = re.search(r"####\s*(-?\d+(?:\.\d+)?)", text)
|
| 30 |
+
if m: return m.group(1).strip()
|
| 31 |
+
m = re.search(r"ANSWER:\s*(-?\d+(?:\.\d+)?)", text, re.IGNORECASE)
|
| 32 |
+
if m: return m.group(1).strip()
|
| 33 |
+
nums = re.findall(r"-?\d+(?:\.\d+)?", text)
|
| 34 |
+
if nums: return nums[-1].strip()
|
| 35 |
+
return ""
|
| 36 |
+
|
| 37 |
+
def build_prompt(question):
|
| 38 |
+
messages = [
|
| 39 |
+
{"role": "system", "content": "You are a helpful assistant that thinks step by step."},
|
| 40 |
+
{"role": "user", "content": question},
|
| 41 |
+
]
|
| 42 |
+
return messages
|
| 43 |
+
|
| 44 |
+
if __name__ == '__main__':
|
| 45 |
+
print("Loading GSM8K...")
|
| 46 |
+
ds = load_dataset("gsm8k", "main", split="test").select(range(N_SAMPLES))
|
| 47 |
+
|
| 48 |
+
print(f"Loading {MODEL}...")
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
|
| 50 |
+
llm = LLM(model=MODEL, trust_remote_code=True, dtype="half", gpu_memory_utilization=0.9)
|
| 51 |
+
|
| 52 |
+
questions = []
|
| 53 |
+
ground_truth = []
|
| 54 |
+
for ex in ds:
|
| 55 |
+
msgs = build_prompt(ex["question"])
|
| 56 |
+
formatted = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 57 |
+
questions.append(formatted)
|
| 58 |
+
gt_match = re.search(r"####\s*(-?\d[\d,]*\.?\d*)", ex["answer"])
|
| 59 |
+
gt = gt_match.group(1).replace(",", "") if gt_match else ex["answer"].strip().split()[-1]
|
| 60 |
+
ground_truth.append(gt)
|
| 61 |
+
|
| 62 |
+
sampling_params = SamplingParams(temperature=0.7, top_p=0.9, max_tokens=512, stop=["<|im_end|>", "<|endoftext|>"])
|
| 63 |
+
outputs = llm.generate(questions, sampling_params)
|
| 64 |
+
|
| 65 |
+
print("\n" + "=" * 70)
|
| 66 |
+
print("BASELINE MODEL OUTPUTS (first 20 samples)")
|
| 67 |
+
print("=" * 70)
|
| 68 |
+
|
| 69 |
+
for i, out in enumerate(outputs):
|
| 70 |
+
raw = out.outputs[0].text.strip()
|
| 71 |
+
pred = extract_answer(raw)
|
| 72 |
+
gt = ground_truth[i]
|
| 73 |
+
match = pred == gt
|
| 74 |
+
|
| 75 |
+
print(f"\n--- Question {i+1} ---")
|
| 76 |
+
print(f"Q: {ds[i]['question']}")
|
| 77 |
+
print(f"GT: {gt} | Pred: {pred} | Match: {match}")
|
| 78 |
+
print(f"--- RAW OUTPUT ---")
|
| 79 |
+
print(raw[:800] + ("..." if len(raw) > 800 else ""))
|
| 80 |
+
print("-" * 70)
|
| 81 |
+
|
| 82 |
+
correct = sum(1 for i in range(N_SAMPLES) if extract_answer(outputs[i].outputs[0].text.strip()) == ground_truth[i])
|
| 83 |
+
print(f"\nScore on first {N_SAMPLES}: {correct}/{N_SAMPLES} = {correct/N_SAMPLES*100:.1f}%")
|