
    i+                     @    d dl mZ ddlmZ  ee      Z G d d      Zy)    )contextmanager   )
get_loggerc                   b    e Zd ZdZdZedefd       ZddZddZ	ddeddfdZ
ed	efd
       Zy)
CacheMixina  
    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                     | j                   d uS )N)_cache_config)selfs    f/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/models/cache_utils.pyis_cache_enabledzCacheMixin.is_cache_enabled#   s    !!--    c                    ddl m}m}m}m}m}m}m}m}	 | j                  r"t        dt        | j                         d      t        ||      r || |       || _        yt        ||      r || |       || _        yt        ||      r || |       || _        yt        ||      r |	| |       || _        yt        dt        |       d      )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TaylorSeerCacheConfigapply_faster_cacheapply_first_block_cache!apply_pyramid_attention_broadcastapply_taylorseer_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   
ValueErrortyper
   
isinstance)
r   configr   r   r   r   r   r   r   r   s
             r   enable_cachezCacheMixin.enable_cache'   s    6		
 		
 		
   8d>P>P9Q8R  S]  ^  f/0tV, $  56#D&1 $  ?@-dF; $  56"40 $ }T&\N:LMNNr   c                    ddl m}m}m}m}m} ddlm}m} ddl	m
}m}	 ddlm}
 ddlm} | j                   t"        j%                  d       y |j'                  |       }t)        | j                   |      r.|j+                  |d	       |j+                  |d	       d | _        y t)        | j                   |      r.|j+                  |	d	       |j+                  |d	       d | _        y t)        | j                   |      r|j+                  |
d	       d | _        y t)        | j                   |      r|j+                  |d	       d | _        y t-        d
t/        | j                          d      )Nr   )r   r   HookRegistryr   r   )_FASTER_CACHE_BLOCK_HOOK_FASTER_CACHE_DENOISER_HOOK)_FBC_BLOCK_HOOK_FBC_LEADER_BLOCK_HOOK)!_PYRAMID_ATTENTION_BROADCAST_HOOK)_TAYLORSEER_CACHE_HOOKzHCaching techniques have not been enabled, so there's nothing to disable.Trecurser   r   )r   r   r   r!   r   r   hooks.faster_cacher"   r#   hooks.first_block_cacher$   r%   !hooks.pyramid_attention_broadcastr&   hooks.taylorseer_cacher'   r
   loggerwarningcheck_if_exists_or_initializer   remove_hookr   r   )r   r   r   r!   r   r   r"   r#   r$   r%   r&   r'   registrys                r   disable_cachezCacheMixin.disable_cache_   sS   	
 	
 	_UYC%NNef==dCd((*;<  !<d K  !94 H " **,AB  !7 F  $ ? " **,KL  !BD Q " **,AB  !7 F " }T$2D2D-E,FFXYZZr   r)   c                 R    ddl m} |j                  |       j                  |       y )Nr   r!   r(   )r   r!   r0   reset_stateful_hooks)r   r)   r!   s      r   _reset_stateful_cachez CacheMixin._reset_stateful_cache   s"    (2248MMV]M^r   namec              #      K   ddl m} |j                  |       }|j                  |       d |j                  d       yw)zFContext manager that provides additional methods for cache management.r   r5   N)r   r!   r0   _set_context)r   r8   r!   r2   s       r   cache_contextzCacheMixin.cache_context   s:      	)==dCd#d#s   ?A)r   N)T)__name__
__module____qualname____doc__r
   propertyboolr   r   r3   r7   r   strr;    r   r   r   r      sd     M.$ . .6$p"B_T _T _
 	$# 	$ 	$r   r   N)
contextlibr   utils.loggingr   r<   r.   r   rC   r   r   <module>rF      s%    & & 
H	x$ x$r   