6
from torch._utils import _get_device_index as _torch_get_device_index
10
device: Any, optional: bool = False, allow_cpu: bool = False
12
r"""Get the device index from :attr:`device`, which can be a torch.device object, a Python integer, or ``None``.
14
If :attr:`device` is a torch.device object, returns the device index if it
15
is a CUDA device. Note that for a CUDA device without a specified index,
16
i.e., ``torch.device('cuda')``, this will return the current default CUDA
17
device if :attr:`optional` is ``True``. If :attr:`allow_cpu` is ``True``,
18
CPU devices will be accepted and ``-1`` will be returned in this case.
20
If :attr:`device` is a Python integer, it is returned as is.
22
If :attr:`device` is ``None``, this will return the current default CUDA
23
device if :attr:`optional` is ``True``.
25
if isinstance(device, int):
27
if isinstance(device, str):
28
device = torch.device(device)
29
if isinstance(device, torch.device):
31
if device.type not in ["cuda", "cpu"]:
32
raise ValueError(f"Expected a cuda or cpu device, but got: {device}")
33
elif device.type != "cuda":
34
raise ValueError(f"Expected a cuda device, but got: {device}")
35
if not torch.jit.is_scripting():
36
if isinstance(device, torch.cuda.device):
38
return _torch_get_device_index(device, optional, allow_cpu)