code: def optimize_contract_finitary_funsor(
      red_op, bin_op, reduced_vars, terms
  ):
      if red_op is ops.null or bin_op is ops.null:
          return None
      if (red_op, bin_op) not in DISTRIBUTIVE_OPS:
          return None
  
      # build opt_einsum optimizer IR
      inputs = [term.input_vars for term in terms]
      size_dict = {
          k: (
              (REAL_SIZE * v.num_elements)
              if v.dtype == "real"
              else v.dtype
          )
          for term in terms
          for k, v in term.inputs.items()
      }
      outputs = frozenset().union(*inputs) - reduced_vars
  
      # optimize path with greedy opt_einsum optimizer
      # TODO switch to new 'auto' strategy
      input_names = [frozenset(term.inputs) for term in terms]
      output_names = frozenset(v.name for v in outputs)
      path = greedy(input_names, output_names, size_dict)
  
      # first prepare a reduce_dim counter to avoid early reduction
      reduce_dim_counter = collections.Counter()
      for input in inputs:
          reduce_dim_counter.update({d: 1 for d in input})
  
      operands = list(terms)
      for a, b in path:
          b, a = tuple(sorted((a, b), reverse=True))
          tb = operands.pop(b)
          ta = operands.pop(a)
  
          # don't reduce a dimension too early - keep a collections.Counter
          # and only reduce when the dimension is removed from all lhs terms in path
          reduce_dim_counter.subtract(
              {d: 1 for d in reduced_vars & ta.input_vars}
          )
          reduce_dim_counter.subtract(
              {d: 1 for d in reduced_vars & tb.input_vars}
          )
  
          # reduce variables that don't appear in other terms
          both_vars = ta.input_vars | tb.input_vars
          path_end_reduced_vars = frozenset(
              d
              for d in reduced_vars & both_vars
              if reduce_dim_counter[d] == 0
          )
  
          # count new appearance of variables that aren't reduced
          reduce_dim_counter.update(
              {
                  d: 1
                  for d in reduced_vars
                  & (both_vars - path_end_reduced_vars)
              }
          )
  
          path_end = Contraction(
              red_op if path_end_reduced_vars else ops.null,
              bin_op,
              path_end_reduced_vars,
              ta,
              tb,
          )
          operands.append(path_end)
  
      # reduce any remaining dims, if necessary
      final_reduced_vars = (
          frozenset(
              d
              for (d, count) in reduce_dim_counter.items()
              if count > 0
          )
          & reduced_vars
      )
      if final_reduced_vars:
          path_end = path_end.reduce(red_op, final_reduced_vars)
      return path_end
  
file_path: funsor/funsor/optimizer.py
imports: import collections
  from opt_einsum.paths import greedy
  import funsor.interpreter
  from funsor.cnf import Contraction
  from funsor.interpretations import DispatchedInterpretation, PrioritizedInterpretation, eager, lazy, normalize_base
  from funsor.interpreter import get_interpretation
  from funsor.ops import DISTRIBUTIVE_OPS, AssociativeOp
  from funsor.terms import Funsor
  from funsor.typing import Variadic
  from .None import ops
label_desc: Use image node_id 5 for calling a global function with example usage: optimize_contract_finitary_funsor(red_op, bin_op, reduced_vars, terms) and returns: path_end, None, None