o
    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deegdf deegdf fddZdeegdf de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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   \/home/www/facesmatcher.com/frenv_anti/lib/python3.10/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                
   @   sT  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
< deegdf deegdf fddZdeegdf deegdf fddZdeegdf ddfddZdeegdf ddfddZdeddfddZdeddfddZededededeef fddZ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      | 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?      	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_callbackK   r&   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_callbackW      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_callback`   r+   z.CompilationCallbackHandler.remove_end_callbackargsc                 C      | j D ]}|| qdS )z9
        Execute all registered start callbacks.
        N)r   r$   r-   r   r   r   r   run_start_callbacksi      

z.CompilationCallbackHandler.run_start_callbacksc                 C   r.   )z7
        Execute all registered end callbacks.
        N)r   r/   r   r   r   run_end_callbacksp   r1   z,CompilationCallbackHandler.run_end_callbackstriggerr   c                 c   s   t ||}zX| j | jdkr| | |  jd7  _W d   n1 s&w   Y  dV  W | j# | jdks<J d| jdkrF| | |  jd8  _W d   dS 1 sXw   Y  dS | j" | jdkslJ d| jdkrv| | |  jd8  _W d   w 1 sw   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   r0   r2   )r$   r3   r   r-   r   r   r   install_callbacksw   s0   




*

z,CompilationCallbackHandler.install_callbacksc                 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   r5      s   

z CompilationCallbackHandler.clear)r    N)r   r   r   r   listr   r   r   r   r   r   int	threadingLockr   r%   r'   r*   r,   r0   r2   r   r   r   r   r   r4   r5   r   r   r   r   r   5   s<   
   

		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      
r=   c                 C   r:   )zS
    Decorator to register a callback function for the end of the compilation.
    )r;   r'   r<   r   r   r   on_compile_end   r>   r?   )__doc__enumr8   collections.abcr   
contextlibr   dataclassesr   r   typingr   r   Enumr   r   r   r;   r=   r?   r   r   r   r   <module>   s.    a

