o
    ٷi|                     @   s^   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 G dd deZ
G dd	 d	ejZdS )
a  
Base cache backend interface for diffusion models.

This module defines the abstract base class that all cache backends must implement.
Cache backends provide a unified interface for applying different caching strategies
to transformer models.

Main cache backend implementations:
1. CacheDiTBackend: Implements cache-dit acceleration (DBCache, SCM, TaylorSeer) using
   the cache-dit library. Inherits from CacheBackend. Used via cache_backend="cache_dit".
2. TeaCacheBackend: Hook-based backend for TeaCache acceleration. Inherits from
   CacheBackend. Used via cache_backend="tea_cache".

All backends implement the same interface:
- enable(pipeline): Enable cache on the pipeline
- refresh(pipeline, num_inference_steps, verbose): Refresh cache state
- is_enabled(): Check if cache is enabled
    )ABCabstractmethod)AnyN)DiffusionCacheConfigc                
   @   sp   e Zd ZdZdefddZededdfdd	Zeddede	de
ddfddZde
fddZdefddZdS )CacheBackenda  
    Abstract base class for cache backends.

    All cache backend implementations (CacheDiTBackend, TeaCacheBackend, etc.) inherit
    from this base class and implement the enable() and refresh() methods to manage
    cache lifecycle.

    Cache backends apply caching strategies to transformer models to accelerate
    inference. Different backends use different underlying mechanisms (e.g., cache-dit
    library for CacheDiTBackend, hooks for TeaCacheBackend), but all share the same
    unified interface.

    Attributes:
        config: DiffusionCacheConfig instance containing cache-specific configuration parameters
        enabled: Boolean flag indicating whether cache is enabled (set to True after enable() is called)
    configc                 C   s   || _ d| _dS )z
        Initialize cache backend with configuration.

        Args:
            config: DiffusionCacheConfig instance with cache-specific parameters
        FN)r   enabled)selfr    r
   R/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/cache/base.py__init__1   s   
zCacheBackend.__init__pipelinereturnNc                 C      t d)a  
        Enable cache on the pipeline.

        This method applies the caching strategy to the transformer(s) in the pipeline.
        The specific implementation depends on the backend (e.g., hooks for TeaCacheBackend,
        cache-dit library for CacheDiTBackend). Called once during pipeline initialization.

        Args:
            pipeline: Diffusion pipeline instance. The backend can extract:
                     - transformer: via pipeline.transformer
                     - model_type: via pipeline.__class__.__name__
        z"Subclasses must implement enable()NotImplementedError)r	   r   r
   r
   r   enable;   s   zCacheBackend.enableTnum_inference_stepsverbosec                 C   r   )aQ  
        Refresh cache state for new generation.

        This method should clear any cached values and reset counters/accumulators.
        Called at the start of each generation to ensure clean state.

        Args:
            pipeline: Diffusion pipeline instance. The backend can extract:
                     - transformer: via pipeline.transformer
            num_inference_steps: Number of inference steps for the current generation.
                                May be used for cache context updates.
            verbose: Whether to log refresh operations (default: True)
        z#Subclasses must implement refresh()r   )r	   r   r   r   r
   r
   r   refreshK   s   zCacheBackend.refreshc                 C   s   | j S )z
        Check if cache is enabled on this backend.

        Returns:
            True if cache is enabled, False otherwise.
        )r   r	   r
   r
   r   
is_enabled\   s   zCacheBackend.is_enabledc                 C   s   | j j d| j dS )Nz(config=))	__class____name__r   r   r
   r
   r   __repr__e   s   zCacheBackend.__repr__T)r   
__module____qualname____doc__r   r   r   r   r   intboolr   r   strr   r
   r
   r
   r   r      s    
	r   c                       s0   e Zd Z fddZddef fddZ  ZS )CachedTransformerc                    s   t    d| _d S )NF)superr   do_true_cfg)r	   kwargsr   r
   r   r   j   s   

zCachedTransformer.__init__Tenable_separate_cfgc                    s   || _ t jdi | d S )Nr
   )r(   r$   __init_subclass__)clsr(   r&   r'   r
   r   r)   n   s   z#CachedTransformer.__init_subclass__r   )r   r   r   r   r!   r)   __classcell__r
   r
   r'   r   r#   i   s    r#   )r   abcr   r   typingr   torch.nnnnvllm_omni.diffusion.datar   r   Moduler#   r
   r
   r
   r   <module>   s   J