
    
3j                     @    S SK Jr  SSKJr  \" \5      r " S S5      rg)    )contextmanager   )
get_loggerc                   v    \ rS rSrSrSr\S\4S j5       rSS jr	SS jr
SS\SS4S	 jjr\S
\4S j5       rSrg)
CacheMixin   a  
A class for enable/disabling caching techniques on diffusion models.

Supported caching techniques:
    - [Pyramid Attention Broadcast](https://huggingface.co/papers/2408.12588)
    - [FasterCache](https://huggingface.co/papers/2410.19355)
    - [FirstBlockCache](https://github.com/chengzeyi/ParaAttention/blob/7a266123671b55e7e5a2fe9af3121f07a36afc78/README.md#first-block-cache-our-dynamic-caching)
Nreturnc                     U R                   S L$ )N_cache_config)selfs    V/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/models/cache_utils.pyis_cache_enabledCacheMixin.is_cache_enabled#   s    !!--    c                 
   SSK JnJnJnJnJnJnJnJn	J	n
J
nJnJn  U R                  (       a"  [        S[        U R                   5       S35      e[#        X5      (       a	  U" X5        O[#        X5      (       a	  U	" X5        O|[#        X5      (       a	  U
" X5        Oc[#        X5      (       a	  U" X5        OJ[#        X5      (       a	  U" X5        O1[#        X5      (       a	  U" X5        O[        S[        U5       S35      eXl        g)a  
Enable caching techniques on the model.

Args:
    config (`PyramidAttentionBroadcastConfig | FasterCacheConfig | FirstBlockCacheConfig | TextKVCacheConfig`):
        The configuration for applying the caching technique. Currently supported caching techniques are:
            - [`~hooks.PyramidAttentionBroadcastConfig`]
            - [`~hooks.FasterCacheConfig`]
            - [`~hooks.FirstBlockCacheConfig`]
            - [`~hooks.TextKVCacheConfig`]

Example:

```python
>>> import torch
>>> from diffusers import CogVideoXPipeline, PyramidAttentionBroadcastConfig

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

>>> config = PyramidAttentionBroadcastConfig(
...     spatial_attention_block_skip_range=2,
...     spatial_attention_timestep_skip_range=(100, 800),
...     current_timestep_callback=lambda: pipe.current_timestep,
... )
>>> pipe.transformer.enable_cache(config)
```
r   )FasterCacheConfigFirstBlockCacheConfigMagCacheConfigPyramidAttentionBroadcastConfigTaylorSeerCacheConfigTextKVCacheConfigapply_faster_cacheapply_first_block_cacheapply_mag_cache!apply_pyramid_attention_broadcastapply_taylorseer_cacheapply_text_kv_cachez&Caching has already been enabled with zJ. To apply a new caching technique, please disable the existing one first.Cache config  is not supported.N)hooksr   r   r   r   r   r   r   r   r   r   r   r   r   
ValueErrortyper   
isinstance)r   configr   r   r   r   r   r   r   r   r   r   r   r   s                 r   enable_cacheCacheMixin.enable_cache'   s    <	
 	
 	
 	
   8d>P>P9Q8R  S]  ^  f00t,66#D1//D)22-@@-d;66"40}T&\N:LMNN#r   c                    SSK JnJnJnJnJnJnJn  SSKJ	nJ
n	  SSKJn
Jn  SSKJnJn  SSKJn  SSKJn  SSKJnJn  U R0                  c  [2        R5                  S	5        g UR7                  U 5      n[9        U R0                  U5      (       a"  UR;                  U	S
S9  UR;                  US
S9  GO.[9        U R0                  U5      (       a!  UR;                  US
S9  UR;                  U
S
S9  O[9        U R0                  U5      (       a!  UR;                  US
S9  UR;                  US
S9  O[9        U R0                  U5      (       a  UR;                  US
S9  O[9        U R0                  U5      (       a!  UR;                  US
S9  UR;                  US
S9  ON[9        U R0                  U5      (       a  UR;                  US
S9  O"[=        S[?        U R0                  5       S35      eS U l        g )Nr   )r   r   HookRegistryr   r   r   r   )_FASTER_CACHE_BLOCK_HOOK_FASTER_CACHE_DENOISER_HOOK)_FBC_BLOCK_HOOK_FBC_LEADER_BLOCK_HOOK)_MAG_CACHE_BLOCK_HOOK_MAG_CACHE_LEADER_BLOCK_HOOK)!_PYRAMID_ATTENTION_BROADCAST_HOOK)_TAYLORSEER_CACHE_HOOK)_TEXT_KV_CACHE_BLOCK_HOOK_TEXT_KV_CACHE_TRANSFORMER_HOOKzHCaching techniques have not been enabled, so there's nothing to disable.Trecurser   r    ) r!   r   r   r)   r   r   r   r   hooks.faster_cacher*   r+   hooks.first_block_cacher,   r-   hooks.mag_cacher.   r/   !hooks.pyramid_attention_broadcastr0   hooks.taylorseer_cacher1   hooks.text_kv_cacher2   r3   r   loggerwarningcheck_if_exists_or_initializer$   remove_hookr"   r#   )r   r   r   r)   r   r   r   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   registrys                      r   disable_cacheCacheMixin.disable_cachej   s   	
 	
 	
 	_UYYCd%NNef==dCd((*;<<  !<d K  !94 H**,ABB  !7 F  $ ?**N;;  !=t L  !6 E**,KLL  !BD Q**,=>>  !@$ O  !:D I**,ABB  !7 F}T$2D2D-E,FFXYZZ!r   r5   c                 L    SSK Jn  UR                  U 5      R                  US9  g )Nr   r)   r4   )r!   r)   r>   reset_stateful_hooks)r   r5   r)   s      r   _reset_stateful_cache CacheMixin._reset_stateful_cache   s"    (2248MMV]M^r   namec              #      #    SSK Jn  UR                  U 5      nUR                  U5        Sv   UR                  S5        g7f)zFContext manager that provides additional methods for cache management.r   rD   N)r!   r)   r>   _set_context)r   rH   r)   r@   s       r   cache_contextCacheMixin.cache_context   s9      	)==dCd#d#s   ?Ar   )r	   N)T)__name__
__module____qualname____firstlineno____doc__r   propertyboolr   r&   rA   rF   r   strrK   __static_attributes__ r   r   r   r      se     M.$ . .A$F)"V_T _T _
 	$# 	$ 	$r   r   N)
contextlibr   utils.loggingr   rM   r<   r   rV   r   r   <module>rY      s%    & & 
H	M$ M$r   