code: def list_or_str_to_tensor(
      alist: _Union[_List[int], str, _torch.Tensor]
  ) -> _torch.Tensor:
      if isinstance(alist, _torch.Tensor):
          return alist
      elif isinstance(alist, str):
          # Safety check since we are calling eval
          range_str_regex = r"^(range)\(\d+(\,?\s*\d+){0,2}\)$"
          assert _re.match(range_str_regex, alist), (
              f"{alist} is invalid.",
              "Please provide a string such as 'range(...)'",
          )
          try:
              alist = eval(alist)
          except Exception:
              _logger.error(
                  f"Invalid range str {alist}.",
                  "Please refer to the documentation for correct usage",
              )
  
      return _torch.tensor(
          _np.ones(
              len(alist),
          )
          * alist,
          dtype=_torch.float32,
          requires_grad=False,
      )
  
file_path: coremltools/coremltools/optimize/torch/_utils/torch_utils.py
imports: import logging
  import operator
  import re
  from contextlib import contextmanager
  from typing import List
  from typing import Tuple
  from typing import Union
  import numpy
  import torch
label_desc: Use image node_id 1 for calling a global function with example usage: list_or_str_to_tensor(alist) and returns: _torch, alist