
    
3jYH                     *   S SK Jr  S SKJrJrJrJr  S SKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJrJrJrJr  \	" \5      rSrSr\R2                  " S// SQ-   5      rS\R6                  S\S\R6                  4S jr\ " S S5      5       r " S S\5      r " S S\5      r  " S S\5      r!S\RD                  RF                  S\SS4S jr$S\RD                  RF                  S\S\SS4S  jr% S#S\RD                  RF                  S\S\S!\&SS4
S" jjr'g)$    )	dataclass)ListOptionalTupleUnionN   )
get_logger)unwrap_module   )"_ALL_TRANSFORMER_BLOCK_IDENTIFIERS)TransformerBlockRegistry)	BaseStateHookRegistry	ModelHookStateManagermag_cache_leader_block_hookmag_cache_block_hook      ?)g`?g?gǝ??g      ?N`?     ?r   `?r   r   `?gǝ??N`?r   r   r   r   r   r   "?r   r   gN`?g"?gN`?g"?g;?	src_arraytarget_lengthreturnc                     [        U 5      nUS:X  a  U SS $ US-
  US-
  -  n[        R                  " XR                  [        R                  S9n[        R
                  " XC-  5      R                  5       nX   $ )zY
Interpolate the source array to the target length using nearest neighbor interpolation.
r   N)devicedtype)lentorcharanger    float32roundlong)r   r   
src_lengthscalegridmapped_indicess         S/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/hooks/mag_cache.pynearest_interpr-   E   sp     YJ~!^ 12E<<.>.>emmTD[[.335N$$    c                       \ rS rSr% SrSr\\S'   Sr\	\S'   Sr
\\S'   S	r\	\S
'   Sr\\\R                   \\   4      \S'   Sr\\S'   S rSrg)MagCacheConfigS   a  
Configuration for [MagCache](https://github.com/Zehong-Ma/MagCache).

Args:
    threshold (`float`, defaults to `0.06`):
        The threshold for the accumulated error. If the accumulated error is below this threshold, the block
        computation is skipped. A higher threshold allows for more aggressive skipping (faster) but may degrade
        quality.
    max_skip_steps (`int`, defaults to `3`):
        The maximum number of consecutive steps that can be skipped (K in the paper).
    retention_ratio (`float`, defaults to `0.2`):
        The fraction of initial steps during which skipping is disabled to ensure stability. For example, if
        `num_inference_steps` is 28 and `retention_ratio` is 0.2, the first 6 steps will never be skipped.
    num_inference_steps (`int`, defaults to `28`):
        The number of inference steps used in the pipeline. This is required to interpolate `mag_ratios` correctly.
    mag_ratios (`torch.Tensor`, *optional*):
        The pre-computed magnitude ratios for the model. These are checkpoint-dependent. If not provided, you must
        set `calibrate=True` to calculate them for your specific model. For Flux models, you can use
        `diffusers.hooks.mag_cache.FLUX_MAG_RATIOS`.
    calibrate (`bool`, defaults to `False`):
        If True, enables calibration mode. In this mode, no blocks are skipped. Instead, the hook calculates the
        magnitude ratios for the current run and logs them at the end. Use this to obtain `mag_ratios` for new
        models or schedulers.
gQ?	threshold   max_skip_stepsg?retention_ratio   num_inference_stepsN
mag_ratiosF	calibratec                 ,   U R                   c  U R                  (       d  [        S5      eU R                  (       d  U R                   b  [        R                  " U R                   5      (       d%  [        R
                  " U R                   5      U l         [        U R                   5      U R                  :w  a^  [        R                  S[        U R                   5       SU R                   35        [        U R                   U R                  5      U l         g g g g )Nat   `mag_ratios` must be provided for MagCache inference because these ratios are model-dependent.
To get them for your model:
1. Initialize `MagCacheConfig(calibrate=True, ...)`
2. Run inference on your model once.
3. Copy the printed ratios array and pass it to `mag_ratios` in the config.
For Flux models, you can import `FLUX_MAG_RATIOS` from `diffusers.hooks.mag_cache`.z%Interpolating mag_ratios from length z to )r8   r9   
ValueErrorr#   	is_tensortensorr"   r7   loggerdebugr-   selfs    r,   __post_init__MagCacheConfig.__post_init__u   s    ??"4>>f  ~~$//"=??4??33"',,t"?4??#t'?'??;C<P;QQUVZVnVnUop #1$BZBZ"[	 @	 #>~r.   )r8   )__name__
__module____qualname____firstlineno____doc__r2   float__annotations__r4   intr5   r7   r8   r   r   r#   Tensorr   r9   boolrB   __static_attributes__ r.   r,   r0   r0   S   sc    2 IuNC OU !!=AJu||T%[89:AIt\r.   r0   c                   2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )MagCacheState   c                    > [         TU ]  5         S U l        S U l        SU l        SU l        SU l        SU l        SU l        / U l	        g NTr           r   )
super__init__previous_residualhead_block_inputshould_computeaccumulated_ratioaccumulated_erraccumulated_steps
step_indexcalibration_ratios)rA   	__class__s    r,   rW   MagCacheState.__init__   sW    /3 PT$( ),&)&'  ! 02r.   c                 f    S U l         SU l        SU l        SU l        SU l        SU l        / U l        g rT   )rX   rZ   r[   r\   r]   r^   r_   r@   s    r,   resetMagCacheState.reset   s9    !%"!$"!""$r.   )r\   r[   r]   r_   rY   rX   rZ   r^   )r   N)rD   rE   rF   rG   rW   rc   rN   __classcell__r`   s   @r,   rQ   rQ      s    2(% %r.   rQ   c                       \ rS rSrSrS\S\4S jrS r\	R                  R                  S\	R                  R                  4S j5       rS	 rS
rg)MagCacheHeadHook   Tstate_managerconfigc                 *    Xl         X l        S U l        g N)rj   rk   	_metadata)rA   rj   rk   s      r,   rW   MagCacheHeadHook.__init__   s    *r.   c                 f    [        U5      n[        R                  " UR                  5      U l        U$ rm   r
   r   getr`   rn   rA   moduleunwrapped_modules      r,   initialize_hook MagCacheHeadHook.initialize_hook   +    (01556F6P6PQr.   rt   c                    U R                   R                  c  U R                   R                  S5        U R                  R                  nU R                  R                  XBU5      nU R                   R                  5       nXVl        SnU R                  R                  (       a  SnGOKUR                  nU[        U R                  R                  5      :  a  Sn	OU R                  R                  U   n	[        U R                  R                  U R                  R                  -  S-   5      n
X:  a  U=R                   U	-  sl        U=R"                  S-  sl        U=R$                  ['        SUR                   -
  5      -  sl        UR(                  bK  UR$                  U R                  R*                  ::  a'  UR"                  U R                  R,                  ::  a  SnOSUl        SUl        SUl        Xvl        U(       GdH  [0        R3                  S	UR                   35        UnUR(                  nUR4                  UR4                  :w  a  UR7                  UR4                  5      nUR8                  UR8                  :X  a  X-   nGOUR:                  S
:X  a  UR:                  S
:X  a  UR8                  S   UR8                  S   :X  a  UR8                  S   UR8                  S   :X  a  UR8                  S   UR8                  S   -
  nUS:  a.  UR=                  5       nUS S 2US 2S S 24   U-   US S 2US 2S S 24'   Oa[0        R?                  SUR8                   SUR8                   S35        O0[0        R?                  SUR8                   SUR8                   S35        U R                  R@                  b  U R                  R                  SX#5      n[C        U R                  RD                  U R                  R@                  5      nS /US-   -  nUUU R                  RD                  '   UUU R                  R@                  '   [G        U5      $ U$ U RH                  RJ                  " U0 UD6nU$ )N	inferenceTr   g      ?r   Fr   rU   zMagCache: Skipping step r3   r   z$MagCache: Dimension mismatch. Input z, Residual zA. Cannot apply residual safely. Returning input without residual.encoder_hidden_states)&rj   _current_contextset_contextrn   hidden_states_argument_name_get_parameter_from_args_kwargs	get_staterY   rk   r9   r^   r"   r8   rK   r5   r7   r[   r]   r\   absrX   r2   r4   rZ   r>   r?   r    toshapendimclonewarning"return_encoder_hidden_states_indexmaxreturn_hidden_states_indextuplefn_reforiginal_forward)rA   rt   argskwargsarg_namehidden_statesstaterZ   current_stepcurrent_scaleretention_stepoutputresdifforiginal_encoder_hidden_statesmax_idxret_lists                    r,   new_forwardMagCacheHeadHook.new_forward   s   ..6**;7>>==FFxW]^#11;;=!.;;  !N !++Ls4;;#9#9:: # $ 6 6| D !<!<t{{?^?^!^ad!deN-''=8'''1,'%%S53J3J-J)KK% ++7--1F1FF//4;;3M3MM%*N.1E+./E+,/E)-LL3E4D4D3EFG #F))CzzV]]*ffV]]+ yyFLL(q HHMLLOsyy|3LLOsyy|3 ||A15!8#\\^F*0DE1*=*CF1deQ;'NN>v||nKX[XaXaWb cZ Z
 :6<<.TWT]T]S^ _V V
 ~~@@L151_1_+T2. NN==t~~?p?p !6Wq[1FLBBCNlJJKX& [[114B6BFMr.   c                 :    U R                   R                  5         U$ rm   )rj   rc   )rA   rt   s     r,   reset_stateMagCacheHeadHook.reset_state  s      "r.   )rn   rk   rj   N)rD   rE   rF   rG   _is_statefulr   r0   rW   rv   r#   compilerdisablennModuler   r   rN   rO   r.   r,   rh   rh      sU    Ll N 

 ^^_%((// _ _Br.   rh   c                      ^  \ rS rSrSS\S\S\4U 4S jjjrS r\	R                  R                  S\	R                  R                  4S j5       rS	\S
\	R                   4S jrS	\4S jrSrU =r$ )MagCacheBlockHooki  rj   is_tailrk   c                 T   > [         TU ]  5         Xl        X l        X0l        S U l        g rm   )rV   rW   rj   r   rk   rn   )rA   rj   r   rk   r`   s       r,   rW   MagCacheBlockHook.__init__   s%    *r.   c                 f    [        U5      n[        R                  " UR                  5      U l        U$ rm   rq   rs   s      r,   rv   !MagCacheBlockHook.initialize_hook'  rx   r.   rt   c                 H   U R                   R                  c  U R                   R                  S5        U R                   R                  5       nUR                  (       Gd  U R
                  R                  nU R
                  R                  XRU5      nU R                  (       a  U R                  U5        U R
                  R                  b  U R
                  R                  SX#5      n[        U R
                  R                  U R
                  R                  5      nS /US-   -  n	XiU R
                  R                  '   XyU R
                  R                  '   [        U	5      $ U$ U R                  R                  " U0 UD6n
U R                  (       Ga  [!        U
[        5      (       a  XR
                  R                     nOU
nUR"                  nUc  U
$ UR$                  UR$                  :X  a  X-
  nOqUR&                  S:X  a_  UR&                  S:X  aO  UR$                  S   UR$                  S   :X  a/  UR$                  S   UR$                  S   -
  nUS:X  a  X-
  nOX-
  nOUnU R(                  R*                  (       a  U R-                  XM5        Xl        U R                  U5        U
$ )Nrz   r{   r   r3   r   r   )rj   r|   r}   r   rZ   rn   r~   r   r   _advance_stepr   r   r   r   r   r   
isinstancerY   r   r   rk   r9   _perform_calibration_steprX   )rA   rt   r   r   r   r   r   r{   r   r   r   
out_hidden	in_hiddenresidualr   s                  r,   r   MagCacheBlockHook.new_forward,  s7   ..6**;7#11;;=###~~AAH NNJJ8[abM||""5)~~@@L(,(V(V+T)% NN==t~~?p?p !6Wq[1FSBBCNcJJKX&  --t>v><<<&%((#NN$M$MN
#
..I  9??2%1A%)..A*=*BRBRSTBUYbYhYhijYkBk q)J,<,<Q,??19)5H)5H &{{$$..u?&.#u%r.   r   current_residualc                 d   UR                   c  SnO[        R                  R                  UR	                  5       SS9n[        R                  R                  UR                   R	                  5       SS9nXES-   -  R                  5       R                  5       nUR                  R                  U5        g )Nr   r   )dimg:0yE>)	rX   r#   linalgnormrI   meanitemr_   append)rA   r   r   ratio	curr_norm	prev_norms         r,   r   +MagCacheBlockHook._perform_calibration_stepk  s    ""* E ))*:*@*@*B)KI))%*A*A*G*G*Ir)RI d"2399;@@BE  ''.r.   c                    U=R                   S-  sl         UR                   U R                  R                  :  a  U R                  R                  (       aE  [	        S5        [	        UR
                   S35        [        R                  SUR
                   35        SUl         SUl        SUl	        SUl
        S Ul        / Ul        g g )Nr   zV
[MagCache] Calibration Complete. Copy these values to MagCacheConfig(mag_ratios=...):
zMagCache Calibration Results: r   r   rU   )r^   rk   r7   r9   printr_   r>   infor[   r]   r\   rX   )rA   r   s     r,   r   MagCacheBlockHook._advance_step{  s    At{{>>>{{$$op112"56<U=U=U<VWX  !E&)E#&'E#$'E!&*E#')E$ ?r.   )rn   rk   r   rj   )FN)rD   rE   rF   rG   r   rM   r0   rW   rv   r#   r   r   r   r   r   rQ   rL   r   r   rN   re   rf   s   @r,   r   r     s    l T Sa  
 ^^<%((// < <|/} /PUP\P\ / *= * *r.   r   rt   rk   c                    [         R                  " U 5        [        [        S0 5      n/ nU R	                  5        Hf  u  pEU[
        ;  d)  [        U[        R                  R                  5      (       d  M:  [        U5       H  u  pgUR                  U SU 3U45        M     Mh     U(       d  [        R                  S5        g[        U5      S:X  a8  US   u  pG[        R                  SU S35        [!        XrUS	S
9  [#        XrU5        gUR%                  S5      u  pUR%                  S5      u  p[        R                  SU 35        [#        XU5        U H  u  pG[!        XrU5        M     [        R                  SU
 35        [!        XUS	S
9  g)z
Applies MagCache to a given module (typically a Transformer).

Args:
    module (`torch.nn.Module`):
        The module to apply MagCache to.
    config (`MagCacheConfig`):
        The configuration for MagCache.
rO   .z5MagCache: No transformer blocks found to apply hooks.Nr   r   z4MagCache: Applying Head+Tail Hooks to single block ''T)r   r   z MagCache: Applying Head Hook to z MagCache: Applying Tail Hook to )r   check_if_exists_or_initializer   rQ   named_childrenr   r   r#   r   
ModuleList	enumerater   r>   r   r"   r   _apply_mag_cache_block_hook_apply_mag_cache_head_hookpop)rt   rk   rj   remaining_blocksname	submoduleindexblockhead_block_name
head_blocktail_block_name
tail_blocks               r,   apply_mag_cacher     sn    ..v6 B7M!00299IW\W_W_WjWjAkAk%i0LE##vQug%6$>? 1 3 NO !&q)J4&PQRS#E&$O"5@"2"6"6q"9O"2"6"6r":O
KK2?2CDEz&A'#E&A ( KK2?2CDE
64Pr.   r   rj   c                     [         R                  " U 5      nUR                  [        5      b  UR	                  [        5        [        X5      nUR                  U[        5        g rm   )r   r   get_hook_MAG_CACHE_LEADER_BLOCK_HOOKremove_hookrh   register_hook)r   rj   rk   registryhooks        r,   r   r     sP    99%@H 56B9:M2D4!=>r.   r   c                     [         R                  " U 5      nUR                  [        5      b  UR	                  [        5        [        XU5      nUR                  U[        5        g rm   )r   r   r   _MAG_CACHE_BLOCK_HOOKr   r   r   )r   rj   rk   r   r   r   s         r,   r   r     sT     99%@H ./;23]V<D4!67r.   )F)(dataclassesr   typingr   r   r   r   r#   utilsr	   utils.torch_utilsr
   _commonr   _helpersr   hooksr   r   r   r   rD   r>   r   r   r=   FLUX_MAG_RATIOSrL   rK   r-   r0   rQ   rh   r   r   r   r   r   rM   r   rO   r.   r,   <module>r      sX   " / /   - 7 . C C 
H	< . 
 ,,ED%ell %3 %5<< % 5\ 5\ 5\p%I %>qy qhk*	 k*\,QEHHOO ,Q^ ,Q ,Q^?ehhoo ?l ?\j ?os ? 	888??88 8 	8
 
8r.   