code: def check_ffbr(
      factors, eliminate, plates, actual_samples, actual_log_prob
  ):
      """
      This can be seen as performing naive tensor variable elimination by
      breaking all plates and creating a single flat joint distribution.
      """
      assert "particle" not in plates
      flat_vars: Dict[str, Variable] = {}
      plate_vars: Dict[str, Variable] = {}
      broken_plates: Dict[str, Tuple[Variable]] = {}
      for name, factor in factors.items():
          for k, d in factor.inputs.items():
              if k in plates:
                  plate_vars[k] = Variable(k, d)
          if name in factor.inputs:  # i.e. if is latent
              broken_plates[name] = tuple(
                  plate_vars[p]
                  for p in sorted(plates.intersection(factor.inputs))
              )
              # I guess we could use Lambda here?
              broken_shape = tuple(
                  p.output.size for p in broken_plates[name]
              )
              domain = Reals[broken_shape + factor.inputs[name].shape]
              flat_vars[name] = Variable("flat_" + name, domain)[
                  broken_plates[name]
              ]
  
      flat_factors = []
      for factor in factors.values():
          f = factor(**flat_vars)
          f = f.reduce(ops.add, plates.intersection(f.inputs))
          flat_factors.append(f)
  
      # Check log prob.
      flat_joint = sum(flat_factors)
      log_Z = flat_joint.reduce(ops.logaddexp)
      flat_samples = {}
      for k, v in actual_samples.items():
          for p in reversed(broken_plates[k]):
              v = Lambda(p, v)
          flat_samples["flat_" + k] = v
      expected_log_prob = flat_joint(**flat_samples) - log_Z
      assert_close(
          actual_log_prob, expected_log_prob, atol=1e-4, rtol=None
      )
  
      # Check sample moments.
      sample_inputs = OrderedDict(
          particle=actual_log_prob.inputs["particle"]
      )
      rng_key = (
          None
          if get_backend() != "jax"
          else np.array([0, 0], dtype=np.uint32)
      )
      flat_deltas = flat_joint.sample(
          {"flat_" + k for k in flat_vars}, sample_inputs, rng_key
      )
      flat_samples = extract_samples(flat_deltas)
      expected_samples = {
          k: flat_samples["flat_" + k][broken_plates[k]]
          for k in flat_vars
      }
      expected_moments = get_moments(expected_samples)
      actual_moments = get_moments(actual_samples)
      assert_close(
          actual_moments, expected_moments, atol=0.02, rtol=None
      )
  
imports: from collections import OrderedDict
  from typing import Dict, Tuple
  import numpy
  import pytest
  import funsor.ops
  from funsor.domains import Bint, Real, Reals
  from funsor.interpretations import memoize
  from funsor.montecarlo import extract_samples
  from funsor.recipes import forward_filter_backward_precondition, forward_filter_backward_rsample
  from funsor.terms import Lambda, Variable
  from funsor.testing import Tensor, assert_close, randn, random_gaussian
  from funsor.util import get_backend
label_desc: Use image node_id 2 for calling a global function with example usage: check_ffbr(factors, eliminate, plates, actual_samples, actual_log_prob) without return types