-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodes.py
More file actions
263 lines (214 loc) · 10.2 KB
/
nodes.py
File metadata and controls
263 lines (214 loc) · 10.2 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# mengqin@gmail.com || Apache-2.0 (apache.org/licenses/LICENSE-2.0)
import logging
import os
import re
import uuid
import copy
import weakref
import torch
import comfy.sd
import comfy.model_patcher
import folder_paths
from .ops import LazyOps
from .loader import safetensors_sd_loader
class UnetBnbModelPatcher(comfy.model_patcher.ModelPatcher):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# module_key list of (strength_patch, patch_obj, strength_model, None, None)
self.bnb_lora_patches = {}
# optional backups for module-local temp data when partially_unload
self._bnb_lora_module_backups = {}
def add_patches(self, patches, strength_patch=1.0, strength_model=1.0):
added = []
for key in patches:
if not isinstance(key, str):
continue
module_key = key.rsplit('.', 1)[0]
try:
module = comfy.utils.get_attr(self.model, module_key)
is_bnb = hasattr(module, 'is_bnb_quantized') and module.is_bnb_quantized()
except Exception:
is_bnb = False
if is_bnb:
self.bnb_lora_patches.setdefault(module_key, []).append(
(strength_patch, patches[key], strength_model, None, None)
)
else:
current = self.patches.get(key, [])
current.append((strength_patch, patches[key], strength_model, None, None))
self.patches[key] = current
added.append(key)
self.patches_uuid = uuid.uuid4()
return added
def clone(self):
cloned = super().clone()
if not isinstance(cloned, UnetBnbModelPatcher):
new_cloned = UnetBnbModelPatcher(cloned.model, cloned.load_device, cloned.offload_device, cloned.size)
new_cloned.patches = cloned.patches
new_cloned.object_patches = cloned.object_patches
cloned = new_cloned
cloned.bnb_lora_patches = copy.deepcopy(self.bnb_lora_patches)
cloned._bnb_lora_module_backups = {}
return cloned
def pre_run(self, *args, **kwargs):
super().pre_run(*args, **kwargs)
for name, module in self.model.named_modules():
try:
if isinstance(module, LazyOps.Linear):
try:
module.patcher = weakref.proxy(self)
except Exception:
module.patcher = self
module.module_key_name = name
module.weight_key_name = f"{name}.weight"
except Exception:
logging.debug(f"pre_run: skip module {name} assignment due to exception", exc_info=True)
self.apply_bnb_patches()
def apply_bnb_patches(self):
for module_key, p_list in list(self.bnb_lora_patches.items()):
try:
module = comfy.utils.get_attr(self.model, module_key)
except Exception:
continue
if getattr(module, "_bnb_lora_attached", False):
continue
module._bnb_lora_attached = True
module._bnb_lora_patch_count = len(p_list)
def get_patches_for_module(self, module_key_name, *, is_bnb=False):
if is_bnb:
return self.bnb_lora_patches.get(module_key_name, None)
else:
return self.patches.get(f"{module_key_name}.weight", None)
def remove_bnb_patches(self):
for module_key in list(self.bnb_lora_patches.keys()):
try:
module = comfy.utils.get_attr(self.model, module_key)
except Exception:
continue
for attr in ("_bnb_lora_attached", "_bnb_lora_patch_count",):
if hasattr(module, attr):
try:
delattr(module, attr)
except Exception:
logging.debug(f"remove_bnb_patches: could not del {attr} on {module_key}", exc_info=True)
self._bnb_lora_module_backups.pop(module_key, None)
def clear_bnb_patches(self):
self.bnb_lora_patches.clear()
self._bnb_lora_module_backups.clear()
def unpatch_model(self, device_to=None, unpatch_weights=True):
super().unpatch_model(device_to=device_to, unpatch_weights=unpatch_weights)
self.remove_bnb_patches()
for name, module in self.model.named_modules():
if hasattr(module, "patcher"):
try:
p = getattr(module, "patcher")
delattr(module, "patcher")
except Exception:
pass
for attr in ("module_key_name", "weight_key_name"):
if hasattr(module, attr):
try:
delattr(module, attr)
except Exception:
pass
return
def partially_unload(self, device_to, memory_to_free=0):
memory_freed = super().partially_unload(device_to, memory_to_free=memory_to_free)
for name, module in self.model.named_modules():
if getattr(module, "_bnb_lora_attached", False):
module_key = getattr(module, "module_key_name", name)
if hasattr(module, "_bnb_lora_patch_count"):
self._bnb_lora_module_backups[module_key] = getattr(module, "_bnb_lora_patch_count", None)
for attr in ("_bnb_lora_attached", "_bnb_lora_patch_count",):
if hasattr(module, attr):
try:
delattr(module, attr)
except Exception:
pass
return memory_freed
def calculate_weight_with_patches(self, module_key_name, base_weight_fp32, is_bnb=False):
if is_bnb:
patches = self.bnb_lora_patches.get(module_key_name, None)
else:
patches = self.patches.get(f"{module_key_name}.weight", None)
if not patches:
return None
try:
base = base_weight_fp32.to(torch.float32)
weight_final_fp32 = comfy.lora.calculate_weight(patches, base, f"{module_key_name}.weight")
return weight_final_fp32.to(torch.float32)
except Exception:
logging.exception(f"calculate_weight_with_patches failed for {module_key_name}")
return None
def get_safetensors_model_list(folder_path_key):
shard_pattern = re.compile(r'.*-(\d{5})-of-(\d{5})\.safetensors$')
try:
initial_list = folder_paths.get_filename_list(folder_path_key)
except KeyError:
logging.error(f"Path type '{folder_path_key}' is not registered.")
return []
sharded_files_to_remove = set()
parent_dirs_to_add = set()
for item in initial_list:
is_dir = False
for basedir in folder_paths.get_folder_paths(folder_path_key):
if os.path.isdir(os.path.join(basedir, item)):
is_dir = True
break
if is_dir:
continue
filename = os.path.basename(item)
if shard_pattern.match(filename):
sharded_files_to_remove.add(item)
parent_dir = os.path.dirname(item)
if parent_dir and parent_dir != ".":
parent_dirs_to_add.add(parent_dir.replace(os.sep, '/'))
final_list = [item for item in initial_list if item not in sharded_files_to_remove]
final_list.extend(list(parent_dirs_to_add))
return sorted(list(set(final_list)))
def is_bnb_4bit(sd: dict) -> bool:
if not isinstance(sd, dict):
return False
for k in sd.keys():
if k.endswith(".quant_state.bitsandbytes__nf4") or k.endswith(".quant_state.bitsandbytes__fp4"):
return True
return False
class UnetBnbModelLoader:
FOLDER_PATH_KEY = "unet"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model_name": (get_safetensors_model_list(s.FOLDER_PATH_KEY),),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "load_model"
CATEGORY = "loaders"
TITLE = "Unet Bnb Model Loader"
def load_model(self, model_name):
model_path = folder_paths.get_full_path(self.FOLDER_PATH_KEY, model_name)
if model_path is None:
for basedir in folder_paths.get_folder_paths(self.FOLDER_PATH_KEY):
candidate_path = os.path.join(basedir, model_name)
if os.path.isdir(candidate_path):
model_path = candidate_path
break
if model_path is None:
raise FileNotFoundError(f"Model not found in the directory configured by '{self.FOLDER_PATH_KEY}' class: {model_name}")
state_dict = safetensors_sd_loader(model_path)
if is_bnb_4bit(state_dict):
model_patcher = comfy.sd.load_diffusion_model_state_dict(state_dict, {"custom_operations": LazyOps()})
custom_patcher = UnetBnbModelPatcher(model_patcher.model, model_patcher.load_device, model_patcher.offload_device, model_patcher.size)
else:
model_patcher = comfy.sd.load_diffusion_model_state_dict(state_dict)
custom_patcher = comfy.model_patcher.ModelPatcher(model_patcher.model, model_patcher.load_device, model_patcher.offload_device, model_patcher.size)
if model_patcher is None:
raise RuntimeError(f"Unable to detect or load UNet model: {model_path}")
return (custom_patcher,)
NODE_CLASS_MAPPINGS = {
"UnetBnbModelLoader": UnetBnbModelLoader,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"UnetBnbModelLoader": "Unet Bnb Model Loader",
}