
    [Th                     j    S SK r S SKrS SKJrJr  S SKJr   " S S5      r " S S\5      r	S
S jr
S	 rg)    N)EmptyQueue)get_device_contextc                   *    \ rS rSrSS jrS rS rSrg)ClosureHandler	   Nc                     g N )selfs    K/var/www/auris/envauris/lib/python3.13/site-packages/torch/_lazy/closure.py__init__ClosureHandler.__init__
   s        c                     U" 5         g)z>Run closure function

Args:
closure: callable function to run
Nr   )r   closures     r   runClosureHandler.run   s	     		r   c                 8    U H  nU R                  U5        M     g r
   )r   )r   closuresr   s      r   __call__ClosureHandler.__call__   s    GHHW  r   r   )returnN)__name__
__module____qualname____firstlineno__r   r   r   __static_attributes__r   r   r   r   r   	   s    r   r   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )AsyncClosureHandler   aM  Handler for Asynchronous Step Closures
Args:
    max_queue_size: The maximum length of the closure queue after which
    the training loop will block until closures are evaluated.
    By default, a reasonable limit of a maximum of 100 on the queue.
    This value can be set using the `XLA_MAX_ASYNC_QUEUE` environment
    variable.
c                 $  > [         TU ]  5         [        [        [        R
                  R                  SU5      5      5      U l        [        5       U l        [        R                  " 5       U l        [        R                  " 5       U l        S U l        g )NLTC_MAX_ASYNC_QUEUE)superr   r   intosenvironget_closure_queue_closure_exception	threadingLock_closure_lockEvent_closure_event_loop_finished_closure_event_loop)r   max_queue_size	__class__s     r   r   AsyncClosureHandler.__init__$   se    %*

4nEF&
 */&^^-,5OO,=)#' r   c                    ^  T R                   c:  U 4S jn[        R                  " US9T l         T R                   R                  5         gg)z'Start closure event loop if not startedNc                    >   TR                   R                  SSS9n U " 5         TR                   R                  5         M>  ! [         ap    TR                     TR                   R                  5       (       a%  TR                  R                  5          S S S 5         g  S S S 5         Mi  ! , (       d  f        Nx= f[         a%  nTR                  R                  U5         S nAg S nAff = f)NT   )blocktimeout)r)   r(   	task_done
EmptyQueuer-   emptyr/   set	Exceptionr*   put)r   er   s     r   
event_loop8AsyncClosureHandler.start_event_loop.<locals>.event_loop2   s    "&"5"5"9"9a"9"P	++557	 
 & '!//#2288:: $ A A E E G & 0/: 0/// % //33A6s;   ;A C(;B(C(C((
B7	2C(7C(C##C()target)r0   r+   Threadstart)r   r@   s   ` r   start_event_loop$AsyncClosureHandler.start_event_loop.   s@    ##+  (1'7'7z'JD$$$**,' ,r   c                 v   U R                      U R                  R                  USS9  U R                  b  U R                  R	                  5       (       d&   U R
                  R                  SS9n[        S5      UeS S S 5        g ! [         a    S U l        U R                  5          N,f = f! , (       d  f       g = f)NT)r7   FzBCannot run asynchronous closure due to previously raised exception)
r-   r)   r>   r0   is_aliver*   r(   RuntimeErrorr:   rE   )r   r   r?   s      r   r   AsyncClosureHandler.runE   s    ##G4#8((0//88::,//33%3@A&\   " ,/3D,))+,  s*   AB*%B!B'$B*&B''B**
B8)r0   r/   r*   r-   r)   )d   )
r   r   r   r   __doc__r   rE   r   r   __classcell__)r2   s   @r   r    r       s    (-., ,r   r    c                    ^  [        5       nU(       a  SOSn[        X4S5      nUc  / n[        X4U5        UR                  U4U 4S jj5        g)a  Adds a closure to the list of the ones to be run at the end of the step.
Many times during model training there is the need to print/report (print to
console, post to tensorboard, etc...) information which require the content of
intermediary tensors to be inspected.
Inspecting different tensors content in different points of the model code
requires many executions and typically causes performance issues.
Adding a step closure will ensure that it will be run after the barrier, when
all the live tensors will be already materialized to device data.
Live tensors which will include the ones captured by the closure arguments.
So using `add_step_closure()` will ensure a single execution will be
performed, even when multiple closures are queued, requiring multiple tensors
to be inspected.
Step closures will be run sequentially in the order they have been queued.
Note that even though using this API the execution will be optimized, it is
advised to throttle the printing/reporting events once every N steps.
Args:
  closure (callable): The function to be called.
  args (tuple): The arguments to be passed to the closure.
  run_async: If True, run the closure asynchronously.
async_step_closuresstep_closuresNc                    > T" U 6 $ r
   r   )ar   s    r   <lambda>"add_step_closure.<locals>.<lambda>q   s	    r   )r   getattrsetattrappend)r   args	run_asyncdevctxclosures_typerP   s   `     r   add_step_closurer\   V   sI    *  !F-6)OMF48M}5$34r   c                     [        5       n [        U SS 5      nUb/  / U l        [        U SS 5      nUc  [        5       nX l        U" U5        [        U SS 5      nUb/  / U l        [        U SS 5      nUc  [        5       nX@l        U" U5        U $ )NrO   async_closure_handlerrP   closure_handler)r   rU   rO   r    r^   rP   r   r_   )rZ   rO   r^   rP   r_   s        r   run_step_closuresr`   t   s    !F!&*?F&%'" '0G N ($7$9!+@(12FOT:M !!&*;TB",.O%4"&Mr   )r   F)r&   r+   queuer   r:   r   torch._lazy.device_contextr   r   r    r\   r`   r   r   r   <module>rc      s2    	  , 9 "9,. 9,x5<r   