
    bi                        d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z	ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ  ej*                  e      ZdZdZdZdZeez   ZdZe G d d             Z G d d      Z G d d      Z  G d de      Z! G d de      Z"de	jF                  jH                  deddfdZ%de	jF                  jH                  deddfdZ&de'dededdfd Z( e	jR                         d!        Z*y)"    N)	dataclass)AnyCallableListOptionalTuple   )AttentionModuleMixin)Transformer2DModelOutput)logging   )_ATTENTION_CLASSES)HookRegistry	ModelHookfaster_cache_denoiserfaster_cache_block)z^blocks.*attnz^transformer_blocks.*attnz ^single_transformer_blocks.*attn)z"^temporal_transformer_blocks.*attn)hidden_statesencoder_hidden_statestimestepattention_maskencoder_attention_maskc                       e Zd ZU dZdZeed<   dZee   ed<   dZ	e
eef   ed<   dZe
eef   ed<   d	Ze
eef   ed
<   dZe
eef   ed<   dZeed<   dZeed<   dZeed<   dZe
eef   ed<   eZe
edf   ed<   eZe
edf   ed<   dZeej6                  j8                  gef   ed<   dZeej6                  j8                  gef   ed<   dZeej6                  j8                  gef   ed<   dZeed<   dZ e!ed<   dZ"eg ef   ed<   e#Z$e%e   ed<   d efd!Z&y)"FasterCacheConfigas  
    Configuration for [FasterCache](https://huggingface.co/papers/2410.19355).

    Attributes:
        spatial_attention_block_skip_range (`int`, defaults to `2`):
            Calculate the attention states every `N` iterations. If this is set to `N`, the attention computation will
            be skipped `N - 1` times (i.e., cached attention states will be re-used) before computing the new attention
            states again.
        temporal_attention_block_skip_range (`int`, *optional*, defaults to `None`):
            Calculate the attention states every `N` iterations. If this is set to `N`, the attention computation will
            be skipped `N - 1` times (i.e., cached attention states will be re-used) before computing the new attention
            states again.
        spatial_attention_timestep_skip_range (`Tuple[float, float]`, defaults to `(-1, 681)`):
            The timestep range within which the spatial attention computation can be skipped without a significant loss
            in quality. This is to be determined by the user based on the underlying model. The first value in the
            tuple is the lower bound and the second value is the upper bound. Typically, diffusion timesteps for
            denoising are in the reversed range of 0 to 1000 (i.e. denoising starts at timestep 1000 and ends at
            timestep 0). For the default values, this would mean that the spatial attention computation skipping will
            be applicable only after denoising timestep 681 is reached, and continue until the end of the denoising
            process.
        temporal_attention_timestep_skip_range (`Tuple[float, float]`, *optional*, defaults to `None`):
            The timestep range within which the temporal attention computation can be skipped without a significant
            loss in quality. This is to be determined by the user based on the underlying model. The first value in the
            tuple is the lower bound and the second value is the upper bound. Typically, diffusion timesteps for
            denoising are in the reversed range of 0 to 1000 (i.e. denoising starts at timestep 1000 and ends at
            timestep 0).
        low_frequency_weight_update_timestep_range (`Tuple[int, int]`, defaults to `(99, 901)`):
            The timestep range within which the low frequency weight scaling update is applied. The first value in the
            tuple is the lower bound and the second value is the upper bound of the timestep range. The callback
            function for the update is called only within this range.
        high_frequency_weight_update_timestep_range (`Tuple[int, int]`, defaults to `(-1, 301)`):
            The timestep range within which the high frequency weight scaling update is applied. The first value in the
            tuple is the lower bound and the second value is the upper bound of the timestep range. The callback
            function for the update is called only within this range.
        alpha_low_frequency (`float`, defaults to `1.1`):
            The weight to scale the low frequency updates by. This is used to approximate the unconditional branch from
            the conditional branch outputs.
        alpha_high_frequency (`float`, defaults to `1.1`):
            The weight to scale the high frequency updates by. This is used to approximate the unconditional branch
            from the conditional branch outputs.
        unconditional_batch_skip_range (`int`, defaults to `5`):
            Process the unconditional branch every `N` iterations. If this is set to `N`, the unconditional branch
            computation will be skipped `N - 1` times (i.e., cached unconditional branch states will be re-used) before
            computing the new unconditional branch states again.
        unconditional_batch_timestep_skip_range (`Tuple[float, float]`, defaults to `(-1, 641)`):
            The timestep range within which the unconditional branch computation can be skipped without a significant
            loss in quality. This is to be determined by the user based on the underlying model. The first value in the
            tuple is the lower bound and the second value is the upper bound.
        spatial_attention_block_identifiers (`Tuple[str, ...]`, defaults to `("blocks.*attn1", "transformer_blocks.*attn1", "single_transformer_blocks.*attn1")`):
            The identifiers to match the spatial attention blocks in the model. If the name of the block contains any
            of these identifiers, FasterCache will be applied to that block. This can either be the full layer names,
            partial layer names, or regex patterns. Matching will always be done using a regex match.
        temporal_attention_block_identifiers (`Tuple[str, ...]`, defaults to `("temporal_transformer_blocks.*attn1",)`):
            The identifiers to match the temporal attention blocks in the model. If the name of the block contains any
            of these identifiers, FasterCache will be applied to that block. This can either be the full layer names,
            partial layer names, or regex patterns. Matching will always be done using a regex match.
        attention_weight_callback (`Callable[[torch.nn.Module], float]`, defaults to `None`):
            The callback function to determine the weight to scale the attention outputs by. This function should take
            the attention module as input and return a float value. This is used to approximate the unconditional
            branch from the conditional branch outputs. If not provided, the default weight is 0.5 for all timesteps.
            Typically, as described in the paper, this weight should gradually increase from 0 to 1 as the inference
            progresses. Users are encouraged to experiment and provide custom weight schedules that take into account
            the number of inference steps and underlying model behaviour as denoising progresses.
        low_frequency_weight_callback (`Callable[[torch.nn.Module], float]`, defaults to `None`):
            The callback function to determine the weight to scale the low frequency updates by. If not provided, the
            default weight is 1.1 for timesteps within the range specified (as described in the paper).
        high_frequency_weight_callback (`Callable[[torch.nn.Module], float]`, defaults to `None`):
            The callback function to determine the weight to scale the high frequency updates by. If not provided, the
            default weight is 1.1 for timesteps within the range specified (as described in the paper).
        tensor_format (`str`, defaults to `"BCFHW"`):
            The format of the input tensors. This should be one of `"BCFHW"`, `"BFCHW"`, or `"BCHW"`. The format is
            used to split individual latent frames in order for low and high frequency components to be computed.
        is_guidance_distilled (`bool`, defaults to `False`):
            Whether the model is guidance distilled or not. If the model is guidance distilled, FasterCache will not be
            applied at the denoiser-level to skip the unconditional branch computation (as there is none).
        _unconditional_conditional_input_kwargs_identifiers (`List[str]`, defaults to `("hidden_states", "encoder_hidden_states", "timestep", "attention_mask", "encoder_attention_mask")`):
            The identifiers to match the input kwargs that contain the batchwise-concatenated unconditional and
            conditional inputs. If the name of the input kwargs contains any of these identifiers, FasterCache will
            split the inputs into unconditional and conditional branches. This must be a list of exact input kwargs
            names that contain the batchwise-concatenated unconditional and conditional inputs.
    r	   "spatial_attention_block_skip_rangeN#temporal_attention_block_skip_range)i  %spatial_attention_timestep_skip_range&temporal_attention_timestep_skip_range)c   i  *low_frequency_weight_update_timestep_range)r   i-  +high_frequency_weight_update_timestep_rangeg?alpha_low_frequencyalpha_high_frequency   unconditional_batch_skip_range)r   i  'unconditional_batch_timestep_skip_range.#spatial_attention_block_identifiers$temporal_attention_block_identifiersattention_weight_callbacklow_frequency_weight_callbackhigh_frequency_weight_callbackBCFHWtensor_formatFis_guidance_distilledcurrent_timestep_callback3_unconditional_conditional_input_kwargs_identifiersreturnc                 Z   d| j                    d| j                   d| j                   d| j                   d| j                   d| j
                   d| j                   d| j                   d	| j                   d
| j                   d| j                   d| j                   d| j                   dS )Nz8FasterCacheConfig(
  spatial_attention_block_skip_range=z(,
  temporal_attention_block_skip_range=z*,
  spatial_attention_timestep_skip_range=z+,
  temporal_attention_timestep_skip_range=z/,
  low_frequency_weight_update_timestep_range=z0,
  high_frequency_weight_update_timestep_range=z,
  alpha_low_frequency=z,
  alpha_high_frequency=z#,
  unconditional_batch_skip_range=z,,
  unconditional_batch_timestep_skip_range=z(,
  spatial_attention_block_identifiers=z),
  temporal_attention_block_identifiers=z,
  tensor_format=z,
))r   r   r   r   r    r!   r"   r#   r%   r&   r'   r(   r-   selfs    W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/hooks/faster_cache.py__repr__zFasterCacheConfig.__repr__   s    4484[4[3\ ]5595]5]4^ _77;7a7a6b c88<8c8c7d e<<@<k<k;l m==A=m=m<n o%%)%=%=$> ?&&*&?&?%@ A0040S0S/T U99=9e9e8f g5595]5]4^ _66:6_6_5` a#112 3	
    )'__name__
__module____qualname____doc__r   int__annotations__r   r   r   r   r   r    r!   r"   floatr#   r%   r&   $_SPATIAL_ATTENTION_BLOCK_IDENTIFIERSr'   str%_TEMPORAL_ATTENTION_BLOCK_IDENTIFIERSr(   r)   r   torchnnModuler*   r+   r-   r.   boolr/   %_UNCOND_COND_INPUT_KWARGS_IDENTIFIERSr0   r   r6    r7   r5   r   r   1   sw   Ph /0&/9='#==F)5c?F>G*E#s(OG CL.c3hKCL/sCxL "%$"%%% +,"C+?H+U38_H;_'sCx_<a(%S/aDHx(95(@AHHL!8UXX__,=u,D#ELIM"Hehhoo->-E$FM M3 "'4'37xC07Ej7cj
# 
r7   r   c                       e Zd ZdZddZd Zy)FasterCacheDenoiserStatezf
    State for [FasterCache](https://huggingface.co/papers/2410.19355) top-level denoiser module.
    Nc                 .    d| _         d | _        d | _        y Nr   	iterationlow_frequency_deltahigh_frequency_deltar3   s    r5   __init__z!FasterCacheDenoiserState.__init__   s    15 26!r7   c                 .    d| _         d | _        d | _        y rK   rL   r3   s    r5   resetzFasterCacheDenoiserState.reset   s    #' $(!r7   r1   Nr8   r9   r:   r;   rP   rR   rG   r7   r5   rI   rI      s    7
)r7   rI   c                       e Zd ZdZddZd Zy)FasterCacheBlockStatez
    State for [FasterCache](https://huggingface.co/papers/2410.19355). Every underlying block that FasterCache is
    applied to will have an instance of this state.
    Nc                 .    d| _         d | _        d | _        y rK   rM   
batch_sizecacher3   s    r5   rP   zFasterCacheBlockState.__init__   s    #8<
r7   c                 .    d| _         d | _        d | _        y rK   rX   r3   s    r5   rR   zFasterCacheBlockState.reset   s    
r7   rS   rT   rG   r7   r5   rV   rV      s    
=
r7   rV   c                       e Zd ZdZdedeeef   dededee   de	g ef   de	e
j                  j                  ge
j                  f   d	e	e
j                  j                  ge
j                  f   d
df fdZd Zede
j                  d
ee
j                  e
j                  f   fd       Zde
j                  j                  d
efdZde
j                  j                  d
e
j                  j                  fdZ xZS )FasterCacheDenoiserHookTr%   r&   r-   r.   $uncond_cond_input_kwargs_identifiersr/   r*   r+   r1   Nc	                     t         	|           || _        || _        || _        || _        || _        || _        || _        || _	        y N)
superrP   r%   r&   r^   r-   r.   r/   r*   r+   )
r4   r%   r&   r-   r.   r^   r/   r*   r+   	__class__s
            r5   rP   z FasterCacheDenoiserHook.__init__   sS     	.L+7^4
 5Y1*%:")B&-J*.L+r7   c                 $    t               | _        |S r`   )rI   stater4   modules     r5   initialize_hookz'FasterCacheDenoiserHook.initialize_hook   s    -/
r7   inputc                 2    | j                  dd      \  }}|S )Nr	   r   dim)chunk)rh   _conds      r5   _get_cond_inputz'FasterCacheDenoiserHook._get_cond_input   s     ++aQ+'4r7   rf   c                 
     j                   d    j                         cxk  xr  j                   d   k  nc } j                  j                  dkD  xr9 |xr5  j                  j                   j                  z  dk7  xr  j
                   }|rt         fd|j                         D              }|rt        j                  d       t        |D cg c]*  }t        j                  |      r j                  |      n|, c}      }|j                         D 	ci c]'  \  }}	|| j                  vr|	n j                  |	      ) }}}	  j                   j"                  |i |}
 j
                  r! j                  xj                  dz  c_        |
S t        j                  |
      r|
}nt%        |
t        t&        f      r|
d   }j)                  d      }|r j                  j*                   j-                  |      z   j                  _         j                  j.                   j1                  |      z   j                  _         j2                  dk(  r|j5                  ddddd      } j2                  dk(  s j2                  d	k(  r|j7                  dd      }t9        |j;                               \  }} j                  j*                  |z   } j                  j.                  |z   }||z   }t        j<                  j?                  |      }t        j<                  jA                  |      jB                  } j2                  dk(  s j2                  d	k(  r(|jE                  d|d
f      }|jE                  d|d
f      } j2                  dk(  r*|j5                  ddddd      }|j5                  ddddd      }|jG                  |jH                        }t        jJ                  ||gd      }n|jM                  dd      \  }} j2                  dk(  r*|j5                  ddddd      }|j5                  ddddd      } j2                  dk(  s j2                  d	k(  r$|j7                  dd      }|j7                  dd      }t9        |j;                               \  }}t9        |j;                               \  }}||z
   j                  _        ||z
   j                  _         j                  xj                  dz  c_        t        j                  |
      r|}
|
S t%        |
t              r|g|
dd  }
|
S ||
_'        |
S c c}w c c}	}w )Nr   r   c              3   :   K   | ]  }|j                   v   y wr`   )r^   ).0kr4   s     r5   	<genexpr>z6FasterCacheDenoiserHook.new_forward.<locals>.<genexpr>  s     %lYZa4+T+T&T%ls   z7FasterCache - Skipping unconditional branch computationr,   r	         BFCHWr   rj   )(r&   r/   rd   rM   r%   r.   anykeysloggerdebugtuplerB   	is_tensorro   itemsr^   fn_reforiginal_forward
isinstancer   sizerN   r*   rO   r+   r-   permuteflatten_split_low_high_freqr>   fft	ifftshiftifft2real	unflattentodtypecatrl   sample)r4   rf   argskwargsis_within_timestep_rangeshould_skip_uncondis_any_kwarg_uncondargrs   voutputr   rY   low_freq_condhigh_freq_condlow_freq_uncondhigh_freq_unconduncond_frequncond_statescond_statess   `                   r5   new_forwardz#FasterCacheDenoiserHook.new_forward  s    88;,,.>::1=> 	! JJ  1$ /(/

$$t'J'JJaO/ ...	 	 "%%l^d^i^i^k%l"l"VWdhi]`5??3;Od2237UXXij !'1 AT%N%NNqTXThThijTkk 
 .--t>v>%%JJ  A% M??6""M(@ AB"1IM"''*
-1ZZ-K-KdNpNpO .DJJ* /3jj.M.MPTPsPsQ /DJJ+ !!W, - 5 5aAq! D!!W,0B0Bg0M - 5 5a ;,@ATATAV,W)M> #jj<<}LO#zz>>O),<<K!II//<M!IIOOM:??M!!W,0B0Bg0M - 7 7J;K L - 7 7J;K L!!W, - 5 5aAq! D - 5 5aAq! D *,,]-@-@AM!II}m&D!LM)6)<)<QA)<)F&M;!!W, - 5 5aAq! D)11!Q1a@!!W,0B0Bg0M - 5 5a ;)11!Q70D]EXEXEZ0[-O-,@ARARAT,U)M>-<}-LDJJ*.>.ODJJ+

!??6""F  &#1fQRj1F  *FMU js   /T8,T=c                 :    | j                   j                          |S r`   rd   rR   re   s     r5   reset_statez#FasterCacheDenoiserHook.reset_statek      

r7   )r8   r9   r:   _is_statefulr<   r   r@   rE   r   r   rB   rC   rD   TensorrP   rg   staticmethodro   r   r   r   __classcell__rb   s   @r5   r]   r]      s5   LM(+M 27sCxM 	M
  $M /33iM $,BG#4M (00A5<<0O'PM )1%((//1BELL1P(QM 
M6 u|| ellELL6P0Q  d%((// ds dL%((// ehhoo r7   r]   c                   z    e Zd ZdZdedeeef   dedeej                  j                  gef   deg ef   ddf fd	Zd
 Zdej                  dej                  dededej                  f
dZdej                  j                  defdZdej                  j                  dej                  j                  fdZ xZS )FasterCacheBlockHookTblock_skip_rangetimestep_skip_ranger.   weight_callbackr/   r1   Nc                 h    t         |           || _        || _        || _        || _        || _        y r`   )ra   rP   r   r   r.   r   r/   )r4   r   r   r.   r   r/   rb   s         r5   rP   zFasterCacheBlockHook.__init__s  s8     	 0#6 %:".)B&r7   c                 $    t               | _        |S r`   )rV   rd   re   s     r5   rg   z$FasterCacheBlockHook.initialize_hook  s    *,
r7   
t_2_outputt_outputweightrY   c                     |j                  d      |k7  r|j                  d      d|z  k(  sJ ||d  }|j                  d      |k7  r|j                  d      d|z  k(  sJ ||d  }|||z
  |z  z   S )Nr   r	   )r   )r4   r   r   r   rY   s        r5   &_compute_approximated_attention_outputz;FasterCacheBlockHook._compute_approximated_attention_output  s     ??1+ ??1%Z777#JK0J==z) ==#q:~555
,H8j0F:::r7   rf   c                    g |D cg c])  }t        j                  |      s|j                  d      + c}|j                         D cg c])  }t        j                  |      s|j                  d      + c}d   }| j                  j
                  || j                  _        | j                  d   | j                         cxk  xr | j                  d   k  nc }|sd}nD| j                  j                  dkD  xr& | j                  j                  | j                  z  dk(  }	|	 }|r'| j                  xs | j                  j
                  |k7  }|rt        j                  d       t        j                  | j                  j                  d         r?| j                  j                  \  }
}| j                  |      }| j                  |
|||      }nd}t!        | j                  j                   D ].  \  }
}| j                  |
|| j                  |      |      }||fz  }0 n1t        j                  d        | j"                  j$                  |i |}t        j                  |      rM|}| j                  s|j                  d      | j                  j
                  k(  rp|j'                  dd	      d   }nYd}|D ]R  }| j                  s>|j                  d      | j                  j
                  k(  r|j'                  dd	      d   }||fz  }T | j                  j                  ||g| j                  _        n*| j                  j                  d   |g| j                  _        | j                  xj                  dz  c_        |S c c}w c c}w )
Nr   r   Fz8FasterCache - Skipping attention and using approximationr   rG   z!FasterCache - Computing attentionr	   rj   )rB   r}   r   valuesrd   rY   r   r/   rM   r   r.   rz   r{   rZ   r   r   zipr   r   rl   )r4   rf   r   r   r   r   rY   r   should_skip_attentionshould_compute_attentionr   r   r   r   resultcache_outputouts                    r5   r   z FasterCacheBlockHook.new_forward  s)   
%)BcU__S-AchhqkB
!'GAEOOA4FaffQiG
 
 ::  ($.DJJ! $$Q'$*H*H*JhTMeMefgMhh 	! ($)!'+zz';';a'?'uDJJDXDX[_[p[pDptuDu$(@$@! $($>$>$e$**BWBW[eBe! LLSTtzz//34'+zz'7'7$
H--f5DDZQY[acmn ,/1A1A,B ((J!HH"Hd.B.B6.JJF vi'F	( LL<=1T[[114B6BF
 ??6"!L--,2C2CA2F$**J_J_2_  ,11!1;A> L '11chhqkTZZEZEZ6Z))A1)-a0C&'
 ::# ,l;DJJ $

 0 0 4lCDJJ

!M CGs   NN	N$Nc                 :    | j                   j                          |S r`   r   re   s     r5   r   z FasterCacheBlockHook.reset_state  r   r7   )r8   r9   r:   r   r<   r   rE   r   rB   rC   rD   r>   rP   rg   r   r   r   r   r   r   r   s   @r5   r   r   p  s    LCC #38_C  $	C
 "588??"3U":;C $,BG#4C 
C";,,;27,,;HM;[^;	;H%((// Hs HT%((// ehhoo r7   r   rf   configr1   c                    t         j                  d       j                  t         j                  d       d _        j                  Bt         j	                  d       dt
        j                  j                  dt        ffd}|_        j                  Bt         j	                  d	       dt
        j                  j                  dt        ffd
}|_	        g d}j                  |vrt        d| dj                   d      t        |        | j                         D ]<  \  }t        |t              st!        fdt"        D              s0t%        |       > y)a  
    Applies [FasterCache](https://huggingface.co/papers/2410.19355) to a given pipeline.

    Args:
        module (`torch.nn.Module`):
            The pytorch module to apply FasterCache to. Typically, this should be a transformer architecture supported
            in Diffusers, such as `CogVideoXTransformer3DModel`, but external implementations may also work.
        config (`FasterCacheConfig`):
            The configuration to use for FasterCache.

    Example:
    ```python
    >>> import torch
    >>> from diffusers import CogVideoXPipeline, FasterCacheConfig, apply_faster_cache

    >>> pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16)
    >>> pipe.to("cuda")

    >>> config = FasterCacheConfig(
    ...     spatial_attention_block_skip_range=2,
    ...     spatial_attention_timestep_skip_range=(-1, 681),
    ...     low_frequency_weight_update_timestep_range=(99, 641),
    ...     high_frequency_weight_update_timestep_range=(-1, 301),
    ...     spatial_attention_block_identifiers=["transformer_blocks"],
    ...     attention_weight_callback=lambda _: 0.3,
    ...     tensor_format="BFCHW",
    ... )
    >>> apply_faster_cache(pipe.transformer, config)
    ```
    a  FasterCache is a purely experimental feature and may not work as expected. Not all models support FasterCache. The API is subject to change in future releases, with no guarantee of backward compatibility. Please report any issues at https://github.com/huggingface/diffusers/issues.NzyNo `attention_weight_callback` provided when enabling FasterCache. Defaulting to using a weight of 0.5 for all timesteps.c                      y)Ng      ?rG   )rm   s    r5   <lambda>z$apply_faster_cache.<locals>.<lambda>  s    r7   zuLow frequency weight callback not provided when enabling FasterCache. Defaulting to behaviour described in the paper.rf   r1   c                     j                   d   j                         cxk  xr j                   d   k  nc }|rj                  S dS Nr   r   g      ?)r    r/   r"   rf   is_within_ranger   s     r5   r*   z9apply_faster_cache.<locals>.low_frequency_weight_callback  sR    AA!D224GCCAFG 
 2A6--IcIr7   zvHigh frequency weight callback not provided when enabling FasterCache. Defaulting to behaviour described in the paper.c                     j                   d   j                         cxk  xr j                   d   k  nc }|rj                  S dS r   )r!   r/   r#   r   s     r5   r+   z:apply_faster_cache.<locals>.high_frequency_weight_callback*  sR    BB1E224HDDQGH 
 3B6..JsJr7   )r,   rw   BCHWz`tensor_format` must be one of z
, but got .c              3   N   K   | ]  }t        j                  |      d u  y wr`   researchrr   
identifiernames     r5   rt   z%apply_faster_cache.<locals>.<genexpr>=  s!     h:ryyT*$6h   "%)rz   warningr)   r*   r{   rB   rC   rD   r>   r+   r-   
ValueError_apply_faster_cache_on_denoisernamed_modulesr   r   rx   _TRANSFORMER_BLOCK_IDENTIFIERS&_apply_faster_cache_on_attention_class)rf   r   r*   r+   supported_tensor_formats	submoduler   s    `    @r5   apply_faster_cacher     s^   @ NN	; ''/
 	 H	
 ,9(++3 D	
	J%((// 	Je 	J 0M,,,4 E	
	K588?? 	Ku 	K 1O-9#;;:;S:TT^_e_s_s^ttuvww#FF3!//1 Li)%78hIghh24FK	Lr7   c           
         t        |j                  |j                  |j                  |j                  |j
                  |j                  |j                  |j                        }t        j                  |       }|j                  |t               y r`   )r]   r%   r&   r-   r.   r0   r/   r*   r+   r   check_if_exists_or_initializeregister_hook_FASTER_CACHE_DENOISER_HOOK)rf   r   hookregistrys       r5   r   r   A  sw    "--66$$BB((,,--	D 99&AH4!<=r7   r   c                     t         fd|j                  D              xr |j                  d uxr t        |dd       }t         fd|j                  D              xr |j
                  d uxr |j                   }d\  }}}|r|j                  }|j                  }d}n|r|j
                  }|j                  }d}||t        j                  d  d	       y t        j                  d
| d         t        |||j                  |j                  |j                        }t        j                   |      }	|	j#                  |t$               y )Nc              3   N   K   | ]  }t        j                  |      d u  y wr`   r   r   s     r5   rt   z9_apply_faster_cache_on_attention_class.<locals>.<genexpr>R  s!     q
BIIj$'t3qr   is_cross_attentionFc              3   N   K   | ]  }t        j                  |      d u  y wr`   r   r   s     r5   rt   z9_apply_faster_cache_on_attention_class.<locals>.<genexpr>W  s!     r
BIIj$'t3rr   )NNNspatialtemporalz4Unable to apply FasterCache to the selected layer: "aK  " because it does not match any of the required criteria for spatial or temporal attention layers. Note, however, that this layer may still be valid for applying PAB. Please specify the correct block identifiers in the configuration or use the specialized `apply_faster_cache_on_module` function to apply FasterCache to this layer.zEnabling FasterCache (z) for layer: )rx   r'   r   getattrr(   r   r   r   r   rz   r{   r   r.   r)   r/   r   r   r   _FASTER_CACHE_BLOCK_HOOK)
r   rf   r   is_spatial_self_attentionis_temporal_self_attentionr   r   
block_typer   r   s
   `         r5   r   r   P  sk   qfFpFpqq 	=55TA	= 4e<<  	rfFqFqrr 	*66dB	*)))  9I5): !DD$JJ
	#!EE$KK
#6#>B4& I; <	
 	
LL)*]4&IJ$$((((D 99&AH4!9:r7   c                    t         j                  j                  |       }t         j                  j                  |      }| j                  dd  \  }}t        ||      dz  }t        j                  t        j                  |      t        j                  |            \  }}|dz  |dz  }	}||z
  dz  ||	z
  dz  z   |dz  k  }
|
j                  d      j                  d      j                  | j                        }| }||z  }||z  }||fS )Nr$   r	   r   )rB   r   fft2fftshiftshapeminmeshgridarange	unsqueezer   device)xr   fft_shiftedheightwidthradiusy_gridx_gridcenter_xcenter_ymasklow_freq_maskhigh_freq_masklow_freq_ffthigh_freq_ffts                  r5   r   r   }  s    
))..
C))$$S)KGGBCLMFE1$F^^ELL$8%,,u:MNNFF!Vq[hHX!#v'8Q&>>&!)KDNN1%//255ahh?M#^N.L.0M&&r7   )+r   dataclassesr   typingr   r   r   r   r   rB   models.attentionr
   models.modeling_outputsr   utilsr   _commonr   hooksr   r   
get_loggerr8   rz   r   r   r?   rA   r   rF   r   rI   rV   r]   r   rC   rD   r   r   r@   r   no_gradr   rG   r7   r5   <module>r     sL   
 ! 7 7  3 >  ' * 
		H	% 6 / ( $
 )P %!EHm!m ) % F
 F
 F
R) )  "Qi Qhs9 slXLuxx XL8I XLd XLv>EHHOO >EV >[_ >); );>R );\m );rv );Z ' 'r7   