
    Ǆg                     0    d dl mZmZ d dlZ G d d      Zy)    )OptionalUnionNc                       e Zd ZdZdeeej                  f   fdZe	d        Z
dee   fdZdee   fdZdej                  fdZd	 Zd
 Zd Zy)_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                 t   d| d}d | _         d | _        d | _        t        |t        j
                        r|| _        nt        |t              r}|j                  d      }t        |      dk(  r|\  | _         | _        net        |      dk(  r5t        j                  |d         r|d   | _        n4|d   | _         d| _        n"t        |      t        dt        |             | j                   | j                   st        |      t	        j
                  | j                        | _        | j                   | j                   j                  d	      }t        |      dk(  rA|d   d
k(  r.|d   j                         rt        |d         | _        d | _         y t        |      t        |      dkD  rt        |      y y )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fieldss       g/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributed/remote_device.py__init__z_remote_device.__init__   s   -m_ =N N 	 !
@DmU\\2(DLs+"((-F6{a28/!4<V!!88C#)!9DL(.q	D%#(DL -->tM?R>STUU (1B1B[)) ||DLL1 (&&,,S1F6{a!9&6!9+<+<+>!$VAYDJ(,D%$[11Vq -- ! )    c                 N    	 t        j                  |        y# t        $ r Y yw xY w)NTF)r   r   	Exception)r   s    r!   r   z%_remote_device._is_valid_local_deviceH   s(    	LL  		s    	$$returnc                     | j                   S )zlReturn the name of remote worker representing the remote device and ``None`` if no worker name is available.)r   r   s    r!   worker_namez_remote_device.worker_nameQ   s       r#   c                     | j                   S )z
        Returns the rank of remote worker representing the remote device.
        Returns ``None`` if no rank is available.
        )r   r(   s    r!   r   z_remote_device.rankU   s    
 zzr#   c                     | j                   S )z-Return the local device on the remote worker.)r   r(   s    r!   r   z_remote_device.device\   s    ||r#   c                 \   | j                   d| j                  | j                   d| j                    S | j                  d| j                   d| j                    S t        | j                         S | j                  | j                   S | j                  | j                   S t	        d      )Nr	   zrank:zInvalid state!)r   r   r   r   RuntimeErrorr(   s    r!   __repr__z_remote_device.__repr__`   s    <<#  ,++,Adll^<<'tzzl!DLL>::4<<((  ,++,-'**&"#344r#   c                     t        |t              xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S N)r   r   r   r   r   )r   others     r!   __eq__z_remote_device.__eq__p   sT    %0 
!3!33 *-*

ekk)	
r#   c                     t        | j                        t        | j                        z  t        | j                        z  S r0   )hashr   r   r   r(   s    r!   __hash__z_remote_device.__hash__w   s/    D%%&dll);;d4::>NNNr#   N)__name__
__module____qualname____doc__r   r   r   r   r"   staticmethodr   r   r)   r   r   r.   r2   r5    r#   r!   r   r      sv    "-.eC,=&> -.^  !Xc] !hsm  5 
Or#   r   )typingr   r   r   r   r;   r#   r!   <module>r=      s    " qO qOr#   