-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompile_node.py
More file actions
73 lines (60 loc) · 2.1 KB
/
compile_node.py
File metadata and controls
73 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import comfy.model_patcher
USE_PRUNA_PRO = False
try:
from pruna_pro import SmashConfig, smash
USE_PRUNA_PRO = True
except ImportError:
print("pruna_pro not installed, skipping")
try:
from pruna import SmashConfig, smash
except ImportError:
print("Neither pruna_pro nor pruna are installed, skipping")
class PrunaCompileModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"compiler": ("STRING", {"default": "x_fast"}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "apply_compilation"
CATEGORY = "Pruna"
SUPPORTED_COMPILERS = {
"x_fast",
"torch_compile",
"x-fast",
} # x-fast is deprecated, but still supported
def apply_compilation(self, model, compiler):
"""
Smash the model using Pruna.
"""
# we assume that the input model is either a comfy.model_patcher.ModelPatcher
# or a comfy.model.Model
if isinstance(model, comfy.model_patcher.ModelPatcher):
smashed_patcher = model.clone()
else:
smashed_patcher = model.patcher
smashed_patcher = smashed_patcher.clone()
smash_config = SmashConfig()
if compiler not in self.SUPPORTED_COMPILERS:
raise ValueError(
f"Compiler {compiler} is not a valid compiler. Supported compilers are: {self.SUPPORTED_COMPILERS}"
)
try:
smash_config["compiler"] = compiler
except KeyError:
raise ValueError(f"Compiler {compiler} is available only with pruna_pro")
smash_config._prepare_saving = False
smashed_diffusion_model = smash(
smashed_patcher.model.diffusion_model,
smash_config,
)
model_ref_name = (
"_PrunaProModel__internal_model_ref" if USE_PRUNA_PRO else "model"
)
smashed_patcher.add_object_patch(
"diffusion_model", smashed_diffusion_model.__getattribute__(model_ref_name)
)
return (smashed_patcher,)