forked from kingoflolz/mesh-transformer-jax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_finetune_tfrecords.py
290 lines (220 loc) · 10.7 KB
/
create_finetune_tfrecords.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 argparse
import os
import re
import random
from pathlib import Path
import ftfy
import tensorflow as tf
from lm_dataformat import Reader
from transformers import GPT2TokenizerFast
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser(description="""
Converts a text dataset into the training data format expected by the model.
Adapted from the script create_tfrecords.py in the gpt-neo repo.
- Your text dataset:
- can be provided as .txt files, or as an archive (.tar.gz, .xz, jsonl.zst).
- can be one file or multiple
- using a single large file may use too much memory and crash - if this occurs, split the file up into a few files
- the model's end-of-text separator is added between the contents of each file
- if the string '<|endoftext|>' appears inside a file, it is treated as the model's end-of-text separator (not the actual string '<|endoftext|>')
- this behavior can be disabled with --treat-eot-as-text
This script creates a single .tfrecords file as output
- Why: the model's data loader ignores "trailing" data (< 1 batch) at the end of a .tfrecords file
- this causes data loss if you have many .tfrecords files
- This is probably not appropriate for very large datasets
""", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("input_dir", type=str, help="Path to where your files are located.")
parser.add_argument("name", type=str,
help="Name of output file will be {name}_{seqnum}.tfrecords, where seqnum is total sequence count")
parser.add_argument("--output-dir", type=str, default="", help="Output directory (default: current directory)")
cleaning_args = parser.add_argument_group('data cleaning arguments')
cleaning_args.add_argument("--normalize-with-ftfy", action="store_true", help="Normalize text with ftfy")
cleaning_args.add_argument("--normalize-with-wikitext-detokenize",
action="store_true", help="Use wikitext detokenizer")
minu_help = "Exclude repetitive documents made up of < MIN_UNIQUE_TOKENS unique tokens. These can produce large gradients."
minu_help += " Set <= 0 to disable. If enabled, 200 is a good default value. (Default: 0)"
cleaning_args.add_argument("--min-unique-tokens", type=int, default=0,
help=minu_help)
shuffle_pack_args = parser.add_argument_group('data shuffling/packing arguments')
repack_ep_help = "Repeat the data N_REPACK_EPOCHS times, shuffled differently in each repetition. Recommended for multi-epoch training (set this to your intended number of epochs)."
shuffle_pack_args.add_argument("--n-repack-epochs",
type=int, default=1,
help=repack_ep_help
)
shuffle_pack_args.add_argument("--seed", type=int, default=10,
help="random seed for shuffling data (default: 10)")
shuffle_pack_args.add_argument("--preserve-data-order",
default=False, action="store_true",
help="Disables shuffling, so the input and output data have the same order.")
misc_args = parser.add_argument_group('miscellaneous arguments')
misc_args.add_argument("--verbose",
default=False, action="store_true",
help="Prints extra information, such as the text removed by --min-unique-tokens")
args = parser.parse_args()
if not args.input_dir.endswith("/"):
args.input_dir = args.input_dir + "/"
return args
def get_files(input_dir):
filetypes = ["jsonl.zst", ".txt", ".xz", ".tar.gz"]
files = [list(Path(input_dir).glob(f"*{ft}")) for ft in filetypes]
# flatten list of list -> list and stringify Paths
return [str(item) for sublist in files for item in sublist]
def wikitext_detokenizer(string):
# contractions
string = string.replace("s '", "s'")
string = re.sub(r"/' [0-9]/", r"/'[0-9]/", string)
# number separators
string = string.replace(" @-@ ", "-")
string = string.replace(" @,@ ", ",")
string = string.replace(" @.@ ", ".")
# punctuation
string = string.replace(" : ", ": ")
string = string.replace(" ; ", "; ")
string = string.replace(" . ", ". ")
string = string.replace(" ! ", "! ")
string = string.replace(" ? ", "? ")
string = string.replace(" , ", ", ")
# double brackets
string = re.sub(r"\(\s*([^\)]*?)\s*\)", r"(\1)", string)
string = re.sub(r"\[\s*([^\]]*?)\s*\]", r"[\1]", string)
string = re.sub(r"{\s*([^}]*?)\s*}", r"{\1}", string)
string = re.sub(r"\"\s*([^\"]*?)\s*\"", r'"\1"', string)
string = re.sub(r"'\s*([^']*?)\s*'", r"'\1'", string)
# miscellaneous
string = string.replace("= = = =", "====")
string = string.replace("= = =", "===")
string = string.replace("= =", "==")
string = string.replace(" " + chr(176) + " ", chr(176))
string = string.replace(" \n", "\n")
string = string.replace("\n ", "\n")
string = string.replace(" N ", " 1 ")
string = string.replace(" 's", "'s")
return string
def _int64_feature(value):
"""
Returns an int64_list from a bool / enum / int / uint.
"""
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def write_to_file(writer, data):
"""
writes data to tfrecord file
"""
feature = {
"text": _int64_feature(data)
}
tf_example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(tf_example.SerializeToString())
def write_tfrecord(sequences, fp):
with tf.io.TFRecordWriter(fp) as writer:
for seq in sequences:
write_to_file(writer, seq)
def split_list(l, n):
# splits list/string into n size chunks
return [l[i:i + n] for i in range(0, len(l), n)]
def enforce_min_unique(seqs, min_unique_tokens, enc, verbose=False):
for seq in tqdm(seqs, mininterval=1, smoothing=0):
if len(set(seq)) >= min_unique_tokens:
yield seq
elif verbose:
text = enc.decode(seq)
print(f"excluding with {len(set(seq))} unique tokens:\n\n{repr(text)}\n\n")
def eot_splitting_generator(string_iterable, encoder):
"""
Given strings, splits them internally on <|endoftext|> and yields (generally more) strings
"""
for doc in string_iterable:
for d in doc.split(encoder.eos_token):
if len(d) > 0:
yield d
def prep_and_tokenize_generator(string_iterable, encoder, normalize_with_ftfy, normalize_with_wikitext_detokenize):
"""
Given strings, does data cleaning / tokenization and yields arrays of tokens
"""
for doc in string_iterable:
if normalize_with_ftfy: # fix text with ftfy if specified
doc = ftfy.fix_text(doc, normalization='NFKC')
if normalize_with_wikitext_detokenize:
doc = wikitext_detokenizer(doc)
tokens = encoder.encode(doc) + [encoder.eos_token_id]
yield tokens
def file_to_tokenized_docs_generator(file_path, encoder, args):
"""
Given a file path, reads the file and tokenizes the contents
Yields token arrays of arbitrary, unequal length
"""
reader = Reader(file_path)
string_iterable = reader.stream_data(threaded=False)
string_iterable = eot_splitting_generator(string_iterable, encoder)
token_list_gen = prep_and_tokenize_generator(string_iterable,
encoder,
normalize_with_ftfy=args.normalize_with_ftfy,
normalize_with_wikitext_detokenize=args.normalize_with_wikitext_detokenize
)
return token_list_gen
def read_files_to_tokenized_docs(files, args, encoder):
docs = []
if args.preserve_data_order:
files = sorted(files)
else:
random.shuffle(files)
for f in tqdm(files, mininterval=10, smoothing=0):
docs.extend(file_to_tokenized_docs_generator(f, encoder, args))
if not args.preserve_data_order:
# shuffle at individual document level
random.shuffle(docs)
return docs
def arrays_to_sequences(token_list_iterable, sequence_length=2049):
"""
Given token arrays of arbitrary lengths, concats/splits them into arrays of equal length
Returns equal-length token arrays, followed by a a final array of trailing tokens (which may be shorter)
"""
accum = []
for l in token_list_iterable:
accum.extend(l)
if len(accum) > sequence_length:
chunks = split_list(accum, sequence_length)
for chunk in chunks[:-1]:
yield chunk
accum = chunks[-1]
if len(accum) > 0:
yield accum
def chunk_and_finalize(arrays, args, encoder):
sequences = list(arrays_to_sequences(arrays))
full_seqs, trailing_data = sequences[:-1], sequences[-1]
if args.min_unique_tokens > 0:
full_seqs = list(enforce_min_unique(full_seqs, args.min_unique_tokens, encoder, args.verbose))
if not args.preserve_data_order:
random.shuffle(full_seqs)
return full_seqs, trailing_data
def create_tfrecords(files, args):
GPT2TokenizerFast.max_model_input_sizes['gpt2'] = 1e20 # disables a misleading warning
encoder = GPT2TokenizerFast.from_pretrained('gpt2')
random.seed(args.seed)
all_sequences_across_epochs = []
docs = read_files_to_tokenized_docs(files, args, encoder)
full_seqs, trailing_data = chunk_and_finalize(docs, args, encoder)
all_sequences_across_epochs.extend(full_seqs)
# ep 2+
for ep_ix in range(1, args.n_repack_epochs):
# re-shuffle
if not args.preserve_data_order:
random.shuffle(docs)
full_seqs, trailing_data = chunk_and_finalize(docs, args, encoder)
else:
# if we're preserving data order, we can still "repack" by shifting everything
# with the trailing data of the last epoch at the beginning
seqs_with_prefix = [trailing_data] + full_seqs
full_seqs, trailing_data = chunk_and_finalize(seqs_with_prefix, args, encoder)
all_sequences_across_epochs.extend(full_seqs)
# final
print(f"dropped {len(trailing_data)} tokens of trailing data")
total_sequence_len = len(all_sequences_across_epochs)
fp = os.path.join(args.output_dir, f"{args.name}_{total_sequence_len}.tfrecords")
write_tfrecord(all_sequences_across_epochs, fp)
if __name__ == "__main__":
args = parse_args()
if args.output_dir:
os.makedirs(args.output_dir, exist_ok=True)
files = get_files(args.input_dir)
results = create_tfrecords(files, args)