
    Vh                         d 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mZ ddlmZ e G d d	             Z e       Zd
eg df   deg df   fdZd
eg df   deg df   fdZy)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                      e Zd ZU  ee      Zeeg df      ed<    ee      Zeeg df      ed<    eddd      Z	e
e   ed<    eddd      Zeed	<    eej                  dd
      Zej                  ed<   deg df   deg df   fdZdeg df   deg df   fdZdeg df   ddfdZdeg df   ddfdZddZddZedefd       Zededeef   fd       ZddZy)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                 <    | j                   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     F/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/callback.pyregister_start_callbackz2CompilationCallbackHandler.register_start_callback2   s     	##H-    c                 <    | j                   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_callback>   s     	!!(+r   c                 :    | j                   j                  |       y)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_callbackH   s     	##H-r   c                 :    | j                   j                  |       y)z
        Remove a registered end callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r   r"   r   s     r   remove_end_callbackz.CompilationCallbackHandler.remove_end_callbackQ   s     	!!(+r   c                 4    | j                   D ]	  } |         y)z9
        Execute all registered start callbacks.
        N)r   r   s     r   run_start_callbacksz.CompilationCallbackHandler.run_start_callbacksZ   s     ,, 	HJ	r   c                 4    | j                   D ]	  } |         y)z7
        Execute all registered end callbacks.
        N)r   r   s     r   run_end_callbacksz,CompilationCallbackHandler.run_end_callbacksa   s     ** 	HJ	r   c                 R    | j                   t        d      | _         | j                   S )Nz*pytorch/dynamo:prevent_duplicate_callbacks)r   r
   r   s    r   prevent_duplicate_callbacksz6CompilationCallbackHandler.prevent_duplicate_callbacksh   s,    --51@<2D. 111r   c              #     K   | j                   r	 | j                  5  | j                  dk(  r| j                          | xj                  dz  c_        ddd       d | j                  5  | j                  dkD  sJ d       | j                  dk(  r| j	                          | xj                  dz  c_        ddd       y	 | j                          d | j	                          y# 1 sw Y   xY w# 1 sw Y   yxY w# | j                  5  | j                  dkD  sJ d       | j                  dk(  r| j	                          | xj                  dz  c_        ddd       w # 1 sw Y   w xY wxY w# | j	                          w xY ww)zc
        Context manager to install the callbacks and run them when the context is exited.
        r      Nz1Pending callbacks counter cannot become negative.)r,   r   r   r'   r)   r+   s    r   install_callbacksz,CompilationCallbackHandler.install_callbacksp   sh    
 ++::: :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 C; 5C#C; F )AC/4	F >E+ F #C,(C; /C84F ;E(AE	E(E%!E((F +E==F c                 l    | j                   j                          | j                  j                          y)z1
        Clear all registered callbacks.
        N)r   clearr   r+   s    r   r1   z CompilationCallbackHandler.clear   s(     	""$  "r   )r   N)__name__
__module____qualname__r   listr   r   __annotations__r   r   r	   boolr   int	threadingLockr   r   r    r#   r%   r'   r)   propertyr,   r   r   r   r/   r1    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    t         j                  |        | S )zU
    Decorator to register a callback function for the start of the compilation.
    )callback_handlerr   r   s    r   on_compile_startr@      s     ,,X6Or   c                 0    t         j                  |        | S )zS
    Decorator to register a callback function for the end of the compilation.
    )r>   r    r?   s    r   on_compile_endrB      s     **84Or   )__doc__r9   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   r	   torch._utils_internalr
   r   r>   r@   rB   r<   r   r   <module>rI      s   6  % % ( * * 1 j# j# j#Z ./ xD1 hr4x6H Xb$h/ HRX4F r   