+
    Lj.                         ^ RI t ^ RIHt ^ RIt^RIHt ^RIHt ]! ]4      t	 ! R R4      t
 ! R R4      t ! R	 R
4      t ! R R4      t ! R R4      tR# )    N)Any)
get_logger)unwrap_modulec                   2   a  ] tR t^t o V 3R lR ltRtV tR# )	BaseStatec                   < V ^8  d   QhRR/#    returnN )format__classdict__s   "A/app/.local/lib/python3.14/site-packages/diffusers/hooks/hooks.py__annotate__BaseState.__annotate__   s     
 
 
    c                    \        R 4      h)zWBaseState::reset is not implemented. Please implement this method in the derived class.)NotImplementedError)selfargskwargss   &*,r   resetBaseState.reset   s    !e
 	
r   r   N)__name__
__module____qualname____firstlineno__r   __static_attributes____classdictcell__r   s   @r   r   r      s     
 
r   r   c                   `   a  ] tR t^"t o R
V 3R lR lltR tV 3R lR ltV 3R lR ltR	tV t	R# )StateManagerNc                    < V ^8  d   QhRS[ /# )r
   	state_cls)r   )r   r   s   "r   r   StateManager.__annotate__#   s     % %) %r   c                b    Wn         Ve   TMRV n        Ve   TM/ V n        / V n        R V n        R # )Nr   )
_state_cls
_init_args_init_kwargs_state_cache_current_context)r   r$   	init_argsinit_kwargss   &&&&r   __init__StateManager.__init__#   s4    #'0'<)"+6+BK $r   c                :   V P                   f   \        R4      hV P                   V P                  P                  4       9  d>   V P                  ! V P
                  / V P                  B V P                  V P                   &   V P                  V P                   ,          # )NzDNo context is set. Please set a context before retrieving the state.)r+   
ValueErrorr*   keysr'   r(   r)   r   s   &r   	get_stateStateManager.get_state*   s}      (cdd  (9(9(>(>(@@7;7m[_[l[l7mDd334  !6!677r   c                $   < V ^8  d   QhRS[ RR/# r
   namer   Nstr)r   r   s   "r   r   r%   1   s     % % % %r   c                    Wn         R # N)r+   r   r8   s   &&r   set_contextStateManager.set_context1   s     $r   c                   < V ^8  d   QhRR/# r	   r   )r   r   s   "r   r   r%   4   s     % % %r   c                    \        V P                  P                  4       4       F2  w  r4VP                  ! V/ VB  V P                  P	                  V4       K4  	  R V n        R # r<   )listr*   itemsr   popr+   )r   r   r   r8   states   &*,  r   r   StateManager.reset4   sQ     1 1 7 7 9:KDKK((!!$' ; !%r   )r+   r(   r)   r*   r'   )NN)
r   r   r   r   r.   r4   r>   r   r   r   r    s   @r   r"   r"   "   s(     % %8% %% %r   r"   c                      a  ] tR t^;t o RtR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R lt
V 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )	ModelHookzd
A hook that contains callbacks to be executed just before and after the forward method of a model.
Fc                    R V n         R # r<   fn_refr3   s   &r   r.   ModelHook.__init__B   s	    /3r   c                v   < V ^8  d   QhRS[ P                  P                  RS[ P                  P                  /# r
   moduler   torchnnModule)r   r   s   "r   r   ModelHook.__annotate__E   s+      ehhoo %((// r   c                    V# )z
Hook that is executed when a model is initialized.

Args:
    module (`torch.nn.Module`):
        The module attached to this hook.
r   r   rO   s   &&r   initialize_hookModelHook.initialize_hookE   	     r   c                v   < V ^8  d   QhRS[ P                  P                  RS[ P                  P                  /# rN   rP   )r   r   s   "r   r   rT   O   s+      uxx 588?? r   c                    V# )z
Hook that is executed when a model is deinitialized.

Args:
    module (`torch.nn.Module`):
        The module attached to this hook.
r   rV   s   &&r   deinitalize_hookModelHook.deinitalize_hookO   rY   r   c                   < V ^8  d   QhRS[ P                  P                  RS[S[S[,          S[S[S[3,          3,          /# rN   )rQ   rR   rS   tupler   dictr:   )r   r   s   "r   r   rT   Y   s?      %((// uUSVZY]^acf^fYgMgGh r   c                    W#3# )a  
Hook that is executed just before the forward method of the model.

Args:
    module (`torch.nn.Module`):
        The module whose forward pass will be executed just after this event.
    args (`tuple[Any]`):
        The positional arguments passed to the module.
    kwargs (`dict[Str, Any]`):
        The keyword arguments passed to the module.
Returns:
    `tuple[tuple[Any], dict[Str, Any]]`:
        A tuple with the treated `args` and `kwargs`.
r   )r   rO   r   r   s   &&*,r   pre_forwardModelHook.pre_forwardY   s     |r   c                T   < V ^8  d   QhRS[ P                  P                  RS[RS[/# )r
   rO   outputr   )rQ   rR   rS   r   )r   r   s   "r   r   rT   j   s*      588?? C C r   c                    V# )a  
Hook that is executed just after the forward method of the model.

Args:
    module (`torch.nn.Module`):
        The module whose forward pass been executed just before this event.
    output (`Any`):
        The output of the module.
Returns:
    `Any`: The processed `output`.
r   )r   rO   re   s   &&&r   post_forwardModelHook.post_forwardj   s	     r   c                v   < V ^8  d   QhRS[ P                  P                  RS[ P                  P                  /# rN   rP   )r   r   s   "r   r   rT   x   s+      %((// ehhoo r   c                    V# )z
Hook that is executed when the hook is detached from a module.

Args:
    module (`torch.nn.Module`):
        The module detached from this hook.
r   rV   s   &&r   detach_hookModelHook.detach_hookx   rY   r   c                H   < V ^8  d   QhRS[ P                  P                  /# )r
   rO   rP   )r   r   s   "r   r   rT      s      %((// r   c                @    V P                   '       d   \        R 4      hV# )zFThis hook is stateful and needs to implement the `reset_state` method.)_is_statefulr   rV   s   &&r   reset_stateModelHook.reset_state   s    %&noor   c                R   < V ^8  d   QhRS[ P                  P                  RS[RR/# )r
   rO   r8   r   N)rQ   rR   rS   r:   )r   r   s   "r   r   rT      s*      588?? # $ r   c                    \        V 4       F7  p\        W4      p\        V\        4      '       g   K&  VP	                  V4       K9  	  V# r<   )dirgetattr
isinstancer"   r>   )r   rO   r8   	attr_nameattrs   &&&  r   _set_contextModelHook._set_context   s<    TI4+D$--  & # r   rJ   N)r   r   r   r   __doc__ro   r.   rW   r\   rb   rg   rk   rp   ry   r   r   r    s   @r   rH   rH   ;   s\      L4   "   
 r   rH   c                   2   a  ] tR t^t o V 3R lR ltRtV tR# )HookFunctionReferencec                   < V ^8  d   QhRR/# r	   r   )r   r   s   "r   r   "HookFunctionReference.__annotate__   s     % %$ %r   c                >    RV n         RV n        RV n        RV n        R# )a^  A container class that maintains mutable references to forward pass functions in a hook chain.

Its mutable nature allows the hook system to modify the execution chain dynamically without rebuilding the
entire forward pass structure.

Attributes:
    pre_forward: A callable that processes inputs before the main forward pass.
    post_forward: A callable that processes outputs after the main forward pass.
    forward: The current forward function in the hook chain.
    original_forward: The original forward function, stored when a hook provides a custom new_forward.

The class enables hook removal by allowing updates to the forward chain through reference modification rather
than requiring reconstruction of the entire chain. When a hook is removed, only the relevant references need to
be updated, preserving the execution order of the remaining hooks.
N)rb   rg   forwardoriginal_forwardr3   s   &r   r.   HookFunctionReference.__init__   s#         $r   )r   r   rg   rb   N)r   r   r   r   r.   r   r   r    s   @r   r}   r}      s     % %r   r}   c                      a a ] tR t^t oV3R lV 3R lltV3R lR ltV3R lR ltRV3R lR lltRV3R	 lR
 llt]	V3R lR l4       t
RV3R lR lltV3R lR ltV3R lR ltRtVtV ;t# )HookRegistryc                L   < V ^8  d   QhRS[ P                  P                  RR/# )r
   
module_refr   NrP   )r   r   s   "r   r   HookRegistry.__annotate__   s#      588?? t r   c                Z   < \         SV `  4        / V n        Wn        . V n        . V n        R # r<   )superr.   hooks_module_ref_hook_order_fn_refs)r   r   	__class__s   &&r   r.   HookRegistry.__init__   s*    +-
%r   c                *   < V ^8  d   QhRS[ RS[RR/# )r
   hookr8   r   N)rH   r:   )r   r   s   "r   r   r      s"     &% &%) &%3 &%4 &%r   c                6   W P                   P                  4       9   d   \        R V R24      hVP                  V P                  4      V n        R R lpV P                  P
                  p\        4       pVP                  Vn        VP                  Vn        WEn        \        VR4      '       dV   WEn
        \        P                  ! \        P                  ! VP                  V P                  4      VP                  4      Vn        V! V4      p\        P                  ! \        P                  ! W`P                  4      V4      V P                  n        WQn        WP                   V&   V P                   P#                  V4       V P$                  P#                  V4       R# )zHook with name zv already exists in the registry. Please use a different name or first remove the existing hook and then add a new one.c                $    V ^8  d   QhR\         /# )r
   function_reference)r}   )r   s   "r   r   0HookRegistry.register_hook.<locals>.__annotate__   s     	 	3H 	r   c                    a  V 3R  lpV# )c                 z   < SP                   ! V .VO5/ VB w  rSP                  ! V/ VB pSP                  W4      # r<   )rb   r   rg   )rO   r   r   re   r   s   &*, r   new_forwardKHookRegistry.register_hook.<locals>.create_new_forward.<locals>.new_forward   sE    1==fVtVvV+33TDVD)66vFFr   r   )r   r   s   f r   create_new_forward6HookRegistry.register_hook.<locals>.create_new_forward   s    G
 r   r   N)r   r2   r1   rW   r   r   r}   rb   rg   hasattrr   	functoolsupdate_wrapperpartialr   rK   r   appendr   )r   r   r8   r   r   rK   rewritten_forwards   &&&    r   register_hookHookRegistry.register_hook   sO   ::??$$!$ (I J 
  //0@0@A	 ""**&(!--"// 4''&-#&55!!$"2"2D4D4DEtGWGWFN /v6#,#;#;/1A1ABDU$
  

4%V$r   c                4   < V ^8  d   QhRS[ RS[R,          /# r7   )r:   rH   )r   r   s   "r   r   r      s      * *S *Y%5 *r   c                :    V P                   P                  VR 4      # r<   )r   getr=   s   &&r   get_hookHookRegistry.get_hook   s    zz~~dD))r   c                *   < V ^8  d   QhRS[ RS[RR/# )r
   r8   recurser   N)r:   bool)r   r   s   "r   r   r      s'     L L Ld Ld Lr   c                H   WP                   P                  4       9   Ed   \        V P                  4      pV P                   V,          pV P                  P	                  V4      pV P
                  V,          pVP                  pVP                  e   VP                  pWS^,
          8X  d   WpP                  n        MWpP
                  V^,           ,          n        VP                  V P                  4      V n        V P                   V V P                  P                  V4       V P
                  P                  V4       V'       d`   V P                  P                  4        F?  w  rVR8X  d   K  \        V	R4      '       g   K"  V	P                  P                  VRR7       KA  	  R # R # )N _diffusers_hookFr   )r   r2   lenr   indexr   r   r   r   r\   rD   named_modulesr   r   remove_hook)
r   r8   r   	num_hooksr   r   rK   old_forwardmodule_namerO   s
   &&&       r   r   HookRegistry.remove_hook   s@   ::??$$D,,-I::d#D$$**40E]]5)F ..K&&2$55A%+6  (3>eai(0#44T5E5EFD

4   'MMe$'+'7'7'E'E'G#"$6#455**66tU6K	 (H r   c                $   < V ^8  d   QhRS[ RR/# )r
   r   r   N)r   )r   r   s   "r   r   r      s     O OD OD Or   c                   \        V P                  4       FE  pV P                  V,          pVP                  '       g   K*  VP	                  V P
                  4       KG  	  V'       ds   \        V P
                  4      P                  4        FI  w  rEVR 8X  d   K  \        V4      p\        VR4      '       g   K-  VP                  P                  RR7       KK  	  R# R# )r   r   Fr   N)reversedr   r   ro   rp   r   r   r   r   r   reset_stateful_hooks)r   r   	hook_namer   r   rO   s   &&    r   r   !HookRegistry.reset_stateful_hooks   s    !$"2"23I::i(D     !1!12 4
 '4T5E5E'F'T'T'V#"$&v.6#455**???N (W r   c                L   < V ^8  d   QhRS[ P                  P                  RR/# )r
   rO   r   r   rP   )r   r   s   "r   r   r     s#     & &588?? &~ &r   c                X    \        VR 4      '       g   V ! V4      Vn        VP                  # )r   )r   r   )clsrO   s   &&r   check_if_exists_or_initialize*HookRegistry.check_if_exists_or_initialize  s(    v011%([F"%%%r   c                2   < V ^8  d   QhRS[ R,          RR/# )r
   r8   Nr   r9   )r   r   s   "r   r   r     s     ( (t (t (r   c                   \        V P                  4       FF  pV P                  V,          pVP                  '       g   K*  VP	                  V P
                  V4       KH  	  V P                  4        F  pVP	                  V4       K  	  R # r<   )r   r   r   ro   ry   r   _get_child_registries)r   r8   r   r   registrys   &&   r   ry   HookRegistry._set_context  sj    !$"2"23I::i(D   !!$"2"2D9 4
 224H!!$' 5r   c                .   < V ^8  d   QhRS[ R,          /# )r
   r   r   )rB   )r   r   s   "r   r   r     s      tN'; r   c                Z   \        V R4      '       g   RV n        V P                  e   V P                  # . p\        V P                  4      P	                  4        FH  w  r#VR8X  d   K  \        V4      p\        VR4      '       g   K-  VP                  VP                  4       KJ  	  Wn        V# )a7  Return registries of child modules, using a cached list when available.

The cache is built on first call and reused for subsequent calls. This avoids the cost of walking the full
module tree via named_modules() on every _set_context call, which is significant for large models (e.g. ~2.7ms
per call on Flux2).
_child_registries_cacheNr   r   )r   r   r   r   r   r   r   )r   
registriesr   rO   s   &   r   r   "HookRegistry._get_child_registries  s     t677+/D(''3///
#01A1A#B#P#P#RKb "6*Fv011!!&"8"89 $S (2$r   c                    < V ^8  d   QhRS[ /# )r
   r   r9   )r   r   s   "r   r   r   ,  s     
4 
4# 
4r   c                   R p\        V P                  4       F  w  r#V P                  V,          P                  P                  \
        P                  Jd#   V P                  V,          P	                  4       pM'V P                  V,          P                  P                  pVRV RV RV 2,          pV\        V P                  4      ^,
          8  g   K  VR,          pK  	  RV R2# )r   z  (z) z - 
zHookRegistry(
z
))	enumerater   r   r   __repr__objectr   r   )r   registry_reprir   	hook_reprs   &    r   r   HookRegistry.__repr__,  s    %d&6&67LAzz)$..77vN JJy1::<	 JJy1;;DD	s1#R	{#i[AAM3t''(1,,% 8 !s33r   )r   r   r   r   r   )Tr<   )r   r   r   r   r.   r   r   r   r   classmethodr   ry   r   r   r   r   __classcell__)r   r   s   @@r   r   r      ss      &% &%P* *L L8O O & &
( ( .
4 
4 
4r   r   )r   typingr   rQ   utils.loggingr   utils.torch_utilsr   r   loggerr   r"   rH   r}   r   r   r   r   <module>r      sY       & - 
H	
 
% %2R Rj% %.O4 O4r   