code: def convert_to_torch_tensor(
      x: TensorStructType, device: Optional[str] = None
  ):
      """Converts any struct to torch.Tensors.
  
      x: Any (possibly nested) struct, the values in which will be
          converted and returned as a new struct with all leaves converted
          to torch tensors.
  
      Returns:
          Any: A new struct with the same structure as `x`, but with all
          values converted to torch Tensor types. This does not convert possibly
          nested elements that are None because torch has no representation for that.
      """
  
      def mapping(item):
          if item is None:
              # Torch has no representation for `None`, so we return None
              return item
  
          # Special handling of "Repeated" values.
          if isinstance(item, RepeatedValues):
              return RepeatedValues(
                  tree.map_structure(mapping, item.values),
                  item.lengths,
                  item.max_len,
              )
  
          # Already torch tensor -> make sure it's on right device.
          if torch.is_tensor(item):
              tensor = item
          # Numpy arrays.
          elif isinstance(item, np.ndarray):
              # Object type (e.g. info dicts in train batch): leave as-is.
              # str type (e.g. agent_id in train batch): leave as-is.
              if item.dtype == object or item.dtype.type is np.str_:
                  return item
              # Non-writable numpy-arrays will cause PyTorch warning.
              elif item.flags.writeable is False:
                  with warnings.catch_warnings():
                      warnings.simplefilter("ignore")
                      tensor = torch.from_numpy(item)
              # Already numpy: Wrap as torch tensor.
              else:
                  tensor = torch.from_numpy(item)
          # Everything else: Convert to numpy, then wrap as torch tensor.
          else:
              tensor = torch.from_numpy(np.asarray(item))
  
          # Floatify all float64 tensors.
          if tensor.is_floating_point():
              tensor = tensor.float()
  
          return tensor if device is None else tensor.to(device)
  
      return tree.map_structure(mapping, x)
  
imports: import logging
  import os
  import warnings
  from typing import Dict, List, Optional, TYPE_CHECKING, Union
  import gymnasium
  import numpy
  import tree
  from gymnasium.spaces import Discrete, MultiDiscrete
  from packaging import version
  import ray
  from ray.rllib.models.repeated_values import RepeatedValues
  from ray.rllib.utils.annotations import Deprecated, PublicAPI, DeveloperAPI
  from ray.rllib.utils.framework import try_import_torch
  from ray.rllib.utils.numpy import SMALL_NUMBER
  from ray.rllib.utils.typing import LocalOptimizer, SpaceStruct, TensorStructType, TensorType
label_desc: Use image node_id 6 for calling a global function with example usage: convert_to_torch_tensor(x, device) and returns: tree, unknown, item, RepeatedValues, item