code: def _cast_to_dtype(
      value: TensorData, dtype: Optional[DType] = None
  ) -> Union[tf.Tensor, tf.Variable]:
      if dtype is None:
          dtype = default_float()
  
      if tf.is_tensor(value):
          # NOTE(awav) TF2.2 resolves issue with cast.
          # From TF2.2, `tf.cast` can be used alone instead of this auxiliary function.
          # workaround for https://github.com/tensorflow/tensorflow/issues/35938
          return tf.cast(value, dtype)
      else:
          return tf.convert_to_tensor(value, dtype=dtype)
  
imports: from enum import Enum
  from typing import TYPE_CHECKING, Any, List, Optional, Sequence, Tuple, Union
  import numpy
  import tensorflow
  import tensorflow_probability
  from check_shapes import check_shapes
  from typing_extensions import Final
  from .config import default_float, default_summary_fmt
  from .type_flags import GENERIC_NP_ARRAYS, NP_TYPE_CHECKING
label_desc: Use image node_id 3 for calling a global function with example usage: _cast_to_dtype(value, dtype) and returns: tf, tf