File size: 8,537 Bytes
c95c7b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
"""GPT-3 Arithmetic Test Dataset."""


import json

import datasets


_CITATION = """\
@inproceedings{NEURIPS2020_1457c0d6,
    author = {Brown, Tom and Mann, Benjamin and Ryder, Nick and Subbiah, Melanie and Kaplan, Jared D and Dhariwal, Prafulla and Neelakantan, Arvind and Shyam, Pranav and Sastry, Girish and Askell, Amanda and Agarwal, Sandhini and Herbert-Voss, Ariel and Krueger, Gretchen and Henighan, Tom and Child, Rewon and Ramesh, Aditya and Ziegler, Daniel and Wu, Jeffrey and Winter, Clemens and Hesse, Chris and Chen, Mark and Sigler, Eric and Litwin, Mateusz and Gray, Scott and Chess, Benjamin and Clark, Jack and Berner, Christopher and McCandlish, Sam and Radford, Alec and Sutskever, Ilya and Amodei, Dario},
    booktitle = {Advances in Neural Information Processing Systems},
    editor = {H. Larochelle and M. Ranzato and R. Hadsell and M. F. Balcan and H. Lin},
    pages = {1877--1901},
    publisher = {Curran Associates, Inc.},
    title = {Language Models are Few-Shot Learners},
    url = {https://proceedings.neurips.cc/paper/2020/file/1457c0d6bfcb4967418bfb8ac142f64a-Paper.pdf},
    volume = {33},
    year = {2020}
}
"""

_DESCRIPTION = """\
A small battery of 10 tests that involve asking language models a simple arithmetic
problem in natural language.
"""

_HOMEPAGE = "https://github.com/openai/gpt-3/tree/master/data"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""


class ArithmeticConfig(datasets.BuilderConfig):
    """BuilderConfig for GPT3 Arithmetic Test Dataset."""

    def __init__(self, url, features, **kwargs):
        """BuilderConfig for GPT3 Arithmetic dataset.

        Args:
        url: *string*, the url to the specific subset of the GPT3 Arithmetic dataset.
        features: *list[string]*, list of the features that will appear in the
            feature dict.
        """
        # Version history:
        super().__init__(version=datasets.Version("0.0.1"), **kwargs)
        self.url = url
        self.features = features


class Arithmetic(datasets.GeneratorBasedBuilder):
    """A small battery of 10 tests involving simple arithmetic problems."""

    BUILDER_CONFIGS = [
        ArithmeticConfig(
            name="arithmetic_2da",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/two_digit_addition.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="2-digit addition",
        ),
        ArithmeticConfig(
            name="arithmetic_2ds",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/two_digit_subtraction.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="2-digit subtraction",
        ),
        ArithmeticConfig(
            name="arithmetic_3da",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/three_digit_addition.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="3-digit addition",
        ),
        ArithmeticConfig(
            name="arithmetic_3ds",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/three_digit_subtraction.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="3-digit subtraction",
        ),
        ArithmeticConfig(
            name="arithmetic_4da",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/four_digit_addition.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="4-digit addition",
        ),
        ArithmeticConfig(
            name="arithmetic_4ds",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/four_digit_subtraction.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="4-digit subtraction",
        ),
        ArithmeticConfig(
            name="arithmetic_5da",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/five_digit_addition.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="5-digit addition",
        ),
        ArithmeticConfig(
            name="arithmetic_5ds",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/five_digit_subtraction.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="5-digit subtraction",
        ),
        ArithmeticConfig(
            name="arithmetic_2dm",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/two_digit_multiplication.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="2-digit multiplication",
        ),
        ArithmeticConfig(
            name="arithmetic_1dc",
            url="https://raw.githubusercontent.com/openai/gpt-3/master/data/single_digit_three_ops.jsonl",
            features=datasets.Features(
                {
                    "context": datasets.Value("string"),
                    "completion": datasets.Value("string"),
                }
            ),
            description="Single digit 3 operations",
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=f"{_DESCRIPTION}\n{self.config.description}",
            features=self.config.features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        urls = self.config.url
        data_dir = dl_manager.download_and_extract(urls)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": data_dir,
                    "split": datasets.Split.VALIDATION,
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        with open(filepath, encoding="utf-8") as f:
            for key, row in enumerate(f):
                data = json.loads(row)
                context = (
                    data["context"]
                    .strip()
                    .replace("\n\n", "\n")
                    .replace("Q:", "Question:")
                    .replace("A:", "Answer:")
                )
                completion = data["completion"]
                yield key, {"context": context, "completion": completion}