-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
290 lines (258 loc) · 7.99 KB
/
dataset.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import os
import json
from typing import Optional
import numpy as np
from tqdm import tqdm
import jax
import tensorflow as tf
import datasets
from datasets import load_dataset, IterableDataset
import datasets.config
from transformers import AutoTokenizer
from utils import (
make_fsarray_from_local_slice,
prefetch_iterator,
threadstart_iterator,
write_note,
)
os.environ["TOKENIZERS_PARALLELISM"] = "true"
datasets.config.STREAMING_READ_MAX_RETRIES = 17280 # 17280 * 5 = 1 day
datasets.config.STREAMING_READ_RETRY_INTERVAL = 5
OPTIONS = tf.data.Options()
OPTIONS.deterministic = True
OPTIONS.threading.private_threadpool_size = 48
OPTIONS.threading.max_intra_op_parallelism = 1
# Stop a whole bunch of magic stuff that eats up all RAM:
OPTIONS.experimental_optimization.inject_prefetch = False
TOKENIZER = "mistralai/Mistral-7B-v0.3"
def prepare_hellaswag(
batch_size: int,
block_size: int,
flat_devices,
tf_prefetch: int = 2,
device_prefetch: int = 0,
):
"""Read file and tokenize the hellaswag dataset."""
write_note("preparing hellaswag")
tokenizer = AutoTokenizer.from_pretrained(
TOKENIZER, trust_remote_code=True, use_fast=True
)
all_data = []
all_beginning_lengths = []
all_seq_lengths = []
all_labels = []
with open("data/hellaswag_val.jsonl", "r") as f:
# iterate over lines and tokenize
for line in tqdm(f, total=10042):
item = json.loads(line)
context = item["ctx"]
endings = item["endings"]
correct_end = item["label"]
beginning_length = len(tokenizer(context)["input_ids"])
data_to_concat = []
beginning_lengths_to_concat = []
seq_lengths_to_concat = []
for ending in endings:
output = tokenizer(context + " " + ending)["input_ids"]
output_len = len(output)
# pad to block_size
if output_len < block_size:
output = output + [tokenizer.eos_token_id] * (
block_size - output_len
)
# max length is block_size
output = output[:block_size]
data_to_concat.append(output)
beginning_lengths_to_concat.append(beginning_length)
seq_lengths_to_concat.append(output_len)
all_data.append(np.array(data_to_concat, dtype=np.uint16))
all_beginning_lengths.append(
np.array(beginning_lengths_to_concat, dtype=np.int32)
)
all_seq_lengths.append(np.array(seq_lengths_to_concat, dtype=np.int32))
all_labels.append(int(correct_end))
all_data = np.array(all_data, dtype=np.uint16)
all_beginning_lengths = np.array(all_beginning_lengths, dtype=np.int32)
all_seq_lengths = np.array(all_seq_lengths, dtype=np.int32)
all_labels = np.array(all_labels, dtype=np.int32)
ds = tf.data.Dataset.from_tensor_slices(
(all_data, all_beginning_lengths, all_seq_lengths, all_labels)
)
ds = ds.shard(jax.process_count(), jax.process_index())
ds = ds.repeat()
ds = ds.batch(
batch_size // jax.process_count(),
drop_remainder=True,
num_parallel_calls=tf.data.AUTOTUNE,
)
ds = ds.with_options(OPTIONS)
ds = ds.prefetch(tf_prefetch)
ds = ds.as_numpy_iterator()
ds = iter(ds)
# ds = threadstart_iterator(ds)
ds = (
jax.tree.map(lambda x: make_fsarray_from_local_slice(x, flat_devices), elem)
for elem in ds
)
if device_prefetch > 0:
ds = prefetch_iterator(ds, device_prefetch)
return ds
def fineweb_edu_dataset(
batch_size: int,
block_size: int,
flat_devices,
fineweb_edu_name: Optional[str] = None,
tf_prefetch: int = 5,
device_prefetch: int = 0,
):
"""Load the fineweb-edu dataset."""
platform = jax.devices()[0].platform
# use /dev/shm if on a TPU vm for more space
if platform == "tpu":
cache_dir = "/dev/shm/huggingface_cache"
else:
cache_dir = None
tokenizer = AutoTokenizer.from_pretrained(
TOKENIZER, trust_remote_code=True, use_fast=True
)
def gen():
hf_ds: IterableDataset = load_dataset(
"HuggingFaceFW/fineweb-edu",
split="train",
name=fineweb_edu_name,
cache_dir=cache_dir,
streaming=True,
)
def tokenize(example):
# mistral tokenizer adds bos token to beginning
tokenized = tokenizer(example)["input_ids"]
# cap tokenized lengths to 10 * block_size to prevent too much
# similarity between blocks in a batch or group of batches
tokenized = [t[: 10 * block_size] for t in tokenized]
return {"tokens": tokenized}
hf_ds = hf_ds.map(tokenize, input_columns="text", batched=True, batch_size=128)
hf_ds = hf_ds.with_format("numpy")
for example in hf_ds:
yield example["tokens"].astype(np.uint16)
ds = tf.data.Dataset.from_generator(
gen, output_signature=tf.TensorSpec(shape=(None,), dtype=tf.uint16)
)
ds = ds.shuffle(128) # shuffle dataset examples
ds = ds.unbatch()
ds = ds.batch(block_size, drop_remainder=True, num_parallel_calls=tf.data.AUTOTUNE)
ds = ds.shuffle(20 * 1024) # shuffle blocks
ds = ds.batch(
batch_size // jax.process_count(),
drop_remainder=True,
num_parallel_calls=tf.data.AUTOTUNE,
)
ds = ds.with_options(OPTIONS)
ds = ds.prefetch(tf_prefetch)
ds = ds.as_numpy_iterator()
ds = iter(ds)
# ds = threadstart_iterator(ds)
ds = (
jax.tree.map(lambda x: make_fsarray_from_local_slice(x, flat_devices), elem)
for elem in ds
)
if device_prefetch > 0:
ds = prefetch_iterator(ds, device_prefetch)
return ds
# fineweb-edu has 96 shards
_fw_shard_names = [
"CC-MAIN-2024-10",
"CC-MAIN-2023-50",
"CC-MAIN-2023-40",
"CC-MAIN-2023-23",
"CC-MAIN-2023-14",
"CC-MAIN-2023-06",
"CC-MAIN-2022-49",
"CC-MAIN-2022-40",
"CC-MAIN-2022-33",
"CC-MAIN-2022-27",
"CC-MAIN-2022-21",
"CC-MAIN-2022-05",
"CC-MAIN-2021-49",
"CC-MAIN-2021-43",
"CC-MAIN-2021-39",
"CC-MAIN-2021-31",
"CC-MAIN-2021-25",
"CC-MAIN-2021-21",
"CC-MAIN-2021-17",
"CC-MAIN-2021-10",
"CC-MAIN-2021-04",
"CC-MAIN-2020-50",
"CC-MAIN-2020-45",
"CC-MAIN-2020-40",
"CC-MAIN-2020-34",
"CC-MAIN-2020-29",
"CC-MAIN-2020-24",
"CC-MAIN-2020-16",
"CC-MAIN-2020-10",
"CC-MAIN-2020-05",
"CC-MAIN-2019-51",
"CC-MAIN-2019-47",
"CC-MAIN-2019-43",
"CC-MAIN-2019-39",
"CC-MAIN-2019-35",
"CC-MAIN-2019-30",
"CC-MAIN-2019-26",
"CC-MAIN-2019-22",
"CC-MAIN-2019-18",
"CC-MAIN-2019-13",
"CC-MAIN-2019-09",
"CC-MAIN-2019-04",
"CC-MAIN-2018-51",
"CC-MAIN-2018-47",
"CC-MAIN-2018-43",
"CC-MAIN-2018-39",
"CC-MAIN-2018-34",
"CC-MAIN-2018-30",
"CC-MAIN-2018-26",
"CC-MAIN-2018-22",
"CC-MAIN-2018-17",
"CC-MAIN-2018-13",
"CC-MAIN-2018-09",
"CC-MAIN-2018-05",
"CC-MAIN-2017-51",
"CC-MAIN-2017-47",
"CC-MAIN-2017-43",
"CC-MAIN-2017-39",
"CC-MAIN-2017-34",
"CC-MAIN-2017-30",
"CC-MAIN-2017-26",
"CC-MAIN-2017-22",
"CC-MAIN-2017-17",
"CC-MAIN-2017-13",
"CC-MAIN-2017-09",
"CC-MAIN-2017-04",
"CC-MAIN-2016-50",
"CC-MAIN-2016-44",
"CC-MAIN-2016-40",
"CC-MAIN-2016-36",
"CC-MAIN-2016-30",
"CC-MAIN-2016-26",
"CC-MAIN-2016-22",
"CC-MAIN-2016-18",
"CC-MAIN-2016-07",
"CC-MAIN-2015-48",
"CC-MAIN-2015-40",
"CC-MAIN-2015-35",
"CC-MAIN-2015-32",
"CC-MAIN-2015-27",
"CC-MAIN-2015-22",
"CC-MAIN-2015-18",
"CC-MAIN-2015-14",
"CC-MAIN-2015-11",
"CC-MAIN-2015-06",
"CC-MAIN-2014-52",
"CC-MAIN-2014-49",
"CC-MAIN-2014-42",
"CC-MAIN-2014-41",
"CC-MAIN-2014-35",
"CC-MAIN-2014-23",
"CC-MAIN-2014-15",
"CC-MAIN-2014-10",
"CC-MAIN-2013-48",
"CC-MAIN-2013-20",
]