pytorch/torch/cuda/_utils.py
Derek Kim fbdafb006e Fix trivial typos in torch.cuda._utils (#16026)
Summary:
Trivial typo fixings.

Maybe the indefinite article "an" is needed before each "specified index" but I'm not perfectly sure.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/16026

Differential Revision: D13709499

Pulled By: ezyang

fbshipit-source-id: 698b000bb8aa063afd81db6e67046456a439b2ce
2019-01-17 10:40:43 -08:00

36 lines
1.3 KiB
Python

import torch
import torch._six
def _get_device_index(device, optional=False):
r"""Gets the device index from :attr:`device`, which can be a torch.device
object, a Python integer, or ``None``.
If :attr:`device` is a torch.device object, returns the device index if it
is a CUDA device. Note that for a CUDA device without a specified index,
i.e., ``torch.device('cuda')``, this will return the current default CUDA
device if :attr:`optional` is ``True``.
If :attr:`device` is a Python integer, it is returned as is.
If :attr:`device` is ``None``, this will return the current default CUDA
device if :attr:`optional` is ``True``.
"""
if isinstance(device, torch._six.string_classes):
device = torch.device(device)
if isinstance(device, torch.device):
dev_type = device.type
if device.type != 'cuda':
raise ValueError('Expected a cuda device, but got: {}'.format(device))
device_idx = device.index
else:
device_idx = device
if device_idx is None:
if optional:
# default cuda device index
return torch.cuda.current_device()
else:
raise ValueError('Expected a cuda device with a specified index '
'or an integer, but got: '.format(device))
return device_idx