code: def reduce_per_replica(values, strategy, reduction):
      """Attempt to reduce the structure `values` to single values.
  
      Given `values` (a `tf.Tensor` or a `PerReplica` structure),
      which represents the values across all the replicas, `reduce_per_replica`
      attempts to "reduce" those values and returns the corresponding structure
      that represents only single values.
  
      Currently, `reduce_per_replica` is only used for reducing the metric results
      from `tf.distribute.Strategy.run()`. Depending on the underlying
      `Strategy` implementation, `values` may be a `PerReplica` object,
       which can be thought of as a collection of values across the replicas,
      or a `tf.Tensor`, if the strategy has already conducted the reduction
      for the downstream library.
  
      There are five possible outcomes of reduction:
  
      1) if the `values` is a structure of simple `tf.Tensor`s, meaning that
         reduction is not actually needed, `reduce_per_replica` returns the
         structure as-is.
      2) else, if `reduction="auto"`, then the best reduction strategy is
         chosen based on the current environment. This should only be used
         for training cases (`fit()`).
      3) else, if `reduction="first"`, then `reduce_per_replica`
         returns the values of the first replica. This is used in the case of
         training and evaluation, where `values` is expected to hold the same
         value across the replicas as a result of `Strategy`'s synchronization
         across the replicas.
         `reduce_per_replica` does not synchronize the values.
      4) else, if `reduction="sum"`, then `reduce_per_replica` returns the sum
         of values for all replicas. This may be used in the custom training loop
         case, where each replica contain different values which are not
         synchronized.
      5) else, if `reduction="concat"`, then `reduce_per_replica`
         returns the concatenation of the values across the replicas, along the
         axis of dimension 0. This is used in the inference case (`predict()`).
  
      Args:
        values: Structure of `PerReplica` objects or `tf.Tensor`s. `tf.Tensor`s
          are returned as-is.
        strategy: `tf.distribute.Strategy` object.
        reduction: One of `"auto"`, `"first"`, `"concat"`, or `"sum"`.
          `"auto"` will select `"first"` when used under a TPUStrategy, or
          `"sum"` otherwise.
  
      Returns:
        Structure of `Tensor`s, representing the result of reduction.
  
      Raises:
        ValueError: if the reduction method is not supported.
      """
  
      if reduction == "auto":
          reduction = "sum"  # Ignore TPU strategy which should default to "first"
  
      def _reduce(v):
          """Reduce a single `PerReplica` object."""
          if _collective_all_reduce_multi_worker(strategy):
              if reduction == "concat":
                  return _multi_worker_concat(v, strategy)
              elif reduction == "sum":
                  return strategy.reduce("SUM", v, axis=None)
  
          if not _is_per_replica_instance(v):
              return v
          elif reduction == "first":
              return strategy.experimental_local_results(v)[0]
          elif reduction == "concat":
              if _is_tpu_multi_host(strategy):
                  return _tpu_multi_host_concat(v, strategy)
              else:
                  return concat(strategy.experimental_local_results(v))
          elif reduction == "sum":
              return tf.reduce_sum(
                  strategy.experimental_local_results(v)
              )
          else:
              raise ValueError(
                  '`reduction` must be "first", "concat", "sum", or "auto". '
                  f"Received: reduction={reduction}."
              )
  
      return tf.nest.map_structure(_reduce, values)
  
imports: import contextlib
  import warnings
  import numpy
  import tensorflow
  import tree
  from packaging.version import Version
  from tensorflow.python.eager import context
  from keras import callbacks
  from keras import metrics
  from keras import optimizers
  from keras.trainers import trainer
  from keras.trainers.data_adapters import data_adapter_utils
  from keras.trainers.epoch_iterator import EpochIterator
  from keras.utils import traceback_utils
label_desc: Use image node_id 1 for calling a global function with example usage: reduce_per_replica(values, strategy, reduction) and returns: tf, v, _multi_worker_concat, strategy, strategy, _tpu_multi_host_concat, concat, tf