U
    ©Ê¹j~  ã                   @   s*   d dl mZmZ d dlZG dd„ dƒZdS )é    )ÚOptionalÚUnionNc                   @   s€   e Zd ZdZeeejf dœdd„Ze	dd„ ƒZ
ee dœdd	„Zee dœd
d„Zejdœdd„Zdd„ Zdd„ Zdd„ ZdS )Ú_remote_devicea@  
    Represents a device on a remote worker.

    Args:
        remote_device (str or torch.device): Represents a device on a remote worker.
            The string format should be one of the following:

                1. "<workername>/<device>", where the device field can be parsed as torch.device type.
                   E.g., "trainer0/cpu", "trainer0", "ps0/cuda:0".
                   In addition, the device field can be optional and the default value is "cpu".
                2. "rank:<rank>/<device>", where <rank> is the rank of the
                   process and device can be parsed as torch.device type.
                   E.g., "rank:0/cpu", "rank:0", "rank:0/cuda:0"
                3. <workername> and <rank> are optional and formats like "cpu"
                    and "cuda:1", just represent local devices.
    )Úremote_devicec                 C   sV  d|› d�}d | _ d | _d | _t|tjƒr2|| _n‚t|tƒr¢| d¡}t|ƒdkr`|\| _ | _q´t|ƒdkr˜t	 
|d ¡r†|d | _q |d | _ d| _q´t|ƒ‚ntdt|ƒ› �ƒ‚| j d k	rÌ| j sÌt|ƒ‚t | j¡| _| j d k	�rR| j  d	¡}t|ƒdk�r<|d d
k�r2|d  ¡ �r2t|d ƒ| _d | _ nt|ƒ‚nt|ƒdk�rRt|ƒ‚d S )NzCould not parse remote_device: zU. The valid format is '<workername>/<device>' or 'rank:<rank>/<device>' or '<device>'ú/é   é   r   Úcpuz Invalid type for remote_device: ú:Úrank)Ú_worker_nameÚ_rankÚ_deviceÚ
isinstanceÚtorchÚdeviceÚstrÚsplitÚlenr   Ú_is_valid_local_deviceÚ
ValueErrorÚ	TypeErrorÚtypeÚisdigitÚint)Úselfr   ÚPARSE_ERRORÚfields© r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/distributed/remote_device.pyÚ__init__   s<    
ÿ




z_remote_device.__init__c                 C   s,   zt  | ¡ W dS  tk
r&   Y dS X d S )NTF)r   r   Ú	Exception)r   r   r   r   r   G   s
    
z%_remote_device._is_valid_local_device)Úreturnc                 C   s   | j S )zŒ
        Returns the name of remote worker representing the remote device.
        Returns ``None`` if no worker name is available.
        )r   ©r   r   r   r   Úworker_nameP   s    z_remote_device.worker_namec                 C   s   | j S )z…
        Returns the rank of remote worker representing the remote device.
        Returns ``None`` if no rank is available.
        )r   r#   r   r   r   r   W   s    z_remote_device.rankc                 C   s   | j S )z@
        Returns the local device on the remote worker.
        )r   r#   r   r   r   r   ^   s    z_remote_device.devicec                 C   s€   | j d k	rP| jd k	r&| j› d| j › �S | jd k	rDd| j› d| j › �S t| j ƒS n,| jd k	rb| j› S | jd k	rt| j› S tdƒ‚d S )Nr   zrank:zInvalid state!)r   r   r   r   ÚRuntimeErrorr#   r   r   r   Ú__repr__d   s    




z_remote_device.__repr__c                 C   s:   t |tƒsdS | j|jkr6| j|jkr6| j|jkr6dS dS )NFT)r   r   r   r   r   )r   Úotherr   r   r   Ú__eq__t   s    

ÿ
þ
ýz_remote_device.__eq__c                 C   s   t | jƒt | jƒA t | jƒA S )N)Úhashr   r   r   r#   r   r   r   Ú__hash__‚   s
    ÿþz_remote_device.__hash__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    Ústaticmethodr   r   r$   r   r   r&   r(   r*   r   r   r   r   r      s   /
r   )Útypingr   r   r   r   r   r   r   r   Ú<module>   s   