
    +j
                        d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	m
Z
 erd dlmZ d dlmZ dZdZddZdddddgfddZdS )    )annotationsN)
WrappersMP)TYPE_CHECKINGCallableOptional)ModelPatcher)WrapperExecutorztorch.compiletorch_compile_kwargscompiled_module_dictdict[str, Callable]returnr   c                     d fd}|S )zK
    Create a wrapper that will refer to the compiled_diffusion_model.
    executorr	   c                   	 i }                                 D ]S\  }}t          j                            | j        |          ||<   t          j                            | j        ||           T | |i ||                                 D ]+\  }}t          j                            | j        ||           ,S # |                                 D ]+\  }}t          j                            | j        ||           ,w xY w)N)itemscomfyutilsget_attr	class_objset_attr)r   argskwargsorig_moduleskeyvaluer   s         E/home/wildlama/comfy/ComfyUI/comfy_api/torch_helpers/torch_compile.pyapply_torch_compile_wrapperz@apply_torch_compile_factory.<locals>.apply_torch_compile_wrapper   s   	EL288:: E E
U$)K$8$89KS$Q$QS!$$X%7eDDDD8T,V,,*0022 E E
U$$X%7eDDDDEl0022 E E
U$$X%7eDDDDEs   A1B5 5AC7)r   r	    )r   r   s   ` r   apply_torch_compile_factoryr      s/    	E 	E 	E 	E 	E 	E '&    Fdiffusion_modelmodelr   backendstroptionsOptional[dict[str, str]]modeOptional[str]dynamicOptional[bool]keys	list[str]c                T   |                      t          j        t                     |sdg}|||||d}	i }
|D ]+}t	          j        dd|                     |          i|	|
|<   ,t          |
          }|                     t          j        t          |           |	| j	        t          <   dS )aX  
    Perform torch.compile that will be applied at sample time for either the whole model or specific params of the BaseModel instance.

    When keys is None, it will default to using ["diffusion_model"], compiling the whole diffusion_model.
    When a list of keys is provided, it will perform torch.compile on only the selected modules.
    r!   )r#   r%   r'   	fullgraphr)   r"   )r   Nr   )remove_wrappers_with_keyr   APPLY_MODELCOMPILE_KEYtorchcompileget_model_objectr   add_wrapper_with_keymodel_optionsTORCH_COMPILE_KWARGS)r"   r#   r%   r'   r.   r)   r+   r   r   compile_kwargscompiled_modulesr   wrapper_funcs                r   set_torch_compile_wrapperr;   !   s     
"":#9;GGG #!"  N    % ! !,,S11! ! !
 /-  L 
z5{LQQQ0>E,---r    )r   r   r   r   )r"   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   )
__future__r   r2   comfy.utilsr   comfy.patcher_extensionr   typingr   r   r   comfy.model_patcherr   r	   r1   r7   r   r;   r   r    r   <module>rA      s    " " " " " "      . . . . . . 4 4 4 4 4 4 4 4 4 4 8000000777777 - ' ' ' '" cg26%ae/@.A$? $? $? $? $? $? $?r    