
    Vh                         d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZddl	m
Z
  ej        e          Z edd          Zd Zd Zdd
Zd Zd Zd ZdS )    )
namedtupleN   )get_xpDistribSpecz5rank world_size local_rank node_rank num_nodes sourcec                     t                      } | j        dk    rdS dt          j        vrdt          j        v s
J d            t          j        d         }t	          j        d                                |gz   dd          j                                                                        }|d	         }|t          j        d<   d
t          j        vrt                      }|j
        }	 t          j                    }||j        z  }n# t          $ r Y nw xY wt          j        |          }|                    dd          }t%          |          t          j        d
<   dt          j        vret%          | j                  t          j        d<   t%          | j                  t          j        d<   t%          | j                  t          j        d<   dS dS )a  Calling this function will set the distributed environement
    including the master addr, master port etc. You shouldn't call
    this if you call `dora.distrib.init`, but it can be useful if you need to let
    some other framework handle the distributed initialization.
    r   NMASTER_ADDRSLURM_JOB_NODELISTzcase not handledzscontrol show hostnamesT)capture_outputcheckr   MASTER_PORTi N  i`  
WORLD_SIZERANK
LOCAL_RANK)get_distrib_spec
world_sizeosenvironsprunsplitstdoutdecoder   sigsubmititJobEnvironmentjob_idRuntimeErrorrandomRandomrandintstrrank
local_rank)	specnodelistnodesmaster_nodexpseedenvrngmaster_ports	            R/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/dora/distrib.pyset_distrib_envr.      s    D!BJ&&#rz1113E111:2306688H:E&*$8 8 88>vvxx 	Ah$/
=!BJ&&XX v	)++C CJDD  	 	 	D	 mD!!kk%//$'$4$4
=!2:%%#&t#7#7
<  ^^
6#&t#7#7
<    &%s   %D 
DDc                     dt           j        v rut          t           j        d                   } t          t           j        d                   }dt           j        v r t          t           j        d                   }n| }d}d}d}nV	 t          j                    }|j        } |j        }|j        }|j        }|j	        }d}n# t          $ r d} d}d}d}d}d}Y nw xY wt          | |||||          S )	zReturn information on the distributed setup, i.e. world size, rank etc.
    This can be used even before distributed training is initialized, which is useful for
    PytorchLightning for instance.
    r   r   r   r   r   r*   r   empty)r   r   intr   r   global_rank	num_tasksr#   node	num_nodesr   r   )r"   r   r#   	node_rankr5   sourcer*   s          r-   r   r   >   s
   
 rz!!2:f%&&L122
2:%%RZ566JJJ			 )++C ?DJJIIFF  	 	 	DJJIIFFF	 tZY	6RRRs   B> >CCncclc                 Z   t           j                                        rdS t                      }|j        dk    rt
                              d           dS t                      }t           j        	                                r%t           j        
                    |j                   n| dk    sJ |j        j        r(dt          j                            |j                  z   }nt%                       d}t           j                            | ||j        |j                   t
                              d|j        |j        |j        |j                   |j        j        rKt           j                                         t)                      d	k    r|j                                         dS dS dS )
z
    Initialize DDP.
    Nr   zworld_size is 1, skipping init.r8   zfile://zenv://)backendinit_methodr   r"   z*Distributed init: %d/%d (local %d) from %sr   )torchdistributedis_initializedr   r   loggerinfor   cudais_available
set_devicer#   dorause_rendezvousr   pathabspathrendezvous_filer.   init_process_groupr"   r7   barrierunlink)r:   r$   r(   r;   s       r-   initrL   a   s    '')) D!5666	Bz   !
do....&    	w "'//"2D"E"EE	((?Y	 )   
 KK4	4?DOT[B B B 
w (!!###66Q;;%%'''''	( (;    c                  &    t                      dk    S Nr   )r"    rM   r-   	is_masterrQ      s    66Q;rM   c                  ~    t           j                                        rt           j                                        S dS rO   )r<   r=   r>   get_rankrP   rM   r-   r"   r"      s3    ''))  ))+++qrM   c                  ~    t           j                                        rt           j                                        S dS )Nr   )r<   r=   r>   get_world_sizerP   rM   r-   r   r      s3    ''))  //111qrM   )r8   )collectionsr   loggingr   r   
subprocessr   r   r<   r(   r   	getLogger__name__r?   r   r.   r   rL   rQ   r"   r   rP   rM   r-   <module>r[      s    # " " " " "  				             		8	$	$ jJL L"8 "8 "8J S  S  SF!( !( !( !(H        rM   