code: def allreduce(
      tensor: tf.Tensor,
      average: bool = True,
      name: str = None,
      device: str = "",
  ) -> tf.Tensor:
      """Perform an allreduce on a tf.Tensor or tf.IndexedSlices.
  
      This function performs a bandwidth-optimal ring allreduce on the input
      tensor. If the input is an tf.IndexedSlices, the function instead does an
      allgather on the values and the indices, effectively doing an allreduce on
      the represented tensor.
  
      Arguments:
          tensor: tf.Tensor, tf.Variable, or tf.IndexedSlices to reduce.
                  The shape of the input must be identical across all ranks.
          average: If True, computes the average over all ranks.
                   Otherwise, computes the sum over all ranks.
          name: A name of the allreduce operation.
          device: Device to be used for dense tensors.
  
      Returns:
          A tensor of the same shape and type as `tensor`, summed across all
          processes.
      """
      if isinstance(tensor, tf.IndexedSlices):
          raise ValueError(
              "Do not support Sparse or Indexed Slices Tensor yet."
          )
      else:
          with tf.device(device):
              bluefog_size = tf.cast(size(), dtype=tensor.dtype)
              summed_tensor = _allreduce(tensor, name)
              new_tensor = (
                  (summed_tensor / bluefog_size)
                  if average
                  else summed_tensor
              )
          return new_tensor
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import re
  import tensorflow
  from tensorflow.python.framework import load_library
  from tensorflow.python.framework import ops
  from tensorflow.python.platform import resource_loader
  from bluefog.common.basics import BlueFogBasics
  from bluefog.common.util import get_ext_suffix
  from bluefog.tensorflow.util import _executing_eagerly
label_desc: Use image node_id 6 for calling a global function with example usage: allreduce(tensor, average, name, device) and returns: new_tensor