code: def test_nested_einsum(
      eqn1, eqn2, optimize1, optimize2, backend1, backend2, einsum_impl
  ):
      inputs1, outputs1, sizes1, operands1, _ = make_einsum_example(
          eqn1, sizes=(3,)
      )
      (
          inputs2,
          outputs2,
          sizes2,
          operands2,
          funsor_operands2,
      ) = make_einsum_example(eqn2, sizes=(3,))
  
      # normalize the probs for ground-truth comparison
      operands1 = [
          operand.abs() / operand.abs().sum(-1, keepdim=True)
          for operand in operands1
      ]
  
      expected1 = pyro_einsum(
          eqn1, *operands1, backend=backend1, modulo_total=True
      )[0]
      expected2 = pyro_einsum(
          outputs1[0] + "," + eqn2,
          *([expected1] + operands2),
          backend=backend2,
          modulo_total=True,
      )[0]
  
      with normalize:
          funsor_operands1 = [
              Categorical(
                  probs=Tensor(
                      operand,
                      inputs=OrderedDict(
                          [(d, Bint[sizes1[d]]) for d in inp[:-1]]
                      ),
                  )
              )(value=Variable(inp[-1], Bint[sizes1[inp[-1]]])).exp()
              for inp, operand in zip(inputs1, operands1)
          ]
  
          output1_naive = einsum_impl(
              eqn1, *funsor_operands1, backend=backend1
          )
          with reflect:
              output1 = (
                  apply_optimizer(output1_naive)
                  if optimize1
                  else output1_naive
              )
          output2_naive = einsum_impl(
              outputs1[0] + "," + eqn2,
              *([output1] + funsor_operands2),
              backend=backend2,
          )
          with reflect:
              output2 = (
                  apply_optimizer(output2_naive)
                  if optimize2
                  else output2_naive
              )
  
      actual1 = reinterpret(output1)
      actual2 = reinterpret(output2)
  
      assert torch.allclose(expected1, actual1.data)
      assert torch.allclose(expected2, actual2.data)
  
file_path: funsor/test/test_optimizer.py
imports: from collections import OrderedDict
  import pytest
  import funsor
  from funsor.domains import Bint
  from funsor.einsum import einsum, naive_contract_einsum, naive_einsum, naive_plated_einsum
  from funsor.interpretations import normalize, reflect
  from funsor.interpreter import reinterpret
  from funsor.optimizer import apply_optimizer
  from funsor.tensor import Tensor
  from funsor.terms import Variable
  from funsor.testing import assert_close, make_chain_einsum, make_einsum_example, make_hmm_einsum, make_plated_hmm_einsum
  from funsor.util import get_backend
label_desc: Use image node_id 2 for calling a global function with example usage: test_nested_einsum(eqn1, eqn2, optimize1, optimize2, backend1, backend2, einsum_impl) without return types