o
    h                     @   s   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	 d	Ze Zd
eg df deg df fddZd
eg df deg df fd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CallableOptional)justknobs_checkc                   @   sX  e Zd ZU eedZeeg df  ed< eedZeeg df  ed< eddddZ	e
e ed< eddddZeed	< eejddd
Zejed< deg df deg df fddZdeg df deg df fddZdeg df ddfddZdeg df ddfddZd ddZd ddZedefddZededeef fddZd ddZdS )!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                 C      | 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    r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/_dynamo/callback.pyregister_start_callback2   s   	z2CompilationCallbackHandler.register_start_callbackc                 C   r   )z
        Register a callback function to be called when the compilation ends.

        Args:
        - callback (Callable): The callback function to register.
        )r   r   r   r   r   r   register_end_callback>   s   z0CompilationCallbackHandler.register_end_callbackc                 C      | j | dS )z
        Remove a registered start callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r   remover   r   r   r   remove_start_callbackH      z0CompilationCallbackHandler.remove_start_callbackc                 C   r   )z
        Remove a registered end callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r   r   r   r   r   r   remove_end_callbackQ   r!   z.CompilationCallbackHandler.remove_end_callbackc                 C      | j D ]}|  qdS )z9
        Execute all registered start callbacks.
        N)r   r   r   r   r   run_start_callbacksZ      
z.CompilationCallbackHandler.run_start_callbacksc                 C   r#   )z7
        Execute all registered end callbacks.
        N)r   r   r   r   r   run_end_callbacksa   r%   z,CompilationCallbackHandler.run_end_callbacksc                 C   s   | j d u r
td| _ | j S )Nz*pytorch/dynamo:prevent_duplicate_callbacks)r   r	   r   r   r   r   prevent_duplicate_callbacksh   s
   
z6CompilationCallbackHandler.prevent_duplicate_callbacksc                 c   s:   | j rzW| j | jdkr|   |  jd7  _W d   n1 s#w   Y  dV  W | j! | jdks9J d| jdkrB|   |  jd8  _W d   n
1 sSw   Y  dS dS | j! | jdksiJ d| jdkrr|   |  jd8  _W d   w 1 sw   Y  w z|   dV  W |   dS |   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'   r   r   r   install_callbacksp   s8   

,
z,CompilationCallbackHandler.install_callbacksc                 C   s   | j   | j  dS )z1
        Clear all registered callbacks.
        N)r   clearr   r'   r   r   r   r+      s   
z CompilationCallbackHandler.clear)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*   r+   r   r   r   r   r
   %   s2   
 


"
	
	
r
   r   r   c                 C      t |  | S )zU
    Decorator to register a callback function for the start of the compilation.
    )callback_handlerr   r   r   r   r   on_compile_start      
r9   c                 C   r6   )zS
    Decorator to register a callback function for the end of the compilation.
    )r7   r   r8   r   r   r   on_compile_end   r:   r;   )__doc__r3   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   r   torch._utils_internalr	   r
   r7   r9   r;   r   r   r   r   <module>   s    m"&