code: def _test_jacobian(self, input_dim, transform):
      jacobian = torch.zeros(input_dim, input_dim)
  
      def nonzero(x):
          return torch.sign(torch.abs(x))
  
      x = torch.randn(1, input_dim)
      y = transform(x)
      if transform.event_dim == 1:
          analytic_ldt = transform.log_abs_det_jacobian(x, y).data
      else:
          analytic_ldt = (
              transform.log_abs_det_jacobian(x, y).sum(-1).data
          )
  
      for j in range(input_dim):
          for k in range(input_dim):
              epsilon_vector = torch.zeros(1, input_dim)
              epsilon_vector[0, j] = self.epsilon
              delta = (
                  transform(x + 0.5 * epsilon_vector)
                  - transform(x - 0.5 * epsilon_vector)
              ) / self.epsilon
              jacobian[j, k] = float(delta[0, k].data.sum())
  
      # Apply permutation for autoregressive flows with a network
      if hasattr(transform, "arn") and "get_permutation" in dir(
          transform.arn
      ):
          permutation = transform.arn.get_permutation()
          permuted_jacobian = jacobian.clone()
          for j in range(input_dim):
              for k in range(input_dim):
                  permuted_jacobian[j, k] = jacobian[
                      permutation[j], permutation[k]
                  ]
          jacobian = permuted_jacobian
  
      # For autoregressive flow, Jacobian is sum of diagonal, otherwise need full determinate
      if (
          hasattr(transform, "autoregressive")
          and transform.autoregressive
      ):
          numeric_ldt = torch.sum(torch.log(torch.diag(jacobian)))
      else:
          numeric_ldt = torch.log(torch.abs(jacobian.det()))
  
      ldt_discrepancy = (analytic_ldt - numeric_ldt).abs()
      assert ldt_discrepancy < self.epsilon
  
      # Test that lower triangular with unit diagonal for autoregressive flows
      if hasattr(transform, "autoregressive"):
          diag_sum = torch.sum(torch.diag(nonzero(jacobian)))
          lower_sum = torch.sum(
              torch.tril(nonzero(jacobian), diagonal=-1)
          )
          assert diag_sum == float(input_dim)
          assert lower_sum == float(0.0)
  
imports: import operator
  from functools import partial, reduce
  from unittest import TestCase
  import pytest
  import torch
  import pyro.distributions
  import pyro.distributions.transforms
  from pyro.distributions import constraints
  from tests.common import assert_close
label_desc: Use image node_id 2 for calling the TransformTests obj's underlying member method code with example usage: obj._test_jacobian(input_dim, transform) and returns: torch