Ë
    (tä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    úk/Volumes/fast/ai/experiments/MLX_z-image/.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}m}m}	m	}
m
} | j                  r"t        dt        | j                  «      › d�«      ‚t        ||«      r || |«       || _        y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 (`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   s               r   Úenable_cachezCacheMixin.enable_cache'   s  € ÷:	
÷ 	
÷ 	
ð × Ò ÜØ8¼¸d×>PÑ>PÓ9QÐ8Rð  S]ð  ^óð ô �fÐ/Ô0Ù˜t VÔ,ð $ˆÕô ˜Ð 5Ô6Ù# D¨&Ô1ð $ˆÕô ˜ Ô/Ù˜D &Ô)ð $ˆÕô ˜Ð ?Ô@Ù-¨d°FÔ;ð $ˆÕô ˜Ð 5Ô6Ù" 4¨Ô0ð $ˆÕô ˜}¬T°&«\¨NÐ:LÐMÓNÐNr   c                 óp  — 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(                  €t*        j-                  d«       y |j/                  | «      }t1        | j(                  |«      r.|j3                  |d	¬
«       |j3                  |d	¬
«       d | _        y t1        | j(                  |«      r.|j3                  |
d	¬
«       |j3                  |	d	¬
«       d | _        y t1        | j(                  |«      r.|j3                  |d	¬
«       |j3                  |d	¬
«       d | _        y t1        | j(                  |«      r|j3                  |d	¬
«       d | _        y t1        | j(                  |«      r|j3                  |d	¬
«       d | _        y t5        dt7        | j(                  «      › d�«      ‚)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+   Úregistrys                   r   Údisable_cachezCacheMixin.disable_cachee   sš  € ÷	
÷ 	
÷ 	_ßUßYÝYÝCà×ÑÐ%Ü�N‰NÐeÔfØà×=Ñ=¸dÓCˆÜ�d×(Ñ(Ð*;Ô<Ø× Ñ Ð!<ÀdÐ ÔKØ× Ñ Ð!9À4Ð ÔHð "ˆÕô ˜×*Ñ*Ð,AÔBØ× Ñ Ð!7ÀÐ ÔFØ× Ñ  ¸$Ð Ô?ð "ˆÕô ˜×*Ñ*¨NÔ;Ø× Ñ Ð!=ÀtÐ ÔLØ× Ñ Ð!6ÀÐ ÔEð "ˆÕô ˜×*Ñ*Ð,KÔLØ× Ñ Ð!BÈDÐ ÔQð "ˆÕô ˜×*Ñ*Ð,AÔBØ× Ñ Ð!7ÀÐ ÔFð "ˆÕô ˜}¬T°$×2DÑ2DÓ-EÐ,FÐFXÐYÓZÐZr   r-   c                 óR   — ddl m} |j                  | «      j                  |¬«       y )Nr   ©r#   r,   )r   r#   r5   Úreset_stateful_hooks)r   r-   r#   s      r   Ú_reset_stateful_cachez CacheMixin._reset_stateful_cache‹   s"   € Ý(à×2Ñ2°4Ó8×MÑMÐV]Ð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   r:   N)r   r#   r5   Ú_set_context)r   r=   r#   r7   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!   r8   r<   r   Ústrr@   © r   r   r   r      sd   „ ñð €Màð. $ò .ó ð.ó<$ó|$"ñL_¨Tð _¸Tó _ð
 ð	$ #ò 	$ó ñ	$r   r   N)Ú
contextlibr   Úutils.loggingr   rA   r3   r   rH   r   r   Ú<module>rK      s%   ðõ &å &ñ 
�HÓ	€÷C$ò C$r   