forked from nikitakit/flax_bert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
431 lines (375 loc) · 16.1 KB
/
data.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Copyright 2020 The FlaxBERT Authors.
#
# 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.
"""Huggingface input pipelines."""
import datasets
import numpy as np
import random
import torch
import jax
class DataPipeline:
def __init__(self, dataset):
self.dataset = dataset
def get_inputs(self, batch_size, split=None, training=False):
dataloader = torch.utils.data.DataLoader(
self.dataset[split],
collate_fn=self.collate,
batch_size=batch_size,
drop_last=training,
shuffle=training,
num_workers=64,
)
if training:
while True:
for batch in iter(dataloader):
yield dict(batch) # The dict-like types from huggingface datasets are not pytrees
else:
for batch in iter(dataloader):
yield dict(batch) # The dict-like types from huggingface datasets are not pytrees
def collate(self, examples):
raise NotImplementedError("DataPipeline subclasess must define a collate function.")
class ClassificationDataPipeline(DataPipeline):
def __init__(self, dataset, tokenizer):
self.tokenizer = tokenizer
# shard train here already to avoid unnecessary tokenization.
dataset['train'] = dataset['train'].shard(jax.host_count(), jax.host_id())
if isinstance(dataset, dict):
single_split = dataset['train']
else:
single_split = dataset
name_a, *names_other = [
name for name, feature in single_split.features.items()
if feature.dtype=='string']
assert len(names_other) <= 1, (
'Only single sentences and sentence pairs allowed.')
if names_other:
name_b = names_other[0]
tokenize = lambda example: self.tokenizer(
example[name_a], example[name_b], truncation=True)
else:
tokenize = lambda example: self.tokenizer(
example[name_a], truncation=True)
mapped_dataset = dataset.map(tokenize, batched=True)
mapped_dataset.set_format('numpy', columns=[
'idx', 'input_ids', 'token_type_ids', 'attention_mask', 'label'])
super().__init__(mapped_dataset)
def collate(self, examples):
return self.tokenizer.pad(
examples,
padding='max_length',
max_length=self.tokenizer.model_max_length,
return_tensors='np',
)
class PretrainingDataPipeline(DataPipeline):
FEATURE_NAMES = [
'attention_mask',
'input_ids',
'token_type_ids',
'next_sentence_label'
]
def __init__(self, dataset, tokenizer,
short_seq_prob=0.1,
max_predictions_per_seq=80):
self.tokenizer = tokenizer
self.short_seq_prob = short_seq_prob
self.max_predictions_per_seq = max_predictions_per_seq
cache_file_name=(
f"cache/"
f"pretrain_new_l{self.tokenizer.model_max_length}"
f"_s{self.short_seq_prob}"
f"_p{self.max_predictions_per_seq}"
f".arrow")
mapped_dataset = dataset.map(
self.examples_from_documents, batched=True,
remove_columns=dataset.column_names,
cache_file_name=cache_file_name,
num_proc=32,
features=datasets.Features(
attention_mask=datasets.features.Sequence(
datasets.features.Value(dtype='int8'), length=-1),
input_ids=datasets.features.Sequence(
datasets.features.Value(dtype='int16'), length=-1),
token_type_ids=datasets.features.Sequence(
datasets.features.Value(dtype='int8'), length=-1),
next_sentence_label=datasets.features.Value(dtype='int8'),
),
fn_kwargs=dict(
rng=random.Random(0),
))
mapped_dataset.set_format('numpy')
super().__init__(mapped_dataset)
def collate(self, examples):
examples = self.tokenizer.pad(
examples,
padding='max_length',
max_length=self.tokenizer.model_max_length,
return_tensors='np',
)
ignore_ids = np.array([
self.tokenizer.cls_token_id,
self.tokenizer.sep_token_id,
self.tokenizer.pad_token_id
], dtype=np.int64)[:, None]
batch_size = examples['input_ids'].shape[0]
examples['input_ids'] = examples['input_ids'].copy()
examples['masked_lm_positions'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.int64)
examples['masked_lm_ids'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.int64)
examples['masked_lm_weights'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.float32)
for i in range(batch_size):
prediction_mask = np.all(examples['input_ids'][i] != ignore_ids, axis=0)
num_tokens = np.sum(examples['attention_mask'][i]).item()
cand_indexes = np.arange(
prediction_mask.shape[0], dtype=np.int32)[prediction_mask]
num_to_predict = min(
self.max_predictions_per_seq, max(1, int(num_tokens * 0.15)))
masked_lm_positions = np.random.choice(
cand_indexes, num_to_predict, replace=False)
masked_lm_positions = np.sort(masked_lm_positions)
input_ids = examples['input_ids'][i].copy()
masked_lm_ids = input_ids[masked_lm_positions]
input_ids[masked_lm_positions] = np.where(
np.random.random(len(masked_lm_ids)) < 0.8,
# 80% of the time, replace with [MASK]
self.tokenizer.mask_token_id,
np.where(np.random.random(len(masked_lm_ids)) < 0.5,
# 10% of the time, keep original
masked_lm_ids,
# 10% of the time, replace with random word
np.random.randint(0, self.tokenizer.vocab_size, masked_lm_ids.shape)))
examples['input_ids'][i, :] = input_ids
examples['masked_lm_positions'][i, :num_to_predict] = masked_lm_positions
examples['masked_lm_ids'][i, :num_to_predict] = masked_lm_ids
examples['masked_lm_weights'][i, :num_to_predict] = 1.0
return examples
def examples_from_documents(self, documents, rng):
max_seq_length = self.tokenizer.model_max_length
# Account for [CLS], [SEP], [SEP]
max_num_tokens = max_seq_length - 3
instances = []
for text in documents['document']:
document = [
self.tokenizer.encode(line, add_special_tokens=False)
for line in text
]
# We *usually* want to fill up the entire sequence since we are padding
# to `max_seq_length` anyways, so short sequences are generally wasted
# computation. However, we *sometimes*
# (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter
# sequences to minimize the mismatch between pre-training and fine-tuning.
# The `target_seq_length` is just a rough target however, whereas
# `max_seq_length` is a hard limit.
target_seq_length = max_num_tokens
if rng.random() < self.short_seq_prob:
target_seq_length = rng.randint(2, max_num_tokens)
# We DON'T just concatenate all of the tokens from a document into a long
# sequence and choose an arbitrary split point because this would make the
# next sentence prediction task too easy. Instead, we split the input into
# segments "A" and "B" based on the actual "sentences" provided by the user
# input.
current_chunk = []
current_length = 0
i = 0
while i < len(document):
segment = document[i]
current_chunk.append(segment)
current_length += len(segment)
if i == len(document) - 1 or current_length >= target_seq_length:
if current_chunk:
# `a_end` is how many segments from `current_chunk` go into the `A`
# (first) sentence.
a_end = 1
if len(current_chunk) >= 2:
a_end = rng.randint(1, len(current_chunk) - 1)
tokens_a = []
for j in range(a_end):
tokens_a.extend(current_chunk[j])
tokens_b = []
# Random next
is_random_next = False
if len(current_chunk) == 1:
continue # XXX
elif rng.random() < 0.5:
is_random_next = True
for j in range(a_end, len(current_chunk)):
tokens_b.extend(current_chunk[j])
# Note(mingdachen): in this case, we just swap tokens_a and tokens_b
tokens_a, tokens_b = tokens_b, tokens_a
# Actual next
else:
is_random_next = False
for j in range(a_end, len(current_chunk)):
tokens_b.extend(current_chunk[j])
tokens_a, tokens_b, _ = self.tokenizer.truncate_sequences(
tokens_a, tokens_b, max(0, len(tokens_a) + len(tokens_b) - max_num_tokens))
instance = self.tokenizer.prepare_for_model(tokens_a, tokens_b)
if any(token not in self.tokenizer.all_special_ids for token in instance['input_ids']):
# Don't add instances that consist entirely of UNK tokens
instances.append(instance)
instance['next_sentence_label'] = int(is_random_next)
current_chunk = []
current_length = 0
i += 1
return {k: [instance[k] for instance in instances] for k in self.FEATURE_NAMES}
class PretrainingDataPipelineV1(DataPipeline):
FEATURE_NAMES = [
'attention_mask',
'input_ids',
'token_type_ids',
'next_sentence_label'
]
def __init__(self, dataset, tokenizer,
short_seq_prob=0.1,
max_predictions_per_seq=80):
self.tokenizer = tokenizer
self.short_seq_prob = short_seq_prob
self.max_predictions_per_seq = max_predictions_per_seq
cache_file_name=(
f"cache/"
f"pretrain_l{self.tokenizer.model_max_length}"
f"_s{self.short_seq_prob}"
f"_p{self.max_predictions_per_seq}"
f".arrow")
mapped_dataset = dataset.map(
self.examples_from_documents, batched=True,
remove_columns=dataset.column_names,
cache_file_name=cache_file_name,
num_proc=32,
features=datasets.Features(
attention_mask=datasets.features.Sequence(
datasets.features.Value(dtype='int8'), length=-1),
input_ids=datasets.features.Sequence(
datasets.features.Value(dtype='int16'), length=-1),
token_type_ids=datasets.features.Sequence(
datasets.features.Value(dtype='int8'), length=-1),
next_sentence_label=datasets.features.Value(dtype='int8'),
),
fn_kwargs=dict(
rng=random.Random(0),
))
mapped_dataset.set_format('numpy')
super().__init__(mapped_dataset)
def collate(self, examples):
examples = self.tokenizer.pad(
examples,
padding='max_length',
max_length=self.tokenizer.model_max_length,
return_tensors='np',
)
ignore_ids = np.array([
self.tokenizer.cls_token_id,
self.tokenizer.sep_token_id,
self.tokenizer.pad_token_id
], dtype=np.int64)[:, None]
batch_size = examples['input_ids'].shape[0]
examples['input_ids'] = examples['input_ids'].copy()
examples['masked_lm_positions'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.int64)
examples['masked_lm_ids'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.int64)
examples['masked_lm_weights'] = np.zeros((batch_size, self.max_predictions_per_seq), dtype=np.float32)
for i in range(batch_size):
prediction_mask = np.all(examples['input_ids'][i] != ignore_ids, axis=0)
num_tokens = np.sum(examples['attention_mask'][i]).item()
cand_indexes = np.arange(
prediction_mask.shape[0], dtype=np.int32)[prediction_mask]
num_to_predict = min(
self.max_predictions_per_seq, max(1, int(num_tokens * 0.15)))
masked_lm_positions = np.random.choice(
cand_indexes, num_to_predict, replace=False)
masked_lm_positions = np.sort(masked_lm_positions)
input_ids = examples['input_ids'][i].copy()
masked_lm_ids = input_ids[masked_lm_positions]
input_ids[masked_lm_positions] = np.where(
np.random.random(len(masked_lm_ids)) < 0.8,
# 80% of the time, replace with [MASK]
self.tokenizer.mask_token_id,
np.where(np.random.random(len(masked_lm_ids)) < 0.5,
# 10% of the time, keep original
masked_lm_ids,
# 10% of the time, replace with random word
np.random.randint(0, self.tokenizer.vocab_size, masked_lm_ids.shape)))
examples['input_ids'][i, :] = input_ids
examples['masked_lm_positions'][i, :num_to_predict] = masked_lm_positions
examples['masked_lm_ids'][i, :num_to_predict] = masked_lm_ids
examples['masked_lm_weights'][i, :num_to_predict] = 1.0
return examples
def examples_from_documents(self, documents, rng):
max_seq_length = self.tokenizer.model_max_length
# Account for [CLS], [SEP], [SEP]
max_num_tokens = max_seq_length - 3
instances = []
for text in documents['text']:
document = [
self.tokenizer.encode(
line.strip().replace("\n", " ").replace("()",""),
add_special_tokens=False)
for line in text.splitlines()
if line.strip() and len(line) >= 80
]
# We *usually* want to fill up the entire sequence since we are padding
# to `max_seq_length` anyways, so short sequences are generally wasted
# computation. However, we *sometimes*
# (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter
# sequences to minimize the mismatch between pre-training and fine-tuning.
# The `target_seq_length` is just a rough target however, whereas
# `max_seq_length` is a hard limit.
target_seq_length = max_num_tokens
if rng.random() < self.short_seq_prob:
target_seq_length = rng.randint(2, max_num_tokens)
# We DON'T just concatenate all of the tokens from a document into a long
# sequence and choose an arbitrary split point because this would make the
# next sentence prediction task too easy. Instead, we split the input into
# segments "A" and "B" based on the actual "sentences" provided by the user
# input.
current_chunk = []
current_length = 0
i = 0
while i < len(document):
segment = document[i]
current_chunk.append(segment)
current_length += len(segment)
if i == len(document) - 1 or current_length >= target_seq_length:
if current_chunk:
# `a_end` is how many segments from `current_chunk` go into the `A`
# (first) sentence.
a_end = 1
if len(current_chunk) >= 2:
a_end = rng.randint(1, len(current_chunk) - 1)
tokens_a = []
for j in range(a_end):
tokens_a.extend(current_chunk[j])
tokens_b = []
# Random next
is_random_next = False
if len(current_chunk) == 1:
continue # XXX
elif rng.random() < 0.5:
is_random_next = True
for j in range(a_end, len(current_chunk)):
tokens_b.extend(current_chunk[j])
# Note(mingdachen): in this case, we just swap tokens_a and tokens_b
tokens_a, tokens_b = tokens_b, tokens_a
# Actual next
else:
is_random_next = False
for j in range(a_end, len(current_chunk)):
tokens_b.extend(current_chunk[j])
tokens_a, tokens_b, _ = self.tokenizer.truncate_sequences(
tokens_a, tokens_b, max(0, len(tokens_a) + len(tokens_b) - max_num_tokens))
instance = self.tokenizer.prepare_for_model(tokens_a, tokens_b)
if any(token not in self.tokenizer.all_special_ids for token in instance['input_ids']):
# Don't add instances that consist entirely of UNK tokens
instances.append(instance)
instance['next_sentence_label'] = int(is_random_next)
current_chunk = []
current_length = 0
i += 1
return {k: [instance[k] for instance in instances] for k in self.FEATURE_NAMES}