
    shm                     ~   S SK Jr  S SKJr  S SKJr  S SKJrJr  S SK	r	\	R                  R                  rSrSrSrS	rS
r/ SQr\S\4S j5       rS\S\S\4S jr SS\S\S\S\S\S\\   4S jjrS\S\S\S\\/\4   S\S\\\      4S jrS\S\S\4S jr    SS\S\S\S\\   S\\\/\4      S\SS4S jjrg)    )Iterable)contextmanager)	timedelta)CallableOptionalNz/num_membersz/last_memberz/TRACEz/TRACING_GATE   )store_timeoutget_allsynchronizebarriertimeoutc              #   ~   #    U R                   nU R                  [        US95        Sv   U R                  U5        g7f)z
This sets the timeout and then restores the old timeout when the context
manager exits.

Args:
    store: the store to set the timeout on
    timeout: the timeout to set
secondsN)r   set_timeoutr   )storer   old_timeouts      y/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/elastic/utils/store.pyr	   r	      s4      --K	i01		k"s   ;=rankprefix
world_sizec                     U R                  [        U5       Vs/ s H  oB U 3PM
     sn5      n[        U UU S3S9nUS:X  a  U R                  U/5        U$ s  snf )a4  
Given a store and a prefix, the method goes through the array of keys
of the following format: ``{prefix}{idx}``, where idx is in a range
from 0 to size, and tries to retrieve the data.

The Rank0 process waits at the end to make sure all other processes
finished the procedure before exiting.

Usage

::

 values = get_all(store, "torchelastic/data", 3)
 value1 = values[0]  # retrieves the data for key torchelastic/data0
 value2 = values[1]  # retrieves the data for key torchelastic/data1
 value3 = values[2]  # retrieves the data for key torchelastic/data2

z	/finishedr   r   
key_prefixr   )	multi_getrange_barrier_nonblockingwait)r   r   r   r   idxdata_arrbarrier_keys          r   r
   r
   /   sm    & E*<MN<MS(3% 0<MNOH&XY'K
 qy 	

K=!O  Os   Adatar   returnc                     [        X5         U R                  U U 3U5        [        XXC5      nUsSSS5        $ ! , (       d  f       g= f)a<  
Synchronizes ``world_size`` agents between each other using the underlying c10d store.
The ``data`` will be available on each of the agents.

Note: The data on the path is not deleted, as a result there can be stale data if
    you use the same key_prefix twice.

Time complexity: O(N) per worker, O(N^2) globally.
N)r	   setr
   )r   r"   r   r   r   r   
agent_datas          r   r   r   R   s>    " 
u	&		ZL'.U*A
 
'	&	&s	   $:
Arank_decodertrace_timeoutc                    ^ ^^^^ T R                  T U [         3S5        UUU UU4S jnUUU 4S jnUS:X  a#  U" 5       nT R                  T [         3S5        U$ U" 5       $ )N<val_ignored>c                  \  > [        5       n Sn[        ST5       Ha  nU[        :  a    U $  US:X  a&  TR                  T U [         3/[        TS95        M=  TR                  T U [         3/[        SS95        Mc     U $ ! [         a     US-  nU R                  T" U5      5         M  f = f)Nr      r   )milliseconds)r%   r   _MAX_TRACE_MISSING_RANKSr   _TRACEr   DistStoreErroradd)missing_rank_inforanks_missingir   r'   r   r(   r   s      r   _find_missing_ranks9_try_detecting_missing_ranks.<locals>._find_missing_rankss   s    Eq*%A  88 ! 
7 A%JJ&<s6(34i6V
 JJ:,qc& :;YTU=VW &  !  " 7"!%%l1o67s   *B$B&B+*B+c                  t   >  TR                  T  [         3/5        ST" S5       S3/$ ! [         a     g f = f)Nz[<check rank 0 (r   z) for missing rank info>])r   _TRACING_GATEr0   )r   r'   r   s   r   _checkin._try_detecting_missing_ranks.<locals>._checkin   sJ    	JJ:,}o678&|A&77PQRR 		s   &* 
77r   )r%   r/   r8   )	r   r   r   r   r'   r(   r5   r9   r2   s	   ``` ``   r   _try_detecting_missing_ranksr;   i   sf     
IITF6(+_=! !* qy/1		ZL0/B  z    c                 |    U[         -   nU[        -   nU R                  US5      nXQ:X  a  U R                  US5        U$ )ze
Does all the non-blocking operations for a barrier and returns the final key
that can be waited on.
r,   r*   )_NUM_MEMBERS_LAST_MEMBER_CHECKINr1   r%   )r   r   r   num_members_keylast_member_keyr   s         r   r   r      sC    
 !</O #77O
))OQ
'C
		/?3r<   barrier_timeoutrank_tracing_decoderc                 t   Uc
  Ub   S5       e[        X5         [        XUS9n U R                  U/5         SSS5        g! [         a[  nUc  Ue[	        U UUUU=(       d    S U5      n	U	b2  [        SR                  UUUSSR                  U	5       S3U5      5      SeUeSnAff = f! , (       d  f       g= f)	aG  
A global lock between agents. This will pause all workers until at least
``world_size`` workers respond.

This uses a fast incrementing index to assign waiting ranks and a success
flag set by the last worker.

Time complexity: O(1) per worker, O(N) globally.

Optionally, passing rank will enable tracing of missing ranks on timeouts.
`rank_tracing_decoder` lambda arg can be used to convert rank data
into a more meaninful information at an app level (e.g. hostname).

Note: Since the data is not removed from the store, the barrier can be used
    once per unique ``key_prefix``.
Nz!Tracing requires rank informationr   c                     [        U 5      $ )N)str)xs    r   <lambda>barrier.<locals>.<lambda>   s    s1vr<   ziTimed out waiting on barrier on rank {}, for key prefix: {} (world_size={}, missing_ranks={}, timeout={})[z, ])r	   r   r   r0   r;   formatjoin)
r   r   r   rB   r   rC   r(   rA   emissing_rankss
             r   r   r      s    4 |#+P-PP+	u	..:
	JJ() 
/	.  	| <(>-=!! !,(ddjdj &&		- 89;+e	  	  G1	 
/	.s)   B)A
B&AB!!B&&B))
B7),  )rP   NN
   )collections.abcr   
contextlibr   datetimer   typingr   r   torch_C_DistStoreErrorr0   r>   r?   r/   r8   r.   __all__floatr	   intrF   r
   byteslistr   r;   r   r    r<   r   <module>r_      s   % %  %  ))% 	  A #% # #    c  s  R 
  	
   
%[.,, , 	,
 C5#:&, , hsm,^C S S & !;?;; ; 	;
 3-; #8SE3J#78; ; 
;r<   