U
    8‹Ñh=  ã                   @   s:   d dl mZ d dlZd dlmZ deeeedœdd„ZdS )	é    )ÚAnyN)Ú_get_device_indexF)ÚdeviceÚoptionalÚ	allow_cpuÚreturnc                 C   sŽ   t | tƒr| S t | tƒr"t | ¡} t | tjƒrd|rL| jdkrdtd| › �ƒ‚n| jdkrdtd| › �ƒ‚tj ¡ s‚t | tj	jƒr‚| j
S t| ||ƒS )a±  Get 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 MTIA device. Note that for a MTIA device without a specified index,
    i.e., ``torch.device('mtia')``, this will return the current default MTIA
    device if :attr:`optional` is ``True``. If :attr:`allow_cpu` is ``True``,
    CPU devices will be accepted and ``-1`` will be returned in this case.

    If :attr:`device` is a Python integer, it is returned as is.

    If :attr:`device` is ``None``, this will return the current default MTIA
    device if :attr:`optional` is ``True``.
    )ÚmtiaÚcpuz(Expected a mtia or cpu device, but got: r   z!Expected a mtia device, but got: )Ú
isinstanceÚintÚstrÚtorchr   ÚtypeÚ
ValueErrorZjitZis_scriptingr   ÚidxÚ_torch_get_device_index)r   r   r   © r   ú?/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/mtia/_utils.pyr   	   s    





r   )FF)Útypingr   r   Ztorch._utilsr   r   Úboolr   r   r   r   r   Ú<module>   s      ÿ  þ