code: def _value_and_gradients(
      fn, fn_arg_list, result=None, grads=None, name=None
  ):
      """Helper to `maybe_call_fn_and_grads`."""
      with tf.name_scope(name or "value_and_gradients"):
  
          def _convert_to_tensor(x, name):
              ctt = (
                  lambda x_: None
                  if x_ is None
                  else tf.convert_to_tensor(  # pylint: disable=g-long-lambda
                      x_, name=name
                  )
              )
              return (
                  [ctt(x_) for x_ in x] if is_list_like(x) else ctt(x)
              )
  
          fn_arg_list = (
              list(fn_arg_list)
              if is_list_like(fn_arg_list)
              else [fn_arg_list]
          )
          fn_arg_list = _convert_to_tensor(fn_arg_list, "fn_arg")
  
          if (
              result is None
              and grads is None
              and (JAX_MODE or not tf.executing_eagerly())
          ):
              # Currently, computing gradient is not working well with caching in
              # tensorflow eager mode (see below), so we will handle that case
              # separately.
              return tfp_math_value_and_gradients(fn, fn_arg_list)
  
          if result is None:
              result = fn(*fn_arg_list)
              if grads is None:
                  assert tf.executing_eagerly()
                  # Ensure we disable bijector cacheing in eager mode.
                  # TODO(b/72831017): Remove this once bijector cacheing is fixed for
                  # eager mode.
                  fn_arg_list = [0 + x for x in fn_arg_list]
  
          result = _convert_to_tensor(result, "fn_result")
  
          if grads is not None:
              grads = _convert_to_tensor(grads, "fn_grad")
              return result, grads
  
          _, grads = tfp_math_value_and_gradients(fn, fn_arg_list)
  
          return result, grads
  
imports: import warnings
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import broadcast_util
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow_probability.python.math.gradient import value_and_gradient
  from tensorflow.python.util import deprecation
label_desc: Use image node_id 13 for calling a global function with example usage: _value_and_gradients(fn, fn_arg_list, result, grads, name) and returns: result,  grads, unknown, tfp_math_value_and_gradients, result,  grads