
    [Thr%              	       d   % S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
Jr  / SQr\R                  " \5      r " S S5      r " S S	\ R"                  5      r " S
 S\ R"                  5      r " S S\ R"                  5      rSq\\   \S'   S\4S jr\ SS\S\\   S\\   4S jj5       rg)    N)contextmanager)getframeinfostack)AnyOptional)TimerRequestTimerClientRequestQueueTimerServer	configureexpiresc                   <    \ rS rSrSr/ SQrS\S\S\4S jr	S r
S	rg
)r      a  
Data object representing a countdown timer acquisition and release
that is used between the ``TimerClient`` and ``TimerServer``.
A negative ``expiration_time`` should be interpreted as a "release"
request.

.. note:: the type of ``worker_id`` is implementation specific.
          It is whatever the TimerServer and TimerClient implementations
          have on to uniquely identify a worker.
	worker_idscope_idexpiration_timer   r   r   c                 (    Xl         X l        X0l        g Nr   )selfr   r   r   s       [/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributed/elastic/timer/api.py__init__TimerRequest.__init__*   s    " .    c                     [        U[        5      (       aY  U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ g)NF)
isinstancer   r   r   r   )r   others     r   __eq__TimerRequest.__eq__/   sY    e\**%//1 BMMU^^3B((E,A,AA
 r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   strfloatr   r   __static_attributes__ r   r   r   r      s-    	 =I/# / /u /
r   r   c                   x    \ rS rSrSr\R                  S\S\SS4S j5       r	\R                  S\4S j5       r
S	rg)
r	   9   z_
Client library to acquire and release countdown timers by communicating
with the TimerServer.
r   r   returnNc                     g)z
Acquires a timer for the worker that holds this client object
given the scope_id and expiration_time. Typically registers
the timer with the TimerServer.
Nr)   )r   r   r   s      r   acquireTimerClient.acquire?       r   c                     g)z
Releases the timer for the ``scope_id`` on the worker this
client represents. After this method is
called, the countdown timer on the scope is no longer in effect.
Nr)   )r   r   s     r   releaseTimerClient.releaseG   r0   r   r)   )r    r!   r"   r#   r$   abcabstractmethodr&   r'   r.   r2   r(   r)   r   r   r	   r	   9   sW    
 	 e    	  r   r	   c                   ~    \ rS rSrSr\R                  S\4S j5       r\R                  S\S\	S\
\   4S j5       rSrg	)
r
   P   z;
Consumer queue holding timer acquisition/release requests
r,   c                     g)a  
Returns the size of the queue at the time this method is called.
Note that by the time ``get`` is called the size of the queue
may have increased. The size of the queue should not decrease
until the ``get`` method is called. That is, the following assertion
should hold:

size = q.size()
res = q.get(size, timeout=0)
assert size == len(res)

-- or --

size = q.size()
res = q.get(size * 2, timeout=1)
assert size <= len(res) <= size * 2
Nr)   r   s    r   sizeRequestQueue.sizeU   r0   r   r:   timeoutc                     g)zh
Gets up to ``size`` number of timer requests in a blocking fashion
(no more than ``timeout`` seconds).
Nr)   )r   r:   r<   s      r   getRequestQueue.geti   r0   r   r)   N)r    r!   r"   r#   r$   r4   r5   intr:   r'   listr   r>   r(   r)   r   r   r
   r
   P   s\     	c  & 	 e \0B  r   r
   c                   V  ^  \ rS rSrSr SS\S\S\4U 4S jjjr\	R                  S\\   SS	4S
 j5       r\	R                  S\\   SS	4S j5       r\	R                  S\S\\\\   4   4S j5       r\	R                  S\S\4S j5       rS\S\4S jrS rS rS rSS jrSS jrSrU =r$ )r   q   z
Entity that monitors active timers and expires them
in a timely fashion. This server is responsible for
reaping workers that have expired timers.
request_queuemax_intervaldaemonc                 b   > [         TU ]  5         Xl        X l        X0l        SU l        SU l        g)z
:param request_queue: Consumer ``RequestQueue``
:param max_interval: max time (in seconds) to wait
                     for an item in the request_queue
:param daemon: whether to run the watchdog thread as a daemon
NF)superr   _request_queue_max_interval_daemon_watchdog_thread_stop_signaled)r   rD   rE   rF   	__class__s       r   r   TimerServer.__init__x   s1     	+)<@#r   timer_requestsr,   Nc                     g)z
Processes the incoming timer requests and registers them with the server.
The timer request can either be a acquire-timer or release-timer request.
Timer requests with a negative expiration_time should be interpreted
as a release-timer request.
Nr)   )r   rP   s     r   register_timersTimerServer.register_timers   r0   r   
worker_idsc                     g)z1
Clears all timers for the given ``worker_ids``.
Nr)   )r   rT   s     r   clear_timersTimerServer.clear_timers   r0   r   deadlinec                     g)z
Returns all expired timers for each worker_id. An expired timer
is a timer for which the expiration_time is less than or equal to
the provided deadline.
Nr)   )r   rX   s     r   get_expired_timersTimerServer.get_expired_timers   r0   r   r   c                     g)z
Reaps the given worker. Returns True if the worker has been
successfully reaped, False otherwise. If any uncaught exception
is thrown from this method, the worker is considered reaped
and all associated timers will be removed.
Nr)   r   r   s     r   _reap_workerTimerServer._reap_worker   r0   r   c                 p     U R                  U5      $ ! [         a    [        R                  S5         gf = f)zp
Wraps ``_reap_worker(worker_id)``, if an uncaught exception is
thrown, then it considers the worker as reaped.
ziUncaught exception thrown from _reap_worker(), check that the implementation correctly catches exceptionsT)r^   	Exceptionlogger	exceptionr]   s     r   _reap_worker_no_throw!TimerServer._reap_worker_no_throw   s@    
	$$Y// 	M 	s    55c                     U R                   (       d%   U R                  5         U R                   (       d  M$  g g ! [         a    [        R	                  S5         N6f = f)NzError running watchdog)rM   _run_watchdogra   rb   rc   r9   s    r   _watchdog_loopTimerServer._watchdog_loop   sJ    %%;""$ %%%  ;  !9:;s   8 AAc                 ^   [        SU R                  R                  5       5      nU R                  R                  XR                  5      nU R                  U5        [        R                  " 5       n[        5       nU R                  U5      R                  5        H  u  pV[        R                  SUU R                  U5      5        U R                  U5      (       a)  [        R                  SU5        UR                  U5        Mj  [        R                  SU5        M     U R!                  U5        g )N   z*Reaping worker_id=[%s]. Expired timers: %szSuccessfully reaped worker=[%s]z7Error reaping worker=[%s]. Will retry on next watchdog.)maxrI   r:   r>   rJ   rR   timesetrZ   itemsrb   info_get_scopesrd   adderrorrV   )r   
batch_sizerP   nowreaped_worker_idsr   expired_timerss          r   rg   TimerServer._run_watchdog   s    D//4467
,,00=O=OP^,iikE)-)@)@)E)K)K)M%IKK<  0
 )))44=yI!%%i0My *N 	+,r   c                 D    U Vs/ s H  o"R                   PM     sn$ s  snf r   )r   )r   rP   rs      r   rq   TimerServer._get_scopes   s    $23Nq

N333s   c                 <   [         R                  S[        U 5      R                  U R                  U R
                  5        [        R                  " U R                  U R
                  S9U l	        [         R                  S5        U R                  R                  5         g )Nz)Starting %s... max_interval=%s, daemon=%s)targetrF   zStarting watchdog thread...)rb   rp   typer    rJ   rK   	threadingThreadrh   rL   startr9   s    r   r   TimerServer.start   ss    7JLL		
 !* 0 0&&t||!
 	12##%r   c                 4   [         R                  S[        U 5      R                  5        SU l        U R
                  (       aB  [         R                  S5        U R
                  R                  U R                  5        S U l        g [         R                  S5        g )NzStopping %sTzStopping watchdog thread...z)No watchdog thread running, doing nothing)rb   rp   r~   r    rM   rL   joinrJ   r9   s    r   stopTimerServer.stop   sg    M4:#6#67"  KK56!!&&t'9'9:$(D!KKCDr   )rK   rJ   rI   rM   rL   )T)r,   N)r    r!   r"   r#   r$   r
   r'   boolr   r4   r5   rA   r   rR   rn   r   rV   dictr&   rZ   r^   rd   rh   rg   rq   r   r   r(   __classcell__)rN   s   @r   r   r   q   s    PT$)$9>$HL$ $  	d<.@ T   	s3x D  
 	5 T#tL?Q:Q5R   	c d  s t ;-*4&E Er   r   _timer_clienttimer_clientc                 b    U q [        R                  S[        [         5      R                  5        g)zE
Configures a timer client. Must be called before using ``expires``.
zTimer client configured to: %sN)r   rb   rp   r~   r    )r   s    r   r   r      s#    
 !M
KK0$}2E2N2NOr   afterscopeclientc              #   Z  #    Uc  [         c  [        S5      e[         nUc4  [        [        5       S   S   5      nUR                   SUR
                   3n[        R                  " 5       U -   nUR                  X5         Sv   UR                  U5        g! UR                  U5        f = f7f)a  
Acquires a countdown timer that expires in ``after`` seconds from now,
unless the code-block that it wraps is finished within the timeframe.
When the timer expires, this worker is eligible to be reaped. The
exact meaning of "reaped" depends on the client implementation. In
most cases, reaping means to terminate the worker process.
Note that the worker is NOT guaranteed to be reaped at exactly
``time.now() + after``, but rather the worker is "eligible" for being
reaped and the ``TimerServer`` that the client talks to will ultimately
make the decision when and how to reap the workers with expired timers.

Usage::

    torch.distributed.elastic.timer.configure(LocalTimerClient())
    with expires(after=10):
        torch.distributed.all_reduce(...)
Nz5Configure timer client before using countdown timers.rk   r   #)	r   RuntimeErrorr   r   filenamelinenorm   r.   r2   )r   r   r   caller
expirations        r   r   r      s     * ~ VWW}egajm,??#1V]]O4u$J
NN5%uus   A<B+?B B+B((B+)NN)r4   loggingr   rm   
contextlibr   inspectr   r   typingr   r   __all__	getLoggerr    rb   r   ABCr	   r
   r   r   __annotations__r   r'   r&   r   r)   r   r   <module>r      s        % '   
		8	$ :#'' .377 ByE#'' yEx (,x$ +PK P OS!!!#!7?7L! !r   