
    shW                        S SK r S SKrS SKrS SKJrJr  S SKJr  S SK	Js  J
r
  S SK	Jr  S SKJrJr  \R                  " 5       (       d#  \" S\R"                  S9  \R$                  " S 5        \S-   r     SS\S	\4S
 jjrSS jrSS\S\S\4S jjrSS\SS4S jjrS\\\4   4S jr SS\S\S\S\SS4
S jjrS\S\SS4S jrS\S\4S jrS r g)    N)partialwraps_rref_context_get_debug_info)FILE_SCHEMATEST_WITH_TSANz"c10d not available, skipping tests)filez{file_name}	setup_rpcclean_shutdownc                 h   ^ ^^^^ T c  [        [        TTTTS9$ [        T 5      UUUU U4S j5       nU$ )aC  
We use this decorator for setting up and tearing down state since
MultiProcessTestCase runs each `test*` method in a separate process and
each process just runs the `test*` method without actually calling
'setUp' and 'tearDown' methods of unittest.

Note: pass the string representation of MessageTypes that should be used
with the faulty agent's send function. By default, all retriable messages
("RREF_FORK_REQUEST", "RREF_CHILD_ACCEPT", "RREF_USER_DELETE",
"CLEANUP_AUTOGRAD_CONTEXT_REQ") will use the faulty send (this default is
set from faulty_rpc_agent_test_fixture.py).
)r
   r   faulty_messagesmessages_to_delayc                   > SS K Js  Js  Jn  SUl        U R
                  U l        U R                  TT5        U R                  nT
(       a  [        (       a7  [        R                  R                  S-  Ul        S[        R                  l        [        R                  " SU R
                  S 3U R                  U R
                  U R                   US9  T	" U /UQ70 UD6nT
(       a  [        R"                  " TS9  U$ )	Nr   F   <   workerd)namebackendrank
world_sizerpc_backend_options)graceful)torch.distributed.rpc.apidistributedrpcapi_ignore_rref_leakr   	worker_idsetup_fault_injectionr   r   	constantsDEFAULT_RPC_TIMEOUT_SECrpc_timeoutDEFAULT_SHUTDOWN_TIMEOUTinit_rpcrpc_backendr   shutdown)selfargkwargsr   r   return_valuer   r   r   old_test_methodr
   s         v/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/testing/_internal/dist_utils.pynew_test_method"dist_init.<locals>.new_test_method7   s     	0/ %""?4EF"66~25--2W2WZ[2[#/9;6LLdii]+((YY??$7 't<c<V<LL.1    )r   	dist_initr   )r,   r
   r   r   r   r.   s   ````` r-   r1   r1      sM    2 )+/
 	
 ?  > r0   returnc                      g )N r4   r0   r-   noopr5   Z   s    r0   r   expected_error_regexc                      [         R                  " SU  3[        SS9  [        R                  " S5        M6  ! [
         a=  n[        R                  " U[        U5      S9(       a  [        U5      s SnA$  SnANDSnAff = f)a'  
Loops until an RPC to the given rank fails. This is used to
indicate that the node has failed in unit tests.
Args:
rank (int): Rank of the node expected to fail
expected_error_regex (optional, str): Regex of exception message expected. Useful to ensure a specific failure
occurs, not just any.
r   r4   )args皙?)patternstringN)	r   rpc_syncr5   timesleep	Exceptionresearchstr)r   r6   es      r-   wait_until_node_failurerD   ^   sf     	LL6$$R8JJsO   	yy!5c!fE1v F	s   38 
A?-A:/A?:A?timeoutc                     [         R                   " 5       n [        5       n[        US   5      n[        US   5      nUS:X  a  US:X  a  g[         R                  " S5        [         R                   " 5       U-
  U :  a  [	        SU SU S35      eMy  )	a  
The RRef protocol holds forkIds of rrefs in a map until those forks are
confirmed by the owner. The message confirming the fork may arrive after
our tests check whether this map is empty, which leads to failures and
flaky tests. to_here also does not guarantee that we have finished
processind the owner's confirmation message for the RRef. This function
loops until the map is empty, which means the messages have been received
as processed. Call this function before asserting the map returned by
_get_debug_info is empty.
num_pending_futuresnum_pending_usersr   r9   z:Timed out waiting to flush pending futures and users, had z pending futures and z pending usersN)r=   r   intr>   
ValueError)rE   start
debug_inforG   rH   s        r-   ,wait_until_pending_futures_and_users_flushedrM   p   s     IIKE
13
!*-B"CD
+> ?@!#(9Q(>

399;(*++@AR@SSac  r0   c                  0    [        5       n U S   nU S   nX4$ )zZ
Retrieves number of OwnerRRefs and forks on this node from
_rref_context_get_debug_info.
num_owner_rrefs	num_forksr   )rref_dbg_info
num_ownersrP   s      r-   get_num_owners_and_forksrS      s*    
 12M01Jk*I  r0   rR   rP   c                 Z   [         R                   " 5       n [        R                  " [        U5      [        SSS9u  pV[        U5      n[        U5      nXP:X  a  Xa:X  a  g[         R                  " S5        [         R                   " 5       U-
  U:  a  [        SU SU  SU S	U SU S
35      eM  )zz
Waits until timeout for num_forks and num_owners to exist on the rank. Used
to ensure proper deletion of RRefs in tests.
r4   r   )r8   rE   N   zTimed out waiting z	 sec for z owners and z forks on rank, had z forks)r=   r   r<   worker_namerS   rI   r>   rJ   )rR   rP   r   rE   rK   num_owners_on_ranknum_forks_on_ranks          r-   #wait_until_owners_and_forks_on_rankrY      s     IIKE
037b!1
- !!34 12+0A0N

199;($WIYzl,yk Z*+<8I7J&R  r0   r   c                 h    [         R                  " 5       (       d  [         R                  " SU UUS9  g g )Ngloo)r   init_methodr   r   )distis_initializedinit_process_group)r\   r   r   s      r-   initialize_pgr`      s0      #!		
 !r0   c                     SU  3$ )Nr   r4   )r   s    r-   rV   rV      s    D6?r0   c                 ^    U  Vs/ s H  o!UR                   ;   d  M  UPM     snS   nU$ s  snf )a5  
Returns the first event that matches partial_event_name in the provided
function_events. These function_events should be the output of
torch.autograd.profiler.function_events().

Args:
function_events: function_events returned by the profiler.
event_name (str): partial key that the event was profiled with.
r   )r   )function_eventspartial_event_nameevents      r-   get_function_eventrf      s2     !0Tu3SUTUVWEL Us   **)NTTNN)r2   N)z.*)   )!r@   sysr=   	functoolsr   r   torch.distributedr   r]   torch.distributed.rpcr   r   $torch.testing._internal.common_utilsr   r   is_availableprintstderrexitINIT_METHOD_TEMPLATEboolr1   r5   rI   rB   rD   rM   tuplerS   rY   r`   rV   rf   r4   r0   r-   <module>rt      s2   
 
  $   # # > L 	
.SZZ@HHQK #]2  BB BJ	# S C $# t 4!%S/ ! @B #+.9<	0
S 
c 
d 
c c r0   