-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbertpre.py
296 lines (233 loc) · 11.3 KB
/
bertpre.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
#import BookscorpusTextFormatting
import WikiTextFormat
import BooksTextFormat
import TextSharding
import argparse
from GooglePretrainedWeightDownloader import GooglePretrainedWeightDownloader
import itertools
import multiprocessing
import os
import pprint
import subprocess
os.environ['BERT_PREP_WORKING_DIR']='/media/cjh/My Passport/Study/Nus_Project'
def main(args):
working_dir= os.environ['BERT_PREP_WORKING_DIR']
print("Bert Preparation")
print('Working Directory:', working_dir)
hdf5_tfrecord_folder_prefix = "/lower_case_" + str(args.do_lower_case) + "_seq_len_" + str(args.max_seq_length) \
+ "_max_pred_" + str(args.max_predictions_per_seq) + "_masked_lm_prob_" + str(
args.masked_lm_prob) \
+ "_random_seed_" + str(args.random_seed) + "_dupe_factor_" + str(args.dupe_factor) \
+ "_shard_" + str(args.n_training_shards) + "_test_split_" + str(
int(args.fraction_test_set * 100))
directory_structure = {
'download': working_dir + '/download', # Downloaded and decompressed
'extracted': working_dir + '/extracted', # Extracted from whatever the initial format is (e.g., wikiextractor)
'formatted': working_dir + '/formatted_one_article_per_line',
# This is the level where all sources should look the same
'sharded': working_dir + '/sharded',
'tfrecord': working_dir + '/tfrecord' + hdf5_tfrecord_folder_prefix,
'hdf5': working_dir + '/hdf5' + hdf5_tfrecord_folder_prefix,
}
if args.action == 'download':
if not os.path.exists(directory_structure['download']):
os.makedirs(directory_structure['download'])
downloader = GooglePretrainedWeightDownloader(directory_structure['download'])
downloader.download()
elif args.action == 'text_formatting':
if not os.path.exists(directory_structure['formatted']):
os.makedirs(directory_structure['formatted'])
if args.dataset == 'wikicorpus_en':
wiki_path = directory_structure['extracted'] + '/wikicorpus_en'
output_filename = directory_structure['formatted'] + '/wikicorpus_en_one_article_per_line.txt'
wiki_formatter = WikiTextFormat.WikicorpusTextFormatting(wiki_path, output_filename, recursive=True)
wiki_formatter.merge()
if args.dataset =='bookscorpus':
books_path = directory_structure['extracted'] + '/bookscorpus/epubtxt'
output_filename = directory_structure['formatted'] + '/bookscorpus_one_book_per_line.txt'
books_formatter = BooksTextFormat.BookscorpusTextFormatting(books_path, output_filename,
recursive=True)
books_formatter.merge()
elif args.action == 'sharding':
args.input_files = [directory_structure['formatted'] + '/bookscorpus_one_book_per_line.txt', directory_structure['formatted'] + '/wikicorpus_en_one_article_per_line.txt']
if not os.path.exists(directory_structure['sharded']):
os.makedirs(directory_structure['sharded'])
if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset):
os.makedirs(directory_structure['sharded'] + '/' + args.dataset)
if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset + '/training'):
os.makedirs(directory_structure['sharded'] + '/' + args.dataset + '/training')
if not os.path.exists(directory_structure['sharded'] + '/' + args.dataset + '/test'):
os.makedirs(directory_structure['sharded'] + '/' + args.dataset + '/test')
output_file_prefix = directory_structure['sharded'] + '/' + args.dataset + '/' + args.dataset
segmenter = TextSharding.NLTKSegmenter()
sharding = TextSharding.Sharding(args.input_files, output_file_prefix, args.n_training_shards,
args.n_test_shards, args.fraction_test_set)
sharding.load_articles()
sharding.segment_articles_into_sentences(segmenter)
sharding.distribute_articles_over_shards()
sharding.write_shards_to_disk()
elif args.action == 'create_tfrecord_files':
if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset):
os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset)
if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset + '/training'):
os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset + '/training')
if not os.path.exists(directory_structure['tfrecord'] + "/" + args.dataset + '/test'):
os.makedirs(directory_structure['tfrecord'] + "/" + args.dataset + '/test')
last_process = None
def create_record_worker(filename_prefix, shard_id, output_format='tfrecord', split='training'):
bert_preprocessing_command = 'python create_pretraining_data.py'
bert_preprocessing_command += ' --input_file=' +'\''+ directory_structure[
'sharded'] +'\'' +'/' + args.dataset + '/' + split + '/' + filename_prefix + '_' + str(shard_id) + '.txt'
bert_preprocessing_command += ' --output_file=' + '\''+directory_structure[
'tfrecord']+'\'' + '/' + args.dataset + '/' + split + '/' + filename_prefix + '_' + str(
shard_id) + '.' + output_format
bert_preprocessing_command += ' --vocab_file=' + str(args.vocab_file)
bert_preprocessing_command += ' --do_lower_case' if args.do_lower_case else ''
bert_preprocessing_command += ' --max_seq_length=' + str(args.max_seq_length)
bert_preprocessing_command += ' --max_predictions_per_seq=' + str(args.max_predictions_per_seq)
bert_preprocessing_command += ' --masked_lm_prob=' + str(args.masked_lm_prob)
bert_preprocessing_command += ' --random_seed=' + str(args.random_seed)
bert_preprocessing_command += ' --dupe_factor=' + str(args.dupe_factor)
bert_preprocessing_process = subprocess.Popen(bert_preprocessing_command, shell=True)
last_process = bert_preprocessing_process
# This could be better optimized (fine if all take equal time)
if shard_id % args.n_processes == 0 and shard_id > 0:
bert_preprocessing_process.wait()
return last_process
output_file_prefix = args.dataset
for i in range(args.n_training_shards):
last_process = create_record_worker(output_file_prefix + '_training', i, 'tfrecord', 'training')
last_process.wait()
for i in range(args.n_test_shards):
last_process = create_record_worker(output_file_prefix + '_test', i, 'tfrecord', 'test')
last_process.wait()
if __name__=='__main__':
parser = argparse.ArgumentParser(
description='Preprocessing Application for Everything BERT-related'
)
parser.add_argument(
'--action',
type=str,
help='Specify the action you want the app to take. e.g., generate vocab, segment, create tfrecords',
choices={
'download', # Download and verify mdf5/sha sums
'text_formatting', # Convert into a file that contains one article/book per line
'sharding', # Convert previous formatted text into shards containing one sentence per line
'create_tfrecord_files', # Turn each shard into a TFrecord with masking and next sentence prediction info
'create_hdf5_files' # Turn each shard into a HDF5 file with masking and next sentence prediction info
}
)
parser.add_argument(
'--dataset',
type=str,
help='Specify the dataset to perform --action on',
choices={
'bookscorpus',
'wikicorpus_en',
'wikicorpus_zh', 'books_wiki_en_corpus',
'pubmed_baseline',
'pubmed_daily_update',
'pubmed_fulltext',
'pubmed_open_access',
'google_pretrained_weights',
'nvidia_pretrained_weights',
'squad',
'mrpc',
'sst-2',
'mnli',
'cola',
'all'
}
)
parser.add_argument(
'--input_files',
type=str,
help='Specify the input files in a comma-separated list (no spaces)'
)
parser.add_argument(
'--n_training_shards',
type=int,
help='Specify the number of training shards to generate',
default=1472
)
parser.add_argument(
'--n_test_shards',
type=int,
help='Specify the number of test shards to generate',
default=1472
)
parser.add_argument(
'--fraction_test_set',
type=float,
help='Specify the fraction (0..1) of the data to withhold for the test data split (based on number of sequences)',
default=0.1
)
parser.add_argument(
'--segmentation_method',
type=str,
help='Specify your choice of sentence segmentation',
choices={
'nltk'
},
default='nltk'
)
parser.add_argument(
'--n_processes',
type=int,
help='Specify the max number of processes to allow at one time',
default=6
)
parser.add_argument(
'--random_seed',
type=int,
help='Specify the base seed to use for any random number generation',
default=12345
)
parser.add_argument(
'--dupe_factor',
type=int,
help='Specify the duplication factor',
default=5
)
parser.add_argument(
'--masked_lm_prob',
type=float,
help='Specify the probability for masked lm',
default=0.15
)
parser.add_argument(
'--max_seq_length',
type=int,
help='Specify the maximum sequence length',
default=512
)
parser.add_argument(
'--max_predictions_per_seq',
type=int,
help='Specify the maximum number of masked words per sequence',
default=20
)
parser.add_argument(
'--do_lower_case',
type=int,
help='Specify whether it is cased (0) or uncased (1) (any number greater than 0 will be treated as uncased)',
default=1
)
parser.add_argument(
'--vocab_file',
type=str,
help='Specify absolute path to vocab file to use)'
)
parser.add_argument(
'--skip_wikiextractor',
type=int,
help='Specify whether to skip wikiextractor step 0=False, 1=True',
default=0
)
parser.add_argument(
'--interactive_json_config_generator',
type=str,
help='Specify the action you want the app to take. e.g., generate vocab, segment, create tfrecords'
)
args=parser.parse_args()
main(args)