code: def test_partition_terms(inputs, dims, expected_num_components):
      ring = LogRing()
      symbol_to_size = dict(zip("abc", [2, 3, 4]))
      shapes = [
          tuple(symbol_to_size[s] for s in input_) for input_ in inputs
      ]
      tensors = [torch.randn(shape) for shape in shapes]
      for input_, tensor in zip(inputs, tensors):
          tensor._pyro_dims = input_
      components = list(_partition_terms(ring, tensors, dims))
  
      # Check that result is a partition.
      expected_terms = sorted(tensors, key=id)
      actual_terms = sorted(
          (x for c in components for x in c[0]), key=id
      )
      assert actual_terms == expected_terms
      assert dims == set.union(set(), *(c[1] for c in components))
  
      # Check that the partition is not too coarse.
      assert len(components) == expected_num_components
  
      # Check that partition is not too fine.
      component_dict = {
          x: i for i, (terms, _) in enumerate(components) for x in terms
      }
      for x in tensors:
          for y in tensors:
              if x is not y:
                  if dims.intersection(x._pyro_dims, y._pyro_dims):
                      assert component_dict[x] == component_dict[y]
  
imports: import itertools
  import numbers
  from collections import OrderedDict
  import opt_einsum
  import pytest
  import torch
  import pyro.ops.jit
  from pyro.distributions.util import logsumexp
  from pyro.ops.contract import _partition_terms, contract_tensor_tree, contract_to_tensor, einsum, naive_ubersum, ubersum
  from pyro.ops.einsum.adjoint import require_backward
  from pyro.ops.rings import LogRing
  from pyro.poutine.indep_messenger import CondIndepStackFrame
  from pyro.util import optional
  from tests.common import assert_equal
label_desc: Use image node_id 5 for calling a global function with example usage: test_partition_terms(inputs, dims, expected_num_components) without return types