code: def get_device(device: TorchDeviceType = None) -> torch.device:
      """
      Instantiates a PyTorch device object.
  
      Parameters
      ----------
      device
          Either `None`, a str ('gpu', 'cuda' or 'cpu') indicating the device to choose, or an already instantiated device
          object. If `None`, the GPU is selected if it is detected, otherwise the CPU is used as a fallback.
  
      Returns
      -------
      The instantiated device object.
      """
      if isinstance(device, torch.device):  # Already a torch device
          return device
      else:  # Instantiate device
          if device is None or device.lower() in ["gpu", "cuda"]:
              torch_device = torch.device(
                  "cuda" if torch.cuda.is_available() else "cpu"
              )
              if torch_device.type == "cpu":
                  logger.warning("No GPU detected, fall back on CPU.")
          else:
              torch_device = torch.device("cpu")
              if device.lower() != "cpu":
                  logger.warning(
                      "Requested device not recognised, fall back on CPU."
                  )
      return torch_device
  
imports: import logging
  from typing import Type
  from alibi_detect.utils._types import TorchDeviceType
  import torch
label_desc: Use image node_id 3 for calling a global function with example usage: get_device(device) and returns: torch_device, device