o
    ٷi                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ eeZded	ed
dfddZdeiZG dd deZdS )z
TeaCache backend implementation.

This module provides the TeaCache backend that implements the CacheBackend
interface using the hooks-based TeaCache system.
    )Any)init_logger)CacheBackend)TeaCacheConfig)TeaCacheHookapply_teacache_hook)DiffusionCacheConfigpipelineconfigreturnNc                    sv   t d|j|jd}| j}|j ddl} fdd}||||_t|| |j|_|| _	t
d|j d|j  dS )	z*
    Enable TeaCache for Bagel model.
    Bageltransformer_typerel_l1_threshcoefficientsr   Nc                    s    |i |S )N )selfargskwargsoriginal_forward_flowr   ^/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/cache/teacache/backend.pyforward_alias%   s   z,enable_bagel_teacache.<locals>.forward_alias$TeaCache applied with rel_l1_thresh=, transformer_class=)r   r   r   bagel_forward_flowtypes
MethodTypeforwardr   transformerloggerinfor   )r	   r
   teacache_configr    r   r   r   r   r   enable_bagel_teacache   s$   

r$   BagelPipelinec                	   @   s>   e Zd ZdZdeddfddZddeded	eddfd
dZdS )TeaCacheBackenda+  
    TeaCache implementation using hooks.

    TeaCache (Timestep Embedding Aware Cache) is an adaptive caching technique
    that speeds up diffusion inference by reusing transformer block computations
    when consecutive timestep embeddings are similar.

    The backend applies TeaCache hooks to the transformer which intercept the
    forward pass and implement the caching logic transparently.

    Example:
        >>> from vllm_omni.diffusion.data import DiffusionCacheConfig
        >>> backend = TeaCacheBackend(DiffusionCacheConfig(rel_l1_thresh=0.2))
        >>> backend.enable(pipeline)
        >>> # Generate with cache enabled
        >>> backend.refresh(pipeline, num_inference_steps=50)  # Refresh before each generation
        >>> # Access config attributes: backend.config.rel_l1_thresh
    r	   r   Nc              
   C   s   |j j}|tv rtd|  t| || j nC|j}|j j}zt|| jj| jj	d}W n t
yI } ztd|  td| dd}~ww t|| td|j d|j  d	| _dS )
a  
        Enable TeaCache on transformer using hooks.

        This creates a TeaCacheConfig from the backend's DiffusionCacheConfig
        and applies the TeaCache hook to the transformer.

        Args:
            pipeline: Diffusion pipeline instance. Extracts transformer and transformer_type:
                     - transformer: pipeline.transformer
                     - transformer_type: pipeline.transformer.__class__.__name__
        z)Using custom TeaCache enabler for model: r   z!Failed to create TeaCacheConfig: z Invalid TeaCache configuration: z. Expected keys: rel_l1_thresh, coefficients (optional). transformer_type is automatically extracted from pipeline.transformer.__class__.__name__.Nr   r   T)	__class____name__CUSTOM_TEACACHE_ENABLERSr!   r"   r
   r    r   r   r   	Exceptionerror
ValueErrorr   r   enabled)r   r	   pipeline_typer    r   r#   er   r   r   enableJ   s4   


	

zTeaCacheBackend.enableTnum_inference_stepsverbosec                 C   s~   |j }t|dr4|jtj}|dur)|jtj |r'td| d dS dS |r2t	d dS dS |r=t	d dS dS )a@  
        Refresh TeaCache state for new generation.

        Clears all cached residuals and resets counters/accumulators.
        Should be called before each generation to ensure clean state.

        Args:
            pipeline: Diffusion pipeline instance. Extracts transformer via pipeline.transformer.
            num_inference_steps: Number of inference steps for the current generation.
                                Currently not used by TeaCache but accepted for interface consistency.
            verbose: Whether to log refresh operations (default: True)
        _hook_registryNz.TeaCache state refreshed (num_inference_steps=)z+TeaCache hook not found, nothing to refreshz=Transformer has no hook registry, TeaCache may not be applied)
r    hasattrr3   get_hookr   
_HOOK_NAME
reset_hookr!   debugwarning)r   r	   r1   r2   r    hookr   r   r   refresh}   s   
zTeaCacheBackend.refresh)T)	r(   
__module____qualname____doc__r   r0   intboolr<   r   r   r   r   r&   6   s     3r&   )r?   typingr   vllm.loggerr   vllm_omni.diffusion.cache.baser   )vllm_omni.diffusion.cache.teacache.configr   'vllm_omni.diffusion.cache.teacache.hookr   r   vllm_omni.diffusion.datar   r(   r!   r$   r)   r&   r   r   r   r   <module>   s   