
    biL                     @    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    W/home/cdr/jupyterlab/.venv/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} | j                  r"t        dt        | j                         d      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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_cachezCacheMixin.enable_cache'   s    6	
 	
   8d>P>P9Q8R  S]  ^  f/0tV, $  56#D&1 $  ?@-dF; $ }T&\N:LMNNr   c                 b   ddl m}m}m}m} ddlm}m} ddlm	}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'        d	t)        | j                         d
      )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_cachezCacheMixin.disable_cache[   s   ss^UY%NNef==dCd((*;<  !<d K  !94 H " **,AB  !7 F  $ ? " **,KL  !BD Q " }T$2D2D-E,FFXYZZr   r&   c                 R    ddl m} |j                  |       j                  |       y )Nr   r   r%   )r   r   r,   reset_stateful_hooks)r   r&   r   s      r   _reset_stateful_cachez CacheMixin._reset_stateful_caches   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   r1   N)r   r   r,   _set_context)r   r4   r   r.   s       r   cache_contextzCacheMixin.cache_contextx   s:      	)==dCd#d#s   ?A)r   N)T)__name__
__module____qualname____doc__r
   propertyboolr   r   r/   r3   r   strr7    r   r   r   r      sc     M.$ . .2$h"0_T _T _
 	$# 	$ 	$r   r   N)
contextlibr   utils.loggingr   r8   r*   r   r?   r   r   <module>rB      s%    & & 
H	k$ k$r   