create_finetuning_data.py 16.3 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Frederick Liu's avatar
Frederick Liu committed
14

15
16
"""BERT finetuning task dataset generator."""

17
import functools
18
import json
19
import os
20

Hongkun Yu's avatar
Hongkun Yu committed
21
# Import libraries
22
23
24
from absl import app
from absl import flags
import tensorflow as tf
25
from official.nlp.data import classifier_data_lib
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
26
from official.nlp.data import sentence_retrieval_lib
27
# word-piece tokenizer based squad_lib
28
from official.nlp.data import squad_lib as squad_lib_wp
29
# sentence-piece tokenizer based squad_lib
30
from official.nlp.data import squad_lib_sp
31
from official.nlp.data import tagging_data_lib
Le Hou's avatar
Le Hou committed
32
from official.nlp.tools import tokenization
33
34
35
36

FLAGS = flags.FLAGS

flags.DEFINE_enum(
Maxim Neumann's avatar
Maxim Neumann committed
37
    "fine_tuning_task_type", "classification",
38
    ["classification", "regression", "squad", "retrieval", "tagging"],
39
    "The name of the BERT fine tuning task for which data "
40
    "will be generated.")
41

42
# BERT classification specific flags.
43
44
45
46
47
flags.DEFINE_string(
    "input_data_dir", None,
    "The input data dir. Should contain the .tsv files (or other data files) "
    "for the task.")

48
49
50
flags.DEFINE_enum(
    "classification_task_name", "MNLI", [
        "AX", "COLA", "IMDB", "MNLI", "MRPC", "PAWS-X", "QNLI", "QQP", "RTE",
51
        "SST-2", "STS-B", "WNLI", "XNLI", "XTREME-XNLI", "XTREME-PAWS-X",
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
52
        "AX-g", "SUPERGLUE-RTE", "CB", "BoolQ", "WIC"
53
54
55
56
57
    ], "The name of the task to train BERT classifier. The "
    "difference between XTREME-XNLI and XNLI is: 1. the format "
    "of input tsv files; 2. the dev set for XTREME is english "
    "only and for XNLI is all languages combined. Same for "
    "PAWS-X.")
58

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
59
# MNLI task-specific flag.
60
61
flags.DEFINE_enum("mnli_type", "matched", ["matched", "mismatched"],
                  "The type of MNLI dataset.")
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
62
63

# XNLI task-specific flag.
Tianqi Liu's avatar
Tianqi Liu committed
64
65
flags.DEFINE_string(
    "xnli_language", "en",
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
66
    "Language of training data for XNLI task. If the value is 'all', the data "
Tianqi Liu's avatar
Tianqi Liu committed
67
68
    "of all languages will be used for training.")

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
69
# PAWS-X task-specific flag.
Tianqi Liu's avatar
Tianqi Liu committed
70
71
flags.DEFINE_string(
    "pawsx_language", "en",
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
72
    "Language of training data for PAWS-X task. If the value is 'all', the data "
Tianqi Liu's avatar
Tianqi Liu committed
73
    "of all languages will be used for training.")
Tianqi Liu's avatar
Tianqi Liu committed
74

75
76
77
78
79
80
# XTREME classification specific flags. Only used in XtremePawsx and XtremeXnli.
flags.DEFINE_string(
    "translated_input_data_dir", None,
    "The translated input data dir. Should contain the .tsv files (or other "
    "data files) for the task.")

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
81
# Retrieval task-specific flags.
82
83
84
flags.DEFINE_enum("retrieval_task_name", "bucc", ["bucc", "tatoeba"],
                  "The name of sentence retrieval task for scoring")

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
85
# Tagging task-specific flags.
86
87
88
flags.DEFINE_enum("tagging_task_name", "panx", ["panx", "udpos"],
                  "The name of BERT tagging (token classification) task.")

89
90
91
flags.DEFINE_bool("tagging_only_use_en_train", True,
                  "Whether only use english training data in tagging.")

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
92
# BERT Squad task-specific flags.
93
94
95
96
flags.DEFINE_string(
    "squad_data_file", None,
    "The input data file in for generating training data for BERT squad task.")

97
98
99
100
101
flags.DEFINE_string(
    "translated_squad_data_folder", None,
    "The translated data folder for generating training data for BERT squad "
    "task.")

102
103
104
105
106
107
108
109
110
111
flags.DEFINE_integer(
    "doc_stride", 128,
    "When splitting up a long document into chunks, how much stride to "
    "take between chunks.")

flags.DEFINE_integer(
    "max_query_length", 64,
    "The maximum number of tokens for the question. Questions longer than "
    "this will be truncated to this length.")

112
113
114
115
flags.DEFINE_bool(
    "version_2_with_negative", False,
    "If true, the SQuAD examples contain some that do not have an answer.")

116
117
118
119
120
flags.DEFINE_bool(
    "xlnet_format", False,
    "If true, then data will be preprocessed in a paragraph, query, class order"
    " instead of the BERT-style class, paragraph, query order.")

121
122
123
# XTREME specific flags.
flags.DEFINE_bool("only_use_en_dev", True, "Whether only use english dev data.")

124
125
126
127
128
129
# Shared flags across BERT fine-tuning tasks.
flags.DEFINE_string("vocab_file", None,
                    "The vocabulary file that the BERT model was trained on.")

flags.DEFINE_string(
    "train_data_output_path", None,
130
    "The path in which generated training input data will be written as tf"
131
    " records.")
132
133
134

flags.DEFINE_string(
    "eval_data_output_path", None,
Tianqi Liu's avatar
Tianqi Liu committed
135
    "The path in which generated evaluation input data will be written as tf"
136
    " records.")
137

Tianqi Liu's avatar
Tianqi Liu committed
138
139
140
flags.DEFINE_string(
    "test_data_output_path", None,
    "The path in which generated test input data will be written as tf"
Tianqi Liu's avatar
Tianqi Liu committed
141
142
    " records. If None, do not generate test data. Must be a pattern template"
    " as test_{}.tfrecords if processor has language specific test data.")
Tianqi Liu's avatar
Tianqi Liu committed
143

144
145
146
147
148
149
150
151
152
153
154
155
156
157
flags.DEFINE_string("meta_data_file_path", None,
                    "The path in which input meta data will be written.")

flags.DEFINE_bool(
    "do_lower_case", True,
    "Whether to lower case the input text. Should be True for uncased "
    "models and False for cased models.")

flags.DEFINE_integer(
    "max_seq_length", 128,
    "The maximum total input sequence length after WordPiece tokenization. "
    "Sequences longer than this will be truncated, and sequences shorter "
    "than this will be padded.")

158
159
160
161
flags.DEFINE_string("sp_model_file", "",
                    "The path to the model used by sentence piece tokenizer.")

flags.DEFINE_enum(
Chen Chen's avatar
Chen Chen committed
162
163
164
165
    "tokenization", "WordPiece", ["WordPiece", "SentencePiece"],
    "Specifies the tokenizer implementation, i.e., whether to use WordPiece "
    "or SentencePiece tokenizer. Canonical BERT uses WordPiece tokenizer, "
    "while ALBERT uses SentencePiece tokenizer.")
166

167
168
169
170
flags.DEFINE_string(
    "tfds_params", "", "Comma-separated list of TFDS parameter assigments for "
    "generic classfication data import (for more details "
    "see the TfdsProcessor class documentation).")
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
171

172
173
174

def generate_classifier_dataset():
  """Generates classifier dataset and returns input meta data."""
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
175
  if FLAGS.classification_task_name in [
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      "COLA",
      "WNLI",
      "SST-2",
      "MRPC",
      "QQP",
      "STS-B",
      "MNLI",
      "QNLI",
      "RTE",
      "AX",
      "SUPERGLUE-RTE",
      "CB",
      "BoolQ",
      "WIC",
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
190
191
192
193
194
  ]:
    assert not FLAGS.input_data_dir or FLAGS.tfds_params
  else:
    assert (FLAGS.input_data_dir and FLAGS.classification_task_name or
            FLAGS.tfds_params)
195

Chen Chen's avatar
Chen Chen committed
196
  if FLAGS.tokenization == "WordPiece":
197
198
199
200
    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
    processor_text_fn = tokenization.convert_to_unicode
  else:
Chen Chen's avatar
Chen Chen committed
201
    assert FLAGS.tokenization == "SentencePiece"
202
203
204
205
    tokenizer = tokenization.FullSentencePieceTokenizer(FLAGS.sp_model_file)
    processor_text_fn = functools.partial(
        tokenization.preprocess_text, lower=FLAGS.do_lower_case)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
206
207
  if FLAGS.tfds_params:
    processor = classifier_data_lib.TfdsProcessor(
208
        tfds_params=FLAGS.tfds_params, process_text_fn=processor_text_fn)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
209
210
211
212
213
214
    return classifier_data_lib.generate_tf_record_from_data_file(
        processor,
        None,
        tokenizer,
        train_data_output_path=FLAGS.train_data_output_path,
        eval_data_output_path=FLAGS.eval_data_output_path,
Tianqi Liu's avatar
Tianqi Liu committed
215
        test_data_output_path=FLAGS.test_data_output_path,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
216
217
218
        max_seq_length=FLAGS.max_seq_length)
  else:
    processors = {
Vincent Etter's avatar
Vincent Etter committed
219
220
        "ax":
            classifier_data_lib.AxProcessor,
Tianqi Liu's avatar
Tianqi Liu committed
221
222
        "cola":
            classifier_data_lib.ColaProcessor,
223
224
        "imdb":
            classifier_data_lib.ImdbProcessor,
Tianqi Liu's avatar
Tianqi Liu committed
225
        "mnli":
226
227
            functools.partial(
                classifier_data_lib.MnliProcessor, mnli_type=FLAGS.mnli_type),
Tianqi Liu's avatar
Tianqi Liu committed
228
229
230
231
        "mrpc":
            classifier_data_lib.MrpcProcessor,
        "qnli":
            classifier_data_lib.QnliProcessor,
232
233
234
235
        "qqp":
            classifier_data_lib.QqpProcessor,
        "rte":
            classifier_data_lib.RteProcessor,
Tianqi Liu's avatar
Tianqi Liu committed
236
237
        "sst-2":
            classifier_data_lib.SstProcessor,
238
239
        "sts-b":
            classifier_data_lib.StsBProcessor,
Tianqi Liu's avatar
Tianqi Liu committed
240
        "xnli":
241
242
243
            functools.partial(
                classifier_data_lib.XnliProcessor,
                language=FLAGS.xnli_language),
Tianqi Liu's avatar
Tianqi Liu committed
244
        "paws-x":
245
246
247
248
249
            functools.partial(
                classifier_data_lib.PawsxProcessor,
                language=FLAGS.pawsx_language),
        "wnli":
            classifier_data_lib.WnliProcessor,
Tianqi Liu's avatar
Tianqi Liu committed
250
        "xtreme-xnli":
251
252
253
254
            functools.partial(
                classifier_data_lib.XtremeXnliProcessor,
                translated_data_dir=FLAGS.translated_input_data_dir,
                only_use_en_dev=FLAGS.only_use_en_dev),
Tianqi Liu's avatar
Tianqi Liu committed
255
        "xtreme-paws-x":
256
257
258
            functools.partial(
                classifier_data_lib.XtremePawsxProcessor,
                translated_data_dir=FLAGS.translated_input_data_dir,
stephenwu's avatar
stephenwu committed
259
260
                only_use_en_dev=FLAGS.only_use_en_dev),
        "ax-g":
stephenwu's avatar
stephenwu committed
261
            classifier_data_lib.AXgProcessor,
262
        "superglue-rte":
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
263
264
265
266
267
            classifier_data_lib.SuperGLUERTEProcessor,
        "cb":
            classifier_data_lib.CBProcessor,
        "boolq":
            classifier_data_lib.BoolQProcessor,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
268
269
        "wic":
            classifier_data_lib.WnliProcessor,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
270
271
272
273
274
    }
    task_name = FLAGS.classification_task_name.lower()
    if task_name not in processors:
      raise ValueError("Task not found: %s" % (task_name))

Tianqi Liu's avatar
Tianqi Liu committed
275
    processor = processors[task_name](process_text_fn=processor_text_fn)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
276
277
278
279
280
281
    return classifier_data_lib.generate_tf_record_from_data_file(
        processor,
        FLAGS.input_data_dir,
        tokenizer,
        train_data_output_path=FLAGS.train_data_output_path,
        eval_data_output_path=FLAGS.eval_data_output_path,
Tianqi Liu's avatar
Tianqi Liu committed
282
        test_data_output_path=FLAGS.test_data_output_path,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
283
        max_seq_length=FLAGS.max_seq_length)
284
285


Maxim Neumann's avatar
Maxim Neumann committed
286
287
def generate_regression_dataset():
  """Generates regression dataset and returns input meta data."""
Chen Chen's avatar
Chen Chen committed
288
  if FLAGS.tokenization == "WordPiece":
Maxim Neumann's avatar
Maxim Neumann committed
289
290
291
292
    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
    processor_text_fn = tokenization.convert_to_unicode
  else:
Chen Chen's avatar
Chen Chen committed
293
    assert FLAGS.tokenization == "SentencePiece"
Maxim Neumann's avatar
Maxim Neumann committed
294
295
296
297
298
299
    tokenizer = tokenization.FullSentencePieceTokenizer(FLAGS.sp_model_file)
    processor_text_fn = functools.partial(
        tokenization.preprocess_text, lower=FLAGS.do_lower_case)

  if FLAGS.tfds_params:
    processor = classifier_data_lib.TfdsProcessor(
300
        tfds_params=FLAGS.tfds_params, process_text_fn=processor_text_fn)
Maxim Neumann's avatar
Maxim Neumann committed
301
302
303
304
305
306
307
308
309
310
311
312
    return classifier_data_lib.generate_tf_record_from_data_file(
        processor,
        None,
        tokenizer,
        train_data_output_path=FLAGS.train_data_output_path,
        eval_data_output_path=FLAGS.eval_data_output_path,
        test_data_output_path=FLAGS.test_data_output_path,
        max_seq_length=FLAGS.max_seq_length)
  else:
    raise ValueError("No data processor found for the given regression task.")


313
314
315
def generate_squad_dataset():
  """Generates squad training dataset and returns input meta data."""
  assert FLAGS.squad_data_file
Chen Chen's avatar
Chen Chen committed
316
  if FLAGS.tokenization == "WordPiece":
317
    return squad_lib_wp.generate_tf_record_from_json_file(
Allen Wang's avatar
Allen Wang committed
318
319
320
        input_file_path=FLAGS.squad_data_file,
        vocab_file_path=FLAGS.vocab_file,
        output_path=FLAGS.train_data_output_path,
321
        translated_input_folder=FLAGS.translated_squad_data_folder,
Allen Wang's avatar
Allen Wang committed
322
323
324
325
326
327
        max_seq_length=FLAGS.max_seq_length,
        do_lower_case=FLAGS.do_lower_case,
        max_query_length=FLAGS.max_query_length,
        doc_stride=FLAGS.doc_stride,
        version_2_with_negative=FLAGS.version_2_with_negative,
        xlnet_format=FLAGS.xlnet_format)
328
  else:
Chen Chen's avatar
Chen Chen committed
329
    assert FLAGS.tokenization == "SentencePiece"
330
    return squad_lib_sp.generate_tf_record_from_json_file(
331
332
333
        input_file_path=FLAGS.squad_data_file,
        sp_model_file=FLAGS.sp_model_file,
        output_path=FLAGS.train_data_output_path,
334
        translated_input_folder=FLAGS.translated_squad_data_folder,
335
336
337
338
339
340
        max_seq_length=FLAGS.max_seq_length,
        do_lower_case=FLAGS.do_lower_case,
        max_query_length=FLAGS.max_query_length,
        doc_stride=FLAGS.doc_stride,
        xlnet_format=FLAGS.xlnet_format,
        version_2_with_negative=FLAGS.version_2_with_negative)
341
342


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
343
344
345
def generate_retrieval_dataset():
  """Generate retrieval test and dev dataset and returns input meta data."""
  assert (FLAGS.input_data_dir and FLAGS.retrieval_task_name)
Chen Chen's avatar
Chen Chen committed
346
  if FLAGS.tokenization == "WordPiece":
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
347
348
349
350
    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
    processor_text_fn = tokenization.convert_to_unicode
  else:
Chen Chen's avatar
Chen Chen committed
351
    assert FLAGS.tokenization == "SentencePiece"
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    tokenizer = tokenization.FullSentencePieceTokenizer(FLAGS.sp_model_file)
    processor_text_fn = functools.partial(
        tokenization.preprocess_text, lower=FLAGS.do_lower_case)

  processors = {
      "bucc": sentence_retrieval_lib.BuccProcessor,
      "tatoeba": sentence_retrieval_lib.TatoebaProcessor,
  }

  task_name = FLAGS.retrieval_task_name.lower()
  if task_name not in processors:
    raise ValueError("Task not found: %s" % task_name)

  processor = processors[task_name](process_text_fn=processor_text_fn)

  return sentence_retrieval_lib.generate_sentence_retrevial_tf_record(
368
369
      processor, FLAGS.input_data_dir, tokenizer, FLAGS.eval_data_output_path,
      FLAGS.test_data_output_path, FLAGS.max_seq_length)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
370
371


372
373
374
def generate_tagging_dataset():
  """Generates tagging dataset."""
  processors = {
375
376
377
378
379
380
381
382
383
384
      "panx":
          functools.partial(
              tagging_data_lib.PanxProcessor,
              only_use_en_train=FLAGS.tagging_only_use_en_train,
              only_use_en_dev=FLAGS.only_use_en_dev),
      "udpos":
          functools.partial(
              tagging_data_lib.UdposProcessor,
              only_use_en_train=FLAGS.tagging_only_use_en_train,
              only_use_en_dev=FLAGS.only_use_en_dev),
385
386
387
388
389
  }
  task_name = FLAGS.tagging_task_name.lower()
  if task_name not in processors:
    raise ValueError("Task not found: %s" % task_name)

Chen Chen's avatar
Chen Chen committed
390
  if FLAGS.tokenization == "WordPiece":
391
392
393
    tokenizer = tokenization.FullTokenizer(
        vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
    processor_text_fn = tokenization.convert_to_unicode
Chen Chen's avatar
Chen Chen committed
394
  elif FLAGS.tokenization == "SentencePiece":
395
396
397
398
    tokenizer = tokenization.FullSentencePieceTokenizer(FLAGS.sp_model_file)
    processor_text_fn = functools.partial(
        tokenization.preprocess_text, lower=FLAGS.do_lower_case)
  else:
Chen Chen's avatar
Chen Chen committed
399
    raise ValueError("Unsupported tokenization: %s" % FLAGS.tokenization)
400
401
402
403
404
405
406
407

  processor = processors[task_name]()
  return tagging_data_lib.generate_tf_record_from_data_file(
      processor, FLAGS.input_data_dir, tokenizer, FLAGS.max_seq_length,
      FLAGS.train_data_output_path, FLAGS.eval_data_output_path,
      FLAGS.test_data_output_path, processor_text_fn)


408
def main(_):
Chen Chen's avatar
Chen Chen committed
409
  if FLAGS.tokenization == "WordPiece":
410
411
412
413
    if not FLAGS.vocab_file:
      raise ValueError(
          "FLAG vocab_file for word-piece tokenizer is not specified.")
  else:
Chen Chen's avatar
Chen Chen committed
414
    assert FLAGS.tokenization == "SentencePiece"
415
416
417
418
    if not FLAGS.sp_model_file:
      raise ValueError(
          "FLAG sp_model_file for sentence-piece tokenizer is not specified.")

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
419
420
421
  if FLAGS.fine_tuning_task_type != "retrieval":
    flags.mark_flag_as_required("train_data_output_path")

422
423
  if FLAGS.fine_tuning_task_type == "classification":
    input_meta_data = generate_classifier_dataset()
Maxim Neumann's avatar
Maxim Neumann committed
424
425
  elif FLAGS.fine_tuning_task_type == "regression":
    input_meta_data = generate_regression_dataset()
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
426
427
  elif FLAGS.fine_tuning_task_type == "retrieval":
    input_meta_data = generate_retrieval_dataset()
428
  elif FLAGS.fine_tuning_task_type == "squad":
429
    input_meta_data = generate_squad_dataset()
430
431
432
  else:
    assert FLAGS.fine_tuning_task_type == "tagging"
    input_meta_data = generate_tagging_dataset()
433

434
  tf.io.gfile.makedirs(os.path.dirname(FLAGS.meta_data_file_path))
435
436
437
438
439
440
441
  with tf.io.gfile.GFile(FLAGS.meta_data_file_path, "w") as writer:
    writer.write(json.dumps(input_meta_data, indent=4) + "\n")


if __name__ == "__main__":
  flags.mark_flag_as_required("meta_data_file_path")
  app.run(main)