code: def naive_plated_einsum(eqn, *terms, **kwargs):
      """
      Implements Tensor Variable Elimination (Algorithm 1 in [Obermeyer et al 2019])
  
      [Obermeyer et al 2019] Obermeyer, F., Bingham, E., Jankowiak, M., Chiu, J.,
          Pradhan, N., Rush, A., and Goodman, N.  Tensor Variable Elimination for
          Plated Factor Graphs, 2019
      """
      plates = kwargs.pop("plates", "")
      if not plates:
          return naive_einsum(eqn, *terms, **kwargs)
  
      backend = kwargs.pop("backend", "torch")
      if backend in BACKEND_OPS:
          sum_op, prod_op = BACKEND_OPS[backend]
      else:
          raise ValueError("{} backend not implemented".format(backend))
  
      assert isinstance(eqn, str)
      assert all(isinstance(term, Funsor) for term in terms)
      inputs, output = eqn.split("->")
      inputs = inputs.split(",")
      assert len(inputs) == len(terms)
      assert len(output.split(",")) == 1
      input_dims = frozenset(d for inp in inputs for d in inp)
      output_dims = frozenset(d for d in output)
      plate_dims = frozenset(plates) - output_dims
      reduce_vars = input_dims - output_dims - frozenset(plates)
  
      output_plates = output_dims & frozenset(plates)
      if not all(output_plates.issubset(inp) for inp in inputs):
          raise NotImplementedError("TODO")
  
      eliminate = plate_dims | reduce_vars
      return sum_product(
          sum_op, prod_op, terms, eliminate, frozenset(plates)
      )
  
file_path: funsor/funsor/einsum/__init__.py
imports: from functools import reduce
  import funsor.ops
  from funsor.cnf import Contraction
  from funsor.optimizer import apply_optimizer
  from funsor.sum_product import sum_product
  from funsor.terms import Funsor, Variable, lazy
label_desc: Use image node_id 3 for calling a global function with example usage: naive_plated_einsum(eqn) and returns: sum_product, naive_einsum