
    +hL                     @    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/james-whalen/.local/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  U R                  (       a"  [        S[        U R                  5       S35      e[        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 (`Union[PyramidAttentionBroadcastConfig]`):
        The configuration for applying the caching technique. Currently supported caching techniques are:
            - [`~hooks.PyramidAttentionBroadcastConfig`]

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PyramidAttentionBroadcastConfigapply_faster_cacheapply_first_block_cache!apply_pyramid_attention_broadcastz&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   
ValueErrortyper   
isinstance)r   configr   r   r   r   r   r   s           r   enable_cacheCacheMixin.enable_cache'   s    6	
 	
   8d>P>P9Q8R  S]  ^  f00t,66#D1@@-d;}T&\N:LMNN#r   c                 J   SSK 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	  U R                  c  [        R                  S5        g UR!                  U 5      n
[#        U R                  U5      (       a!  U
R%                  USS9  U
R%                  USS9  O[#        U R                  U5      (       a!  U
R%                  USS9  U
R%                  USS9  ON[#        U R                  U5      (       a  U
R%                  U	SS9  O"['        S	[)        U R                  5       S
35      eS U l        g )Nr   )r   r   HookRegistryr   )_FASTER_CACHE_BLOCK_HOOK_FASTER_CACHE_DENOISER_HOOK)_FBC_BLOCK_HOOK_FBC_LEADER_BLOCK_HOOK)!_PYRAMID_ATTENTION_BROADCAST_HOOKzHCaching techniques have not been enabled, so there's nothing to disable.Trecurser   r   )r   r   r   r#   r   hooks.faster_cacher$   r%   hooks.first_block_cacher&   r'   !hooks.pyramid_attention_broadcastr(   r   loggerwarningcheck_if_exists_or_initializer   remove_hookr   r   )r   r   r   r#   r   r$   r%   r&   r'   r(   registrys              r   disable_cacheCacheMixin.disable_cache[   s   ss^UY%NNef==dCd((*;<<  !<d K  !94 H**,ABB  !7 F  $ ?**,KLL  !BD Q}T$2D2D-E,FFXYZZ!r   r*   c                 L    SSK Jn  UR                  U 5      R                  US9  g )Nr   r#   r)   )r   r#   r0   reset_stateful_hooks)r   r*   r#   s      r   _reset_stateful_cache CacheMixin._reset_stateful_caches   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   r6   N)r   r#   r0   _set_context)r   r:   r#   r2   s       r   cache_contextCacheMixin.cache_contextx   s9      	)==dCd#d#s   ?Ar   )r	   N)T)__name__
__module____qualname____firstlineno____doc__r   propertyboolr   r    r3   r8   r   strr=   __static_attributes__ r   r   r   r      sc     M.$ . .2$h"0_T _T _
 	$# 	$ 	$r   r   N)
contextlibr   utils.loggingr   r?   r.   r   rH   r   r   <module>rK      s%    & & 
H	k$ k$r   