
    {Yh                     H   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ  G d dej                  Ze G d	 d
                      Ze G d d                      Z e            Zdeegdf         deegdf         fdZdeegdf         deegdf         fdZdS )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                       e Zd ZdZdZdZdZdS )CallbackTrigger            N)__name__
__module____qualname__DYNAMOLAZY_BACKWARDTRITON_AUTOTUNINGCUDAGRAPH_RECORDING     h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/_dynamo/callback.pyr
   r
   $   s&        FMr   r
   c                   $    e Zd ZU eed<   eed<   dS )CallbackArgscallback_trigger
compile_idN)r   r   r   r
   __annotations__strr   r   r   r   r   /   s%         %%%%OOOOOr   r   c            
          e Zd ZU  ee          Zeeegdf                  ed<    ee          Z	eeegdf                  ed<    eddd          Z
eed<    eej        dd	          Zej        ed
<   deegdf         deegdf         fdZdeegdf         deegdf         fdZdeegdf         ddfdZdeegdf         ddfdZdeddfdZdeddfdZededededeef         fd            ZddZdS )CompilationCallbackHandler)default_factoryNstart_callbacksend_callbacksr   F)defaultinitrepr6_CompilationCallbackHandler__pending_callbacks_counter)r!   r%   r&   ;_CompilationCallbackHandler__pending_callbacks_counter_lockcallbackreturnc                 :    | j                             |           |S )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_callbackz2CompilationCallbackHandler.register_start_callback?   s      	##H---r   c                 :    | j                             |           |S )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_callbackz0CompilationCallbackHandler.register_end_callbackK   s      	!!(+++r   c                 :    | j                             |           dS )z
        Remove a registered start callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r"   remover-   s     r   remove_start_callbackz0CompilationCallbackHandler.remove_start_callbackW   s!     	##H-----r   c                 :    | j                             |           dS )z
        Remove a registered end callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r#   r3   r-   s     r   remove_end_callbackz.CompilationCallbackHandler.remove_end_callback`   s!     	!!(+++++r   argsc                 0    | j         D ]} ||           dS )z9
        Execute all registered start callbacks.
        N)r"   r.   r7   r)   s      r   run_start_callbacksz.CompilationCallbackHandler.run_start_callbacksi   s/     , 	 	HHTNNNN	 	r   c                 0    | j         D ]} ||           dS )z7
        Execute all registered end callbacks.
        N)r#   r9   s      r   run_end_callbacksz,CompilationCallbackHandler.run_end_callbacksp   s/     * 	 	HHTNNNN	 	r   triggerr   c              #   n  K   t          ||          }	 | j        5  | j        dk    r|                     |           | xj        dz  c_        ddd           n# 1 swxY w Y   dV  | j        5  | j        dk    s
J d            | j        dk    r|                     |           | xj        dz  c_        ddd           dS # 1 swxY w Y   dS # | j        5  | j        dk    s
J d            | j        dk    r|                     |           | xj        dz  c_        ddd           w # 1 swxY w Y   w xY w)zc
        Context manager to install the callbacks and run them when the context is exited.
        r   r   Nz1Pending callbacks counter cannot become negative.)r   r(   r'   r:   r<   )r.   r=   r   r7   s       r   install_callbacksz,CompilationCallbackHandler.install_callbacksw   s<      GZ00	66 6 63q88,,T22200A5006 6 6 6 6 6 6 6 6 6 6 6 6 6 6 EEE6 6 67!;;;G <;; 3q88**400000A5006 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 67!;;;G <;; 3q88**400000A5006 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6sf   C 1AC AC A C .ACCCD4AD(D4(D,,D4/D,0D4c                     | j                                          | j                                         | j        dk    sJ dS )z1
        Clear all registered callbacks.
        r   N)r"   clearr#   r'   )r.   s    r   rA   z CompilationCallbackHandler.clear   sH     	""$$$  """/1444444r   )r*   N)r   r   r   r   listr"   r   r   r   r#   r'   int	threadingLockr(   r/   r1   r4   r6   r:   r<   r   r
   r   r   r   r?   rA   r   r   r   r    r    5   s9        <AERV<W<W<WOT(L>4#789WWW:?%PT:U:U:UM4,!567UUU',uQU'O'O'OOOO7<u!U8 8 8$in   
 ,!56
	<.$&	'
 
 
 

 ,!56
	<.$&	'
 
 
 
.h~t7K.L .QU . . . .,Hl^T5I,J ,t , , , ,     l t     6&6476	4c>	"6 6 6 ^6,5 5 5 5 5 5r   r    r)   r*   c                 :    t                               |            | S )zU
    Decorator to register a callback function for the start of the compilation.
    )callback_handlerr/   r)   s    r   on_compile_startrI      s     ,,X666Or   c                 :    t                               |            | S )zS
    Decorator to register a callback function for the end of the compilation.
    )rG   r1   rH   s    r   on_compile_endrK      s     **8444Or   )__doc__enumrD   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   Enumr
   r   r    rG   rI   rK   r   r   r   <module>rS      s   6      % % % % % % % % % % % % ( ( ( ( ( ( ( (                    di           
 ^5 ^5 ^5 ^5 ^5 ^5 ^5 ^5B .-// ~t+,|nd"#   ~t+,|nd"#     r   