
    [Th                         S r SSKrSSKJr  SSKJr  SSKJrJr  SSK	J
r
JrJr  SSKJr  \ " S S	5      5       r\" 5       rS
\/ S4   S\/ S4   4S jrS
\/ S4   S\/ S4   4S jrg)a  
This module provides callback management functionality for TorchDynamo's compilation process.

It implements a thread-safe system for registering, managing and executing callbacks that run
at the start and end of TorchDynamo compilations. Key features include:

- Registration and deregistration of compilation callbacks
- Thread-safe callback handling with proper locking mechanisms
- Prevention of duplicate callback execution when configured
- Decorator utilities for easy callback registration
- Context manager for controlled callback lifecycle

The module centers around the CompilationCallbackHandler class which maintains separate
lists for start and end callbacks, manages their execution order, and ensures thread-safety.
Utility decorators @on_compile_start and @on_compile_end provide a convenient way to
register compilation hooks.

Example usage:
    @on_compile_start
    def my_start_callback():
        print("Starting compilation")

    @on_compile_end
    def my_end_callback():
        print("Compilation complete")
    N)	Generator)contextmanager)	dataclassfield)AnyCallableOptional)justknobs_checkc                      \ rS rSr% \" \S9r\\/ S4      \S'   \" \S9r	\\/ S4      \S'   \" SSSS9r
\\   \S'   \" S	SSS9r\\S
'   \" \R                   SSS9r\R                   \S'   S\/ S4   S\/ S4   4S jrS\/ S4   S\/ S4   4S jrS\/ S4   SS4S jrS\/ S4   SS4S jrSS jrSS jr\S\4S j5       r\S\S\\4   4S j5       rSS jrSrg)CompilationCallbackHandler%   )default_factoryNstart_callbacksend_callbacksF)defaultinitrepr8_CompilationCallbackHandler__prevent_duplicate_callbacksr   6_CompilationCallbackHandler__pending_callbacks_counter)r   r   r   ;_CompilationCallbackHandler__pending_callbacks_counter_lockcallbackreturnc                 <    U R                   R                  U5        U$ )z
Register a callback function to be called when the compilation starts.

Args:
- callback (Callable): The callback function to register.
)r   appendselfr   s     N/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/callback.pyregister_start_callback2CompilationCallbackHandler.register_start_callback2   s     	##H-    c                 <    U R                   R                  U5        U$ )z
Register a callback function to be called when the compilation ends.

Args:
- callback (Callable): The callback function to register.
)r   r   r   s     r   register_end_callback0CompilationCallbackHandler.register_end_callback>   s     	!!(+r    c                 :    U R                   R                  U5        g)zm
Remove a registered start callback function.

Args:
- callback (Callable): The callback function to remove.
N)r   remover   s     r   remove_start_callback0CompilationCallbackHandler.remove_start_callbackH   s     	##H-r    c                 :    U R                   R                  U5        g)zk
Remove a registered end callback function.

Args:
- callback (Callable): The callback function to remove.
N)r   r%   r   s     r   remove_end_callback.CompilationCallbackHandler.remove_end_callbackQ   s     	!!(+r    c                 8    U R                    H
  nU" 5         M     g)z)
Execute all registered start callbacks.
N)r   r   s     r   run_start_callbacks.CompilationCallbackHandler.run_start_callbacksZ   s     ,,HJ -r    c                 8    U R                    H
  nU" 5         M     g)z'
Execute all registered end callbacks.
N)r   r   s     r   run_end_callbacks,CompilationCallbackHandler.run_end_callbacksa   s     **HJ +r    c                 T    U R                   c  [        S5      U l         U R                   $ )Nz*pytorch/dynamo:prevent_duplicate_callbacks)r   r
   r   s    r   prevent_duplicate_callbacks6CompilationCallbackHandler.prevent_duplicate_callbacksh   s,    --51@<2D. 111r    c              #   6  #    U R                   (       a   U R                     U R                  S:X  a  U R                  5         U =R                  S-  sl        SSS5        Sv   U R                     U R                  S:  d   S5       eU R                  S:X  a  U R	                  5         U =R                  S-  sl        SSS5        g U R                  5         Sv   U R	                  5         g! , (       d  f       N= f! , (       d  f       g= f! U R                     U R                  S:  d   S5       eU R                  S:X  a  U R	                  5         U =R                  S-  sl        SSS5        f ! , (       d  f       f = f= f! U R	                  5         f = f7f)zS
Context manager to install the callbacks and run them when the context is exited.
r      Nz1Pending callbacks counter cannot become negative.)r3   r   r   r,   r/   r2   s    r   install_callbacks,CompilationCallbackHandler.install_callbacksp   sN    
 ++:::771<0024494 ; ::;;a? K? 771<..04494 ;:)((*&&(% ;: ;:T::;;a? K? 771<..04494 ;:: &&(s}   FD 6C+D #F/AC<<	FF F+
C95D <
D
FFAE0'	F0
E>:FFFFc                 l    U R                   R                  5         U R                  R                  5         g)z!
Clear all registered callbacks.
N)r   clearr   r2   s    r   r:    CompilationCallbackHandler.clear   s(     	""$  "r    )__prevent_duplicate_callbacks)r   N) __name__
__module____qualname____firstlineno__r   listr   r   __annotations__r   r   r	   boolr   int	threadingLockr   r   r"   r&   r)   r,   r/   propertyr3   r   r   r   r7   r:   __static_attributes__ r    r   r   r   %   se   05d0KOT(2t8,-K.3D.IM4T*+I495u5!8D>  (-QU'OO7<!U8$inn 
 T*
	"d(	
hr4x.@ XbRVhEW .hr4x.@ .T .,HRX,> ,4 , 2T 2 2 )9T3^#< ) )4#r    r   r   r   c                 0    [         R                  U 5        U $ )zM
Decorator to register a callback function for the start of the compilation.
)callback_handlerr   r   s    r   on_compile_startrM      s     ,,X6Or    c                 0    [         R                  U 5        U $ )zK
Decorator to register a callback function for the end of the compilation.
)rK   r"   rL   s    r   on_compile_endrO      s     **84Or    )__doc__rE   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   r	   torch._utils_internalr
   r   rK   rM   rO   rI   r    r   <module>rV      s   6  % % ( * * 1 j# j# j#Z ./ xD1 hr4x6H Xb$h/ HRX4F r    