U
    d                     @   s,   d dl mZmZ d dlZG dd de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|| _nt|tr|d}t|dkr`|\| _ | _qt|dkrt	
|d r|d | _q|d | _ d| _qt|ntdt| | j d k	r| j st|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   C/tmp/pip-unpacked-wheel-ua33x9lu/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   objectr   r   r   r   r   <module>   s   