o
    ٷi>t                     @   s  d dl Z d dlZ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mZm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 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 z
d dl m!Z! dZ"W n e#y   dZ"Y nw ee$Z%G dd dZ&dS )    N)	ExitStack)DictListTupleAnyUnionCallableOptional)DiffusionPipeline
ModelMixin   )	CacheType)BlockAdapter)FakeDiffusionPipeline)ParamsModifier)BlockAdapterRegister)ContextManager)BasicCacheConfig)CalibratorConfig)UnifiedBlocks)init_logger)hooksTFc                   @   sN  e Zd Zdd Zedeeeej	j
ef deeef fddZededefdd	Zedefd
dZededeee eeeef  f fddZededeee eeeef  f fddZedeeef deeef deeef fddZed2defddZededee deej	j
 fddZedeeej	jf dej	j
dee dee dee dedej	j
fd d!Zededee deeeej	jf  fd"d#Zededee fd$d%Zeejjdefd&d'Z edeeeej	j
f fd(d)Z!e	*	*	*d3deeef d+e"e# d,e"e# d-e"e# fd.d/Z$edej	j
fd0d1Z%d*S )4CachedAdapterc                 O   s   | j |i |S N)apply)selfargskwargs r   b/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/caching/cache_adapters/cache_adapter.py__call__#   s   zCachedAdapter.__call__pipe_or_adapterreturnc                 K   s  |d usJ dt |ttjjtfr\t|rSt	|j
j d t|}|d us4J d|j
j d|dd  }r?||_| j|fi |}t |trP|jS |jS t|j
j dt |tscJ t	d |jd u rx|dd  }rx||_| j|fi |S )Nz(pipe or block_adapter can not both None!zR is officially supported by cache-dit. Use it's pre-defined BlockAdapter directly!zBlockAdapter for z should not be None!params_modifierszK is not officially supported by cache-dit, please set BlockAdapter instead!z6Adapting Cache Acceleration using custom BlockAdapter!)
isinstancer
   torchnnModuler   r   is_supportedloggerinfo	__class____name__get_adapterpopr#   cachifypipetransformer
ValueErrorr   )clsr!   context_kwargsblock_adapterr#   r   r   r   r   &   s@   





zCachedAdapter.applyr5   c                 K   sX   |j rt|}t|r*t|}t|r|S | j|fi |\}}| || |S r   )autor   auto_block_adaptercheck_block_adapter	normalize	is_cachedcreate_contextmock_blocks)r3   r5   r4   _contexts_kwargsr   r   r   r/   _   s$   



zCachedAdapter.cachifyc                 K   s   |d }|d usJ d|j d u r>t|r&d|_ td|jjj d n(t|j|_ td|j  d|jjj d ntd|j  d	|jjj d |d
d }|d urxt	|t
sgJ dt| d||jksxJ d| d|j d|S )Ncache_configcache_config can not be None.TzDUse custom 'enable_separate_cfg' from BlockAdapter: True. Pipeline: .z?Use default 'enable_separate_cfg' from block adapter register: z, Pipeline: z<Use custom 'enable_separate_cfg' from cache context kwargs: z. Pipeline: 
cache_typez+cache_type must be CacheType Enum, but got z cache_type from context_kwargs (z.) must be the same as that from cache_config (z).)enable_separate_cfgr   has_separate_cfgr)   r*   r0   r+   r,   r.   r$   r   typerB   )r3   r5   r4   r?   rB   r   r   r   check_context_kwargs   sP   

z"CachedAdapter.check_context_kwargsc                    s6  t   t  jrtd d  jfS j fi |}|dd }|d us,J d jjj	}t
| dtt j |jt jtdj fi |\ j_jsy jjjt fdd}| jj_ jj_ntD ]\}}j|fi | q~d jj_  fS )	NzDPipeline has been already cached, skip creating cache context again.r?   r@   r=   )namerB   persistent_contextc              
      s   t  3}tD ]\}}|j|fi | q	| g|R i |}  |W  d    S 1 s9w   Y  d S r   )r   zipenter_contextreset_contextapply_stats_hooks)r   r   r   stackcontext_namer4   outputsr5   r3   context_managerr>   flatten_contextsoriginal_callr   r   new_call   s   
$z.CachedAdapter.create_context.<locals>.new_callT)r   assert_normalizedr:   r0   r)   warningrF   getr+   r,   r   hashidrB   r$   r   modify_context_params_context_managerrH   r    	functoolswraps_original_callrI   rK   
_is_cachedapply_params_hooks)r3   r5   r4   r?   pipe_cls_namerT   rN   r   rP   r   r;      sD   








zCachedAdapter.create_contextc                    s   t |j} fddtt|D }tt|D ]
}|| || d< q|jd u r@tt|D ]}| jdi ||  q/||fS t |j}ttt|t|D ]}| || j	|| ||< | jdi ||  qQ||fS )Nc                    s   g | ]}t  qS r   )copydeepcopy).0r=   r4   r   r   
<listcomp>   s    z7CachedAdapter.modify_context_params.<locals>.<listcomp>rG   r   )
r   flattenunique_blocks_namerangelenr#   _config_messagesmin_modify_context_params_context_kwargs)r3   r5   r4   rR   r>   iflatten_modifiersr   re   r   rZ      s0   

z#CachedAdapter.modify_context_paramsnew_context_kwargsold_context_kwargsc                 C   s,  t |}d|v r|dd }|dd }|d urTt|ts(J dt| d|dd d u r5||d< nt|d tsHJ dt|d  d|d jdi |  |d urt|tsgJ dt| d|dd d u ru||d< |S t|d tsJ dt|d  d|d jdi |  |S )Nr?   calibrator_configz/cache_config must be BasicCacheConfig, but got rA   z4calibrator_config must be CalibratorConfig, but got r   )	rb   rc   rW   r$   r   rE   updateas_dictr   )r3   rq   rr   modified_context_kwargsnew_cache_confignew_calibrator_configr   r   r   rm     sH   



z$CachedAdapter._modify_context_paramsTloggingc                 K   sl   | dd }| dd }d}|d ur-d|  }|d ur)|d|jdd 7 }n|d7 }|r4t| |S )	Nr?   rs    zCollected Context Config: z, Calibrator Config: T)detailsz, Calibrator Config: None)rW   strifyr)   r*   )r3   ry   r>   r?   rs   messager   r   r   rk   I  s   
zCachedAdapter._config_messagesr>   c              	   C   sf   t | t |jr|jS t| |||j|j|j|jD ]\}}}}}| 	|||||| q|jS r   )
r   rU   r:   r1   rI   collect_unified_blocksblocks_namerh   dummy_blocks_namesmock_transformer)r3   r5   r>   unified_blocksr1   r   rh   r   r   r   r   r<   X  s8   
		zCachedAdapter.mock_blocksr   r1   r   rh   r   c              	      s   t j |jttsJ tr8d  t|dd d ur7|j t	|dr7t
dt dt|j d |jnd  jjttjsFJ |_
|_	
f	dd fdd	}tt||||_|_d
|_|S )N_hf_hook_old_forwardzN_hf_hook is not None, so, we have to re-direct transformer's original_forward(z ) to transformer's _old_forward()c              
      s   t  U}tD ]\}}|tjj| ||  q	D ]}|tjj| | q|i |}jrH rP	  W d    |S W d    |S W d    |S 1 s[w   Y  |S r   )
r   rI   rJ   unittestmockpatchobjectrH   is_pre_refreshedrL   )r   r   r   rM   rG   rN   
dummy_namerO   )	r5   r   r3   rQ   dummy_blocksr   original_forwardr   rh   r   r   new_forward  s.   


z3CachedAdapter.mock_transformer.<locals>.new_forwardc                    sf    d urt  dr j| g|R i |\}}| g|R i |} d ur1t  dr1 | |}|S )Npre_forwardpost_forward)hasattrr   r   )r   r   r   rO   )r   r   r   r   new_forward_with_hf_hook  s   z@CachedAdapter.mock_transformer.<locals>.new_forward_with_hf_hookT)r%   r&   
ModuleListforwardr$   list_accelerate_is_availblegetattrr   r   r)   rV   rY   r   r0   r[   r   _supported_managers_context_namesr\   update_wrapperpartial_original_forwardr_   )r3   r   r1   r   rh   r   r5   r   r   )r   r5   r   r3   rQ   r   r   r   r   r   rh   r   r     s>   



zCachedAdapter.mock_transformerc                 C   s   t | g }t|jdsJ t|jjtjsJ tt	|j
D ]Y}i }tt	|j| D ]F}||t	|j|  |  d }tjt|j| | |j
| |j| | |j|j|j| | |j| | |jj|jd	g||j| | < q-|| q |S )Nr[   r?   )r1   forward_patterncheck_forward_patterncheck_num_outputscache_prefixcache_contextrQ   rB   )r   rU   r   r0   r$   r[   r   r   ri   rj   r1   blocksr%   r&   r   r   r   r   r   r   rh   rB   append)r3   r5   r>   total_cached_blocksro   unified_blocks_bind_contextjr?   r   r   r   r~     s@   
z$CachedAdapter.collect_unified_blocksc                 C   s   |d |j _d}tt|jD ]?}|j|j| _|j|j| _|| |j| _|j	| }tt|D ]}|j| | || _|||  || _q2|t|7 }qd S )Nr   )
r0   rn   ri   rj   r1   r   _forward_patternrD   _has_separate_cfgr   )r3   r5   r>   params_shiftro   r   r   r   r   r   r`     s   
z CachedAdapter.apply_params_hooksc                 C   sx   ddl m} |jj}tt|jD ](}||j| |j| d |d t|j	| |j| D ]\}}||||d q-qd S )Nr   )apply_stats)r   rQ   )
cache_blocksr   r0   r[   ri   rj   r1   rh   rI   r   )r3   r5   r   rQ   ro   r   unique_namer   r   r   rL     s&   zCachedAdapter.apply_stats_hooksc           	         s   dd }dd }dd }|  |||| dd   fd	d
}dd }|  | || ddlm} |  |||| ddlm} |  |||| d S )Nc                 S   s   d S r   r   r   r   r   r   _release_blocks_hooks=  s   z@CachedAdapter.maybe_release_hooks.<locals>._release_blocks_hooksc                 S   s   t | dr| j}|| | _| `t | dr| `t | dr7| j}t|tjr)|	  z| `W n	 t
y6   Y nw t | dr@| `d S d S )Nr   r_   r[   r   )r   r   __get__r   r_   r[   r$   r   r   clear_contexts	Exceptionr   )r1   r   rQ   r   r   r   _release_transformer_hooks@  s$   



zECachedAdapter.maybe_release_hooks.<locals>._release_transformer_hooksc                 S   s`   t | dr| jj}|| j_| j`t | dr$| j}t|tjr"|  | `t | dr.| j`	d S d S )Nr^   r[   r_   )
r   r+   r^   r    r[   r$   r   r   r   r_   )r0   rS   rQ   r   r   r   _release_pipeline_hooksR  s   



zBCachedAdapter.maybe_release_hooks.<locals>._release_pipeline_hooksc                 S   s$   t | dr| `t | dr| `d S d S )Nr   rn   )r   r   rn   r   r   r   r   _release_blocks_paramsg  s
   

zACachedAdapter.maybe_release_hooks.<locals>._release_blocks_paramsc                    sF   t | dr| `t | dr| `t | dr| `t| D ]} | qd S )Nr   r   rn   )r   r   r   rn   r   find_blocks)r1   r   r   r   r   _release_transformer_paramsm  s   



zFCachedAdapter.maybe_release_hooks.<locals>._release_transformer_paramsc                 S   s   t | dr	| `d S d S )Nrn   )r   rn   )r0   r   r   r   _release_pipeline_paramsw  s   
zCCachedAdapter.maybe_release_hooks.<locals>._release_pipeline_paramsr   )remove_statsr   )remove_parallelism_stats)release_hooksr   r   cache_dit.parallelismr   )	r3   r!   r   r   r   r   r   r   r   r   r   r   maybe_release_hooks3  s6   

z!CachedAdapter.maybe_release_hooksN_release_blocks_release_transformer_release_pipelinec           	      C   s  t |tr1|}|d ur|| t|dr|d ur||j t|dr-|d ur/||j d S d S d S t |trj|}t| |d urF||j t|jD ]
}|d urV|| qLt|j	D ]
}|d urg|| q]d S t |t
jjr|}|d ur{|| t|D ]}|d ur|| qd S d S )Nr1   transformer_2)r$   r
   r   r1   r   r   rU   r0   rg   r   r%   r&   r'   r   )	r3   r!   r   r   r   r0   adapterr1   r   r   r   r   r     sH   






zCachedAdapter.release_hooksc           
   
   K   s   | dd}t|dstd d S |j}t|tjsJ |js'td d S t	|dg }|s6td d S |D ]C}|
|}t	|di }t|}	|| | ||	}	|j|fi |	 |rstd	| d
| jdddi|	  t|dr{|	|_q8d S )NverboseFr[   zOTransformer has no attribute '_context_manager', skip refreshing cache context.zOTransformer's context manager is not persistent, skip refreshing cache context.r   z[Transformer has no attribute '_context_names' or it's empty, skip refreshing cache context._init_kwargsu   ✅ Refreshed cache context: z, ry   rn   r   )r.   r   r)   rV   r[   r$   r   r   rH   r   get_contextrb   rc   remove_contextrm   rK   r*   rk   rn   )
r3   r1   force_refresh_kwargsr   rQ   context_namesrN   current_contextold_init_kwargsnew_init_kwargsr   r   r   maybe_refresh_context  sV   




z#CachedAdapter.maybe_refresh_context)T)NNN)&r,   
__module____qualname__r    classmethodr   r
   r   r%   r&   r'   r   r   r/   rF   r   r   strr   r   r;   rZ   rm   boolrk   r<   r   r   r~   r`   compilerdisablerL   r   r	   r   r   r   r   r   r   r   r   !   s    
8!,I%


*
'Q.a'r   )'rb   r%   r   r\   
contextlibr   typingr   r   r   r   r   r   r	   	diffusersr
   r   cache_typesr   block_adaptersr   r   r   r   cache_contextsr   r   r   r   r   cache_dit.loggerr   
accelerater   r   ImportErrorr,   r)   r   r   r   r   r   <module>   s2    $