mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-09-05 20:27:14 +08:00
Make torch compile node use wrapper instead of object_patch for the entire diffusion_models object, allowing key assotiations on diffusion_models to not break (loras, getting attributes, etc.)
This commit is contained in:
parent
87f9130778
commit
ae78eda8f4
@ -1,5 +1,40 @@
|
|||||||
|
from __future__ import annotations
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
from comfy.patcher_extension import WrappersMP, WrapperExecutor
|
||||||
|
from typing import TYPE_CHECKING, Callable, Optional
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from comfy.model_patcher import ModelPatcher
|
||||||
|
|
||||||
|
|
||||||
|
COMPILE_KEY = "torch.compile"
|
||||||
|
|
||||||
|
|
||||||
|
def apply_torch_compile_factory(compiled_diffusion_model: Callable) -> Callable:
|
||||||
|
def apply_torch_compile_wrapper(executor: WrapperExecutor, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
orig_diffusion_model = executor.class_obj.diffusion_model
|
||||||
|
executor.class_obj.diffusion_model = compiled_diffusion_model
|
||||||
|
return executor(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
executor.class_obj.diffusion_model = orig_diffusion_model
|
||||||
|
return apply_torch_compile_wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def set_torch_compile_wrapper(model: ModelPatcher, backend: str, options: Optional[dict[str,str]]=None, *args, **kwargs):
|
||||||
|
# clear out any other torch.compile wrappers
|
||||||
|
model.remove_wrappers_with_key(WrappersMP.APPLY_MODEL, COMPILE_KEY)
|
||||||
|
# add torch.compile wrapper
|
||||||
|
wrapper_func = apply_torch_compile_factory(
|
||||||
|
torch.compile(
|
||||||
|
model=model.get_model_object("diffusion_model"),
|
||||||
|
backend=backend,
|
||||||
|
options=options,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
model.add_wrapper_with_key(WrappersMP.APPLY_MODEL, COMPILE_KEY, wrapper_func)
|
||||||
|
|
||||||
|
|
||||||
class TorchCompileModel:
|
class TorchCompileModel:
|
||||||
@classmethod
|
@classmethod
|
||||||
def INPUT_TYPES(s):
|
def INPUT_TYPES(s):
|
||||||
@ -14,7 +49,7 @@ class TorchCompileModel:
|
|||||||
|
|
||||||
def patch(self, model, backend):
|
def patch(self, model, backend):
|
||||||
m = model.clone()
|
m = model.clone()
|
||||||
m.add_object_patch("diffusion_model", torch.compile(model=m.get_model_object("diffusion_model"), backend=backend))
|
set_torch_compile_wrapper(model=m, backend=backend)
|
||||||
return (m, )
|
return (m, )
|
||||||
|
|
||||||
NODE_CLASS_MAPPINGS = {
|
NODE_CLASS_MAPPINGS = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user