code: def safe_dtype_range(
      data: Any, dtype: DtypeLike | torch.dtype = None
  ) -> Any:
      """
      Utility to safely convert the input data to target dtype.
  
      Args:
          data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc.
              will convert to target dtype and keep the original type.
              for dictionary, list or tuple, convert every item.
          dtype: target data type to convert.
      """
  
      def _safe_dtype_range(data, dtype):
          output_dtype = dtype if dtype is not None else data.dtype
          dtype_bound_value = get_dtype_bound_value(output_dtype)
          if data.ndim == 0:
              data_bound = (data, data)
          else:
              if isinstance(data, torch.Tensor):
                  data_bound = (torch.min(data), torch.max(data))
              else:
                  data_bound = (np.min(data), np.max(data))
          if (data_bound[1] > dtype_bound_value[1]) or (
              data_bound[0] < dtype_bound_value[0]
          ):
              if isinstance(data, torch.Tensor):
                  return torch.clamp(
                      data, dtype_bound_value[0], dtype_bound_value[1]
                  )
              elif isinstance(data, np.ndarray):
                  return np.clip(
                      data, dtype_bound_value[0], dtype_bound_value[1]
                  )
              elif has_cp and isinstance(data, cp_ndarray):
                  return cp.clip(
                      data, dtype_bound_value[0], dtype_bound_value[1]
                  )
          else:
              return data
  
      if has_cp and isinstance(data, cp_ndarray):
          return cp.asarray(_safe_dtype_range(data, dtype))
      elif isinstance(data, np.ndarray):
          return np.asarray(_safe_dtype_range(data, dtype))
      elif isinstance(data, torch.Tensor):
          return _safe_dtype_range(data, dtype)
      elif isinstance(data, (float, int, bool)) and dtype is None:
          return data
      elif isinstance(data, (float, int, bool)) and dtype is not None:
          output_dtype = dtype
          dtype_bound_value = get_dtype_bound_value(output_dtype)
          data = (
              dtype_bound_value[1]
              if data > dtype_bound_value[1]
              else data
          )
          data = (
              dtype_bound_value[0]
              if data < dtype_bound_value[0]
              else data
          )
          return data
  
      elif isinstance(data, list):
          return [safe_dtype_range(i, dtype=dtype) for i in data]
      elif isinstance(data, tuple):
          return tuple(safe_dtype_range(i, dtype=dtype) for i in data)
      elif isinstance(data, dict):
          return {
              k: safe_dtype_range(v, dtype=dtype)
              for k, v in data.items()
          }
      return data
  
imports: from __future__ import annotations
  import re
  from collections.abc import Sequence
  from typing import Any
  import numpy
  import torch
  import monai
  from monai.config.type_definitions import DtypeLike, NdarrayTensor
  from monai.utils import optional_import
label_desc: Use image node_id 14 for calling a global function with example usage: safe_dtype_range(data, dtype) and returns: data, cp, data, np, torch, _safe_dtype_range, np, data, cp, data, unknown, tuple, unknown