-
Notifications
You must be signed in to change notification settings - Fork 10
/
preprocess_data.py
334 lines (301 loc) · 11.5 KB
/
preprocess_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
from __future__ import annotations
import argparse
import os
from argparse import Namespace
from copy import deepcopy
from typing import Any
import datasets
from datasets import DatasetDict
from transformers import AutoTokenizer, PreTrainedTokenizer
from vectorlm.utils.data_utils import Config
def parse_args() -> Namespace:
"""Parse the command-line arguments.
Returns
-------
The parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--config_path", default="configs/config.yaml")
return parser.parse_args()
def validate_config(config: Config) -> None:
"""Validate config arguments.
Args:
----
config: The main config.
"""
preprocess_args = config.dataset_preprocess
if preprocess_args.get("from_disk"):
if "load_path" not in preprocess_args:
msg = "`from_disk` set but `load_path` missing."
raise KeyError(msg)
path_exists = os.path.exists(preprocess_args.get("from_disk"))
if not path_exists:
msg = "`load_path` does not exist."
raise Exception(msg)
if preprocess_args.get("truncate") and preprocess_args.get("packing_type"):
msg = "`truncate` and `packing_type` cannot both be set."
raise ValueError(msg)
if (
not preprocess_args.get("truncate")
) and (
not preprocess_args.get("packing_type")
):
print(
"Warning: neither `truncate` nor `packing_type` are set. This ",
"can cause issues during the forward pass of the model if ",
"tokenized example lengths exceed max sequence lengths.",
)
if not preprocess_args.get("save_path"):
msg = "`save_path` missing from config."
raise KeyError(msg)
def tokenize_dataset(
examples: dict[str, Any],
tokenizer: PreTrainedTokenizer,
data_field: str,
pre_pend: str | None = None,
truncate: bool = False,
separator: str | None = None,
add_bos_eos: bool = True,
) -> dict[str, list[int]]:
"""Tokenize the text dataset.
Args:
----
examples: The dictionary containing raw examples.
tokenizer: The tokenizer.
data_field: The dictionary key containing the examples to be tokenized.
pre_pend: An optional prompt you can prepend.
truncate: Whether to truncate examples that exceed context length.
separator: A string separator s.t. everything before the separator
is not going to be backpropped, and everything after will. Similar
to supervised finetuning.
add_bos_eos: Whether to add the BOS and EOS tokens to the tokenized
sequence.
Returns:
-------
A dictionary containing the input ids, labels, and attention masks.
"""
all_input_ids = []
all_attention_mask = []
all_labels = []
# Adding bos/eos
if add_bos_eos:
bos, eos = tokenizer.bos_token, tokenizer.eos_token
else:
bos, eos = "", ""
for example in examples[data_field]:
# If we want to include a prepended prompt to each datapoint
if pre_pend:
prompt = f"{bos}{pre_pend}{example}{eos}"
else:
prompt = f"{bos}{example}{eos}"
# If we've specified a separator present in each sequence
if not separator:
tokenized = tokenizer.encode(prompt, add_special_tokens=False)
if truncate and len(tokenized) > tokenizer.model_max_length:
tokenized = tokenized[:tokenizer.model_max_length - 1]
tokenized.append(tokenizer.eos_token_id)
all_labels.append(deepcopy(tokenized))
else:
if separator not in prompt:
continue
# Perform tokenization separately to allow for conditional prompting
separation_idx = prompt.find(separator) + len(separator)
prefix, postfix = prompt[:separation_idx], prompt[separation_idx:]
tokenized_prefix = tokenizer.encode(
prefix, add_special_tokens=False,
)
tokenized_postfix = tokenizer.encode(
postfix, add_special_tokens=False,
)
tokenized = tokenized_prefix + tokenized_postfix
if truncate and len(tokenized) > tokenizer.model_max_length:
tokenized = tokenized[:tokenizer.model_max_length - 1]
tokenized.append(tokenizer.eos_token_id)
# We need to address this separately, because labels need to
# backprop on bos/eos tokens
if add_bos_eos:
label = (
[tokenizer.bos_token_id]
+ ([-100] * (len(tokenized_prefix) - 1))
+ deepcopy(tokenized_postfix)
)
else:
label = (
[-100] * len(tokenized_prefix)
+ deepcopy(tokenized_postfix)
)
# If truncated, labels should be the same.
if truncate and len(label) > tokenizer.model_max_length:
label = label[:tokenizer.model_max_length - 1]
label.append(tokenizer.eos_token_id)
all_labels.append(label)
all_input_ids.append(tokenized)
all_attention_mask.append([1] * len(tokenized))
return {
"input_ids": all_input_ids,
"attention_mask": all_attention_mask,
"labels": all_labels,
}
def pack_examples(
examples: dict[str, list[int]],
tokenizer: PreTrainedTokenizer,
overlap: int = 0,
packing_type: str = "full",
add_bos_eos: bool = True,
) -> dict[str, list[int]]:
"""Pack the tokenized dataset.
Args:
----
examples: The dictionary containing tokenized results.
tokenizer: The tokenizer.
overlap: The amount of overlap between two examples while packing.
packing_type: In `partial` packing, original individual training
examples are chunked with the option of `overlap`. In `full`
packing, the entire dataset is fully packed meaning that no
empty space is left in sequences.
add_bos_eos: Whether to add the BOS and EOS tokens to the tokenized
and packed sequence.
Returns:
-------
Same type of input dictionary, but now with examples packed.
"""
chunk_size = tokenizer.model_max_length
if add_bos_eos:
# For BOS and EOS tokens.
chunk_size -= 2
bos, eos = [tokenizer.bos_token_id], [tokenizer.eos_token_id]
else:
bos, eos = [], []
stride = chunk_size - overlap
all_keys = list(examples.keys())
if packing_type == "full":
joined_examples = {k: sum(examples[k], []) for k in all_keys}
total_length = len(joined_examples["input_ids"])
result = {}
for k, v in joined_examples.items():
value_chunked_lst = []
for i in range(0, total_length, stride):
if k != "attention_mask":
value_chunked_lst.append(bos + v[i:i + chunk_size] + eos)
else:
if add_bos_eos:
# Need to do this explicitly because attention mask
# is just 1s or 0s.
value_chunked_lst.append(
[1] + v[i:i + chunk_size] + [1]
)
else:
value_chunked_lst.append(v[i:i + chunk_size])
elif packing_type == "partial":
result = {k:[] for k in examples}
_key = all_keys[0]
for idx in range(len(examples[_key])):
total_length = len(examples[_key][idx])
for key in all_keys:
for i in range(0, total_length, stride):
if key != "attention_mask":
sliced_example = [
bos + examples[key][idx][i:i + chunk_size] + eos
]
else:
if add_bos_eos:
sliced_example = [
[1] + examples[key][idx][i:i + chunk_size] + [1]
]
else:
sliced_example = [
examples[key][idx][i:i + chunk_size]
]
result[key].extend(sliced_example)
else:
msg = "`packing_type` needs to either be `full` or `partial`."
raise ValueError(msg)
return result
def add_indices(
dataset: datasets.Dataset,
base_idx: int = 0,
) -> datasets.Dataset:
"""Add the `id` column to examples. This is used for dataset checkpointing.
Args:
----
dataset: The dataset we are adding the column to.
base_idx: An optional base index to start the ids from. Default is 0.
Returns:
-------
The dataset with the new `id` column.
"""
indices = [i + base_idx for i in range(len(dataset))]
return dataset.add_column("id", indices)
def main(config: Config) -> None:
"""Definition of main function.
Args:
----
config: The main config.
"""
preprocess_args = config.dataset_preprocess
tokenizer = AutoTokenizer.from_pretrained(config.model)
tokenizer.model_max_length = config.train_parameters.max_seq_len
if preprocess_args.from_disk:
ds = datasets.load_from_disk(preprocess_args.load_path)
if isinstance(ds, DatasetDict):
if not preprocess_args.get("split"):
msg = "Loaded dataset is a dictionary. Please specify `split`."
raise KeyError(msg)
ds = ds[preprocess_args.get("split")]
else:
ds = datasets.load_dataset(
preprocess_args.load_path,
split=preprocess_args.get("split"),
)
will_pack = False
if preprocess_args.get("packing_type"):
will_pack = True
if preprocess_args.get("add_bos_eos_tokens", True):
special_tokens_created = isinstance(
tokenizer.bos_token_id, int,
) and isinstance(
tokenizer.eos_token_id, int,
)
if not special_tokens_created:
msg = (
"BOS and EOS tokens are not set in the tokenizer.",
"Cannot add these tokens during tokenization.",
)
raise TypeError(msg)
ds = ds.map(
lambda examples: tokenize_dataset(
examples,
tokenizer,
preprocess_args.data_field,
preprocess_args.get("pre_pend"),
preprocess_args.get("truncate", False),
preprocess_args.get("seperator"),
# Note that if packing, we add the special tokens after packing.
not will_pack and preprocess_args.get("add_bos_eos_tokens", True),
),
batched=True,
batch_size=250,
remove_columns=ds.column_names,
num_proc=32,
)
if preprocess_args.get("packing_type"):
ds = ds.map(
lambda examples: pack_examples(
examples,
tokenizer,
preprocess_args.get("overlap", 0),
preprocess_args.packing_type,
will_pack and preprocess_args.get("add_bos_eos_tokens", True),
),
batched=True,
batch_size=2000,
remove_columns=ds.column_names,
num_proc=8,
)
ds = add_indices(ds)
ds.save_to_disk(preprocess_args.save_path, max_shard_size="1GB")
if __name__ == "__main__":
args = parse_args()
config = Config(args.config_path)
validate_config(config)
main(config)