"""Mathematics Aptitude Test of Heuristics (MATH) dataset, lighteval format with correct builder configs.""" import json import os from datasets import load_dataset, Dataset, DatasetDict, GeneratorBasedBuilder, BuilderConfig, DatasetInfo, Value, Features, Split, SplitGenerator, Version _CITATION = """\ @article{hendrycksmath2021, title={Measuring Mathematical Problem Solving With the MATH Dataset}, author={Dan Hendrycks and Collin Burns and Saurav Kadavath and Akul Arora and Steven Basart and Eric Tang and Dawn Song and Jacob Steinhardt}, journal={arXiv preprint arXiv:2103.03874}, year={2021} } """ _DESCRIPTION = """\ The Mathematics Aptitude Test of Heuristics (MATH) dataset consists of problems from mathematics competitions, including the AMC 10, AMC 12, AIME, and more. Each problem in MATH has a full step-by-step solution, which can be used to teach models to generate answer derivations and explanations. This version of the dataset includes appropriate builder configs s.t. it can be used as a drop-in replacement for the now missing lighteval/MATH dataset. """ _HOMEPAGE = "https://github.com/hendrycks/math" _LICENSE = "https://github.com/hendrycks/math/blob/main/LICENSE" # Original data URL: "https://people.eecs.berkeley.edu/~hendrycks/MATH.tar" _URL = "data/MATH.zip" class FilteredTypeConfig(BuilderConfig): def __init__(self, type_value, type_name, **kwargs): super().__init__(**kwargs) self.type_value = type_value self.type_name = type_name class FilteredTypeDatasetBuilder(GeneratorBasedBuilder): """Mathematics Aptitude Test of Heuristics (MATH) dataset.""" VERSION = Version("1.0.0") BUILDER_CONFIGS = [FilteredTypeConfig( name="default", version="1.0.0", description=f"default builder config", type_name="default", # for builder config type_value="default", # in original dataset )] + [ FilteredTypeConfig( name=type_name, version="1.0.0", description=f"Dataset filtered by type: {type_value}", type_name=type_name, # for builder config type_value=type_value, # in original dataset ) for type_name, type_value in [("algebra", "Algebra"), ("counting_and_probability", "Counting & Probability"), ("geometry", "Geometry"), ("intermediate_algebra", "Intermediate Algebra"), ("number_theory", "Number Theory"), ("prealgebra", "Prealgebra"), ("precalculus", "Precalculus")] ] def _info(self): return DatasetInfo( description=_DESCRIPTION, features=Features({ "problem": Value("string"), "level": Value("string"), "solution": Value("string"), "type": Value("string"), }), supervised_keys=None, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" download_dir = dl_manager.download_and_extract(_URL) return [ SplitGenerator( name=Split.TRAIN, gen_kwargs={"data_dir": dl_manager.iter_files(os.path.join(download_dir, "MATH", "train"))}, ), SplitGenerator( name=Split.TEST, gen_kwargs={"data_dir": dl_manager.iter_files(os.path.join(download_dir, "MATH", "test"))}, ), ] def _generate_examples(self, data_dir): type_value = self.config.type_value # Access the type value for the current config """Yields examples as (key, example) tuples. Filters by type if appropriate builder config is given.""" for id_, filepath in enumerate(data_dir): with open(filepath, "rb") as fin: example = json.load(fin) if type_value == "default" or example["type"] == type_value: yield id_, example