code: def _binary_einslogsumexp(formula, a, b):
      """Compute `einsum` in log space for the case of two tensor arguments."""
      lhs, rhs = formula.split("->")
      lhs_a, lhs_b = lhs.split(",")
  
      # First extract any 'diagonals'. These are repeated indices within `lhs_a`
      # or `lhs_b`.
      lhs_a, a = _extract_diagonals(lhs_a, a)
      lhs_b, b = _extract_diagonals(lhs_b, b)
  
      # At this point there should be no repeated indices in `lhs_a` or
      # `lhs_b`.
  
      # Now do any simple reductions. These correspond to indices that appear
      # in just one of `lhs_a` or `lhs_b` and not in the final result.
      lhs_a, a = _internal_contract(lhs_a, a, lhs_b + rhs)
      lhs_b, b = _internal_contract(lhs_b, b, lhs_a + rhs)
  
      intermediates = lhs_a
      for i in lhs_b:
          if i not in lhs_a:
              intermediates += i
      reductions = ""
      for i in lhs_a:
          if i not in rhs:
              reductions += i
      for i in lhs_b:
          if i not in rhs:
              reductions += i
      reduced = ""
      for i in intermediates:
          if i in rhs:
              reduced += i
  
      a = rearrange(lhs_a, intermediates, a)
      b = rearrange(lhs_b, intermediates, b)
  
      # Note that the intermediate `a + b` will likely be materialized
      # at this point. This makes the implementation of `logeinsumexp` less
      # efficient in its use of memory than it could be.
      # It is possible that future versions of TensorFlow will fuse this
      # operation so that the intermediate isn't materialised.
      # Note however that we are only materializing the sum for binary
      # `einsum`s and that we extract diagonals and perform internal
      # contractions to minimize the size of the materialized array.
      #
      # A proposed way to avoid the intermediate is presented
      # here:
      # https://stackoverflow.com/questions/23630277/numerically-stable-way-to-multiply-log-probability-matrices-in-numpy
      # That method can result in -inf entries even though all of the
      # numbers are well within a domain in which `logeinsumexp` shouldn't
      # underflow.
      almost = tf.reduce_logsumexp(
          a + b, axis=tuple(set(map(intermediates.index, reductions)))
      )
  
      return rearrange(reduced, rhs, almost)
  
file_path: probability/tensorflow_probability/python/experimental/marginalize/logeinsumexp.py
imports: import opt_einsum
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import prefer_static
label_desc: Use image node_id 7 for calling a global function with example usage: _binary_einslogsumexp(formula, a, b) and returns: rearrange