code: def _check_loss_and_grads(expected_loss, actual_loss):
      assert_equal(
          actual_loss,
          expected_loss,
          msg="Expected:\n{}\nActual:\n{}".format(
              expected_loss.detach().cpu().numpy(),
              actual_loss.detach().cpu().numpy(),
          ),
      )
  
      if (
          "TEST_ENUM_PYRO_BACKEND" in os.environ
      ):  # only log if we manually set a backend
          logging.debug(
              "Expected:\n{}\nActual:\n{}".format(
                  expected_loss.detach().cpu().numpy(),
                  actual_loss.detach().cpu().numpy(),
              )
          )
  
      names = pyro.get_param_store().keys()
      params = [pyro.param(name).unconstrained() for name in names]
      actual_grads = grad(
          actual_loss, params, allow_unused=True, retain_graph=True
      )
      expected_grads = grad(
          expected_loss, params, allow_unused=True, retain_graph=True
      )
      for name, actual_grad, expected_grad in zip(
          names, actual_grads, expected_grads
      ):
          if actual_grad is None or expected_grad is None:
              continue
          assert not torch_isnan(actual_grad)
          assert not torch_isnan(expected_grad)
          assert_equal(
              actual_grad,
              expected_grad,
              msg="{}\nExpected:\n{}\nActual:\n{}".format(
                  name,
                  expected_grad.detach().cpu().numpy(),
                  actual_grad.detach().cpu().numpy(),
              ),
          )
  
imports: import logging
  import os
  import pyroapi
  import pytest
  import torch
  from torch.autograd import grad
  from torch.distributions import constraints
  from pyro.ops.indexing import Vindex
  from pyro.util import torch_isnan
  from tests.common import assert_equal, xfail_param
label_desc: Use image node_id 1 for calling a global function with example usage: _check_loss_and_grads(expected_loss, actual_loss) without return types