File size: 4,468 Bytes
a5b192f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Grade School Math 8k dataset."""

import json
import textwrap

import datasets


_CITATION = """\
@misc{cobbe2021training,
      title={Training Verifiers to Solve Math Word Problems},
      author={Karl Cobbe and Vineet Kosaraju and Mohammad Bavarian and Jacob Hilton and Reiichiro Nakano and Christopher Hesse and John Schulman},
      year={2021},
      eprint={2110.14168},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}
"""

_DESCRIPTION = """\
GSM8K (Grade School Math 8K) is a dataset of 8.5K high quality
linguistically diverse grade school math word problems. The
dataset was created to support the task of question answering
on basic mathematical problems that require multi-step reasoning.
"""

_HOMEPAGE = "https://openai.com/blog/grade-school-math"

_LICENSE = "MIT"

_BASE_URL = "https://raw.githubusercontent.com/openai/grade-school-math/master/grade_school_math/data/"


class Gsm8kConfig(datasets.BuilderConfig):
    """BuilderConfig for GSM8K."""

    def __init__(self, urls, **kwargs):
        """BuilderConfig for GSM8K.

        Args:
        urls: *dict[string]*, the urls for each split of the GSM8k set.
        """
        super().__init__(version=datasets.Version("1.1.0"), **kwargs)
        self.urls = urls


class Gsm8k(datasets.GeneratorBasedBuilder):
    """Grade School Math 8k (GSM8K)"""

    BUILDER_CONFIGS = [
        Gsm8kConfig(
            name="main",
            description=textwrap.dedent(
                """
                It is segmented into 7.5K training problems and 1K test problems.
                These problems take between 2 and 8 steps to solve, and solutions
                primarily involve performing a sequence of elementary calculations
                using basic arithmetic operations (+ - / *) to reach the final
                answer. A bright middle school student should be able to solve
                every problem.
                """,
            ),
            urls={
                "train": _BASE_URL + "train.jsonl",
                "test": _BASE_URL + "test.jsonl",
            },
        ),
        Gsm8kConfig(
            name="socratic",
            description=textwrap.dedent(
                """
                Additionally, there is a modified solution format that injects
                automatically generated "Socratic subquestions" before each step.
                """
            ),
            urls={
                "train": _BASE_URL + "train_socratic.jsonl",
                "test": _BASE_URL + "test_socratic.jsonl",
            },
        ),
    ]

    def _info(self):
        features = datasets.Features(
            {
                "question": datasets.Value("string"),
                "answer": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(self.config.urls)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": data_dir["train"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": data_dir["test"],
                },
            ),
        ]

    def _generate_examples(self, filepath):
        with open(filepath, encoding="utf-8") as f:
            for key, row in enumerate(f):
                data = json.loads(row)
                yield key, {
                    "question": data["question"],
                    "answer": data["answer"],
                }