code: def convert_nonref_to_tensor(
      value,
      dtype=None,
      dtype_hint=None,
      as_shape_tensor=False,
      name=None,
  ):
      """Converts the given `value` to a `Tensor` if input is nonreference type.
  
      This function converts Python objects of various types to `Tensor` objects
      only if the input has nonreference semantics. Reference semantics are
      characterized by `tensor_util.is_ref` and is any object which is a
      `tf.Variable` or instance of `tf.Module`. This function accepts any input
      which `tf.convert_to_tensor` would also.
  
      Note: This function diverges from default Numpy behavior for `float` and
        `string` types when `None` is present in a Python list or scalar. Rather
        than silently converting `None` values, an error will be thrown.
  
      Args:
        value: An object whose type has a registered `Tensor` conversion function.
        dtype: Optional element type for the returned tensor. If missing, the
          type is inferred from the type of `value`.
        dtype_hint: Optional element type for the returned tensor,
          used when dtype is None. In some cases, a caller may not have a
          dtype in mind when converting to a tensor, so dtype_hint
          can be used as a soft preference.  If the conversion to
          `dtype_hint` is not possible, this argument has no effect.
        as_shape_tensor: Optional boolean when if `True` uses
          `prefer_static.convert_to_shape_tensor` instead of `tf.convert_to_tensor`
          for JAX compatibility.
        name: Optional name to use if a new `Tensor` is created.
  
      Returns:
        tensor: A `Tensor` based on `value`.
  
      Raises:
        TypeError: If no conversion function is registered for `value` to `dtype`.
        RuntimeError: If a registered conversion function returns an invalid value.
        ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
  
  
      #### Examples:
  
      ```python
      from tensorflow_probability.python.internal import tensor_util
  
      x = tf.Variable(0.)
      y = tensor_util.convert_nonref_to_tensor(x)
      x is y
      # ==> True
  
      x = tf.constant(0.)
      y = tensor_util.convert_nonref_to_tensor(x)
      x is y
      # ==> True
  
      x = np.array(0.)
      y = tensor_util.convert_nonref_to_tensor(x)
      x is y
      # ==> False
      tf.is_tensor(y)
      # ==> True
  
      x = tfp.util.DeferredTensor(13.37, lambda x: x)
      y = tensor_util.convert_nonref_to_tensor(x)
      x is y
      # ==> True
      tf.is_tensor(y)
      # ==> True
      tf.equal(y, 13.37)
      # ==> True
      ```
  
      """
      # We explicitly do not use a tf.name_scope to avoid graph clutter.
      if value is None:
          return None
      if is_ref(value):
          if dtype is None:
              return value
          dtype_base = dtype_util.base_dtype(dtype)
          value_dtype_base = dtype_util.base_dtype(value.dtype)
          if dtype_base != value_dtype_base:
              raise TypeError(
                  'Mutable type must be of dtype "{}" but is "{}".'.format(
                      dtype_util.name(dtype_base),
                      dtype_util.name(value_dtype_base),
                  )
              )
          return value
      if as_shape_tensor:
          return prefer_static.convert_to_shape_tensor(
              value, dtype=dtype, dtype_hint=dtype_hint, name=name
          )
      return tf.convert_to_tensor(
          value, dtype=dtype, dtype_hint=dtype_hint, name=name
      )
  
file_path: probability/tensorflow_probability/python/internal/tensor_util.py
imports: import tensorflow.compat.v2
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
label_desc: Use image node_id 1 for calling a global function with example usage: convert_nonref_to_tensor(value, dtype, dtype_hint, as_shape_tensor, name) and returns: tf, None, value, prefer_static, value