o
    i5                     @  s   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dZdddddgfd 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                   s   d fdd}|S )zK
    Create a wrapper that will refer to the compiled_diffusion_model.
    executorr   c                   s   z8i }   D ]\}}tj| j|||< tj| j|| q| |i |W |  D ]\}}tj| j|| q*S |  D ]\}}tj| j|| q=w )N)itemscomfyutilsget_attr	class_objset_attr)r   argskwargsorig_moduleskeyvaluer
    C/mnt/c/Users/fbmor/ComfyUI/comfy_api/torch_helpers/torch_compile.pyapply_torch_compile_wrapper   s   z@apply_torch_compile_factory.<locals>.apply_torch_compile_wrapperN)r   r   r   )r
   r   r   r   r   apply_torch_compile_factory   s   
r   Fdiffusion_modelmodelr   backendstroptionsOptional[dict[str, str]]modeOptional[str]dynamicOptional[bool]keys	list[str]c                 O  s|   |  tjt |sdg}|||||d}	i }
|D ]}tjdd| |i|	|
|< qt|
d}| 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_funcr   r   r   set_torch_compile_wrapper!   s*   
r7   )r
   r   r   r   )r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   )
__future__r   r.   comfy.utilsr   comfy.patcher_extensionr   typingr   r   r   comfy.model_patcherr   r   r-   r3   r   r7   r   r   r   r   <module>   s    
