forked from slinusc/bench360
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_benchmark_apertus.py
More file actions
executable file
·230 lines (188 loc) · 7.32 KB
/
Copy pathrun_benchmark_apertus.py
File metadata and controls
executable file
·230 lines (188 loc) · 7.32 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Script to run multiple benchmarks (MMLU, HellaSwag, ARC) against Apertus server.
"""
import json
import requests
import time
import argparse
from typing import List, Dict, Any
import sys
import os
from pathlib import Path
# Set up Hugging Face authentication
if "HF_TOKEN" in os.environ:
os.environ["HUGGINGFACE_HUB_TOKEN"] = os.environ["HF_TOKEN"]
# Try to login, but continue if it fails
try:
from huggingface_hub import login
login(token=os.environ["HF_TOKEN"])
print("Hugging Face authentication successful")
except Exception as e:
print(f"Warning: Hugging Face authentication failed: {e}")
print("Continuing without authentication...")
# Add benchmark module to path
sys.path.append(str(Path(__file__).parent / "benchmark"))
from tasks.mmlu import MMLUTask
from tasks.hellaswag import HellaSwagTask
from tasks.arc import ARCTask
class ApertusClient:
"""Client for Apertus OpenAI-compatible API"""
def __init__(self, base_url: str = "http://localhost:8000"):
self.base_url = base_url.rstrip('/')
self.session = requests.Session()
def health_check(self) -> bool:
"""Check if server is running"""
try:
response = self.session.get(f"{self.base_url}/health", timeout=5)
return response.status_code == 200
except:
return False
def generate(self, prompt: str, max_tokens: int = 1024) -> str:
"""Generate response from Apertus model"""
payload = {
"model": "adamo1139/Apertus-8B-Instruct-2509-ungated",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"stream": False
}
response = self.session.post(
f"{self.base_url}/v1/chat/completions",
json=payload,
timeout=60
)
response.raise_for_status()
result = response.json()
return result["choices"][0]["message"]["content"]
def get_task_instance(benchmark: str, **kwargs):
"""Get task instance based on benchmark name."""
if benchmark.lower() == "mmlu":
return MMLUTask(subjects=kwargs.get("subjects"), split="test", seed=42)
elif benchmark.lower() == "hellaswag":
return HellaSwagTask(split="validation", seed=42)
elif benchmark.lower() == "arc-easy":
return ARCTask(variant="ARC-Easy", split="test", seed=42)
elif benchmark.lower() == "arc-challenge":
return ARCTask(variant="ARC-Challenge", split="test", seed=42)
else:
raise ValueError(f"Unknown benchmark: {benchmark}")
def run_benchmark(
client: ApertusClient,
benchmark: str,
num_examples: int = None,
subjects: List[str] = None,
output_file: str = None
) -> Dict[str, Any]:
"""Run specified benchmark"""
# Set default num_examples based on benchmark
if num_examples is None:
defaults = {
"mmlu": 14042,
"hellaswag": 10042,
"arc-easy": 2376,
"arc-challenge": 1172
}
num_examples = defaults.get(benchmark.lower(), 100)
# Initialize task
task = get_task_instance(benchmark, subjects=subjects)
print(f"Generating {num_examples} {benchmark.upper()} prompts...")
prompts, references = task.generate_prompts(num_examples)
print(f"Generated {len(prompts)} prompts")
results = []
correct = 0
total = 0
start_time = time.time()
for i, (prompt, reference) in enumerate(zip(prompts, references)):
print(f"Processing example {i+1}/{len(prompts)}", end="\r")
try:
# Get model response
response = client.generate(prompt, max_tokens=10) # Short response for multiple choice
# Evaluate
metrics = task.quality_metrics(response, reference)
accuracy = metrics["accuracy"]
if accuracy == 1.0:
correct += 1
total += 1
results.append({
"example_id": i,
"prompt": prompt,
"reference": reference,
"generated": response,
"metrics": metrics
})
# Brief pause to avoid overwhelming the server
time.sleep(0.1)
except Exception as e:
print(f"\nError processing example {i+1}: {e}")
results.append({
"example_id": i,
"prompt": prompt,
"reference": reference,
"generated": None,
"error": str(e),
"metrics": {"accuracy": 0.0}
})
total += 1
end_time = time.time()
# Calculate final metrics
overall_accuracy = correct / total if total > 0 else 0
duration = end_time - start_time
summary = {
"benchmark": benchmark,
"total_examples": total,
"correct": correct,
"accuracy": overall_accuracy,
"duration_seconds": duration,
"examples_per_second": total / duration if duration > 0 else 0,
"subjects": subjects if benchmark.lower() == "mmlu" else None,
"results": results
}
print(f"\n\nResults:")
print(f"Accuracy: {overall_accuracy:.3f} ({correct}/{total})")
print(f"Duration: {duration:.1f} seconds")
print(f"Speed: {total/duration:.1f} examples/second")
# Save results if output file specified
if output_file:
with open(output_file, 'w') as f:
json.dump(summary, f, indent=2)
print(f"Results saved to {output_file}")
return summary
def main():
parser = argparse.ArgumentParser(description="Run benchmarks against Apertus server")
parser.add_argument("benchmark", choices=["mmlu", "hellaswag", "arc-easy", "arc-challenge"],
help="Benchmark to run")
parser.add_argument("--server-url", default="http://localhost:8000",
help="Apertus server URL (default: http://localhost:8000)")
parser.add_argument("--num-examples", type=int,
help="Number of examples to test (default: full dataset)")
parser.add_argument("--subjects", nargs="*",
help="MMLU subjects to test (only for MMLU benchmark)")
parser.add_argument("--output", "-o", help="Output file for results")
args = parser.parse_args()
# Initialize client
client = ApertusClient(args.server_url)
# Check server health
print(f"Checking server at {args.server_url}...")
if not client.health_check():
print(f"Error: Cannot connect to Apertus server at {args.server_url}")
print("Make sure the server is running and accessible.")
sys.exit(1)
print("Server is healthy!")
# Run benchmark
try:
results = run_benchmark(
client=client,
benchmark=args.benchmark,
num_examples=args.num_examples,
subjects=args.subjects,
output_file=args.output
)
print(f"\nBenchmark completed successfully!")
except KeyboardInterrupt:
print("\nBenchmark interrupted by user.")
sys.exit(1)
except Exception as e:
print(f"\nBenchmark failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()