o
    ٷi2                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlZddlmZmZmZ ddlmZmZmZ d dlmZ eeZejG dd	 d	ZdS )
    N)defaultdict)AnyDefaultDictDictListOptionalUnionTuple   )BasicCacheConfigExtraCacheConfigDBCacheConfig)
CalibratorCalibratorBaseCalibratorConfig)init_loggerc                   @   s  e Zd ZU dZeed< ejedZ	e
eef ed< ejedZeeef ed< ejedZeed< dZee ed< dZee ed	< dZee ed
< dZee ed< dZee ed< dZeed< dZeed< ejedZee ed< ejdd dZ e!ee"eB f ed< dZ#e"ed< dZ$eed< ejedZ%ee ed< ejdd dZ&e!ee"eB f ed< dZ'e"ed< dZ(eed< dd Z)dd Z*d d! Z+d"d# Z,d$e-fd%d&Z.d'd( Z/d)d* Z0d+d, Z1d-d. Z2d/d0 Z3d1d2 Z4d$e5eef fd3d4Z6d$e5eef fd5d6Z7d7e"e8j9B fd8d9Z:d:d; Z;d<d= Z<d>d? Z=d@dA Z>dBdC Z?dDdE Z@dFdG ZAdHdI ZBdJdK ZCdLdM ZDeEd$ee fdNdOZFdPdQ ZGdRdS ZHdTdU ZIdS )VCachedContextdefaultname)default_factorybufferscache_configextra_cache_configNcalibrator_config
calibratorencoder_calibratorcfg_calibratorcfg_encoder_calibratorr   executed_stepstransformer_executed_stepscached_stepsc                   C      t tS Nr   float r%   r%   b/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/caching/cache_contexts/cache_context.py<lambda>;       zCachedContext.<lambda>residual_diffs        accumulated_residual_diffcontinuous_cached_stepscfg_cached_stepsc                   C   r!   r"   r#   r%   r%   r%   r&   r'   A   r(   cfg_residual_diffscfg_accumulated_residual_diffcfg_continuous_cached_stepsc                 C   s   t tjrt d| j  | jjr!| jjr!| jj	du s!J d| j
d urP| j
jr:t| j
| _| jjr:t| j
| _| j
jrRt| j
| _| jjrTt| j
| _d S d S d S d S )NzCreated CachedContext: FzLcfg_compute_first must set as False if cfg_diff_compute_separate is enabled.)loggerisEnabledForloggingDEBUGinfor   r   enable_separate_cfgcfg_diff_compute_separatecfg_compute_firstr   enable_calibratorr   r   r   enable_encoder_calibratorr   r   selfr%   r%   r&   __post_init__F   s&   
zCachedContext.__post_init__c                 C      | j d ur	| j jS dS NF)r   r9   r;   r%   r%   r&   r9   \      
zCachedContext.enable_calibratorc                 C   r>   r?   )r   r:   r;   r%   r%   r&   r:   a   r@   z'CachedContext.enable_encoder_calibratorc                 C   r>   )Nresidual)r   calibrator_cache_typer;   r%   r%   r&   rB   f   r@   z#CachedContext.calibrator_cache_typereturnc                 C   s   | j d ur| j jp| j jS dS r?   )r   r9   r:   r;   r%   r%   r&   has_calibratorsk   s
   
zCachedContext.has_calibratorsc                 C   s4   | j j}| jjd ur| jj}t|tjr| }|S r"   )r   residual_diff_thresholdr   l1_hidden_states_diff_threshold
isinstancetorchTensoritem)r<   rE   r%   r%   r&   get_residual_diff_thresholds   s   z)CachedContext.get_residual_diff_thresholdc                 C   s   | j |S r"   )r   getr<   r   r%   r%   r&   
get_buffer|      zCachedContext.get_bufferc                 C   s   || j |< d S r"   r   )r<   r   bufferr%   r%   r&   
set_buffer      zCachedContext.set_bufferc                 C   s   || j v r| j |= d S d S r"   rP   rM   r%   r%   r&   remove_buffer   s   
zCachedContext.remove_bufferc                 C   s   | j   d S r"   )r   clearr;   r%   r%   r&   clear_buffers   rS   zCachedContext.clear_buffersc                 C   s  |  j d7  _ | jjs|  jd7  _n| jjs#|  s"|  jd7  _n|  r.|  jd7  _|  dkrx| j  | j	  | j
  | j  |  rx|  \}}|d urZ|  |d urb|  |  \}}|d urp|  |d urx|  |  r| jjr|  s|  \}}|d ur|  |d ur|  d S d S |  \}}|d ur|  |d ur|  d S d S |  \}}|d ur|  |d ur|  d S d S d S )Nr
   r   )r   r   r6   r   r8   is_separate_cfg_stepget_current_transformer_stepr    rU   r)   r-   r.   rD   get_calibratorsreset_cacheget_cfg_calibratorsmark_step_begin)r<   r   r   r   r   r%   r%   r&   r\      s^   



zCachedContext.mark_step_beginc                 C      | j | jfS r"   )r   r   r;   r%   r%   r&   rY      rO   zCachedContext.get_calibratorsc                 C   r]   r"   )r   r   r;   r%   r%   r&   r[      rO   z!CachedContext.get_cfg_calibratorsdiffc                 C   s   t |tjr
| }t|  }|  s/|| jvr+|| j|< |dkr-|  j|7  _d S d S d S || j	vrF|| j	|< |dkrH|  j
|7  _
d S d S d S )Nr*   )rG   rH   rI   rJ   strget_current_steprW   r)   r+   r.   r/   )r<   r^   stepr%   r%   r&   add_residual_diff   s    



zCachedContext.add_residual_diffc                 C   
   | j  S r"   )r)   copyr;   r%   r%   r&   get_residual_diffs      
z CachedContext.get_residual_diffsc                 C   rc   r"   )r.   rd   r;   r%   r%   r&   get_cfg_residual_diffs   rf   z$CachedContext.get_cfg_residual_diffsc                 C      | j S r"   )r+   r;   r%   r%   r&   get_accumulated_residual_diff      z+CachedContext.get_accumulated_residual_diffc                 C   rh   r"   )r/   r;   r%   r%   r&   !get_cfg_accumulated_residual_diff   rj   z/CachedContext.get_cfg_accumulated_residual_diffc                 C   s   |   }|  s:| jr+| jd }|| dkr*| jdkr#|  jd7  _n|  jd7  _n|  jd7  _| j| d S | jr]| jd }|| dkr\| jdkrU|  jd7  _n|  jd7  _n|  jd7  _| j| d S )Nr
   r      )r`   rW   r    r,   appendr-   r0   )r<   curr_cached_stepprev_cached_stepprev_cfg_cached_stepr%   r%   r&   add_cached_step   s(   



zCachedContext.add_cached_stepc                 C   rc   r"   )r    rd   r;   r%   r%   r&   get_cached_steps  rf   zCachedContext.get_cached_stepsc                 C   rc   r"   )r-   rd   r;   r%   r%   r&   get_cfg_cached_steps  rf   z"CachedContext.get_cfg_cached_stepsc                 C   
   | j d S Nr
   )r   r;   r%   r%   r&   r`     rf   zCachedContext.get_current_stepc                 C   ru   rv   )r   r;   r%   r%   r&   rX     rf   z*CachedContext.get_current_transformer_stepc                 C   s4   | j jsdS | j jr|  d dkS |  d dkS )NFrm   r   )r   r6   r8   rX   r;   r%   r%   r&   rW     s
   z"CachedContext.is_separate_cfg_stepc                 C   sj   | j jd ur)d}| j jD ]}|dkr|d7 }q t| j j|}ttd|| j jS ttd| j j| j jS )Nr   r
   )r   steps_computation_maskminmax_warmup_stepslistrangewarmup_interval)r<   first_continuous_compute_stepsm
max_warmupr%   r%   r&   warmup_steps  s0   
zCachedContext.warmup_stepsc                 C   s   |   | jv S r"   )r`   r   r;   r%   r%   r&   is_in_warmup8  rS   zCachedContext.is_in_warmupc                 C   s<   | j jd u rdS |  }|t| j jk r| j j| dkS dS )NFr
   )r   rw   r`   len)r<   current_stepr%   r%   r&   is_in_full_compute_steps;  s   z&CachedContext.is_in_full_compute_stepsc                 C   s   | j jS r"   )r   steps_computation_policyr;   r%   r%   r&   get_steps_computation_policyC  s   z*CachedContext.get_steps_computation_policy)J__name__
__module____qualname__r   r_   __annotations__dataclassesfielddictr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   intr   rz   r    r   r)   r   r$   r+   r,   r-   r.   r/   r0   r=   r9   r:   rB   boolrD   rK   rN   rR   rT   rV   r\   r	   rY   r[   rH   rI   rb   re   rg   ri   rk   rr   rs   rt   r`   rX   rW   propertyr   r   r   r   r%   r%   r%   r&   r      s|   
 	>	r   )r3   r   collectionsr   typingr   r   r   r   r   r   r	   rH   r   r   r   r   calibratorsr   r   r   cache_dit.loggerr   r   r1   	dataclassr   r%   r%   r%   r&   <module>   s   $