_utils.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import torch
  2. from typing import Any
  3. # The _get_device_index has been moved to torch.utils._get_device_index
  4. from torch._utils import _get_device_index as _torch_get_device_index
  5. def _get_device_index(device: Any, optional: bool = False,
  6. allow_cpu: bool = False) -> int:
  7. r"""Gets the device index from :attr:`device`, which can be a torch.device
  8. object, a Python integer, or ``None``.
  9. If :attr:`device` is a torch.device object, returns the device index if it
  10. is a CUDA device. Note that for a CUDA device without a specified index,
  11. i.e., ``torch.device('cuda')``, this will return the current default CUDA
  12. device if :attr:`optional` is ``True``. If :attr:`allow_cpu` is ``True``,
  13. CPU devices will be accepted and ``-1`` will be returned in this case.
  14. If :attr:`device` is a Python integer, it is returned as is.
  15. If :attr:`device` is ``None``, this will return the current default CUDA
  16. device if :attr:`optional` is ``True``.
  17. """
  18. if isinstance(device, int):
  19. return device
  20. if isinstance(device, str):
  21. device = torch.device(device)
  22. if isinstance(device, torch.device):
  23. if allow_cpu:
  24. if device.type not in ['cuda', 'cpu']:
  25. raise ValueError('Expected a cuda or cpu device, but got: {}'.format(device))
  26. elif device.type != 'cuda':
  27. raise ValueError('Expected a cuda device, but got: {}'.format(device))
  28. if not torch.jit.is_scripting():
  29. if isinstance(device, torch.cuda.device):
  30. return device.idx
  31. return _torch_get_device_index(device, optional, allow_cpu)
  32. def _dummy_type(name: str) -> type:
  33. def get_err_fn(is_init: bool):
  34. def err_fn(obj, *args, **kwargs):
  35. if is_init:
  36. class_name = obj.__class__.__name__
  37. else:
  38. class_name = obj.__name__
  39. raise RuntimeError(
  40. "Tried to instantiate dummy base class {}".format(class_name))
  41. return err_fn
  42. return type(name, (object,), {"__init__": get_err_fn(True), "__new__": get_err_fn(False)})