+
    &j'                     x    ^ RI t ^ RIHt ^ RIt^ RIt^ RIHt . ROt ! R R4      tR t	R R	 lt
 ! R
 R4      tR# )    N)OrderedDictAnyRemovableHandleBackwardHookc                      a  ] tR t^
t o Rt^ tRR/V 3R lR lltV 3R lR ltR 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tRtV tR# )r   aA  
A handle which provides the capability to remove a hook.

Args:
    hooks_dict (dict): A dictionary of hooks, indexed by hook ``id``.
    extra_dict (Union[dict, List[dict]]): An additional dictionary or list of
        dictionaries whose keys will be deleted when the same keys are
        removed from ``hooks_dict``.

extra_dictNc                *   < V ^8  d   QhRS[ RS[ RR/# )   
hooks_dictr	   returnNr   )format__classdict__s   "i/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/torch/utils/hooks.py__annotate__RemovableHandle.__annotate__   s'     	L 	L3 	Ls 	Ld 	L    c                  \         P                  ! V4      V n        \        P                  V n        \        ;P                  ^,          un        RV n        \        V\        4      '       d   \         P                  ! V4      3V n        R# \        V\        4      '       d4   \        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      V n        R# R# )   c              3   N   "   T F  p\         P                  ! V4      x  K  	  R # 5iNweakrefref.0ds   & r   	<genexpr>+RemovableHandle.__init__.<locals>.<genexpr>!   s     'K
1A
   #%N )r   r   hooks_dict_refr   next_ididextra_dict_ref
isinstancedictlisttuple)selfr   r	   s   &&$r   __init__RemovableHandle.__init__   s    %kk*5!))1$%'j$''#*;;z#:"<D
D))"'%'K
'K%%'K
'K"KD *r   c                   < V ^8  d   QhRR/# r   r   Nr!   )r   r   s   "r   r   r   #   s     ( ( (r   c                    V P                  4       pVe   V P                  V9   d   WP                   V P                   F/  pV! 4       pVf   K  V P                  V9   g   K#  W0P                   K1  	  R # r   )r"   r$   r%   )r*   r   r   r	   s   &   r   removeRemovableHandle.remove#   s]    ((*
!dgg&;77#&&CJ%$''Z*?ww' 'r   c                   V P                   f   V P                  4       V P                  3# V P                  4       V P                  \        ;QJ d    . R V P                    4       F  NK  	  53# ! R V P                    4       4      3# )Nc              3   .   "   T F  q! 4       x  K  	  R # 5ir   r!   )r   r   s   & r   r   /RemovableHandle.__getstate__.<locals>.<genexpr>1   s     9_K^C#%%K^s   )r%   r"   r$   r)   r*   s   &r   __getstate__RemovableHandle.__getstate__-   sk    &'')47733'')477EE9_4K^K^9_E``E9_4K^K^9_4_``r   c                   < V ^8  d   QhRR/# r.   r!   )r   r   s   "r   r   r   3   s     J JT Jr   c                   V^ ,          f%   \         P                  ! \        4       4      V n        M"\         P                  ! V^ ,          4      V n        V^,          V n        \        \        P                  V P                  ^,           4      \        n        \        V4      ^8  g   V^,          f
   RV n	        R# \        ;QJ d    . R V^,           4       F  NK  	  5M! R V^,           4       4      V n	        R# )r   Nc              3   N   "   T F  p\         P                  ! V4      x  K  	  R # 5ir   r   r   s   & r   r   /RemovableHandle.__setstate__.<locals>.<genexpr>?   s     'I1Ar    r!   )r   r   r   r"   r$   maxr   r#   lenr%   r)   )r*   states   &&r   __setstate__RemovableHandle.__setstate__3   s    8")++km"<D")++eAh"7D("%o&=&=tww{"Ku:>U1X-"$D"'%'Ia'I%%'Ia'I"IDr   c                   < V ^8  d   QhRR/# )r   r   r   r!   )r   r   s   "r   r   r   A   s      , r   c                    V # r   r!   r5   s   &r   	__enter__RemovableHandle.__enter__A   s    r   c                0   < V ^8  d   QhRS[ RS[ RS[ RR/# )r   typevaluetbr   Nr   )r   r   s   "r   r   r   D   s)      S  # $ r   c                &    V P                  4        R # r   )r0   )r*   rF   rG   rH   s   &&&&r   __exit__RemovableHandle.__exit__D   s    r   c                2   < V ^8  d   Qh/ S[ ;R&   S[ ;R&   # )r   r$   r#   )int)r   r   s   "r   r   r   
   s      	G   r   )r%   r"   r$   )__name__
__module____qualname____firstlineno____doc__r#   r+   r0   r6   r?   rC   rJ   __annotate_func____static_attributes____classdictcell__r   s   @r   r   r   
   s]      G	LT 	L 	L( (aJ J  u  r   c                    RV n         V # )z
Mark a function as an unserializable hook with this decorator.

This suppresses warnings that would otherwise arise if you attempt
to serialize a tensor that has a hook.
T)__torch_unserializable__)fs   &r   unserializable_hookrZ   H   s     "&AHr   c                    V ^8  d   QhRR/# r.   r!   )r   s   "r   r   r   S   s     H H Hr   c                     V P                   '       db   V P                    FO  pV P                   V,          p\        VR 4      '       d   K*  \        P                  ! R\	        V4       R2^R7       KQ  	  R# R# )rX   zbackward hook z on tensor will not be serialized.  If this is expected, you can decorate the function with @torch.utils.hooks.unserializable_hook to suppress this warning
stacklevelN)_backward_hookshasattrwarningswarnrepr)tensorkhooks   &  r   warn_if_has_hooksrg   S   si    ''A))!,D4!;<<tDzl ;9 9 FGH ( r   c                   f   a  ] tR t^]t o RtV 3R lR ltR tR tV 3R lR ltR t	R	 t
R
 tRtV tR# )r   a  
A wrapper class to implement nn.Module backward hooks.

It handles:
  - Ignoring non-Tensor inputs and replacing them by None before calling the user hook
  - Generating the proper Node to capture a set of Tensor's gradients
  - Linking the gradients captures for the outputs with the gradients captured for the input
  - Calling the user hook once both output and input gradients are available
c                   < V ^8  d   QhRR/# r.   r!   )r   r   s   "r   r   BackwardHook.__annotate__h   s     	( 	(d 	(r   c                p    W n         W0n        Wn        R V n        RV n        R V n        RV n        R V n        R # )N)
user_hooksuser_pre_hooksmodulegrad_outputs	n_outputsoutput_tensors_indexn_inputsinput_tensors_index)r*   ro   rm   rn   s   &&&&r   r+   BackwardHook.__init__h   s8    $, $(!#' r   c                `    R .V,          p\        WRR7       F	  w  rVWdV&   K  	  \        V4      # )NTstrict)zipr)   )r*   indicesvaluessizeresidxvals   &&&&   r   _pack_with_noneBackwardHook._pack_with_nones   s3    ftmGD9HCH : Szr   c                P    V Uu. uF  q2V,          NK  	  pp\        V4      # u upi r   )r)   )r*   rz   r{   r~   r}   s   &&&  r   _unpack_noneBackwardHook._unpack_nonez   s)    &-.gsc{{g.Sz /s   #c                   < V ^8  d   QhRR/# r.   r!   )r   r   s   "r   r   rj      s     $ $ $r   c                6   a  V 3R  lpVP                  V4       R# )c           	        < SP                   f   R # SP                  SP                  V SP                  4      pSP                   Fe  pV! SP
                  VSP                   4      pVf   K'  \        V4      \        V4      8w  d$   \        R\        V4       R\        V4       24      hTpKg  	  R Sn         SP                  SP                  V4      # )Nz<Backward hook returned an invalid number of grad_input, got , but expected )	rp   r   rt   rs   rm   ro   r=   RuntimeErrorr   )
grad_input_r}   rf   outr*   s   &&   r   rf   )BackwardHook._set_user_hook.<locals>.hook   s      ( &&t'?'?T]][C4;;T->->?;s8s3x'& (..1#hZs3xj(R S S  ( !%D$$T%=%=sCCr   Nregister_hook)r*   grad_fnrf   s   f& r   _set_user_hookBackwardHook._set_user_hook   s    	D0 	d#r   c                   . p. pR p\        V4       F[  w  rg\        V\        P                  4      '       g   K'  VP	                  V4       VP	                  V4       WWP
                  ,          pK]  	  V'       d   \        P                  ! 4       '       g   VR3# \        P                  P                  P                  P                  P                  ! V!  p\        V4      ^ 8X  d   \        R4      hV U	u. uF?  qP                  f   K  V	P                  P                  4       R8X  g   K3  V	P                  NKA  	  p
p	\        V
4      ^ 8X  d   \        R4      hV! V
^ ,          4       \!        V4      p\#        W8RR7       F	  w  rWV&   K  	  \%        V4      \&        J d   \'        V4      pW3# \%        V4      ! V!  pW3# u up	i )FNzCCannot set Module backward hook for a Module with no input Tensors.BackwardHookFunctionBackwardzaError while setting up backward hooks. Please open an issue with a code sample to reproduce this.Trw   )	enumerater&   torchTensorappendrequires_gradis_grad_enablednnmodules
_functionsBackwardHookFunctionapplyr=   r   r   namer(   ry   rF   r)   )r*   fnargstensors_idxtensorsr   iargnew_tensorstgrad_fnsarg_listr~   r   r   s   &&&            r   _apply_on_tensorsBackwardHook._apply_on_tensors   s    oFA#u||,,""1%s#!2!22	 & %"7"7"9"9:hh&&11FFLLgV{q dee'2  D{!iiIPQPYPYP^P^P`  eC  QCIAII{  Dx=A  P Q Q 	8A;:KTBHCSM C :/C  t*h'C Ds   3F<F<(F<c                n   a  R  V 3R llpS P                  W!4      w  r4\        V4      S n        VS n        V# )c                    V ^8  d   QhRR/# r.   r!   )r   s   "r   r   3BackwardHook.setup_input_hook.<locals>.__annotate__   s     	) 	)4 	)r   c                 *   < SP                  V 4       R # r   )r   )r   r*   s   &r   r   )BackwardHook.setup_input_hook.<locals>.fn   s    (r   )r   r=   rs   rt   )r*   r   r   r}   	input_idxs   f&   r   setup_input_hookBackwardHook.setup_input_hook   s9    	) 	) //9D	#, 
r   c                   a  R  V 3R llpRp\        V\        4      '       g   V3pRpS P                  W!4      w  rE\        V4      S n        VS n        V'       g
   V^ ,          pV# )c                    V ^8  d   QhRR/# r.   r!   )r   s   "r   r   4BackwardHook.setup_output_hook.<locals>.__annotate__   s     +	( +	(4 +	(r   c                 6   < V3R  lpV P                  V4       R# )c                   <a	 S
P                  S
P                  VS
P                  4      S
n        S
P                  '       du   \        S
P                  4      pS
P                   FO  pV! S
P                  S
P                  4      pVf   K&  \        V4      pWR8w  d   \        RV RV 24      hVS
n        KQ  	  S
P                  o	S
P                  f   \        P                  ! R^R7       S
P                  . . S
P                  4      pS
P                   F  pV! S
P                  VS
P                  4      pVf   K'  \        V\        4      '       d=   \        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       d   Ky  \        R4      h	  R S
n        S	e`   S
P                  f   \!        R	4      h\        ;QJ d!    . V	3R
 lS
P                   4       F  NK  	  5# ! V	3R
 lS
P                   4       4      # R # )NzABackward pre hook returned an invalid number of grad_output, got r   zFull backward hook is firing when gradients are computed with respect to module outputs since no inputs require gradients. See https://docs.pytorch.org/docs/main/generated/torch.nn.Module.html#torch.nn.Module.register_full_backward_hook for more details.r]   c              3   (   "   T F  qR J x  K
  	  R # 5ir   r!   )r   els   & r   r   KBackwardHook.setup_output_hook.<locals>.fn.<locals>.hook.<locals>.<genexpr>   s     RlhkbdY]S]hks   FTzoBackward hook for Modules where no input requires gradient should always return None or None for all gradients.zEoutput_tensors_index should not be None when grad_outputs is not Nonec              3   6   <"   T F  pSV,          x  K  	  R # 5ir   r!   )r   r   local_grad_outputss   & r   r   r      s      Z@Y1!3A!6!6@Ys   )r   rr   rq   rp   rn   r=   ro   r   rt   ra   rb   rs   rm   r&   r)   allAssertionError)r   grad_outputexpected_lenuser_pre_hookhook_grad_outputs
actual_lengrad_inputs	user_hookr}   r   r*   s   &&       @r   rf   8BackwardHook.setup_output_hook.<locals>.fn.<locals>.hook   s   $($8$89R9R9D9=%I! &&&#&t'8'8#9L)-)<)<,9$++tGXGX,Y),4$%():%;
%5". 066@\Q]P^0` #a a,=) *= &*%6%6" ++3MM #6 ./	0
 #'"6"6r2t}}"MK%)__	'[$BSBST?JsE4J4JssRlhkRlsssRlhkRlOlOl". 0o #p p &5
 )-D%%1008,-tuu 5 Z@Y@Y Z5Z5 Z@Y@Y ZZZ 2r   Nr   )r   rf   r*   s   & r   r   *BackwardHook.setup_output_hook.<locals>.fn   s    ([T !!$'r   TF)r&   r)   r   r=   rq   rr   )r*   r   r   is_tupler}   
output_idxs   f&    r   setup_output_hookBackwardHook.setup_output_hook   sd    +	( +	(Z $&&7DH00:T$.!a&C
r   )rp   rt   ro   rs   rq   rr   rm   rn   N)rN   rO   rP   rQ   rR   r+   r   r   r   r   r   r   rT   rU   rV   s   @r   r   r   ]   s=     	( 	(
$ $6# J9 9r   )r   rZ   rg   r   )r   collectionsr   r   ra   typingr   __all__r   rZ   rg   r   r!   r   r   <module>r      s;     #   
Y; ;|Hd dr   