Ë
    øÿæiö  ã                   ó  — d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ  G d„ dej                  «      Ze	 G d	„ d
«      «       Ze	 G d„ d«      «       Z e«       Zdeegdf   deegdf   fd„Zdeegdf   de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)ÚCallableÚ	Generator)Úcontextmanager)Ú	dataclassÚfield)ÚAnyc                   ó   — e Zd ZdZdZdZdZy)ÚCallbackTriggeré   é   é   é   N)Ú__name__Ú
__module__Ú__qualname__ÚDYNAMOÚLAZY_BACKWARDÚTRITON_AUTOTUNINGÚCUDAGRAPH_RECORDING© ó    úk/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torch/_dynamo/callback.pyr
   r
   $   s   „ à€Fà€MàÐàÑr   r
   c                   ó"   — e Zd ZU eed<   eed<   y)ÚCallbackArgsÚcallback_triggerÚ
compile_idN)r   r   r   r
   Ú__annotations__Ústrr   r   r   r   r   /   s   … à%Ó%Ø„Or   r   c            
       ó¬  — e Zd ZU  ee¬«      Zeeegdf      ed<    ee¬«      Z	eeegdf      ed<    eddd¬«      Z
eed<    eej                  dd¬	«      Zej                  ed
<   deegdf   deegdf   fd„Zdeegdf   deegdf   fd„Zdeegdf   ddfd„Zdeegdf   ddfd„Zdeddfd„Zdeddfd„Zededededeef   fd„«       Zdd„Zy)Ú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                 ó<   — | 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     r   Úregister_start_callbackz2CompilationCallbackHandler.register_start_callback?   s   € ð 	×Ñ×#Ñ# HÔ-Øˆr   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_callbackK   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_callbackW   s   € ð 	×Ñ×#Ñ# HÕ-r   c                 ó:   — | j                   j                  |«       y)z‹
        Remove a registered end callback function.

        Args:
        - callback (Callable): The callback function to remove.
        N)r#   r3   r-   s     r   Úremove_end_callbackz.CompilationCallbackHandler.remove_end_callback`   s   € ð 	×Ñ×!Ñ! (Õ+r   Úargsc                 ó6   — | j                   D ]
  } ||«       Œ y)z9
        Execute all registered start callbacks.
        N)r"   ©r.   r7   r)   s      r   Úrun_start_callbacksz.CompilationCallbackHandler.run_start_callbacksi   s   € ð ×,Ô,ˆHÙ�T�Nñ -r   c                 ó6   — | j                   D ]
  } ||«       Œ y)z7
        Execute all registered end callbacks.
        N)r#   r9   s      r   Úrun_end_callbacksz,CompilationCallbackHandler.run_end_callbacksp   s   € ð ×*Ô*ˆHÙ�T�Nñ +r   Útriggerr   c              #   ó”  K  — t        ||«      }	 | j                  5  | xj                  dz  c_        | j                  dk(  r| j                  |«       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# 1 sw Y   Œnx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­w)zc
        Context manager to install the callbacks and run them when the context is exited.
        r   Nr   z1Pending callbacks counter cannot become negative.)r   r(   r'   r:   r<   )r.   r=   r   r7   s       r   Úinstall_callbacksz,CompilationCallbackHandler.install_callbacksw   s)  è ø€ ô ˜G ZÓ0ˆð	6Ø×6Ó6Ø×0Ò0°AÑ5Õ0Ø×3Ñ3°qÒ8Ø×,Ñ,¨TÔ2÷ 7ó à×6Ó6Ø×7Ñ7¸!Ò;ð ØGóÐ;ð ×3Ñ3°qÒ8Ø×*Ñ*¨4Ô0Ø×0Ò0°AÑ5Õ0÷ 7Ð6÷ 7Ð6ú÷ 7Ð6û�×6Ó6Ø×7Ñ7¸!Ò;ð ØGóÐ;ð ×3Ñ3°qÒ8Ø×*Ñ*¨4Ô0Ø×0Ò0°AÑ5Õ0÷ 7×6Ñ6ÿse   ‚E�C œ6B?ÁC ÁEÁ*ACÂ6	EÂ?CÃC ÃCÃEÃEÃ$AD9Ä0	EÄ9EÄ>EÅEc                 óŽ   — | j                   j                  «        | j                  j                  «        | j                  dk(  sJ ‚y)z1
        Clear all registered callbacks.
        r   N)r"   Úclearr#   r'   )r.   s    r   rA   z CompilationCallbackHandler.clearŽ   s<   € ð 	×Ñ×"Ñ"Ô$Ø×Ñ× Ñ Ô"Ø×/Ñ/°1Ò4Ð4Ñ4r   )r*   N)r   r   r   r   Úlistr"   r   r   r   r#   r'   ÚintÚ	threadingÚLockr(   r/   r1   r4   r6   r:   r<   r   r
   r   r   r   r?   rA   r   r   r   r    r    5   st  … á<AÐRVÔ<W€O�T˜( L >°4Ð#7Ñ8Ñ9ÓWÙ:?ÐPTÔ:U€M�4˜ , °Ð!5Ñ6Ñ7ÓUá',°Q¸UÈÔ'OÐ ÓOÙ7<Ø!Ÿ™¨U¸ô8Ð$ i§n¡nó ð
Ø  , °Ð!5Ñ6ð
à	�<�. $Ð&Ñ	'ó
ð
Ø  , °Ð!5Ñ6ð
à	�<�. $Ð&Ñ	'ó
ð.¨h¸°~ÀtÐ7KÑ.Lð .ÐQUó .ð,¨H°l°^ÀTÐ5IÑ,Jð ,Ètó ,ð¨ð ¸ó ð lð °tó ð ð6Ø&ð6Ø47ð6à	�4˜˜c�>Ñ	"ò6ó ð6ô,5r   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_startrI   š   s   € ô ×,Ñ,¨XÔ6Ø€Or   c                 ó0   — t         j                  | «       | S )zS
    Decorator to register a callback function for the end of the compilation.
    )rG   r1   rH   s    r   Úon_compile_endrK   ¤   s   € ô ×*Ñ*¨8Ô4Ø€Or   )Ú__doc__ÚenumrD   Úcollections.abcr   r   Ú
contextlibr   Údataclassesr   r   Útypingr   ÚEnumr
   r   r    rG   rI   rK   r   r   r   Ú<module>rS      sÉ   ðñó6 Û ß /Ý %ß (Ý ô�d—i‘iô ð ÷ð ó ðð
 ÷^5ð ^5ó ð^5ñB .Ó/Ð ðØ˜�~ tÐ+Ñ,ðàˆ|ˆn˜dÐ"Ñ#óðØ˜�~ tÐ+Ñ,ðàˆ|ˆn˜dÐ"Ñ#ôr   