Example #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,
       hub_module_url=FLAGS.hub_module_url)
   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
Example #2
0
def predict_squad_customized(strategy,
                             input_meta_data,
                             bert_config,
                             checkpoint_path,
                             predict_tfrecord_path,
                             num_steps):
  """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(
      strategy.experimental_distribute_datasets_from_function(
          predict_dataset_fn))

  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'],
        hub_module_url=FLAGS.hub_module_url)

  if checkpoint_path is None:
    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 = 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)

    outputs = strategy.run(_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
Example #3
0
  def test_squad_model(self):
    model, core_model = bert_models.squad_model(
        self._bert_test_config,
        max_seq_length=5,
        initializer=None,
        hub_module_url=None,
        hub_module_trainable=None)
    self.assertIsInstance(model, tf.keras.Model)
    self.assertIsInstance(core_model, tf.keras.Model)

    # Expect two output from model: start positions and end positions
    self.assertIsInstance(model.output, list)
    self.assertLen(model.output, 2)
Example #4
0
    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)
        optimizer = optimization.create_optimizer(FLAGS.learning_rate,
                                                  steps_per_epoch * epochs,
                                                  warmup_steps, FLAGS.end_lr,
                                                  FLAGS.optimizer_type)

        squad_model.optimizer = performance.configure_optimizer(
            optimizer, use_float16=common_flags.use_float16())
        return squad_model, core_model
def get_squad_model_to_predict(strategy, bert_config, checkpoint_path,
                               input_meta_data):
    """Gets a squad model to make predictions."""
    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'],
            hub_module_url=FLAGS.hub_module_url)

    if checkpoint_path is None:
        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()
    return squad_model
Example #6
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 = MODEL_CLASSES[FLAGS.model_type][0].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)
def export_bert_squad_tfhub(bert_config: configs.BertConfig,
                            model_checkpoint_path: Text,
                            hub_destination: Text,
                            vocab_file: Text,
                            do_lower_case: bool = None):
  """Restores a tf.keras.Model for BERT with SQuAD and saves for TF-Hub."""
  # If do_lower_case is not explicit, default to checking whether "uncased" is
  # in the vocab file name
  if do_lower_case is None:
    do_lower_case = "uncased" in vocab_file
    logging.info("Using do_lower_case=%s based on name of vocab_file=%s",
                 do_lower_case, vocab_file)
  span_labeling, _ = bert_models.squad_model(bert_config, max_seq_length=None)
  checkpoint = tf.train.Checkpoint(model=span_labeling)
  checkpoint.restore(model_checkpoint_path).assert_existing_objects_matched()
  span_labeling.vocab_file = tf.saved_model.Asset(vocab_file)
  span_labeling.do_lower_case = tf.Variable(do_lower_case, trainable=False)
  span_labeling.save(hub_destination, include_optimizer=False, save_format="tf")
Example #8
0
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.
  tf.keras.mixed_precision.experimental.set_policy('float32')
  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)
    def test_squad_model(self):
        model, core_model = bert_models.squad_model(self._bert_test_config,
                                                    max_seq_length=5,
                                                    initializer=None,
                                                    hub_module_url=None,
                                                    hub_module_trainable=None)
        self.assertIsInstance(model, tf.keras.Model)
        self.assertIsInstance(core_model, tf.keras.Model)

        # Expect two output from model: start positions and end positions
        self.assertIsInstance(model.output, list)
        self.assertLen(model.output, 2)

        # Expect two output from core_model: sequence and classification output.
        self.assertIsInstance(core_model.output, list)
        self.assertLen(core_model.output, 2)
        # shape should be [batch size, None, hidden_size]
        self.assertEqual(core_model.output[0].shape.as_list(),
                         [None, None, 16])
        # shape should be [batch size, hidden_size]
        self.assertEqual(core_model.output[1].shape.as_list(), [None, 16])