Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-yardsticks/blob/main/yardsticks/mojollama-eval-runners/eval/mmlu.py # aien-yardsticks aien-dev/aien-yardsticks Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-yardsticks / yardsticks / mojollama-eval-runners / eval / mmlu.py` 172 lines · 6193 bytes Raw Blame History 1 """MMLU (Massive Multitask Language Understanding) benchmark. 2 3 57 subjects, 5-shot, multiple-choice evaluation. 4 Uses the lukaemon/mmlu dataset format from Hugging Face. 5 """ 6 7 import json 8 import os 9 import re 10 import time 11 from typing import Optional 12 13 from .base import BaseBenchmark, LlamaCppEvaluator, extract_letter_answer, normalize_answer 14 from .dataset import get_dataset_stream, get_subjects, get_dataset_path, DATASET_SOURCES 15 16 17 # Few-shot examples for MMLU (5-shot) 18 FEW_SHOT_EXAMPLES = { 19 "abstract_algebra": [ 20 { 21 "question": "Find the degree for the given field extension Q(sqrt(2), sqrt(3), sqrt(5)) over Q.", 22 "choices": ["A. 4", "B. 6", "C. 8", "D. 2"], 23 "answer": "C" 24 }, 25 { 26 "question": "Let V be the set of all solutions to the differential equation y'' + 4y' + 3y = 0. What is the dimension of V as a real vector space?", 27 "choices": ["A. 0", "B. 1", "C. 2", "D. 3"], 28 "answer": "C" 29 }, 30 { 31 "question": "How many elements of order 5 are in S_7?", 32 "choices": ["A. 504", "B. 126", "C. 21", "D. 252"], 33 "answer": "D" 34 }, 35 { 36 "question": "What is the value of the infinite sum 1 + 1/4 + 1/9 + 1/16 + ...?", 37 "choices": ["A. pi^2/4", "B. pi^2/6", "C. pi^2/8", "D. pi^2/2"], 38 "answer": "B" 39 }, 40 { 41 "question": "How many ring homomorphisms are there from Z/5Z to Z/20Z?", 42 "choices": ["A. 1", "B. 2", "C. 3", "D. 5"], 43 "answer": "A" 44 } 45 ] 46 } 47 48 _DEFAULT_FEW_SHOT = [ 49 {"question": "What is the capital of France?", "choices": ["A. London", "B. Paris", "C. Berlin", "D. Madrid"], "answer": "B"}, 50 {"question": "Which planet is known as the Red Planet?", "choices": ["A. Venus", "B. Jupiter", "C. Mars", "D. Saturn"], "answer": "C"}, 51 {"question": "What is 2 + 2?", "choices": ["A. 3", "B. 4", "C. 5", "D. 6"], "answer": "B"}, 52 ] 53 54 55 def _build_mmlu_prompt(question, choices, subject=None, few_shot=None): 56 """Build a prompt for MMLU with few-shot examples.""" 57 if few_shot is None: 58 few_shot = FEW_SHOT_EXAMPLES.get(subject, _DEFAULT_FEW_SHOT) 59 60 prompt_lines = [ 61 "The following are multiple choice questions (with answers).", 62 "" 63 ] 64 for ex in few_shot: 65 prompt_lines.append(ex["question"]) 66 for ch in ex["choices"]: 67 prompt_lines.append(ch) 68 prompt_lines.append(f"Answer: {ex['answer']}") 69 prompt_lines.append("") 70 71 # The actual question 72 prompt_lines.append(question) 73 for ch in choices: 74 prompt_lines.append(ch) 75 prompt_lines.append("Answer:") 76 77 return "\n".join(prompt_lines) 78 79 80 class MMLUBenchmark(BaseBenchmark): 81 """MMLU benchmark — 57 subjects, 5-shot multiple choice.""" 82 83 @property 84 def name(self) -> str: 85 return "MMLU" 86 87 def run(self, backend_url: str, model_name: str, 88 max_samples: int = 0, subjects: Optional[list] = None, 89 **kwargs) -> dict: 90 evaluator = LlamaCppEvaluator(backend_url, temperature=0.0, max_tokens=50) 91 t0 = time.time() 92 93 all_subjects = subjects or get_subjects("mmlu") 94 per_category = {} 95 total_correct = 0 96 total_all = 0 97 98 for subject in all_subjects: 99 records = get_dataset_stream("mmlu", subject) 100 if not records: 101 print(f" ⚠ No data for {subject}, skipping") 102 continue 103 104 if max_samples > 0: 105 records = records[:max_samples] 106 107 subject_correct = 0 108 subject_total = 0 109 110 for record in records: 111 question = record.get("question", "") 112 choices = record.get("choices", []) 113 answer_idx = record.get("answer", -1) 114 115 # Convert answer index to letter 116 labels = ["A", "B", "C", "D"] 117 answer_letter = labels[answer_idx] if 0 <= answer_idx < len(labels) else None 118 if answer_letter is None: 119 continue 120 121 # Format choices as A. text, B. text, etc. 122 formatted_choices = [ 123 f"{labels[i]}. {choices[i]}" 124 for i in range(len(choices)) 125 ] 126 127 prompt = _build_mmlu_prompt(question, formatted_choices, subject) 128 129 try: 130 generated = evaluator.complete(prompt, max_tokens=32) 131 predicted = extract_letter_answer(generated) 132 if predicted is None: 133 # Try first letter in output 134 m = re.search(r'\b([A-D])\b', generated) 135 if m: 136 predicted = m.group(1) 137 if predicted == answer_letter: 138 subject_correct += 1 139 except Exception as e: 140 print(f" ⚠ Error on {subject}: {e}") 141 142 subject_total += 1 143 144 per_category[subject] = { 145 "correct": subject_correct, 146 "total": subject_total, 147 "accuracy": subject_correct / subject_total if subject_total > 0 else 0 148 } 149 total_correct += subject_correct 150 total_all += subject_total 151 152 pct = (subject_correct / subject_total * 100) if subject_total > 0 else 0 153 print(f" {subject:40s} {pct:6.2f}% ({subject_correct}/{subject_total})") 154 155 elapsed = time.time() - t0 156 stats = evaluator.get_stats() 157 158 return { 159 "name": "MMLU", 160 "accuracy": total_correct / total_all if total_all > 0 else 0, 161 "correct": total_correct, 162 "total": total_all, 163 "per_category": per_category, 164 "metrics": { 165 "elapsed": elapsed, 166 "api_calls": stats["api_calls"], 167 "api_errors": stats["api_errors"], 168 "total_tokens": stats["total_tokens"], 169 "subjects_evaluated": len([s for s in per_category if per_category[s]["total"] > 0]) 170 }, 171 "samples": total_all 172 }