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   úR/var/www/html/assistant/venv/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ÚreprÚ6_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 s¬0    Y  n^| jD | jdksÒJ 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   Údataclassesr   r   Útypingr   r   ÚEnumr   r   r   r5   r7   r8   r   r   r   r   Ú<module>   s$   aþþ