run_pretraining.py 6.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
Hongkun Yu's avatar
Hongkun Yu committed
15
"""Run masked LM/next sentence pre-training for BERT in TF 2.x."""
16
17
18
19
20
21
22
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl import app
from absl import flags
from absl import logging
Hongkun Yu's avatar
Hongkun Yu committed
23
import gin
24
import tensorflow as tf
25
from official.modeling import performance
26
from official.nlp import optimization
27
from official.nlp.bert import bert_models
28
from official.nlp.bert import common_flags
29
from official.nlp.bert import configs
30
from official.nlp.bert import input_pipeline
31
from official.nlp.bert import model_training_utils
32
from official.utils.misc import distribution_utils
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

flags.DEFINE_string('input_files', None,
                    'File path to retrieve training data for pre-training.')
# Model training specific flags.
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.')
flags.DEFINE_integer('max_predictions_per_seq', 20,
                     'Maximum predictions per sequence_output.')
flags.DEFINE_integer('train_batch_size', 32, 'Total batch size for training.')
flags.DEFINE_integer('num_steps_per_epoch', 1000,
                     'Total number of training steps to run per epoch.')
flags.DEFINE_float('warmup_steps', 10000,
                   'Warmup steps for Adam weight decay optimizer.')
50
51
flags.DEFINE_bool('use_next_sentence_label', True,
                  'Whether to use next sentence label to compute final loss.')
52

53
54
common_flags.define_common_bert_flags()

55
56
57
FLAGS = flags.FLAGS


Hongkun Yu's avatar
Hongkun Yu committed
58
def get_pretrain_dataset_fn(input_file_pattern, seq_length,
59
60
                            max_predictions_per_seq, global_batch_size,
                            use_next_sentence_label=True):
61
  """Returns input dataset from input file string."""
62
  def _dataset_fn(ctx=None):
63
    """Returns tf.data.Dataset for distributed BERT pretraining."""
Hongkun Yu's avatar
Hongkun Yu committed
64
    input_patterns = input_file_pattern.split(',')
Hongkun Yu's avatar
Hongkun Yu committed
65
    batch_size = ctx.get_per_replica_batch_size(global_batch_size)
66
    train_dataset = input_pipeline.create_pretrain_dataset(
Hongkun Yu's avatar
Hongkun Yu committed
67
        input_patterns,
68
69
70
71
        seq_length,
        max_predictions_per_seq,
        batch_size,
        is_training=True,
72
73
        input_pipeline_context=ctx,
        use_next_sentence_label=use_next_sentence_label)
74
75
    return train_dataset

Hongkun Yu's avatar
Hongkun Yu committed
76
  return _dataset_fn
77
78


79
def get_loss_fn():
80
81
82
  """Returns loss function for BERT pretraining."""

  def _bert_pretrain_loss_fn(unused_labels, losses, **unused_args):
83
    return tf.reduce_mean(losses)
84
85
86
87
88
89

  return _bert_pretrain_loss_fn


def run_customized_training(strategy,
                            bert_config,
André Susano Pinto's avatar
André Susano Pinto committed
90
                            init_checkpoint,
91
92
93
94
                            max_seq_length,
                            max_predictions_per_seq,
                            model_dir,
                            steps_per_epoch,
95
                            steps_per_loop,
96
97
98
                            epochs,
                            initial_lr,
                            warmup_steps,
99
100
                            end_lr,
                            optimizer_type,
101
                            input_files,
102
                            train_batch_size,
Chen Chen's avatar
Chen Chen committed
103
104
                            use_next_sentence_label=True,
                            custom_callbacks=None):
105
106
  """Run BERT pretrain model training using low-level API."""

Hongkun Yu's avatar
Hongkun Yu committed
107
108
  train_input_fn = get_pretrain_dataset_fn(input_files, max_seq_length,
                                           max_predictions_per_seq,
109
110
                                           train_batch_size,
                                           use_next_sentence_label)
111
112

  def _get_pretrain_model():
113
    """Gets a pretraining model."""
114
    pretrain_model, core_model = bert_models.pretrain_model(
115
116
        bert_config, max_seq_length, max_predictions_per_seq,
        use_next_sentence_label=use_next_sentence_label)
117
    optimizer = optimization.create_optimizer(
118
        initial_lr, steps_per_epoch * epochs, warmup_steps,
119
        end_lr, optimizer_type)
120
121
122
123
    pretrain_model.optimizer = performance.configure_optimizer(
        optimizer,
        use_float16=common_flags.use_float16(),
        use_graph_rewrite=common_flags.use_graph_rewrite())
124
125
    return pretrain_model, core_model

126
  trained_model = model_training_utils.run_customized_training_loop(
127
128
      strategy=strategy,
      model_fn=_get_pretrain_model,
129
130
      loss_fn=get_loss_fn(),
      scale_loss=FLAGS.scale_loss,
131
      model_dir=model_dir,
André Susano Pinto's avatar
André Susano Pinto committed
132
      init_checkpoint=init_checkpoint,
133
134
      train_input_fn=train_input_fn,
      steps_per_epoch=steps_per_epoch,
135
      steps_per_loop=steps_per_loop,
Chen Chen's avatar
Chen Chen committed
136
      epochs=epochs,
Chen Chen's avatar
Chen Chen committed
137
138
      sub_model_export_name='pretrained/bert_model',
      custom_callbacks=custom_callbacks)
139

140
141
  return trained_model

142

Chen Chen's avatar
Chen Chen committed
143
def run_bert_pretrain(strategy, custom_callbacks=None):
144
145
  """Runs BERT pre-training."""

146
  bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file)
147
148
149
150
  if not strategy:
    raise ValueError('Distribution strategy is not specified.')

  # Runs customized training loop.
Chen Chen's avatar
Chen Chen committed
151
  logging.info('Training using customized training loop TF 2.0 with distributed'
152
153
               'strategy.')

154
155
  performance.set_mixed_precision_policy(common_flags.dtype())

156
157
158
  return run_customized_training(
      strategy,
      bert_config,
André Susano Pinto's avatar
André Susano Pinto committed
159
      FLAGS.init_checkpoint,  # Used to initialize only the BERT submodel.
160
161
162
163
      FLAGS.max_seq_length,
      FLAGS.max_predictions_per_seq,
      FLAGS.model_dir,
      FLAGS.num_steps_per_epoch,
164
      FLAGS.steps_per_loop,
165
166
167
      FLAGS.num_train_epochs,
      FLAGS.learning_rate,
      FLAGS.warmup_steps,
168
169
      FLAGS.end_lr,
      FLAGS.optimizer_type,
170
      FLAGS.input_files,
171
      FLAGS.train_batch_size,
Chen Chen's avatar
Chen Chen committed
172
173
      FLAGS.use_next_sentence_label,
      custom_callbacks=custom_callbacks)
174
175
176
177


def main(_):
  # Users should always run this script under TF 2.x
Hongkun Yu's avatar
Hongkun Yu committed
178
  gin.parse_config_files_and_bindings(FLAGS.gin_file, FLAGS.gin_param)
179
180
  if not FLAGS.model_dir:
    FLAGS.model_dir = '/tmp/bert20/'
181
182
183
184
  strategy = distribution_utils.get_distribution_strategy(
      distribution_strategy=FLAGS.distribution_strategy,
      num_gpus=FLAGS.num_gpus,
      tpu_address=FLAGS.tpu)
185
186
187
  if strategy:
    print('***** Number of cores used : ', strategy.num_replicas_in_sync)

188
  run_bert_pretrain(strategy)
189
190
191
192


if __name__ == '__main__':
  app.run(main)