code: def _variadic_reduce(t, init, axis, reducer):
      """Implements a basic variadic reduce by repeated halving.
  
      The function executes recursively with each call trimming off one axis. Per
      axis, the computation uses `tf.while_loop` to repeatedly reduce the two halves
      of the tensor along the axis in question (padding with `init` as needed).
  
      Args:
        t: The tuple of tensors to reduce.
        init: A tuple of scalar initializations with dtypes aligned with `t`.
        axis: A sequence of python `int`.
        reducer: The function implementing the reduction. Each of the two arguments
          to `reducer` is a tuple like `t`.
  
      Returns:
        reduced: A tuple like `t` with the given reduction applied.
      """
      if not axis:
          return t
  
      if len(axis) > 1:
          if any(ax < 0 for ax in axis):
              raise ValueError(
                  "All axis args must be non-negative: {}".format(axis)
              )
          axis = sorted(axis)
          # Reduce innermost dims first, so that positive `axis` args remain aligned.
          reduced_innermost_axis = _variadic_reduce(
              t, init, axis[-1:], reducer
          )
          return _variadic_reduce(
              reduced_innermost_axis, init, axis[:-1], reducer
          )
  
      ax = axis[0]
      if any(part.shape[ax] != t[0].shape[ax] for part in t):
          raise ValueError(
              "Mismatched shapes along axis {}: {}".format(
                  ax, [part.shape for part in t]
              )
          )
  
      def cond(*t):
          return tf.not_equal(tf.shape(t[0])[ax], 1)
  
      def body(*t):
          dim = tf.shape(t[0])[ax]
          lhs, rhs = [], []
          for part, part_init in zip(t, init):
              p0, p1 = tf.split(
                  part, [dim // 2, dim // 2 + dim % 2], axis=ax
              )
              paddings = [(0, 0)] * tensorshape_util.rank(p0.shape)
              # Ensure we have two `f` operands of matching size:
              # 1. pad 0 sized dim up to 1.
              # 2. pad smaller-by-1 dim up to larger.
              paddings[ax] = (
                  0,
                  tf.maximum(tf.shape(p1)[ax], 1) - tf.shape(p0)[ax],
              )
              p0 = tf.pad(p0, paddings, constant_values=part_init)
              lhs.append(p0)
              paddings[ax] = (0, tf.shape(p0)[ax] - tf.shape(p1)[ax])
              # p1 may need padding if both are size 0 in the dim.
              p1 = tf.pad(p1, paddings, constant_values=part_init)
              rhs.append(p1)
          return reducer(tuple(lhs), tuple(rhs))
  
      shape_invariants = tuple(
          part.shape[:ax] + (None,) + part.shape[ax + 1 :] for part in t
      )
      t = tuple(t)
      result = tf.while_loop(
          cond, body, t, shape_invariants=shape_invariants
      )
      # Squeeze out the singleton dim in ax.
      return tuple(tf.squeeze(part, axis=ax) for part in result)
  
file_path: probability/tensorflow_probability/python/internal/variadic_reduce.py
imports: import numpy
  import tensorflow.compat.v1
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import custom_gradient
  from tensorflow_probability.python.internal import implementation_selection
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow.python.ops import control_flow_util
label_desc: Use image node_id 1 for calling a global function with example usage: _variadic_reduce(t, init, axis, reducer) and returns: tuple, t, _variadic_reduce, tf, reducer