o
    ۷i                     @   s2   d dl mZ ddlmZ eeZG dd dZdS )    )contextmanager   )
get_loggerc                   @   s`   e Zd ZdZdZedefddZdddZddd	Z	ddeddfddZ
edefddZdS )
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                 C   s
   | j d uS )N)_cache_config)self r	   R/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/models/cache_utils.pyis_cache_enabled#   s   
zCacheMixin.is_cache_enabledc                 C   s   ddl m}m}m}m}m}m}m}m}	m	}
m
} | jr&tdt| j dt||r1|| | n6t||r<|| | n+t||rG|	| | n t||rR|
| | nt||r]|| | n
tdt| d|| _dS )a$  
        Enable caching techniques on the model.

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

        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apply_faster_cacheapply_first_block_cacheapply_mag_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   r   r   
ValueErrortyper   
isinstance)r   configr   r   r   r   r   r   r   r   r   r   r	   r	   r
   enable_cache'   s"   0





zCacheMixin.enable_cachec                 C   s`  ddl m}m}m}m}m}m} ddlm}m	} ddl
m}	m}
 ddlm}m} ddlm} ddlm} | jd u r@td d S || }t| j|rZ|j|d	d
 |j|d	d
 nQt| j|ro|j|
d	d
 |j|	d	d
 n<t| j|r|j|d	d
 |j|d	d
 n't| j|r|j|d	d
 nt| j|r|j|d	d
 ntdt| j dd | _d S )Nr   )r   r   HookRegistryr   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_HOOKzHCaching techniques have not been enabled, so there's nothing to disable.Trecurser   r   )r   r   r   r   r   r   r   hooks.faster_cacher   r    hooks.first_block_cacher!   r"   hooks.mag_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#   r$   r%   r&   registryr	   r	   r
   disable_cachee   s2    



zCacheMixin.disable_cacheTr(   c                 C   s"   ddl m} || j|d d S )Nr   r   r'   )r   r   r0   reset_stateful_hooks)r   r(   r   r	   r	   r
   _reset_stateful_cache   s   z CacheMixin._reset_stateful_cachenamec                 c   s6    ddl m} || }|| dV  |d dS )zFContext manager that provides additional methods for cache management.r   r4   N)r   r   r0   _set_context)r   r7   r   r2   r	   r	   r
   cache_context   s   

zCacheMixin.cache_context)r   N)T)__name__
__module____qualname____doc__r   propertyboolr   r   r3   r6   r   strr9   r	   r	   r	   r
   r      s    	

>&r   N)
contextlibr   utils.loggingr   r:   r.   r   r	   r	   r	   r
   <module>   s   