def metric_max_over_ground_truths(metric_fn, predictions, ground_truths):
  """Take the average best score against all ground truth answers.

  This is a bit different than SQuAD in that there are multiple answers
  **and** predictions that we average over. For some situations (e.g., *top k*
  beams or multiple human references) we might want to calculate the average
  performance. In most cases, however, predictions will be a list of length 1.

  Args:
    metric_fn: Callable on (prediction, ground_truth).
    predictions: List of whitespace separated prediction tokens.
    ground_truths: List of whitespace separated answer tokens.

  Returns:
    max_score: Max output of metric_fn.
  """
  all_metrics = []
  for prediction in predictions:
    scores_for_ground_truths = []
    for ground_truth in ground_truths:
      score = metric_fn(prediction, ground_truth)
      scores_for_ground_truths.append(score)
    all_metrics.append(max(scores_for_ground_truths))
  return sum(all_metrics) / len(all_metrics)