3
I4W^G  ã               @   s   d dl Z d dlZ ddd„ZdS )é    NFc             C   sv   t | tjjƒrtj| ƒ} t | tjƒrJ| j}| jdkrBtdj| ƒƒ‚| j}n| }|dkrr|rdtj	j
ƒ S tdj| ƒƒ‚|S )aI  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``.
    Úcudaz#Expected a cuda device, but got: {}NzFExpected a cuda device with a specified index or an integer, but got: )Ú
isinstanceÚtorchZ_sixZstring_classesÚdeviceÚtypeÚ
ValueErrorÚformatÚindexr   Zcurrent_device)r   ÚoptionalZdev_typeZ
device_idx© r   ú2/tmp/pip-build-e66j97b2/torch/torch/cuda/_utils.pyÚ_get_device_index   s    


r   )F)r   Z
torch._sixr   r   r   r   r   Ú<module>   s   