
    9i                        S r SSK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   " S S\R                  5      r\ " S	 S
5      5       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Callablec                   $    \ rS rSrSrSrSrSrSrg)CallbackTrigger$                N)	__name__
__module____qualname____firstlineno__DYNAMOLAZY_BACKWARDTRITON_AUTOTUNINGCUDAGRAPH_RECORDING__static_attributes__r       V/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/callback.pyr
   r
   $   s    FMr   r
   c                   *    \ rS rSr% \\S'   \\S'   Srg)CallbackArgs/   callback_trigger
compile_idr   N)r   r   r   r   r
   __annotations__strr   r   r   r   r   r   /   s    %%Or   r   c            
          \ rS rSr% \" \S9r\\\/S4      \	S'   \" \S9r
\\\/S4      \	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S4S jrS\SS4S jr\S\S\S\S\\4   4S j5       rSS jrSrg)CompilationCallbackHandler5   )default_factoryNstart_callbacksend_callbacksr   F)defaultinitrepr6_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     r   register_start_callback2CompilationCallbackHandler.register_start_callback?   s     	##H-r   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(   r1   r2   s     r   register_end_callback0CompilationCallbackHandler.register_end_callbackK   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'   remover2   s     r   remove_start_callback0CompilationCallbackHandler.remove_start_callbackW   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:   r2   s     r   remove_end_callback.CompilationCallbackHandler.remove_end_callback`   s     	!!(+r   argsc                 :    U R                    H  nU" U5        M     g)z)
Execute all registered start callbacks.
N)r'   r3   r@   r.   s      r   run_start_callbacks.CompilationCallbackHandler.run_start_callbacksi   s     ,,HTN -r   c                 :    U R                    H  nU" U5        M     g)z'
Execute all registered end callbacks.
N)r(   rB   s      r   run_end_callbacks,CompilationCallbackHandler.run_end_callbacksp   s     **HTN +r   triggerr    c              #     #    [        X5      n U R                     U =R                  S-  sl        U R                  S:X  a  U R                  U5        SSS5        Sv   U R                     U R                  S:  d   S5       eU R                  S:X  a  U R	                  U5        U =R                  S-  sl        SSS5        g! , (       d  f       Nu= f! , (       d  f       g= f! U R                     U R                  S:  d   S5       eU R                  S:X  a  U R	                  U5        U =R                  S-  sl        SSS5        f ! , (       d  f       f = f= f7f)zS
Context manager to install the callbacks and run them when the context is exited.
r   Nr   z1Pending callbacks counter cannot become negative.)r   r-   r,   rC   rF   )r3   rH   r    r@   s       r   install_callbacks,CompilationCallbackHandler.install_callbacksw   s&     G0	66600A5033q8,,T2 7 6677!; G; 33q8**4000A50 76 76 766677!; G; 33q8**4000A50 766se   EC# 7CC# E*AC8	E
CC# 
C E#E0AE>	E
EEEc                     U R                   R                  5         U R                  R                  5         U R                  S:X  d   eg)z!
Clear all registered callbacks.
r   N)r'   clearr(   r,   )r3   s    r   rM    CompilationCallbackHandler.clear   s<     	""$  "//1444r   r   )r/   N)r   r   r   r   r   listr'   r   r   r!   r(   r,   int	threadingLockr-   r4   r7   r;   r>   rC   rF   r   r
   r"   r   r   rJ   rM   r   r   r   r   r$   r$   5   st   <ARV<WOT(L>4#789W:?PT:UM4,!567U',QU'OO7<!U8$inn 
 ,!56
	<.$&	'

 ,!56
	<.$&	'
.h~t7K.L .QU .,Hl^T5I,J ,t ,  l t  6&6476	4c>	"6 6,5r   r$   r.   r/   c                 0    [         R                  U 5        U $ )zM
Decorator to register a callback function for the start of the compilation.
)callback_handlerr4   r.   s    r   on_compile_startrV      s     ,,X6Or   c                 0    [         R                  U 5        U $ )zK
Decorator to register a callback function for the end of the compilation.
)rT   r7   rU   s    r   on_compile_endrX      s     **84Or   )__doc__enumrQ   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   Enumr
   r   r$   rT   rV   rX   r   r   r   <module>r`      s   6   % % (  dii    
 ^5 ^5 ^5B ./ ~t+,|nd"#~t+,|nd"#r   