a
    h                     @   s   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 dejZeG d	d
 d
ZeG dd dZe Zeegdf eegdf dddZeegdf eegdf d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Callablec                   @   s   e Zd ZdZdZdZdZdS )CallbackTrigger            N)__name__
__module____qualname__ZDYNAMOZLAZY_BACKWARDZTRITON_AUTOTUNINGZCUDAGRAPH_RECORDING r   r   D/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/callback.pyr   $   s   r   c                   @   s   e Zd ZU eed< eed< dS )CallbackArgsZcallback_trigger
compile_idN)r   r   r   r   __annotations__strr   r   r   r   r   /   s   
r   c                   @   sH  e Zd ZU eedZeeegdf  ed< eedZ	eeegdf  ed< eddddZ
eed< eejddd	Zejed
< eegdf eegdf dddZeegdf eegdf dddZeegdf ddddZeegdf ddddZeddddZeddddZeeeedeef dddZdd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                 C   s   | 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   r   r   register_start_callback?   s    	z2CompilationCallbackHandler.register_start_callbackc                 C   s   | 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#   r   r   r   register_end_callbackK   s    	z0CompilationCallbackHandler.register_end_callbackc                 C   s   | 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_callbackW   s    z0CompilationCallbackHandler.remove_start_callbackc                 C   s   | j | dS )z
        Remove a registered end callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r   r'   r#   r   r   r   remove_end_callback`   s    z.CompilationCallbackHandler.remove_end_callback)argsr!   c                 C   s   | j D ]}|| qdS )z9
        Execute all registered start callbacks.
        N)r   r$   r*   r    r   r   r   run_start_callbacksi   s    
z.CompilationCallbackHandler.run_start_callbacksc                 C   s   | j D ]}|| qdS )z7
        Execute all registered end callbacks.
        N)r   r+   r   r   r   run_end_callbacksp   s    
z,CompilationCallbackHandler.run_end_callbacks)triggerr   r!   c                 c   s  t ||}z| j2 | jdkr(| | |  jd7  _W d   n1 sJ0    Y  dV  W | jD | jdksvJ d| jdkr| | |  jd8  _W d   n1 s0    Y  n^| jD | jdksJ d| jdkr| | |  jd8  _W d   n1 s
0    Y  0 dS )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   r*   r   r   r   install_callbacksw   s*    


,

.

z,CompilationCallbackHandler.install_callbacks)r!   c                 C   s&   | j   | j  | jdks"J dS )z1
        Clear all registered callbacks.
        r   N)r   clearr   r   )r$   r   r   r   r0      s    

z CompilationCallbackHandler.clear)r   r   r   r   listr   r   r   r   r   r   int	threadingLockr   r%   r&   r(   r)   r,   r-   r   r   r   r   r   r/   r0   r   r   r   r   r   5   s*   
  		r   r   c                 C   s   t |  | S )zU
    Decorator to register a callback function for the start of the compilation.
    )callback_handlerr%   r    r   r   r   on_compile_start   s    
r7   c                 C   s   t |  | S )zS
    Decorator to register a callback function for the end of the compilation.
    )r5   r&   r6   r   r   r   on_compile_end   s    
r8   )__doc__enumr3   collections.abcr   
contextlibr   Zdataclassesr   r   typingr   r   Enumr   r   r   r5   r7   r8   r   r   r   r   <module>   s$   a