-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMLU.py
More file actions
489 lines (398 loc) · 17 KB
/
MMLU.py
File metadata and controls
489 lines (398 loc) · 17 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import ollama
from deepeval.benchmarks import MMLU
from deepeval.benchmarks.mmlu.task import MMLUTask
import pandas as pd
import re
import json
import os
from datetime import datetime
import time
import signal
from contextlib import contextmanager
class OllamaModel:
def __init__(self, model_name: str):
self.model_name = model_name
self.timeout_seconds = 60
self.max_retries = 3
self.call_count = 0
# Détecter le type de modèle et ajuster les paramètres
if 'deepseek' in model_name.lower():
self.num_predict = 500
self.is_reasoning_model = True
elif 'qwen' in model_name.lower():
self.num_predict = 200
self.is_reasoning_model = False
else:
self.num_predict = 50
self.is_reasoning_model = False
def extract_answer_from_deepseek(self, text: str) -> str:
"""Extrait la réponse d'un modèle DeepSeek-R1 qui utilise des balises <think>"""
if '</think>' in text:
answer_part = text.split('</think>')[-1].strip()
else:
answer_part = text.strip()
# Chercher une lettre A/B/C/D
match = re.search(r'\b[A-D]\b', answer_part, re.IGNORECASE)
if match:
return match.group(0).upper()
# Fallback : chercher dans tout le texte
match = re.search(r'\b[A-D]\b', text, re.IGNORECASE)
if match:
return match.group(0).upper()
return None
def generate(self, prompt: str) -> str:
"""Génère une réponse avec timeout et retry automatique"""
self.call_count += 1
# Afficher les 3 premiers prompts
if self.call_count <= 3:
print(f"\n{'='*60}")
print(f"📝 PROMPT #{self.call_count}")
print(f"{'='*60}")
print(prompt[:500])
if len(prompt) > 500:
print("... (tronqué)")
print(f"{'='*60}")
for attempt in range(self.max_retries):
try:
response = ollama.generate(
model=self.model_name,
prompt=prompt,
options={
'num_predict': self.num_predict,
'temperature': 0.1
}
)
answer = response["response"].strip()
# Afficher la réponse brute pour les 3 premiers
if self.call_count <= 3:
print(f"\n🤖 RÉPONSE BRUTE DU MODÈLE:")
print(answer if answer else "(vide)")
print(f"Longueur: {len(answer)} caractères")
# EXTRACTION D'ABORD - avant de vérifier la longueur
extracted = None
if self.is_reasoning_model:
extracted = self.extract_answer_from_deepseek(answer)
else:
match = re.search(r'\b[A-D]\b', answer, re.IGNORECASE)
if match:
extracted = match.group(0).upper()
elif answer and len(answer) > 0 and answer[0].upper() in ['A', 'B', 'C', 'D']:
extracted = answer[0].upper()
# Si on a trouvé une lettre valide, on retourne immédiatement
if extracted:
if self.call_count <= 3:
print(f"\n✅ RÉPONSE EXTRAITE: {extracted}")
print(f"{'='*60}\n")
return extracted
# Sinon, vérifier si la réponse est vraiment vide/invalide
if not answer or len(answer) == 0:
if self.call_count <= 3:
print(f"\n⚠️ RÉPONSE VIDE")
# Réessayer avec plus de tokens
if attempt < self.max_retries - 1:
if self.call_count <= 3:
print(f"Réessai avec plus de tokens...")
self.num_predict = min(self.num_predict * 2, 1000)
time.sleep(1)
continue
# Aucune lettre trouvée, réessayer
if attempt < self.max_retries - 1:
if self.call_count <= 3:
print(f"\n⚠️ AUCUNE LETTRE A/B/C/D TROUVÉE, réessai...")
self.num_predict = min(self.num_predict * 2, 1000)
time.sleep(1)
continue
else:
if self.call_count <= 3:
print(f"\n⚠️ AUCUNE LETTRE TROUVÉE après {self.max_retries} tentatives, retour 'A' par défaut")
print(f"{'='*60}\n")
return 'A'
except Exception as e:
print(f"⚠️ Erreur (tentative {attempt + 1}/{self.max_retries}): {e}")
if attempt < self.max_retries - 1:
time.sleep(2)
continue
else:
print(f"❌ Échec après {self.max_retries} tentatives, retour 'A'")
if self.call_count <= 3:
print(f"{'='*60}\n")
return 'A'
return 'A'
def run_mmlu_single_model(model_name):
"""Run MMLU benchmark for a single model"""
# Create output directory
output_dir = "./MMLU"
os.makedirs(output_dir, exist_ok=True)
print("\n" + "="*60)
print("MMLU BENCHMARK - SINGLE MODEL")
print("="*60)
print(f"\nTesting model: {model_name}")
# Define tasks
tasks = [
MMLUTask.FORMAL_LOGIC,
MMLUTask.GLOBAL_FACTS,
MMLUTask.COLLEGE_COMPUTER_SCIENCE,
MMLUTask.COLLEGE_MATHEMATICS,
MMLUTask.MARKETING,
MMLUTask.HIGH_SCHOOL_MACROECONOMICS
]
print(f"Tasks: {len(tasks)} MMLU categories")
for task in tasks:
task_name = task.value if hasattr(task, 'value') else str(task)
print(f" - {task_name}")
print("-"*60)
try:
# Initialize model
print(f"\n🔄 Chargement du modèle {model_name}...")
model = OllamaModel(model_name)
benchmark = MMLU(
tasks=tasks,
n_shots=3
)
# Évaluation avec sauvegarde progressive
print("\n🚀 Début de l'évaluation (affichage des 3 premières questions)...")
benchmark.evaluate(model=model)
# Store results
task_names = [task.value if hasattr(task, 'value') else str(task) for task in tasks]
# Calculate per-task scores from predictions
task_scores = {}
if hasattr(benchmark, 'predictions') and benchmark.predictions is not None:
predictions_df = benchmark.predictions if isinstance(benchmark.predictions, pd.DataFrame) else pd.DataFrame(benchmark.predictions)
# Afficher les 3 premières prédictions avec comparaison
print(f"\n{'='*60}")
print("📋 COMPARAISON DES 3 PREMIÈRES QUESTIONS")
print(f"{'='*60}")
for idx in range(min(3, len(predictions_df))):
row = predictions_df.iloc[idx]
print(f"\nQuestion #{idx + 1}:")
print(f" Tâche: {row.get('Task', 'N/A')}")
print(f" Réponse attendue: {row.get('Expected Output', 'N/A')}")
print(f" Réponse du modèle: {row.get('Actual Output', 'N/A')}")
print(f" Correct: {'✅ OUI' if row.get('Correct', False) else '❌ NON'}")
print("-" * 60)
for task_name in task_names:
task_preds = predictions_df[predictions_df['Task'] == task_name]
if len(task_preds) > 0:
score = task_preds['Correct'].mean()
task_scores[task_name] = score
# Sauvegarde progressive par tâche
checkpoint_data = {
'model_name': model_name,
'task_name': task_name,
'score': score,
'num_questions': len(task_preds),
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
save_checkpoint(model_name, task_name, checkpoint_data, output_dir)
result = {
'model_name': model_name,
'overall_score': benchmark.overall_score,
'tasks': task_names,
'task_scores': task_scores,
'num_tasks': len(tasks),
'n_shots': 3,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'status': 'success',
'predictions': benchmark.predictions
}
# Print per-task scores
if task_scores:
print(f"\n📊 Per-Task Scores:")
for task_name, score in task_scores.items():
print(f" {task_name:35s}: {score:.4f} ({score*100:.2f}%)")
print(f"\n✅ Overall Score: {benchmark.overall_score:.4f} ({benchmark.overall_score*100:.2f}%)")
# Save results
save_results(result, output_dir, model_name)
return result
except Exception as e:
print(f"❌ Error testing {model_name}: {e}")
import traceback
traceback.print_exc()
tasks = [
MMLUTask.FORMAL_LOGIC,
MMLUTask.GLOBAL_FACTS,
MMLUTask.COLLEGE_COMPUTER_SCIENCE,
MMLUTask.COLLEGE_MATHEMATICS,
MMLUTask.MARKETING,
MMLUTask.HIGH_SCHOOL_MACROECONOMICS
]
task_names = [task.value if hasattr(task, 'value') else str(task) for task in tasks]
result = {
'model_name': model_name,
'overall_score': 0.0,
'tasks': task_names,
'task_scores': {},
'num_tasks': len(tasks),
'n_shots': 3,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'status': 'failed',
'error': str(e),
'predictions': []
}
save_results(result, output_dir, model_name)
return result
finally:
# Décharger le modèle
unload_model(model_name)
def save_checkpoint(model_name, task_name, results, output_dir):
"""Sauvegarde progressive après chaque tâche"""
checkpoint_dir = os.path.join(output_dir, "checkpoints")
os.makedirs(checkpoint_dir, exist_ok=True)
clean_model_name = model_name.replace(':', '_').replace('/', '_')
clean_task_name = task_name.replace(' ', '_')
checkpoint_file = os.path.join(
checkpoint_dir,
f"{clean_model_name}_{clean_task_name}_checkpoint.json"
)
with open(checkpoint_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"💾 Checkpoint sauvegardé: {clean_task_name}")
def unload_model(model_name):
"""Décharge un modèle de la mémoire"""
try:
print(f"\n🧹 Déchargement du modèle {model_name}...")
ollama.generate(model=model_name, prompt="", keep_alive=0)
print(f"✅ Modèle {model_name} déchargé de la mémoire")
return True
except Exception as e:
print(f"⚠️ Erreur lors du déchargement de {model_name}: {e}")
return False
def save_results(result, output_dir, model_name):
"""Save results to JSON and CSV files"""
# Clean model name for filename (replace special characters)
clean_model_name = model_name.replace(':', '_').replace('/', '_')
# Create a copy without predictions (only keep scores)
result_copy = {
'model_name': result['model_name'],
'overall_score': result['overall_score'],
'tasks': result['tasks'],
'task_scores': result.get('task_scores', {}),
'num_tasks': result.get('num_tasks', len(result['tasks'])),
'n_shots': result['n_shots'],
'timestamp': result['timestamp'],
'status': result['status']
}
# Include error if present
if 'error' in result:
result_copy['error'] = result['error']
# Save to JSON (without detailed predictions, only scores)
json_path = os.path.join(output_dir, f'{clean_model_name}_MMLU.json')
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(result_copy, f, indent=2, ensure_ascii=False)
print(f"\n💾 Results saved to: {json_path}")
# Prepare data for CSV (without detailed predictions)
csv_row = {
'model_name': result['model_name'],
'overall_score': result['overall_score'],
'tasks': ', '.join(result['tasks']) if isinstance(result['tasks'], list) else result['tasks'],
'num_tasks': result.get('num_tasks', len(result['tasks']) if isinstance(result['tasks'], list) else 1),
'n_shots': result['n_shots'],
'timestamp': result['timestamp'],
'status': result['status']
}
# Add per-task scores as separate columns
task_scores = result.get('task_scores', {})
for task_name, score in task_scores.items():
csv_row[f'score_{task_name}'] = score
if 'error' in result:
csv_row['error'] = result['error']
# Save to CSV
df = pd.DataFrame([csv_row])
csv_path = os.path.join(output_dir, f'{clean_model_name}_MMLU.csv')
df.to_csv(csv_path, index=False)
print(f"💾 Results saved to: {csv_path}")
def get_available_models():
"""Get all available models from Ollama"""
try:
models_list = ollama.list()
# Extract model names, excluding embedding models
model_names = [model['model'] for model in models_list['models']
if 'embed' not in model['model'].lower()]
return sorted(model_names)
except Exception as e:
print(f"❌ Error getting model list: {e}")
return []
def run_mmlu_for_all_models():
"""Run MMLU benchmark for all available Ollama models"""
print("\n" + "="*60)
print("MMLU BENCHMARK - ALL MODELS")
print("="*60)
# Show tasks being tested
tasks = [
MMLUTask.FORMAL_LOGIC,
MMLUTask.GLOBAL_FACTS,
MMLUTask.COLLEGE_COMPUTER_SCIENCE,
MMLUTask.COLLEGE_MATHEMATICS,
MMLUTask.MARKETING,
MMLUTask.HIGH_SCHOOL_MACROECONOMICS
]
print(f"\n📚 Testing {len(tasks)} MMLU task categories:")
for task in tasks:
task_name = task.value if hasattr(task, 'value') else str(task)
print(f" - {task_name}")
# Get all available models
models = get_available_models()
if not models:
print("❌ No models found in Ollama!")
return []
print(f"\n✅ Found {len(models)} model(s) to test:")
for model in models:
print(f" - {model}")
print()
# Store all results for summary
all_results = []
# Test each model
for idx, model_name in enumerate(models, 1):
print(f"\n{'='*60}")
print(f"[{idx}/{len(models)}] Testing: {model_name}")
print('='*60)
result = run_mmlu_single_model(model_name)
all_results.append(result)
# Print immediate result
if result['status'] == 'success':
print(f"✅ {model_name}: Score = {result['overall_score']:.4f}")
else:
print(f"❌ {model_name}: Failed - {result.get('error', 'Unknown error')}")
print(f"\n{'='*60}")
print(f"Modèle {model_name} terminé. Mémoire libérée.")
print('='*60)
# Print final summary
print_summary(all_results)
return all_results
def print_summary(results):
"""Print summary of all benchmark results"""
print("\n" + "="*60)
print("BENCHMARK SUMMARY")
print("="*60)
# Filter successful results
successful_results = [r for r in results if r['status'] == 'success']
failed_results = [r for r in results if r['status'] == 'failed']
print(f"\n📊 Total models tested: {len(results)}")
print(f" ✅ Successful: {len(successful_results)}")
print(f" ❌ Failed: {len(failed_results)}")
if successful_results:
# Sort by score
sorted_results = sorted(successful_results, key=lambda x: x['overall_score'], reverse=True)
print("\n🏆 RANKINGS:")
print("-"*60)
for i, result in enumerate(sorted_results, 1):
score_pct = result['overall_score'] * 100
print(f"{i:2d}. {result['model_name']:30s} - {result['overall_score']:.4f} ({score_pct:.2f}%)")
# Statistics
scores = [r['overall_score'] for r in successful_results]
print("\n📈 STATISTICS:")
print("-"*60)
print(f"Mean score: {sum(scores)/len(scores):.4f}")
print(f"Median score: {sorted(scores)[len(scores)//2]:.4f}")
print(f"Best score: {max(scores):.4f}")
print(f"Worst score: {min(scores):.4f}")
if failed_results:
print("\n⚠️ FAILED MODELS:")
print("-"*60)
for result in failed_results:
print(f" - {result['model_name']}: {result.get('error', 'Unknown error')}")
print("\n" + "="*60)
if __name__ == "__main__":
# Run benchmark for all models
print('starting')
run_mmlu_single_model('granite4:1b-h')