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# "datasets",
|
| 7 |
+
# "torch>=2.0.0",
|
| 8 |
+
# "huggingface-hub",
|
| 9 |
+
# "accelerate",
|
| 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 AutoModelForCausalLM, AutoTokenizer
|
| 19 |
+
from peft import PeftModel
|
| 20 |
+
|
| 21 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 22 |
+
if hf_token:
|
| 23 |
+
os.environ.setdefault("HF_HUB_TOKEN", hf_token)
|
| 24 |
+
|
| 25 |
+
BASE_MODEL = os.getenv("BASE_MODEL", "Qwen/Qwen3.5-4B")
|
| 26 |
+
ADAPTER_REPO = os.getenv("ADAPTER_REPO", "oExz/qwen3.5-4b-reasoning-lora")
|
| 27 |
+
MAX_SAMPLES = int(os.getenv("MAX_SAMPLES", "250"))
|
| 28 |
+
|
| 29 |
+
def extract_answer(text):
|
| 30 |
+
text = text.strip()
|
| 31 |
+
# Look for #### X format
|
| 32 |
+
m = re.search(r"####\s*(-?\d+(?:\.\d+)?)", text)
|
| 33 |
+
if m:
|
| 34 |
+
return m.group(1).strip()
|
| 35 |
+
# Look for ANSWER: X format
|
| 36 |
+
m = re.search(r"ANSWER:\s*(-?\d+(?:\.\d+)?)", text, re.IGNORECASE)
|
| 37 |
+
if m:
|
| 38 |
+
return m.group(1).strip()
|
| 39 |
+
# Last number fallback
|
| 40 |
+
nums = re.findall(r"-?\d+(?:\.\d+)?", text)
|
| 41 |
+
if nums:
|
| 42 |
+
return nums[-1].strip()
|
| 43 |
+
return ""
|
| 44 |
+
|
| 45 |
+
def build_prompt(question):
|
| 46 |
+
return (
|
| 47 |
+
"<|im_start|>system\nYou are a helpful assistant that solves math problems. "
|
| 48 |
+
"Show your reasoning step by step, then give the final answer as ANSWER: <number>.\n"
|
| 49 |
+
"<|im_end|>\n"
|
| 50 |
+
"<|im_start|>user\n" + question + "\n<|im_end|>\n"
|
| 51 |
+
"<|im_start|>assistant\n"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == '__main__':
|
| 55 |
+
print(f"Loading GSM8K dataset (max {MAX_SAMPLES} samples)...")
|
| 56 |
+
ds = load_dataset("gsm8k", "main", split="test")
|
| 57 |
+
if MAX_SAMPLES:
|
| 58 |
+
ds = ds.select(range(min(MAX_SAMPLES, len(ds))))
|
| 59 |
+
|
| 60 |
+
questions = [build_prompt(ex["question"]) for ex in ds]
|
| 61 |
+
ground_truth = [extract_answer(ex["answer"]) for ex in ds]
|
| 62 |
+
|
| 63 |
+
print(f"Loading base model {BASE_MODEL} + adapter {ADAPTER_REPO}...")
|
| 64 |
+
|
| 65 |
+
# Merge adapter with base model and save to temp dir
|
| 66 |
+
print("Merging LoRA adapter with base model...")
|
| 67 |
+
base = AutoModelForCausalLM.from_pretrained(
|
| 68 |
+
BASE_MODEL,
|
| 69 |
+
trust_remote_code=True,
|
| 70 |
+
torch_dtype="auto",
|
| 71 |
+
device_map="auto",
|
| 72 |
+
)
|
| 73 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 74 |
+
model = PeftModel.from_pretrained(base, ADAPTER_REPO)
|
| 75 |
+
model = model.merge_and_unload()
|
| 76 |
+
|
| 77 |
+
temp_dir = "/tmp/merged-qwen-tuned"
|
| 78 |
+
print(f"Saving merged model to {temp_dir}...")
|
| 79 |
+
model.save_pretrained(temp_dir)
|
| 80 |
+
tokenizer.save_pretrained(temp_dir)
|
| 81 |
+
|
| 82 |
+
# Free memory before vLLM loads
|
| 83 |
+
del base, model
|
| 84 |
+
import torch
|
| 85 |
+
torch.cuda.empty_cache()
|
| 86 |
+
|
| 87 |
+
print("Loading merged model with vLLM...")
|
| 88 |
+
llm = LLM(
|
| 89 |
+
model=temp_dir,
|
| 90 |
+
trust_remote_code=True,
|
| 91 |
+
dtype="half",
|
| 92 |
+
gpu_memory_utilization=0.9,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
sampling_params = SamplingParams(
|
| 96 |
+
temperature=0,
|
| 97 |
+
max_tokens=512,
|
| 98 |
+
stop=["<|im_end|>", "<|endoftext|>"],
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
print(f"Generating answers for {len(questions)} questions...")
|
| 102 |
+
outputs = llm.generate(questions, sampling_params)
|
| 103 |
+
|
| 104 |
+
correct = 0
|
| 105 |
+
results = []
|
| 106 |
+
for i, out in enumerate(outputs):
|
| 107 |
+
pred_text = out.outputs[0].text
|
| 108 |
+
pred_answer = extract_answer(pred_text)
|
| 109 |
+
gt = ground_truth[i]
|
| 110 |
+
is_correct = pred_answer == gt
|
| 111 |
+
if is_correct:
|
| 112 |
+
correct += 1
|
| 113 |
+
results.append({
|
| 114 |
+
"question": ds[i]["question"],
|
| 115 |
+
"ground_truth": gt,
|
| 116 |
+
"predicted": pred_answer,
|
| 117 |
+
"correct": is_correct,
|
| 118 |
+
"raw_output": pred_text,
|
| 119 |
+
})
|
| 120 |
+
|
| 121 |
+
accuracy = correct / len(ds) * 100
|
| 122 |
+
print(f"\n=== GSM8K TUNED EVAL RESULTS ===")
|
| 123 |
+
print(f"Correct: {correct}/{len(ds)}")
|
| 124 |
+
print(f"Accuracy: {accuracy:.2f}%")
|
| 125 |
+
|
| 126 |
+
# Save results
|
| 127 |
+
with open("/tmp/gsm8k_tuned_results.json", "w") as f:
|
| 128 |
+
json.dump({
|
| 129 |
+
"base_model": BASE_MODEL,
|
| 130 |
+
"adapter_repo": ADAPTER_REPO,
|
| 131 |
+
"accuracy": accuracy,
|
| 132 |
+
"correct": correct,
|
| 133 |
+
"total": len(ds),
|
| 134 |
+
"results": results,
|
| 135 |
+
}, f, indent=2)
|
| 136 |
+
|
| 137 |
+
print("Results saved to /tmp/gsm8k_tuned_results.json")
|