示例#1
0
 def _get_squad_model():
     """Get Squad model and optimizer."""
     squad_model, core_model = bert_models.squad_model(
         bert_config,
         max_seq_length,
         float_type=tf.float16 if use_float16 else tf.float32)
     squad_model.optimizer = optimization.create_optimizer(
         FLAGS.learning_rate, steps_per_epoch * epochs, warmup_steps)
     if use_float16:
         # Wraps optimizer with a LossScaleOptimizer. This is done automatically
         # in compile() with the "mixed_float16" policy, but since we do not call
         # compile(), we must wrap the optimizer manually.
         squad_model.optimizer = (
             tf.keras.mixed_precision.experimental.LossScaleOptimizer(
                 squad_model.optimizer,
                 loss_scale=common_flags.get_loss_scale()))
     if FLAGS.fp16_implementation == 'graph_rewrite':
         # Note: when flags_obj.fp16_implementation == "graph_rewrite", dtype as
         # determined by flags_core.get_tf_dtype(flags_obj) would be 'float32'
         # which will ensure tf.compat.v2.keras.mixed_precision and
         # tf.train.experimental.enable_mixed_precision_graph_rewrite do not double
         # up.
         squad_model.optimizer = tf.train.experimental.enable_mixed_precision_graph_rewrite(
             squad_model.optimizer)
     return squad_model, core_model
示例#2
0
def predict_squad_customized(strategy, input_meta_data, bert_config,
                             predict_tfrecord_path, num_steps):
    """Make predictions using a Bert-based squad model."""
    primary_cpu_task = '/job:worker' if FLAGS.tpu else ''

    with tf.device(primary_cpu_task):
        predict_dataset = input_pipeline.create_squad_dataset(
            predict_tfrecord_path,
            input_meta_data['max_seq_length'],
            FLAGS.predict_batch_size,
            is_training=False)
        predict_iterator = iter(
            strategy.experimental_distribute_dataset(predict_dataset))

        with strategy.scope():
            # Prediction always uses float32, even if training uses mixed precision.
            tf.keras.mixed_precision.experimental.set_policy('float32')
            squad_model, _ = bert_models.squad_model(
                bert_config,
                input_meta_data['max_seq_length'],
                float_type=tf.float32)

        checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)
        logging.info('Restoring checkpoints from %s', checkpoint_path)
        checkpoint = tf.train.Checkpoint(model=squad_model)
        checkpoint.restore(checkpoint_path).expect_partial()

        @tf.function
        def predict_step(iterator):
            """Predicts on distributed devices."""
            def _replicated_step(inputs):
                """Replicated prediction calculation."""
                x, _ = inputs
                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)

            outputs = strategy.experimental_run_v2(_replicated_step,
                                                   args=(next(iterator), ))
            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
示例#3
0
 def _get_squad_model():
     """Get Squad model and optimizer."""
     squad_model, core_model = bert_models.squad_model(
         bert_config,
         max_seq_length,
         float_type=tf.float16 if use_float16 else tf.float32)
     squad_model.optimizer = optimization.create_optimizer(
         FLAGS.learning_rate, steps_per_epoch * epochs, warmup_steps)
     if use_float16:
         squad_model.optimizer = (
             tf.keras.mixed_precision.experimental.LossScaleOptimizer(
                 squad_model.optimizer,
                 loss_scale=common_flags.get_loss_scale()))
     return squad_model, core_model
示例#4
0
 def _get_squad_model():
   """Get Squad model and optimizer."""
   squad_model, core_model = bert_models.squad_model(
       bert_config,
       max_seq_length,
       float_type=tf.float16 if use_float16 else tf.float32)
   squad_model.optimizer = optimization.create_optimizer(
       FLAGS.learning_rate, steps_per_epoch * epochs, warmup_steps)
   if use_float16:
     # Wraps optimizer with a LossScaleOptimizer. This is done automatically
     # in compile() with the "mixed_float16" policy, but since we do not call
     # compile(), we must wrap the optimizer manually.
     squad_model.optimizer = (
         tf.keras.mixed_precision.experimental.LossScaleOptimizer(
             squad_model.optimizer, loss_scale=common_flags.get_loss_scale()))
   return squad_model, core_model
示例#5
0
def export_squad(model_export_path, input_meta_data):
  """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.

  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)
  bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)

  squad_model, _ = bert_models.squad_model(
      bert_config, input_meta_data['max_seq_length'], float_type=tf.float32)
  model_saving_utils.export_bert_model(
      model_export_path, model=squad_model, checkpoint_dir=FLAGS.model_dir)
示例#6
0
 def _get_squad_model():
     squad_model, core_model = bert_models.squad_model(
         bert_config, max_seq_length, float_type=tf.float32)
     squad_model.optimizer = optimization.create_optimizer(
         FLAGS.learning_rate, steps_per_epoch * epochs, warmup_steps)
     return squad_model, core_model