a
    h                     @   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|| _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 ur| j st|t| j| _| j d u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   M/var/www/auris/lib/python3.9/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 0 d S )NTF)r   r   	Exception)r   r   r   r   r   H   s
    
z%_remote_device._is_valid_local_device)returnc                 C   s   | 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_nameQ   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   U   s    z_remote_device.rankc                 C   s   | j S )z-Return the local device on the remote worker.)r   r#   r   r   r   r   \   s    z_remote_device.devicec                 C   s   | j d urP| jd ur&| j d| j  S | jd urDd| j d| j  S t| j S n,| jd urb| j S | jd urt| j S tdd S )Nr   zrank:zInvalid state!)r   r   r   r   RuntimeErrorr#   r   r   r   __repr__`   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__p   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__w   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   