run_squad_helper.py 18.5 KB
Newer Older
Frederick Liu's avatar
Frederick Liu committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Chen Chen's avatar
Chen Chen committed
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

Chen Chen's avatar
Chen Chen committed
15
16
17
"""Library for running BERT family models on SQuAD 1.1/2.0 in TF 2.x."""

import collections
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
18
import json
Chen Chen's avatar
Chen Chen committed
19
import os
Hongkun Yu's avatar
Hongkun Yu committed
20

Chen Chen's avatar
Chen Chen committed
21
22
23
from absl import flags
from absl import logging
import tensorflow as tf
24
from official.modeling import performance
Chen Chen's avatar
Chen Chen committed
25
26
27
28
29
from official.nlp import optimization
from official.nlp.bert import bert_models
from official.nlp.bert import common_flags
from official.nlp.bert import input_pipeline
from official.nlp.bert import model_saving_utils
30
from official.nlp.bert import model_training_utils
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
31
from official.nlp.bert import squad_evaluate_v1_1
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
32
from official.nlp.bert import squad_evaluate_v2_0
Chen Chen's avatar
Chen Chen committed
33
34
35
36
37
38
39
from official.nlp.data import squad_lib_sp
from official.utils.misc import keras_utils


def define_common_squad_flags():
  """Defines common flags used by SQuAD tasks."""
  flags.DEFINE_enum(
Hongkun Yu's avatar
Hongkun Yu committed
40
41
42
43
      'mode', 'train_and_eval', [
          'train_and_eval', 'train_and_predict', 'train', 'eval', 'predict',
          'export_only'
      ], 'One of {"train_and_eval", "train_and_predict", '
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
44
45
46
      '"train", "eval", "predict", "export_only"}. '
      '`train_and_eval`: train & predict to json files & compute eval metrics. '
      '`train_and_predict`: train & predict to json files. '
Chen Chen's avatar
Chen Chen committed
47
      '`train`: only trains the model. '
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
48
      '`eval`: predict answers from squad json file & compute eval metrics. '
Chen Chen's avatar
Chen Chen committed
49
50
51
52
53
54
55
56
57
58
59
60
      '`predict`: predict answers from the squad json file. '
      '`export_only`: will take the latest checkpoint inside '
      'model_dir and export a `SavedModel`.')
  flags.DEFINE_string('train_data_path', '',
                      'Training data path with train tfrecords.')
  flags.DEFINE_string(
      'input_meta_data_path', None,
      'Path to file that contains meta data about input '
      'to be used for training and evaluation.')
  # Model training specific flags.
  flags.DEFINE_integer('train_batch_size', 32, 'Total batch size for training.')
  # Predict processing related.
Hongkun Yu's avatar
Hongkun Yu committed
61
62
63
64
65
66
  flags.DEFINE_string(
      'predict_file', None, 'SQuAD prediction json file path. '
      '`predict` mode supports multiple files: one can use '
      'wildcard to specify multiple files and it can also be '
      'multiple file patterns separated by comma. Note that '
      '`eval` mode only supports a single predict file.')
Chen Chen's avatar
Chen Chen committed
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
  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_float(
      'null_score_diff_threshold', 0.0,
      'If null_score - best_non_null is greater than the threshold, '
      'predict null. This is only used for SQuAD v2.')
  flags.DEFINE_bool(
      'verbose_logging', False,
      'If true, all of the warnings related to data processing will be '
      'printed. A number of warnings are expected for a normal SQuAD '
      'evaluation.')
  flags.DEFINE_integer('predict_batch_size', 8,
                       'Total batch size for prediction.')
  flags.DEFINE_integer(
      'n_best_size', 20,
      'The total number of n-best predictions to generate in the '
      'nbest_predictions.json output file.')
  flags.DEFINE_integer(
      'max_answer_length', 30,
      'The maximum length of an answer that can be generated. This is needed '
      'because the start and end predictions are not conditioned on one '
      'another.')

  common_flags.define_common_bert_flags()


FLAGS = flags.FLAGS


Hongkun Yu's avatar
Hongkun Yu committed
98
def squad_loss_fn(start_positions, end_positions, start_logits, end_logits):
Chen Chen's avatar
Chen Chen committed
99
100
101
102
103
104
105
106
107
108
  """Returns sparse categorical crossentropy for start/end logits."""
  start_loss = tf.keras.losses.sparse_categorical_crossentropy(
      start_positions, start_logits, from_logits=True)
  end_loss = tf.keras.losses.sparse_categorical_crossentropy(
      end_positions, end_logits, from_logits=True)

  total_loss = (tf.reduce_mean(start_loss) + tf.reduce_mean(end_loss)) / 2
  return total_loss


109
def get_loss_fn():
Chen Chen's avatar
Chen Chen committed
110
111
112
113
114
115
  """Gets a loss function for squad task."""

  def _loss_fn(labels, model_outputs):
    start_positions = labels['start_positions']
    end_positions = labels['end_positions']
    start_logits, end_logits = model_outputs
Hongkun Yu's avatar
Hongkun Yu committed
116
117
    return squad_loss_fn(start_positions, end_positions, start_logits,
                         end_logits)
Chen Chen's avatar
Chen Chen committed
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

  return _loss_fn


RawResult = collections.namedtuple('RawResult',
                                   ['unique_id', 'start_logits', 'end_logits'])


def get_raw_results(predictions):
  """Converts multi-replica predictions to RawResult."""
  for unique_ids, start_logits, end_logits in zip(predictions['unique_ids'],
                                                  predictions['start_logits'],
                                                  predictions['end_logits']):
    for values in zip(unique_ids.numpy(), start_logits.numpy(),
                      end_logits.numpy()):
      yield RawResult(
          unique_id=values[0],
          start_logits=values[1].tolist(),
          end_logits=values[2].tolist())


def get_dataset_fn(input_file_pattern, max_seq_length, global_batch_size,
                   is_training):
  """Gets a closure to create a dataset.."""

  def _dataset_fn(ctx=None):
    """Returns tf.data.Dataset for distributed BERT pretraining."""
    batch_size = ctx.get_per_replica_batch_size(
        global_batch_size) if ctx else global_batch_size
    dataset = input_pipeline.create_squad_dataset(
        input_file_pattern,
        max_seq_length,
        batch_size,
        is_training=is_training,
        input_pipeline_context=ctx)
    return dataset

  return _dataset_fn


158
159
160
def get_squad_model_to_predict(strategy, bert_config, checkpoint_path,
                               input_meta_data):
  """Gets a squad model to make predictions."""
Chen Chen's avatar
Chen Chen committed
161
162
  with strategy.scope():
    # Prediction always uses float32, even if training uses mixed precision.
Reed Wanderman-Milne's avatar
Reed Wanderman-Milne committed
163
    tf.keras.mixed_precision.set_global_policy('float32')
Chen Chen's avatar
Chen Chen committed
164
165
166
167
168
    squad_model, _ = bert_models.squad_model(
        bert_config,
        input_meta_data['max_seq_length'],
        hub_module_url=FLAGS.hub_module_url)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
169
170
  if checkpoint_path is None:
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)
Chen Chen's avatar
Chen Chen committed
171
172
173
  logging.info('Restoring checkpoints from %s', checkpoint_path)
  checkpoint = tf.train.Checkpoint(model=squad_model)
  checkpoint.restore(checkpoint_path).expect_partial()
174
175
176
  return squad_model


Hongkun Yu's avatar
Hongkun Yu committed
177
178
def predict_squad_customized(strategy, input_meta_data, predict_tfrecord_path,
                             num_steps, squad_model):
179
180
181
182
183
184
185
  """Make predictions using a Bert-based squad model."""
  predict_dataset_fn = get_dataset_fn(
      predict_tfrecord_path,
      input_meta_data['max_seq_length'],
      FLAGS.predict_batch_size,
      is_training=False)
  predict_iterator = iter(
Chenkai Kuang's avatar
Chenkai Kuang committed
186
      strategy.distribute_datasets_from_function(predict_dataset_fn))
Chen Chen's avatar
Chen Chen committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

  @tf.function
  def predict_step(iterator):
    """Predicts on distributed devices."""

    def _replicated_step(inputs):
      """Replicated prediction calculation."""
      x, _ = inputs
      unique_ids = x.pop('unique_ids')
      start_logits, end_logits = squad_model(x, training=False)
      return dict(
          unique_ids=unique_ids,
          start_logits=start_logits,
          end_logits=end_logits)

202
    outputs = strategy.run(_replicated_step, args=(next(iterator),))
Chen Chen's avatar
Chen Chen committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    return tf.nest.map_structure(strategy.experimental_local_results, outputs)

  all_results = []
  for _ in range(num_steps):
    predictions = predict_step(predict_iterator)
    for result in get_raw_results(predictions):
      all_results.append(result)
    if len(all_results) % 100 == 0:
      logging.info('Made predictions for %d records.', len(all_results))
  return all_results


def train_squad(strategy,
                input_meta_data,
                bert_config,
                custom_callbacks=None,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
219
                run_eagerly=False,
220
221
                init_checkpoint=None,
                sub_model_export_name=None):
Chen Chen's avatar
Chen Chen committed
222
223
224
225
226
  """Run bert squad training."""
  if strategy:
    logging.info('Training using customized training loop with distribution'
                 ' strategy.')
  # Enables XLA in Session Config. Should not be set for TPU.
227
  keras_utils.set_session_config(FLAGS.enable_xla)
228
  performance.set_mixed_precision_policy(common_flags.dtype())
Chen Chen's avatar
Chen Chen committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

  epochs = FLAGS.num_train_epochs
  num_train_examples = input_meta_data['train_data_size']
  max_seq_length = input_meta_data['max_seq_length']
  steps_per_epoch = int(num_train_examples / FLAGS.train_batch_size)
  warmup_steps = int(epochs * num_train_examples * 0.1 / FLAGS.train_batch_size)
  train_input_fn = get_dataset_fn(
      FLAGS.train_data_path,
      max_seq_length,
      FLAGS.train_batch_size,
      is_training=True)

  def _get_squad_model():
    """Get Squad model and optimizer."""
    squad_model, core_model = bert_models.squad_model(
        bert_config,
        max_seq_length,
        hub_module_url=FLAGS.hub_module_url,
        hub_module_trainable=FLAGS.hub_module_trainable)
248
249
    optimizer = optimization.create_optimizer(FLAGS.learning_rate,
                                              steps_per_epoch * epochs,
Hongkun Yu's avatar
Hongkun Yu committed
250
                                              warmup_steps, FLAGS.end_lr,
251
                                              FLAGS.optimizer_type)
252
253
254

    squad_model.optimizer = performance.configure_optimizer(
        optimizer,
255
        use_float16=common_flags.use_float16())
Chen Chen's avatar
Chen Chen committed
256
257
    return squad_model, core_model

Zongwei Zhou's avatar
Zongwei Zhou committed
258
259
260
261
262
263
  # Only when explicit_allreduce = True, post_allreduce_callbacks and
  # allreduce_bytes_per_pack will take effect. optimizer.apply_gradients() no
  # longer implicitly allreduce gradients, users manually allreduce gradient and
  # pass the allreduced grads_and_vars to apply_gradients().
  # With explicit_allreduce = True, clip_by_global_norm is moved to after
  # allreduce.
Chen Chen's avatar
Chen Chen committed
264
265
266
  model_training_utils.run_customized_training_loop(
      strategy=strategy,
      model_fn=_get_squad_model,
267
      loss_fn=get_loss_fn(),
Chen Chen's avatar
Chen Chen committed
268
269
270
271
272
      model_dir=FLAGS.model_dir,
      steps_per_epoch=steps_per_epoch,
      steps_per_loop=FLAGS.steps_per_loop,
      epochs=epochs,
      train_input_fn=train_input_fn,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
273
      init_checkpoint=init_checkpoint or FLAGS.init_checkpoint,
274
      sub_model_export_name=sub_model_export_name,
Chen Chen's avatar
Chen Chen committed
275
      run_eagerly=run_eagerly,
Zongwei Zhou's avatar
Zongwei Zhou committed
276
      custom_callbacks=custom_callbacks,
Zongwei Zhou's avatar
Zongwei Zhou committed
277
      explicit_allreduce=FLAGS.explicit_allreduce,
278
279
      pre_allreduce_callbacks=[
          model_training_utils.clip_by_global_norm_callback
Zongwei Zhou's avatar
Zongwei Zhou committed
280
281
      ],
      allreduce_bytes_per_pack=FLAGS.allreduce_bytes_per_pack)
Chen Chen's avatar
Chen Chen committed
282
283


284
285
def prediction_output_squad(strategy, input_meta_data, tokenizer, squad_lib,
                            predict_file, squad_model):
Chen Chen's avatar
Chen Chen committed
286
287
288
289
290
291
292
  """Makes predictions for a squad dataset."""
  doc_stride = input_meta_data['doc_stride']
  max_query_length = input_meta_data['max_query_length']
  # Whether data should be in Ver 2.0 format.
  version_2_with_negative = input_meta_data.get('version_2_with_negative',
                                                False)
  eval_examples = squad_lib.read_squad_examples(
293
      input_file=predict_file,
Chen Chen's avatar
Chen Chen committed
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
      is_training=False,
      version_2_with_negative=version_2_with_negative)

  eval_writer = squad_lib.FeatureWriter(
      filename=os.path.join(FLAGS.model_dir, 'eval.tf_record'),
      is_training=False)
  eval_features = []

  def _append_feature(feature, is_padding):
    if not is_padding:
      eval_features.append(feature)
    eval_writer.process_feature(feature)

  # TPU requires a fixed batch size for all batches, therefore the number
  # of examples must be a multiple of the batch size, or else examples
  # will get dropped. So we pad with fake examples which are ignored
  # later on.
  kwargs = dict(
      examples=eval_examples,
      tokenizer=tokenizer,
      max_seq_length=input_meta_data['max_seq_length'],
      doc_stride=doc_stride,
      max_query_length=max_query_length,
      is_training=False,
      output_fn=_append_feature,
      batch_size=FLAGS.predict_batch_size)

  # squad_lib_sp requires one more argument 'do_lower_case'.
  if squad_lib == squad_lib_sp:
    kwargs['do_lower_case'] = FLAGS.do_lower_case
  dataset_size = squad_lib.convert_examples_to_features(**kwargs)
  eval_writer.close()

  logging.info('***** Running predictions *****')
  logging.info('  Num orig examples = %d', len(eval_examples))
  logging.info('  Num split examples = %d', len(eval_features))
  logging.info('  Batch size = %d', FLAGS.predict_batch_size)

  num_steps = int(dataset_size / FLAGS.predict_batch_size)
Hongkun Yu's avatar
Hongkun Yu committed
333
334
335
  all_results = predict_squad_customized(strategy, input_meta_data,
                                         eval_writer.filename, num_steps,
                                         squad_model)
Chen Chen's avatar
Chen Chen committed
336

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  all_predictions, all_nbest_json, scores_diff_json = (
      squad_lib.postprocess_output(
          eval_examples,
          eval_features,
          all_results,
          FLAGS.n_best_size,
          FLAGS.max_answer_length,
          FLAGS.do_lower_case,
          version_2_with_negative=version_2_with_negative,
          null_score_diff_threshold=FLAGS.null_score_diff_threshold,
          verbose=FLAGS.verbose_logging))

  return all_predictions, all_nbest_json, scores_diff_json


Hongkun Yu's avatar
Hongkun Yu committed
352
353
354
355
356
357
def dump_to_files(all_predictions,
                  all_nbest_json,
                  scores_diff_json,
                  squad_lib,
                  version_2_with_negative,
                  file_prefix=''):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
358
  """Save output to json files."""
359
360
361
362
363
364
  output_prediction_file = os.path.join(FLAGS.model_dir,
                                        '%spredictions.json' % file_prefix)
  output_nbest_file = os.path.join(FLAGS.model_dir,
                                   '%snbest_predictions.json' % file_prefix)
  output_null_log_odds_file = os.path.join(FLAGS.model_dir, file_prefix,
                                           '%snull_odds.json' % file_prefix)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
365
366
  logging.info('Writing predictions to: %s', (output_prediction_file))
  logging.info('Writing nbest to: %s', (output_nbest_file))
Chen Chen's avatar
Chen Chen committed
367

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
368
369
370
371
372
373
  squad_lib.write_to_json_files(all_predictions, output_prediction_file)
  squad_lib.write_to_json_files(all_nbest_json, output_nbest_file)
  if version_2_with_negative:
    squad_lib.write_to_json_files(scores_diff_json, output_null_log_odds_file)


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
def _get_matched_files(input_path):
  """Returns all files that matches the input_path."""
  input_patterns = input_path.strip().split(',')
  all_matched_files = []
  for input_pattern in input_patterns:
    input_pattern = input_pattern.strip()
    if not input_pattern:
      continue
    matched_files = tf.io.gfile.glob(input_pattern)
    if not matched_files:
      raise ValueError('%s does not match any files.' % input_pattern)
    else:
      all_matched_files.extend(matched_files)
  return sorted(all_matched_files)


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
390
391
392
393
394
395
def predict_squad(strategy,
                  input_meta_data,
                  tokenizer,
                  bert_config,
                  squad_lib,
                  init_checkpoint=None):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
396
  """Get prediction results and evaluate them to hard drive."""
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
397
398
  if init_checkpoint is None:
    init_checkpoint = tf.train.latest_checkpoint(FLAGS.model_dir)
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

  all_predict_files = _get_matched_files(FLAGS.predict_file)
  squad_model = get_squad_model_to_predict(strategy, bert_config,
                                           init_checkpoint, input_meta_data)
  for idx, predict_file in enumerate(all_predict_files):
    all_predictions, all_nbest_json, scores_diff_json = prediction_output_squad(
        strategy, input_meta_data, tokenizer, squad_lib, predict_file,
        squad_model)
    if len(all_predict_files) == 1:
      file_prefix = ''
    else:
      # if predict_file is /path/xquad.ar.json, the `file_prefix` may be
      # "xquad.ar-0-"
      file_prefix = '%s-' % os.path.splitext(
          os.path.basename(all_predict_files[idx]))[0]
    dump_to_files(all_predictions, all_nbest_json, scores_diff_json, squad_lib,
                  input_meta_data.get('version_2_with_negative', False),
                  file_prefix)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
417
418


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
419
420
421
422
423
424
def eval_squad(strategy,
               input_meta_data,
               tokenizer,
               bert_config,
               squad_lib,
               init_checkpoint=None):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
425
  """Get prediction results and evaluate them against ground truth."""
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
426
427
  if init_checkpoint is None:
    init_checkpoint = tf.train.latest_checkpoint(FLAGS.model_dir)
428
429
430
431
432
433
434
435

  all_predict_files = _get_matched_files(FLAGS.predict_file)
  if len(all_predict_files) != 1:
    raise ValueError('`eval_squad` only supports one predict file, '
                     'but got %s' % all_predict_files)

  squad_model = get_squad_model_to_predict(strategy, bert_config,
                                           init_checkpoint, input_meta_data)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
436
  all_predictions, all_nbest_json, scores_diff_json = prediction_output_squad(
437
438
      strategy, input_meta_data, tokenizer, squad_lib, all_predict_files[0],
      squad_model)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
439
440
441
  dump_to_files(all_predictions, all_nbest_json, scores_diff_json, squad_lib,
                input_meta_data.get('version_2_with_negative', False))

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
442
443
444
  with tf.io.gfile.GFile(FLAGS.predict_file, 'r') as reader:
    dataset_json = json.load(reader)
    pred_dataset = dataset_json['data']
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
445
  if input_meta_data.get('version_2_with_negative', False):
Hongkun Yu's avatar
Hongkun Yu committed
446
    eval_metrics = squad_evaluate_v2_0.evaluate(pred_dataset, all_predictions,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
447
                                                scores_diff_json)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
448
449
  else:
    eval_metrics = squad_evaluate_v1_1.evaluate(pred_dataset, all_predictions)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
450
  return eval_metrics
Chen Chen's avatar
Chen Chen committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466


def export_squad(model_export_path, input_meta_data, bert_config):
  """Exports a trained model as a `SavedModel` for inference.

  Args:
    model_export_path: a string specifying the path to the SavedModel directory.
    input_meta_data: dictionary containing meta data about input and model.
    bert_config: Bert configuration file to define core bert layers.

  Raises:
    Export path is not specified, got an empty string or None.
  """
  if not model_export_path:
    raise ValueError('Export path is not specified: %s' % model_export_path)
  # Export uses float32 for now, even if training uses mixed precision.
Reed Wanderman-Milne's avatar
Reed Wanderman-Milne committed
467
  tf.keras.mixed_precision.set_global_policy('float32')
Chen Chen's avatar
Chen Chen committed
468
469
470
471
  squad_model, _ = bert_models.squad_model(bert_config,
                                           input_meta_data['max_seq_length'])
  model_saving_utils.export_bert_model(
      model_export_path, model=squad_model, checkpoint_dir=FLAGS.model_dir)