Repository navigation
Expand file tree
/
Copy pathrun_instructions.py
More file actions
126 lines (106 loc) · 5.13 KB
/
Copy pathrun_instructions.py
File metadata and controls
126 lines (106 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import json
import threading
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Optional
from tqdm import tqdm
from datasets import load_dataset
from mastermind.models import AnthropicModel, GenerationArgs, HFModel, OpenAIModel, VLLMModel
from mastermind.utils import parse_guess, make_output_path
def parse_optional_bool(value: Optional[str]) -> Optional[bool]:
if value is None:
return None
normalized = value.strip().lower()
if normalized in {"true", "1", "yes", "y", "on"}:
return True
if normalized in {"false", "0", "no", "n", "off"}:
return False
raise ValueError(f"Invalid boolean value: {value}")
def evaluate(model, dataset, args):
log = []
log_lock = threading.Lock()
pbar = tqdm(total=len(dataset), desc="Evaluating.")
def process(dp):
last_output = None
try:
last_output = model(dp["instruction"])
turn = last_output[-1]
guess = parse_guess(turn)
return {
"guess": guess,
"secret_code": dp["secret_code"],
"correct": guess == dp["secret_code"],
"valid": len(guess) == len(dp["secret_code"]),
"raw_response": turn.get("content") if isinstance(turn, dict) else None,
"reasoning_content": turn.get("reasoning_content") if isinstance(turn, dict) else None,
"finish_reason": turn.get("finish_reason") if isinstance(turn, dict) else None,
"model": model.get_model_info(),
"dataset": args.dataset,
}
except Exception:
turn = last_output[-1] if last_output else None
return {
"guess": turn if turn else None,
"secret_code": dp["secret_code"],
"correct": False,
"valid": False,
"raw_response": turn.get("content") if isinstance(turn, dict) else turn,
"reasoning_content": turn.get("reasoning_content") if isinstance(turn, dict) else None,
"finish_reason": turn.get("finish_reason") if isinstance(turn, dict) else None,
"model": model.get_model_info(),
"dataset": args.dataset,
}
with ThreadPoolExecutor(max_workers=args.num_parallel) as executor:
futures = {executor.submit(process, dp): dp for dp in dataset}
for future in as_completed(futures):
result = future.result()
with log_lock:
log.append(result)
pbar.update(1)
pbar.close()
output_path = make_output_path(base_path=args.save_path, game_type="deductive_reasoning")
with open(output_path / "results.jsonl", "w") as f:
for item in log:
f.write(json.dumps(item) + "\n")
with open(output_path / "info.json", "w") as f:
json.dump({"model": model.get_model_info(), "dataset": args.dataset}, f)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--model_type", type=str, default="hf")
parser.add_argument("--model", type=str, default="Qwen/Qwen3-4B")
parser.add_argument("--dataset", default="flair/mastermind_24_prompt")
parser.add_argument("--num_samples", type=int, default=100, help="Number of shuffled training examples to evaluate.")
parser.add_argument("--base_url", type=str, default=None, help="Base URL for compatible API backends such as vLLM.")
parser.add_argument("--save_path", type=str, default=None, help="Base directory for saving results.")
parser.add_argument("--num_parallel", type=int, default=1, help="Number of samples to evaluate in parallel (useful with vLLM).")
parser.add_argument("--max_tokens", type=int, default=2048, help="Max generation tokens (raise for thinking mode).")
parser.add_argument(
"--enable_thinking",
type=parse_optional_bool,
default=None,
help="Enable or disable thinking mode for supported chat templates such as Qwen3.",
)
arguments = parser.parse_args()
dataset = load_dataset(arguments.dataset, split="train")
dataset = dataset.shuffle(seed=42)
dataset = dataset.select(range(min(arguments.num_samples, len(dataset))))
def apply_chat_template(example):
example['instruction'] = [{"role": "user", "content": example['instruction']}]
return example
dataset = dataset.map(apply_chat_template)
if arguments.model_type == "hf":
model = HFModel(model_name=arguments.model, enable_thinking=arguments.enable_thinking)
elif arguments.model_type == "anthropic":
model = AnthropicModel(model_name=arguments.model)
elif arguments.model_type == "openai":
model = OpenAIModel(model_name=arguments.model)
elif arguments.model_type == "vllm":
model = VLLMModel(
model_name=arguments.model,
base_url=arguments.base_url,
enable_thinking=arguments.enable_thinking,
generation_args=GenerationArgs(max_tokens=arguments.max_tokens),
)
else:
raise ValueError(f"Invalid model type: {arguments.model_type}")
evaluate(model, dataset, arguments)