o
    ٷi                     @   sv   d dl Z d dlZd dl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 eeZejG d	d
 d
e
ZdS )    N)List   )	CacheType   )DBPruneConfig)CachedContext)init_loggerc                       s  e Zd ZU ejedZeed< ejedZ	e
e ed< ejedZe
e ed< ejedZe
e ed< ejedZe
e ed<  fddZd	d
 Z fddZdeejB fddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z  ZS )"PrunedContext)default_factorycache_configpruned_blocksactual_blockscfg_pruned_blockscfg_actual_blocksc                    s`   t    t| jtstd| jjtjkr*| j	d ur,| jj
r.td| jj| j_d S d S d S d S )Nz:PrunedContext only supports DBPruneConfig as cache_config.   )super__post_init__
isinstancer   r   
ValueError
cache_typer   DBPrunecalibrator_configforce_reduce_calibrator_vrammaxextra_cache_configdownsample_factorself	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/caching/cache_contexts/prune_context.pyr      s   

zPrunedContext.__post_init__c                 C   s   | j j}t|tjr| }| j jr|t|  }t	|dkr|t|| j
v r|t| j
| ts/J | j
| d d }dd |D }|r|t|t| }|| j j }| j jd u rZd| }n| j j}||k rj||krh|n|}ttjr|td| d|d |S )	Nr      c                 S   s   g | ]}|d kr|qS )g        r    ).0dr    r    r!   
<listcomp>7   s    z=PrunedContext.get_residual_diff_threshold.<locals>.<listcomp>r   z!Dynamic prune threshold for step z: z.6f)r   residual_diff_thresholdr   torchTensoritemenable_dynamic_prune_thresholdstrget_current_stepintresidual_diffslistsumlen#dynamic_prune_threshold_relax_ratiomax_dynamic_prune_thresholdloggerisEnabledForloggingDEBUGdebug)r   r&   stepdiffs	mean_diffrelaxed_diffr3   r    r    r!   get_residual_diff_threshold)   s6   
z)PrunedContext.get_residual_diff_thresholdc                    s2   t    |  dkr| j  | j  d S d S )Nr   )r   mark_step_beginget_current_transformer_stepr   clearr   r   r   r    r!   r>   N   s
   

zPrunedContext.mark_step_begindiffc                 C   sx   t |tjr
| }t|  }|  s(|| jvrg | j|< | j| | d S || j	vr2g | j	|< | j	| | d S N)
r   r'   r(   r)   r+   r,   is_separate_cfg_stepr.   appendcfg_residual_diffs)r   rA   r9   r    r    r!   add_residual_diffW   s   



zPrunedContext.add_residual_diffc                 C   sD   |   }|  s|| jvr|   d S d S || jvr |   d S d S rB   )r,   rC   cached_stepsadd_cached_stepcfg_cached_steps)r   curr_cached_stepr    r    r!   add_pruned_steph   s   

zPrunedContext.add_pruned_stepc                 C   (   |   s| j| d S | j| d S rB   )rC   r   rD   r   r   
num_blocksr    r    r!   add_pruned_blockr      zPrunedContext.add_pruned_blockc                 C   rL   rB   )rC   r   rD   r   rM   r    r    r!   add_actual_blockx   rP   zPrunedContext.add_actual_blockc                 C   
   | j  S rB   )r   copyr   r    r    r!   get_pruned_blocks~      
zPrunedContext.get_pruned_blocksc                 C   rR   rB   )r   rS   r   r    r    r!   get_cfg_pruned_blocks   rU   z#PrunedContext.get_cfg_pruned_blocksc                 C   rR   rB   )r   rS   r   r    r    r!   get_actual_blocks   rU   zPrunedContext.get_actual_blocksc                 C   rR   rB   )r   rS   r   r    r    r!   get_cfg_actual_blocks   rU   z#PrunedContext.get_cfg_actual_blocksc                 C      |   S rB   )get_cached_stepsr   r    r    r!   get_pruned_steps      zPrunedContext.get_pruned_stepsc                 C   rY   rB   )get_cfg_cached_stepsr   r    r    r!   get_cfg_pruned_steps   r\   z"PrunedContext.get_cfg_pruned_steps) __name__
__module____qualname__dataclassesfieldr   r   __annotations__r/   r   r   r-   r   r   r   r   r=   r>   floatr'   r(   rF   rK   rO   rQ   rT   rV   rW   rX   r[   r^   __classcell__r    r    r   r!   r	      s*   
 %	
r	   )r'   r6   rb   typingr   cache_typesr   prune_configr   cache_contextr   cache_dit.loggerr   r_   r4   	dataclassr	   r    r    r    r!   <module>   s    