MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / eval / dataset.py
| 1 | """Dataset downloader — Caches evaluation datasets locally. |
| 2 | |
| 3 | Downloads from Hugging Face datasets or mirrors. Supports MMLU, GSM8K, |
| 4 | CEval, HellaSwag, ARC, BBH, HumanEval. |
| 5 | """ |
| 6 | |
| 7 | import json |
| 8 | import os |
| 9 | import urllib.request |
| 10 | import urllib.error |
| 11 | import gzip |
| 12 | import shutil |
| 13 | import hashlib |
| 14 | import re |
| 15 | from pathlib import Path |
| 16 | from typing import Optional |
| 17 | |
| 18 | CACHE_DIR = os.path.expanduser("~/.mojollama/eval_datasets") |
| 19 | os.makedirs(CACHE_DIR, exist_ok=True) |
| 20 | |
| 21 | DATASET_SOURCES = { |
| 22 | "mmlu": { |
| 23 | "url": "https://huggingface.co/datasets/lukaemon/mmlu/resolve/main/data/{subject}_test.json", |
| 24 | "fallback_url": "https://huggingface.co/datasets/lukaemon/mmlu/raw/main/data/{subject}_test.json", |
| 25 | "subjects": [ |
| 26 | "abstract_algebra", "anatomy", "astronomy", "business_ethics", |
| 27 | "clinical_knowledge", "college_biology", "college_chemistry", |
| 28 | "college_computer_science", "college_mathematics", "college_medicine", |
| 29 | "college_physics", "computer_security", "conceptual_physics", |
| 30 | "econometrics", "electrical_engineering", "elementary_mathematics", |
| 31 | "formal_logic", "global_facts", "high_school_biology", |
| 32 | "high_school_chemistry", "high_school_computer_science", |
| 33 | "high_school_european_history", "high_school_geography", |
| 34 | "high_school_government_and_politics", "high_school_macroeconomics", |
| 35 | "high_school_mathematics", "high_school_microeconomics", |
| 36 | "high_school_physics", "high_school_psychology", |
| 37 | "high_school_statistics", "high_school_us_history", |
| 38 | "high_school_world_history", "human_aging", "human_sexuality", |
| 39 | "international_law", "jurisprudence", "logical_fallacies", |
| 40 | "machine_learning", "management", "marketing", "medical_genetics", |
| 41 | "miscellaneous", "moral_disputes", "moral_scenarios", |
| 42 | "nutrition", "philosophy", "prehistory", "professional_accounting", |
| 43 | "professional_law", "professional_medicine", "professional_psychology", |
| 44 | "public_relations", "security_studies", "sociology", |
| 45 | "us_foreign_policy", "virology", "world_religions" |
| 46 | ] |
| 47 | }, |
| 48 | "gsm8k": { |
| 49 | "url": "https://huggingface.co/datasets/openai/gsm8k/resolve/main/data/test.jsonl", |
| 50 | "fallback_url": "https://huggingface.co/datasets/openai/gsm8k/raw/main/data/test.jsonl" |
| 51 | }, |
| 52 | "ceval": { |
| 53 | "url": "https://huggingface.co/datasets/ceval/ceval-exam/resolve/main/data/{subject}_test.json", |
| 54 | "subjects": [ |
| 55 | "accountant", "advanced_mathematics", "art_studies", "basic_medicine", |
| 56 | "business_administration", "chinese_language_and_literature", |
| 57 | "college_chemistry", "college_physics", "computer_network", |
| 58 | "computer_organization_and_architecture", "criminal_law", "criminal_procedure_law", |
| 59 | "discrete_mathematics", "education_science", "electrical_engineer", |
| 60 | "environmental_impact_assessment_engineer", "fire_fighting", |
| 61 | "high_school_biology", "high_school_chemistry", "high_school_chinese", |
| 62 | "high_school_geography", "high_school_history", "high_school_mathematics", |
| 63 | "high_school_physics", "high_school_politics", "ideological_and_moral_cultivation", |
| 64 | "internet_information_service_engineer", "law", "lawyer_qualification", |
| 65 | "legal_professional", "legal_system_and_legal_institutions", |
| 66 | "logistics_engineering_and_management", "mao_zedong_thought", |
| 67 | "marxism", "metrology_engineer", "middle_school_biology", |
| 68 | "middle_school_chemistry", "middle_school_geography", "middle_school_history", |
| 69 | "middle_school_mathematics", "middle_school_physics", "middle_school_politics", |
| 70 | "modern_chinese_history", "operating_system", "pharmacy", |
| 71 | "principles_of_computer_composition", "probability_and_statistics", |
| 72 | "professional_geography_of_civil_engineering", "professional_knowledge_of_civil_engineering", |
| 73 | "sports_training", "tax_law_agent", "teacher_qualification", |
| 74 | "tour_guide", "urban_and_rural_planning", "veterinary_medicine", |
| 75 | "water_resources", "writer" |
| 76 | ] |
| 77 | }, |
| 78 | "hellaswag": { |
| 79 | "url": "https://huggingface.co/datasets/rowan/hellaswag/resolve/main/data/hellaswag_test.jsonl", |
| 80 | "fallback_url": "https://huggingface.co/datasets/rowan/hellaswag/raw/main/data/hellaswag_test.jsonl" |
| 81 | }, |
| 82 | "arc": { |
| 83 | "challenge_url": "https://huggingface.co/datasets/ai2_arc/resolve/main/data/ARC-Challenge-Test.jsonl", |
| 84 | "easy_url": "https://huggingface.co/datasets/ai2_arc/resolve/main/data/ARC-Easy-Test.jsonl", |
| 85 | "challenge_fallback": "https://huggingface.co/datasets/ai2_arc/raw/main/data/ARC-Challenge-Test.jsonl", |
| 86 | "easy_fallback": "https://huggingface.co/datasets/ai2_arc/raw/main/data/ARC-Easy-Test.jsonl" |
| 87 | }, |
| 88 | "bbh": { |
| 89 | "url_templates": { |
| 90 | "boolean_expressions": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/boolean_expressions/test.jsonl", |
| 91 | "causal_judgment": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/causal_judgment/test.jsonl", |
| 92 | "date_understanding": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/date_understanding/test.jsonl", |
| 93 | "disambiguation_qa": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/disambiguation_qa/test.jsonl", |
| 94 | "dyck_languages": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/dyck_languages/test.jsonl", |
| 95 | "formal_fallacies": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/formal_fallacies/test.jsonl", |
| 96 | "geometric_shapes": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/geometric_shapes/test.jsonl", |
| 97 | "hyperbaton": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/hyperbaton/test.jsonl", |
| 98 | "logical_deduction_five_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/logical_deduction_five_objects/test.jsonl", |
| 99 | "logical_deduction_seven_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/logical_deduction_seven_objects/test.jsonl", |
| 100 | "logical_deduction_three_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/logical_deduction_three_objects/test.jsonl", |
| 101 | "movie_recommendation": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/movie_recommendation/test.jsonl", |
| 102 | "multistep_arithmetic_two": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/multistep_arithmetic_two/test.jsonl", |
| 103 | "navigate": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/navigate/test.jsonl", |
| 104 | "object_counting": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/object_counting/test.jsonl", |
| 105 | "penguins_in_a_table": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/penguins_in_a_table/test.jsonl", |
| 106 | "reasoning_about_colored_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/reasoning_about_colored_objects/test.jsonl", |
| 107 | "ruin_names": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/ruin_names/test.jsonl", |
| 108 | "salient_translation_error_detection": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/salient_translation_error_detection/test.jsonl", |
| 109 | "snarks": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/snarks/test.jsonl", |
| 110 | "sports_understanding": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/sports_understanding/test.jsonl", |
| 111 | "temporal_sequences": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/temporal_sequences/test.jsonl", |
| 112 | "tracking_shuffled_objects_five_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/tracking_shuffled_objects_five_objects/test.jsonl", |
| 113 | "tracking_shuffled_objects_seven_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/tracking_shuffled_objects_seven_objects/test.jsonl", |
| 114 | "tracking_shuffled_objects_three_objects": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/tracking_shuffled_objects_three_objects/test.jsonl", |
| 115 | "web_of_lies": "https://huggingface.co/datasets/lukaemon/bbh/resolve/main/data/web_of_lies/test.jsonl", |
| 116 | } |
| 117 | }, |
| 118 | "humaneval": { |
| 119 | "url": "https://huggingface.co/datasets/openai/openai_humaneval/resolve/main/data/test.jsonl", |
| 120 | "fallback_url": "https://huggingface.co/datasets/openai/openai_humaneval/raw/main/data/test.jsonl" |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | # Embedded mini test data for benchmarks that don't need full download |
| 125 | EMBEDDED_SAMPLES = {} |
| 126 | |
| 127 | |
| 128 | def _download_file(url: str, dest: str) -> bool: |
| 129 | """Download a file with progress. Returns True on success.""" |
| 130 | try: |
| 131 | print(f" Downloading {url}...") |
| 132 | urllib.request.urlretrieve(url, dest) |
| 133 | return True |
| 134 | except Exception as e: |
| 135 | print(f" Download failed: {e}") |
| 136 | return False |
| 137 | |
| 138 | |
| 139 | def get_dataset_path(name: str, subject: Optional[str] = None, force_download: bool = False) -> Optional[str]: |
| 140 | """Get cached path for a dataset. Downloads if needed. |
| 141 | |
| 142 | Args: |
| 143 | name: Dataset name (mmlu, gsm8k, ceval, hellaswag, arc, bbh, humaneval) |
| 144 | subject: Subject name for multi-subject datasets (mmlu, ceval) |
| 145 | force_download: Re-download even if cached |
| 146 | |
| 147 | Returns: |
| 148 | Path to cached file, or None if download failed |
| 149 | """ |
| 150 | source = DATASET_SOURCES.get(name) |
| 151 | if not source: |
| 152 | return None |
| 153 | |
| 154 | if subject: |
| 155 | cache_file = os.path.join(CACHE_DIR, name, f"{subject}.json") |
| 156 | os.makedirs(os.path.join(CACHE_DIR, name), exist_ok=True) |
| 157 | else: |
| 158 | cache_file = os.path.join(CACHE_DIR, f"{name}.jsonl") |
| 159 | os.makedirs(CACHE_DIR, exist_ok=True) |
| 160 | |
| 161 | if os.path.exists(cache_file) and not force_download: |
| 162 | return cache_file |
| 163 | |
| 164 | # Build URL |
| 165 | if subject: |
| 166 | url = source.get("url", "").replace("{subject}", subject) |
| 167 | fallback = source.get("fallback_url", "").replace("{subject}", subject) |
| 168 | else: |
| 169 | url = source.get("url", "") |
| 170 | fallback = source.get("fallback_url", "") |
| 171 | |
| 172 | if not url: |
| 173 | return None |
| 174 | |
| 175 | if _download_file(url, cache_file): |
| 176 | return cache_file |
| 177 | if fallback and _download_file(fallback, cache_file): |
| 178 | return cache_file |
| 179 | |
| 180 | return None |
| 181 | |
| 182 | |
| 183 | def get_dataset_stream(name: str, subject: Optional[str] = None): |
| 184 | """Get a stream of JSON records for a dataset. |
| 185 | |
| 186 | Usage: for record in get_dataset_stream('gsm8k'): |
| 187 | question = record['question'] |
| 188 | |
| 189 | Returns an iterable of parsed JSON dicts, or empty list on failure. |
| 190 | """ |
| 191 | path = get_dataset_path(name, subject) |
| 192 | if not path: |
| 193 | return [] |
| 194 | |
| 195 | records = [] |
| 196 | try: |
| 197 | with open(path, "r") as f: |
| 198 | for line in f: |
| 199 | line = line.strip() |
| 200 | if line: |
| 201 | records.append(json.loads(line)) |
| 202 | except json.JSONDecodeError: |
| 203 | # Try as JSON array |
| 204 | try: |
| 205 | with open(path, "r") as f: |
| 206 | data = json.load(f) |
| 207 | if isinstance(data, list): |
| 208 | records = data |
| 209 | except Exception: |
| 210 | pass |
| 211 | except Exception: |
| 212 | pass |
| 213 | |
| 214 | return records |
| 215 | |
| 216 | |
| 217 | def get_subjects(name: str) -> list: |
| 218 | """Get list of subjects for multi-subject datasets.""" |
| 219 | source = DATASET_SOURCES.get(name, {}) |
| 220 | return source.get("subjects", []) |
| 221 | |
| 222 | |
| 223 | def list_available_datasets() -> dict: |
| 224 | """List which datasets are already cached.""" |
| 225 | available = {} |
| 226 | for name in DATASET_SOURCES: |
| 227 | path = os.path.join(CACHE_DIR, f"{name}.jsonl") |
| 228 | if os.path.exists(path): |
| 229 | available[name] = { |
| 230 | "path": path, |
| 231 | "size": os.path.getsize(path) |
| 232 | } |
| 233 | else: |
| 234 | subj_dir = os.path.join(CACHE_DIR, name) |
| 235 | if os.path.isdir(subj_dir): |
| 236 | files = os.listdir(subj_dir) |
| 237 | available[name] = { |
| 238 | "path": subj_dir, |
| 239 | "subjects": len(files), |
| 240 | "size": sum(os.path.getsize(os.path.join(subj_dir, f)) for f in files if os.path.isfile(os.path.join(subj_dir, f))) |
| 241 | } |
| 242 | return available |