o
    ٷiq                     @   sh   d dl Z d dlZd dlmZmZmZmZ ddlmZ ddl	m
Z
 d dlmZ eeZG dd deZdS )	    N)DictListTupleUnion   )CachedContextManager)PrunedContext)init_loggerc                       s  e Zd Zd*def fddZdefddZdeeB def fd	d
Zd*dedef fddZdeeB def fddZ	e
jjdd Ze
jjdd Ze
jjdd Ze
jjdee fddZe
jjdee fddZe
jjdee fddZe
jjdee fddZe
jjdee fddZe
jjdee fdd Ze
jjejd!d"d#edee fd$d%Ze
jjdefd&d'Ze
jjdee
jee
jdf f fd(d)Z   Z!S )+PrunedContextManagerNnamec                    s$   t  j|fi | d | _i | _d S N)super__init___current_context_cached_context_manager)selfr   kwargs	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/caching/cache_contexts/prune_manager.pyr      s   
zPrunedContextManager.__init__returnc                 O   s*   t |i |}||_||_|| j|j< |S r   )r   
_init_args_init_kwargsr   r   )r   argsr   _contextr   r   r   new_context   s
   z PrunedContextManager.new_contextcached_contextc                       t  |S r   )r   set_contextr   r   r   r   r   r      s   z PrunedContextManager.set_contextc                    r   r   )r   get_context)r   r   r   r   r   r!   $   s   z PrunedContextManager.get_contextc                    s   t  j|g|R i |S r   )r   reset_context)r   r   r   r   r   r   r   r"   '   s   z"PrunedContextManager.reset_contextc                 C   s$   |   }|d usJ d|  d S N!cached_context must be set before)r!   add_pruned_stepr    r   r   r   r%   0   s   z$PrunedContextManager.add_pruned_stepc                 C   &   |   }|d usJ d|| d S r#   )r!   add_pruned_blockr   
num_blocksr   r   r   r   r'   6      z%PrunedContextManager.add_pruned_blockc                 C   r&   r#   )r!   add_actual_blockr(   r   r   r   r+   <   r*   z%PrunedContextManager.add_actual_blockc                 C       |   }|d usJ d| S r#   )r!   get_pruned_stepsr    r   r   r   r-   B      z%PrunedContextManager.get_pruned_stepsc                 C   r,   r#   )r!   get_cfg_pruned_stepsr    r   r   r   r/   H   r.   z)PrunedContextManager.get_cfg_pruned_stepsc                 C   r,   r#   )r!   get_pruned_blocksr    r   r   r   r0   N   r.   z&PrunedContextManager.get_pruned_blocksc                 C   r,   r#   )r!   get_actual_blocksr    r   r   r   r1   T   r.   z&PrunedContextManager.get_actual_blocksc                 C   r,   r#   )r!   get_cfg_pruned_blocksr    r   r   r   r2   Z   r.   z*PrunedContextManager.get_cfg_pruned_blocksc                 C   r,   r#   )r!   get_cfg_actual_blocksr    r   r   r   r3   `   r.   z*PrunedContextManager.get_cfg_actual_blocks   )maxsizer)   c                    s    d usJ d dksJ dt t|   k r|  n }t t |   k r-|  n   }|  }|d us?J dt t|| |jj } fdd|D }t|S )Nznum_blocks must be providedr   z!num_blocks must be greater than 0r$   c                    s   g | ]}| k r|qS r   r   ).0dr)   r   r   
<listcomp>   s    zAPrunedContextManager.get_non_prune_blocks_ids.<locals>.<listcomp>)	listrangeFn_compute_blocksBn_compute_blocksr!   setcache_confignon_prune_block_idssorted)r   r)   Fn_compute_blocks_idsBn_compute_blocks_idscontextnon_prune_blocks_idsr   r8   r   get_non_prune_blocks_idsf   s8   z-PrunedContextManager.get_non_prune_blocks_idsc                 O      | j |i |S r   )	can_cacher   r   r   r   r   r   	can_prune      zPrunedContextManager.can_prunec                 O   rG   r   )apply_cacherI   r   r   r   apply_prune   rK   z PrunedContextManager.apply_pruner   )"__name__
__module____qualname__strr   r   r   r   r!   r"   torchcompilerdisabler%   r'   r+   r   intr-   r/   r0   r1   r2   r3   	functools	lru_cacherF   boolrJ   r   Tensorr   rM   __classcell__r   r   r   r   r
      sN    	



!,r
   )rR   rV   typingr   r   r   r   cache_managerr   prune_contextr   cache_dit.loggerr	   rN   loggerr
   r   r   r   r   <module>   s    