Yukang commited on
Commit
fc8c11f
1 Parent(s): 9b1ff89

Create pile.py

Browse files
Files changed (1) hide show
  1. pile.py +262 -0
pile.py ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """The Pile dataset."""
16
+
17
+ import json
18
+
19
+ import datasets
20
+
21
+
22
+ _CITATION = """\
23
+ @misc{gao2020pile,
24
+ title={The Pile: An 800GB Dataset of Diverse Text for Language Modeling},
25
+ author={Leo Gao and Stella Biderman and Sid Black and Laurence Golding and Travis Hoppe and Charles Foster and Jason Phang and Horace He and Anish Thite and Noa Nabeshima and Shawn Presser and Connor Leahy},
26
+ year={2020},
27
+ eprint={2101.00027},
28
+ archivePrefix={arXiv},
29
+ primaryClass={cs.CL}
30
+ }
31
+ """
32
+
33
+ _DESCRIPTION = """\
34
+ The Pile is a 825 GiB diverse, open source language modelling data set that consists of 22 smaller, high-quality
35
+ datasets combined together.
36
+ """
37
+
38
+ _HOMEPAGE = "https://pile.eleuther.ai/"
39
+
40
+ _LICENSES = {
41
+ "all": "Multiple: see each subset license",
42
+ "enron_emails": "Unknown",
43
+ "europarl": "Unknown",
44
+ "free_law": "Unknown",
45
+ "hacker_news": "Unknown",
46
+ "nih_exporter": "Unknown",
47
+ "pubmed": "Unknown",
48
+ "pubmed_central": "Unknown",
49
+ "ubuntu_irc": "Unknown",
50
+ "uspto": "Unknown",
51
+ "github": "Unknown",
52
+ }
53
+
54
+ _HOST_URL = "https://the-eye.eu"
55
+ _DATA_URLS = {
56
+ "all": {
57
+ "train": [f"{_HOST_URL}/public/AI/pile/train/{i:0>2}.jsonl.zst" for i in range(5)],
58
+ "validation": [f"{_HOST_URL}/public/AI/pile/val.jsonl.zst"],
59
+ "test": [f"{_HOST_URL}/public/AI/pile/test.jsonl.zst"],
60
+ },
61
+ "enron_emails": "https://www.cs.cmu.edu/~enron/enron_mail_20150507.tar.gz",
62
+ "europarl": f"{_HOST_URL}/public/AI/pile_preliminary_components/EuroParliamentProceedings_1996_2011.jsonl.zst",
63
+ "free_law": f"{_HOST_URL}/public/AI/pile_preliminary_components/FreeLaw_Opinions.jsonl.zst",
64
+ "hacker_news": f"{_HOST_URL}/public/AI/pile_preliminary_components/hn.tar.gz",
65
+ "nih_exporter": f"{_HOST_URL}/public/AI/pile_preliminary_components/NIH_ExPORTER_awarded_grant_text.jsonl.zst",
66
+ "pubmed": f"{_HOST_URL}/public/AI/pile_preliminary_components/PUBMED_title_abstracts_2019_baseline.jsonl.zst",
67
+ "pubmed_central": f"{_HOST_URL}/public/AI/pile_preliminary_components/PMC_extracts.tar.gz",
68
+ "ubuntu_irc": f"{_HOST_URL}/public/AI/pile_preliminary_components/ubuntu_irc_until_2020_9_1.jsonl.zst",
69
+ "uspto": f"{_HOST_URL}/public/AI/pile_preliminary_components/pile_uspto.tar",
70
+ "github": f"{_HOST_URL}/public/AI/pile_preliminary_components/github.tar",
71
+ }
72
+
73
+ _FEATURES = {
74
+ "all": datasets.Features(
75
+ {
76
+ "text": datasets.Value("string"),
77
+ "meta": {"pile_set_name": datasets.Value("string")},
78
+ }
79
+ ),
80
+ "enron_emails": datasets.Features(
81
+ {
82
+ "text": datasets.Value("string"),
83
+ "meta": datasets.Value("string"),
84
+ }
85
+ ),
86
+ "europarl": datasets.Features(
87
+ {
88
+ "text": datasets.Value("string"),
89
+ "meta": datasets.Value("string"),
90
+ }
91
+ ),
92
+ "free_law": datasets.Features(
93
+ {
94
+ "text": datasets.Value("string"),
95
+ "meta": datasets.Value("string"),
96
+ }
97
+ ),
98
+ "hacker_news": datasets.Features(
99
+ {
100
+ "text": datasets.Value("string"),
101
+ "meta": datasets.Value("string"),
102
+ }
103
+ ),
104
+ "nih_exporter": datasets.Features(
105
+ {
106
+ "text": datasets.Value("string"),
107
+ "meta": datasets.Value("string"),
108
+ }
109
+ ),
110
+ "pubmed": datasets.Features(
111
+ {
112
+ "text": datasets.Value("string"),
113
+ "meta": datasets.Value("string"),
114
+ }
115
+ ),
116
+ "pubmed_central": datasets.Features(
117
+ {
118
+ "text": datasets.Value("string"),
119
+ "meta": datasets.Value("string"),
120
+ }
121
+ ),
122
+ "ubuntu_irc": datasets.Features(
123
+ {
124
+ "text": datasets.Value("string"),
125
+ "meta": datasets.Value("string"),
126
+ }
127
+ ),
128
+ "uspto": datasets.Features(
129
+ {
130
+ "text": datasets.Value("string"),
131
+ "meta": datasets.Value("string"),
132
+ }
133
+ ),
134
+ "github": datasets.Features(
135
+ {
136
+ "text": datasets.Value("string"),
137
+ "meta": datasets.Value("string"),
138
+ }
139
+ ),
140
+ }
141
+
142
+
143
+ class ThePileConfig(datasets.BuilderConfig):
144
+ """BuilderConfig for The Pile."""
145
+
146
+ def __init__(self, *args, subsets, **kwargs):
147
+ """BuilderConfig for The Pile.
148
+
149
+ Args:
150
+ subsets (:obj:`List[str]`): List of subsets to load.
151
+ **kwargs: keyword arguments forwarded to super.
152
+ """
153
+ super().__init__(
154
+ *args,
155
+ name="+".join(subsets),
156
+ **kwargs,
157
+ )
158
+ self.subsets = subsets
159
+
160
+
161
+ class ThePile(datasets.GeneratorBasedBuilder):
162
+ """The Pile dataset."""
163
+
164
+ VERSION = datasets.Version("1.1.0")
165
+
166
+ BUILDER_CONFIG_CLASS = ThePileConfig
167
+ BUILDER_CONFIGS = [ThePileConfig(subsets=[subset]) for subset in _DATA_URLS]
168
+ DEFAULT_CONFIG_NAME = "all"
169
+
170
+ def _info(self):
171
+ """Give information and typings for the dataset."""
172
+ return datasets.DatasetInfo(
173
+ # This is the description that will appear on the datasets page.
174
+ description=_DESCRIPTION,
175
+ # This defines the different columns of the dataset and their types
176
+ features=_FEATURES.get(self.config.name),
177
+ # If there's a common (input, target) tuple from the features,
178
+ # specify them here. They'll be used if as_supervised=True in
179
+ # builder.as_dataset.
180
+ supervised_keys=None,
181
+ # Homepage of the dataset for documentation
182
+ homepage=_HOMEPAGE,
183
+ # License for the dataset if available
184
+ license=_LICENSES.get(self.config.name, "Multiple: see each subset license"),
185
+ # Citation for the dataset
186
+ citation=_CITATION,
187
+ )
188
+
189
+ def _split_generators(self, dl_manager):
190
+ """Return SplitGenerators."""
191
+ if self.config.name == "all":
192
+ data_dir = dl_manager.download(_DATA_URLS[self.config.name])
193
+ return [
194
+ datasets.SplitGenerator(
195
+ name=split,
196
+ gen_kwargs={
197
+ "files": data_dir[split],
198
+ },
199
+ )
200
+ for split in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]
201
+ ]
202
+ else:
203
+ data_urls = {subset: _DATA_URLS[subset] for subset in self.config.subsets}
204
+ archive = dl_manager.download(data_urls)
205
+ return [
206
+ datasets.SplitGenerator(
207
+ name=datasets.Split.TRAIN,
208
+ gen_kwargs={
209
+ "files": {
210
+ subset: dl_manager.iter_archive(archive[subset])
211
+ if ".tar" in data_urls[subset]
212
+ else archive[subset]
213
+ for subset in self.config.subsets
214
+ },
215
+ },
216
+ ),
217
+ ]
218
+
219
+ def _generate_examples(self, files):
220
+ """Yield examples as (key, example) tuples."""
221
+ key = 0
222
+ if isinstance(files, list):
223
+ import zstandard as zstd
224
+
225
+ for path in files:
226
+ with zstd.open(open(path, "rb"), "rt", encoding="utf-8") as f:
227
+ for row in f:
228
+ data = json.loads(row)
229
+ yield key, data
230
+ key += 1
231
+ else:
232
+ for subset in files:
233
+ if subset in {"europarl", "free_law", "nih_exporter", "pubmed", "ubuntu_irc"}:
234
+ import zstandard as zstd
235
+
236
+ with zstd.open(open(files[subset], "rb"), "rt", encoding="utf-8") as f:
237
+ for row in f:
238
+ data = json.loads(row)
239
+ yield key, data
240
+ key += 1
241
+ elif subset in {"enron_emails", "hacker_news", "pubmed_central"}:
242
+ for path, file in files[subset]:
243
+ if subset == "enron_emails":
244
+ meta = {"file": path}
245
+ else:
246
+ id_ = path.split("/")[-1].split(".")[0]
247
+ meta = {"id": id_}
248
+ text = file.read().decode("utf-8", errors="ignore") # encoding errors in enron_emails
249
+ yield key, {
250
+ "text": text,
251
+ "meta": str(meta),
252
+ }
253
+ key += 1
254
+ elif subset in {"uspto", "github"}:
255
+ import zstandard as zstd
256
+
257
+ for path, file in files[subset]:
258
+ with zstd.open(file, "rt", encoding="utf-8") as f:
259
+ for row in f:
260
+ data = json.loads(row)
261
+ yield key, data
262
+ key += 1