def _ExtractThroughputAndRuntime(output):
  """Extract throughput and runtime from Horovod output.

  Args:
    output: Horvod output

  Returns:
    Tuple of:
      Average throuput in images per second (float),
      Runtime in seconds (float).
  """
  # Start from last line and iterate backwards.
  throughput_samples = []
  runtime = 0
  for line in output.splitlines()[::-1]:
    split_line = line.split()
    if split_line[0].startswith('Finished'):
      runtime = float(split_line[2])
      continue
    split_line = line.split()
    if split_line[0] == '1':  # We are done parsing.
      break
    throughput_samples.append(float(split_line[2]))
  avg_throughput = sum(throughput_samples) / len(throughput_samples)
  return round(avg_throughput, 1), round(runtime, 1)