ó
    oM‡jÛ  ã                   ó@   • 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    ÚªC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\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Ñ>PÓ9QÐ8Rð  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à×ÑÑ%Ü�N‰NÐeÔfØà×=Ñ=¸dÓCˆÜ�d×(Ñ(Ð*;×<Ñ<Ø× Ñ Ð!<ÀdÐ ÑKØ× Ñ Ð!9À4Ð ÓHÜ˜×*Ñ*Ð,A×BÑBØ× Ñ Ð!7ÀÐ ÑFØ× Ñ  ¸$Ð Ò?Ü˜×*Ñ*¨N×;Ñ;Ø× Ñ Ð!=ÀtÐ ÑLØ× Ñ Ð!6ÀÐ ÒEÜ˜×*Ñ*Ð,K×LÑLØ× Ñ Ð!BÈDÐ ÒQÜ˜×*Ñ*Ð,=×>Ñ>Ø× Ñ Ð!@È$Ð ÑOØ× Ñ Ð!:ÀDÐ ÒIÜ˜×*Ñ*Ð,A×BÑBØ× Ñ Ð!7ÀÐ ÒFä˜}¬T°$×2DÑ2DÓ-EÐ,FÐFXÐ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Ñ2°4Ó8×MÑMÐV]Ð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   