o
    ib                     @   s   d dl Z G dd dZdS )    Nc                   @   s|   e Zd ZdZdeejB fddZedd Z	dedB fd	d
Z
dedB fddZdejf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   sN  d| d}d | _ d | _d | _t|tjr|| _n@t|trP|d}t|dkr0|\| _ | _n)t|dkrLt	
|d rC|d | _n|d | _ d| _nt|tdt| | j d ure| j set|t| j| _| j d ur| j d	}t|dkr|d d
kr|d  rt|d | _d | _ d S t|t|dkrt|d S 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   a/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/distributed/remote_device.py__init__   s@   






z_remote_device.__init__c                 C   s&   zt |  W dS  ty   Y dS w )NTF)r   r   	Exception)r   r   r   r   r   J   s   
z%_remote_device._is_valid_local_devicereturnNc                 C      | j S )zlReturn the name of remote worker representing the remote device and ``None`` if no worker name is available.)r
   r   r   r   r   worker_nameS      z_remote_device.worker_namec                 C   r!   )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   r!   )z-Return the local device on the remote worker.)r   r"   r   r   r   r   ^   r$   z_remote_device.devicec                 C   sz   | j d ur'| jd ur| j d| j  S | jd ur"d| j d| j  S t| j S | jd ur0| j S | jd ur9| j S td)Nr   zrank:zInvalid state!)r   r
   r   r   RuntimeErrorr"   r   r   r   __repr__b   s   





z_remote_device.__repr__c                 C   s.   t |to| j|jko| j|jko| j|jkS N)r   r   r
   r   r   )r   otherr   r   r   __eq__r   s   


z_remote_device.__eq__c                 C   s   t | jt | jA t | jA S r'   )hashr
   r   r   r"   r   r   r   __hash__y   s   z_remote_device.__hash__)__name__
__module____qualname____doc__r   r   r   r   staticmethodr   r#   r   r	   r&   r)   r+   r   r   r   r   r      s    2
r   )r   r   r   r   r   r   <module>   s   