+
    &j                         R t ^ RIt^ RIt^ RIHtHt ^ RIHt ^ RIH	t	H
t
 ^ RIHt  ! R R]P                  4      t]	 ! R R	4      4       t]	 ! R
 R4      4       t]! 4       tR R ltR R ltR# )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                   &    ] tR t^$t^t^t^t^tRtR# )CallbackTrigger N)	__name__
__module____qualname____firstlineno__DYNAMOLAZY_BACKWARDTRITON_AUTOTUNINGCUDAGRAPH_RECORDING__static_attributes__r
       n/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/torch/_dynamo/callback.pyr	   r	   $   s    FMr   r	   c                   ,   a  ] tR t^/t o V 3R ltRtV tR# )CallbackArgsc                2   < V ^8  d   Qh/ S[ ;R&   S[;R&   # )   callback_trigger
compile_id)r	   str)format__classdict__s   "r   __annotate__CallbackArgs.__annotate__/   s     %%  O r   r
   N)r   r   r   r   __annotate_func__r   __classdictcell__r   s   @r   r   r   /   s      r   r   c                   *  a  ] tR t^5t o ]! ]R7      t]! ]R7      t]! ^ RRR7      t]! ]	P                  RRR7      tV 3R lR ltV 3R lR ltV 3R	 lR
 ltV 3R lR ltV 3R lR ltV 3R lR lt]V 3R lR l4       tV 3R lR ltV 3R ltRtV tR# )CompilationCallbackHandler)default_factoryF)defaultinitrepr)r&   r(   r)   c                R   < V ^8  d   QhRS[ S[.R3,          RS[ S[.R3,          /# r   callbackNreturnr   r   )r   r   s   "r   r   'CompilationCallbackHandler.__annotate__?   5     
 
 ,!56
	<.$&	'
r   c                <    V P                   P                  V4       V# )z
Register a callback function to be called when the compilation starts.

Args:
- callback (Callable): The callback function to register.
)start_callbacksappendselfr,   s   &&r   register_start_callback2CompilationCallbackHandler.register_start_callback?   s     	##H-r   c                R   < V ^8  d   QhRS[ S[.R3,          RS[ S[.R3,          /# r+   r.   )r   r   s   "r   r   r/   K   r0   r   c                <    V P                   P                  V4       V# )z
Register a callback function to be called when the compilation ends.

Args:
- callback (Callable): The callback function to register.
)end_callbacksr3   r4   s   &&r   register_end_callback0CompilationCallbackHandler.register_end_callbackK   s     	!!(+r   c                :   < V ^8  d   QhRS[ S[.R3,          RR/# r+   r.   )r   r   s   "r   r   r/   W   s(     . .h~t7K.L .QU .r   c                <    V P                   P                  V4       R# )zm
Remove a registered start callback function.

Args:
- callback (Callable): The callback function to remove.
N)r2   remover4   s   &&r   remove_start_callback0CompilationCallbackHandler.remove_start_callbackW   s     	##H-r   c                :   < V ^8  d   QhRS[ S[.R3,          RR/# r+   r.   )r   r   s   "r   r   r/   `   s'     , ,Hl^T5I,J ,t ,r   c                <    V P                   P                  V4       R# )zk
Remove a registered end callback function.

Args:
- callback (Callable): The callback function to remove.
N)r:   r?   r4   s   &&r   remove_end_callback.CompilationCallbackHandler.remove_end_callback`   s     	!!(+r   c                $   < V ^8  d   QhRS[ RR/# r   argsr-   Nr   )r   r   s   "r   r   r/   i   s        r   c                <    V P                    F  pV! V4       K  	  R# )z)
Execute all registered start callbacks.
N)r2   r5   rH   r,   s   && r   run_start_callbacks.CompilationCallbackHandler.run_start_callbacksi   s     ,,HTN -r   c                $   < V ^8  d   QhRS[ RR/# rG   rI   )r   r   s   "r   r   r/   p   s      l t r   c                <    V P                    F  pV! V4       K  	  R# )z'
Execute all registered end callbacks.
N)r:   rK   s   && r   run_end_callbacks,CompilationCallbackHandler.run_end_callbacksp   s     **HTN +r   c          	      D   < V ^8  d   QhRS[ RS[RS[RS[S[3,          /# )r   triggerr   r-   N)r	   r   r   r   )r   r   s   "r   r   r/   x   s0     6 6&6476	4c>	"6r   c           	   #    "   \        W4      p V P                  ;_uu_ 4        V ;P                  ^,          un        V P                  ^8X  d   V P                  V4       RRR4       Rx  V P                  ;_uu_ 4        V P                  ^ 8  g   Q R4       hV P                  ^8X  d   V P	                  V4       V ;P                  ^,          un        RRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i  T P                  ;_uu_ 4        T P                  ^ 8  g   Q R4       hT P                  ^8X  d   T P	                  T4       T ;P                  ^,          un        RRR4       i   + '       g   i     i ; i; i5i)zS
Context manager to install the callbacks and run them when the context is exited.
Nz1Pending callbacks counter cannot become negative.)r   ;_CompilationCallbackHandler__pending_callbacks_counter_lock6_CompilationCallbackHandler__pending_callbacks_counterrL   rP   )r5   rS   r   rH   s   &&& r   install_callbacks,CompilationCallbackHandler.install_callbacksw   s5     G0	666600A5033q8,,T2 7 66677!; G; 33q8**4000A50 76 76 76666677!; G; 33q8**4000A50 7666se   FD <CD +F?AC0
FC-	(D 0D	;	FF	AE6	-	F	6FF		Fc                   < V ^8  d   QhRR/# )r   r-   Nr
   )r   r   s   "r   r   r/      s     5 5t 5r   c                    V P                   P                  4        V P                  P                  4        V P                  ^ 8X  g   Q hR# )z!
Clear all registered callbacks.
N)r2   clearr:   rV   )r5   s   &r   r[    CompilationCallbackHandler.clear   s<     	""$  "//1444r   c                   < V ^8  d   Qh/ S[ S[S[.R3,          ,          ;R&   S[ S[S[.R3,          ,          ;R&   S[;R&   S[P
                  ;R&   # )r   Nr2   r:   rV   rU   )listr   r   int	threadingLock)r   r   s   "r   r   r/   5   s_     (L>4#789W  ,!567U 
 "%O  '0nn  r   r
   N)r   r   r   r   r   r^   r2   r:   rV   r`   ra   rU   r6   r;   r@   rD   rL   rP   r   rW   r[   r!   r   r"   r#   s   @r   r%   r%   5   s     <ARV<WO:?PT:UM',QU'O7<!U8$
 

 
. ., ,   6 6,5 5s  r   r%   c                h    V ^8  d   QhR\         \        .R3,          R\         \        .R3,          /# r+   r.   )r   s   "r   r   r      4      ~t+,|nd"#r   c                0    \         P                  V 4       V # )zM
Decorator to register a callback function for the start of the compilation.
)callback_handlerr6   r,   s   &r   on_compile_startrg      s     ,,X6Or   c                h    V ^8  d   QhR\         \        .R3,          R\         \        .R3,          /# r+   r.   )r   s   "r   r   r      rc   r   c                0    \         P                  V 4       V # )zK
Decorator to register a callback function for the end of the compilation.
)re   r;   rf   s   &r   on_compile_endrj      s     **84Or   )__doc__enumr`   collections.abcr   r   
contextlibr   dataclassesr   r   typingr   Enumr	   r   r%   re   rg   rj   r
   r   r   <module>rr      sy   6   / % ( dii    
 ^5 ^5 ^5B ./ r   