-
Notifications
You must be signed in to change notification settings - Fork 295
/
nodes.py
408 lines (325 loc) · 14.1 KB
/
nodes.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import copy
import os
import torch
from .utils import convert_images_to_tensors
from comfy.model_management import get_torch_device
import folder_paths
from .hydit_v1_1.diffusion.pipeline import StableDiffusionPipeline
from .hydit_v1_1.diffusion.pipeline_controlnet import StableDiffusionControlNetPipeline
from .hydit_v1_1.config_comfyui import get_args
from .hydit_v1_1.inference_comfyui import End2End
from pathlib import Path
from .hydit_v1_1.constants import SAMPLER_FACTORY
from diffusers import schedulers
from .constant import HUNYUAN_PATH, SCHEDULERS_hunyuan, T5_PATH, LORA_PATH
from .dit import load_checkpoint, load_vae
from .clip import CLIP
from .hydit_v1_1.modules.controlnet import HunYuanControlNet
from .hydit_v1_1.modules.models import HUNYUAN_DIT_CONFIG
from loguru import logger
import numpy as np
from torchvision import transforms as T
norm_transform = T.Compose(
[
T.ToTensor(),
T.Normalize([0.5], [0.5]),
]
)
from PIL import Image
def _to_tuple(val):
if isinstance(val, (list, tuple)):
if len(val) == 1:
val = [val[0], val[0]]
elif len(val) == 2:
val = tuple(val)
else:
raise ValueError(f"Invalid value: {val}")
elif isinstance(val, (int, float)):
val = (val, val)
else:
raise ValueError(f"Invalid value: {val}")
return val
class DiffusersPipelineLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {"required": {"pipeline_folder_name": (os.listdir(HUNYUAN_PATH),), },
"optional": {"lora": ("lora_path",), }}
RETURN_TYPES = ("PIPELINE",)
FUNCTION = "create_pipeline"
CATEGORY = "Diffusers"
def create_pipeline(self, pipeline_folder_name, lora=None):
if lora != None:
LORA_PATH = lora
else:
LORA_PATH = None
args_hunyuan = get_args()
gen = End2End(args_hunyuan[0], Path(os.path.join(HUNYUAN_PATH, pipeline_folder_name)), LOAR_PATH=LORA_PATH)
return (gen,)
class DiffusersCheckpointLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model_name": (folder_paths.get_filename_list("checkpoints"),),
"version": (list(["v1.1", "v1.2"]),), }}
RETURN_TYPES = ("MODEL",)
FUNCTION = "load_checkpoint"
CATEGORY = "Diffusers"
def load_checkpoint(self, model_name, version):
MODEL_PATH = folder_paths.get_full_path("checkpoints", model_name)
out = load_checkpoint(MODEL_PATH, version)
return out
class DiffusersVAELoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model_name": (folder_paths.get_filename_list("vae"),), }}
RETURN_TYPES = ("VAE",)
FUNCTION = "load_vae"
CATEGORY = "Diffusers"
def load_vae(self, model_name):
MODEL_PATH = folder_paths.get_full_path("vae", model_name)
out = load_vae(MODEL_PATH)
return out
class DiffusersLoraLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {"required": {"lora_name": (os.listdir(LORA_PATH),), }}
RETURN_TYPES = ("lora_path",)
FUNCTION = "load_lora"
CATEGORY = "Diffusers"
def load_lora(self, lora_name):
MODEL_PATH = os.path.join(LORA_PATH, lora_name)
return (MODEL_PATH,)
class DiffusersCLIPLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {"required": {
"text_encoder_path": (folder_paths.get_filename_list("clip"),),
"t5_text_encoder_path": (os.listdir(T5_PATH),), }}
RETURN_TYPES = ("CLIP",)
FUNCTION = "load_clip"
CATEGORY = "Diffusers"
def load_clip(self, text_encoder_path, t5_text_encoder_path):
CLIP_PATH = folder_paths.get_full_path("clip", text_encoder_path)
t5_file = os.path.join(T5_PATH, t5_text_encoder_path)
root = None
out = CLIP(False, root, CLIP_PATH, t5_file)
return (out,)
class DiffusersControlNetLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
self.torch_device = get_torch_device()
@classmethod
def INPUT_TYPES(s):
return {"required": {
"controlnet_path": (folder_paths.get_filename_list("controlnet"),), }}
RETURN_TYPES = ("CTRL",)
FUNCTION = "load_controlnet"
CATEGORY = "Diffusers"
def load_controlnet(self, controlnet_path):
DiffusersControlNetLoader_PATH = folder_paths.get_full_path("controlnet", controlnet_path)
args_hunyuan = get_args()
args = args_hunyuan[0]
image_size = _to_tuple(args.image_size)
latent_size = (image_size[0] // 8, image_size[1] // 8)
model_config = HUNYUAN_DIT_CONFIG[args.model]
controlnet = HunYuanControlNet(args,
input_size=latent_size,
**model_config,
log_fn=logger.info,
).half().to(self.torch_device)
controlnet_state_dict = torch.load(DiffusersControlNetLoader_PATH)
controlnet.load_state_dict(controlnet_state_dict)
controlnet.eval()
return (controlnet,)
class DiffusersSchedulerLoader:
def __init__(self):
self.tmp_dir = folder_paths.get_temp_directory()
self.dtype = torch.float32
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"scheduler_name": (list(SCHEDULERS_hunyuan),),
}
}
RETURN_TYPES = ("SCHEDULER",)
FUNCTION = "load_scheduler"
CATEGORY = "Diffusers"
def load_scheduler(self, scheduler_name):
# Load sampler from factory
kwargs = SAMPLER_FACTORY[scheduler_name]['kwargs']
scheduler = SAMPLER_FACTORY[scheduler_name]['scheduler']
args_hunyuan = get_args()
args_hunyuan = args_hunyuan[0]
# Update sampler according to the arguments
kwargs['beta_schedule'] = args_hunyuan.noise_schedule
kwargs['beta_start'] = args_hunyuan.beta_start
kwargs['beta_end'] = args_hunyuan.beta_end
kwargs['prediction_type'] = args_hunyuan.predict_type
# Build scheduler according to the sampler.
scheduler_class = getattr(schedulers, scheduler)
scheduler = scheduler_class(**kwargs)
return (scheduler,)
class DiffusersModelMakeup:
def __init__(self):
self.torch_device = get_torch_device()
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipeline": ("PIPELINE",),
"scheduler": ("SCHEDULER",),
},
"optional":
{"controlnet": ("CTRL",), }
}
RETURN_TYPES = ("MAKED_PIPELINE",)
FUNCTION = "makeup_pipeline"
CATEGORY = "Diffusers"
def makeup_pipeline(self, pipeline, scheduler, controlnet=None):
progress_bar_config = {}
if not controlnet:
pipe = StableDiffusionPipeline(vae=pipeline.vae,
text_encoder=pipeline.clip_text_encoder,
tokenizer=pipeline.tokenizer,
unet=pipeline.model,
scheduler=scheduler,
feature_extractor=None,
safety_checker=None,
requires_safety_checker=False,
progress_bar_config=progress_bar_config,
embedder_t5=pipeline.embedder_t5,
infer_mode=pipeline.infer_mode,
)
else:
pipe = StableDiffusionControlNetPipeline(vae=pipeline.vae,
text_encoder=pipeline.clip_text_encoder,
tokenizer=pipeline.tokenizer,
unet=pipeline.model,
scheduler=scheduler,
feature_extractor=None,
safety_checker=None,
requires_safety_checker=False,
progress_bar_config=progress_bar_config,
embedder_t5=pipeline.embedder_t5,
infer_mode=pipeline.infer_mode,
controlnet=controlnet
)
pipe = pipe.to(pipeline.device)
pipeline.pipeline = pipe
return (pipeline,)
class DiffusersClipTextEncode:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"positive": ("STRING", {"multiline": True}),
"negative": ("STRING", {"multiline": True}),
}}
RETURN_TYPES = ("STRINGC", "STRINGC",)
RETURN_NAMES = ("positive", "negative",)
FUNCTION = "concat_embeds"
CATEGORY = "Diffusers"
def concat_embeds(self, positive, negative):
return (positive, negative,)
class DiffusersSampler:
def __init__(self):
self.torch_device = get_torch_device()
@classmethod
def INPUT_TYPES(s):
return {"required": {
"maked_pipeline": ("MAKED_PIPELINE",),
"positive": ("STRINGC",),
"negative": ("STRINGC",),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 128, "step": 1}),
"width": ("INT", {"default": 1024, "min": 1, "max": 8192, "step": 1}),
"height": ("INT", {"default": 1024, "min": 1, "max": 8192, "step": 1}),
"steps": ("INT", {"default": 20, "min": 1, "max": 10000}),
"control_weight": ("FLOAT", {"default": 0.7, "min": 0, "max": 1.0}),
"cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step": 0.1, "round": 0.01}),
"seed": ("INT", {"default": 0, "min": 0, "max": 2 ** 32 - 1}),
},
"optional": {"image": ("IMAGE",), }}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "sample"
CATEGORY = "Diffusers"
def sample(self, maked_pipeline, positive, negative, batch_size, width, height, steps, cfg, seed, control_weight,
image=None):
if not isinstance(image, torch.Tensor):
results = maked_pipeline.predict(positive,
height=height,
width=width,
seed=int(seed),
enhanced_prompt=None,
negative_prompt=negative,
infer_steps=steps,
guidance_scale=cfg,
batch_size=batch_size,
src_size_cond=[height, width],
image=image
)
else:
# import pdb
# pdb.set_trace()
img = 255. * image.cpu().numpy()
# print(i.size())
img = Image.fromarray(np.clip(img[0], 0, 255).astype(np.uint8))
img = img.convert('RGB').resize((height, width))
image = norm_transform(img)
image = image.unsqueeze(0).cuda()
# print(image.size)
results = maked_pipeline.predict(positive,
height=height,
width=width,
seed=int(seed),
enhanced_prompt=None,
negative_prompt=negative,
infer_steps=steps,
guidance_scale=cfg,
batch_size=batch_size,
src_size_cond=[height, width],
image=image,
control_weight=control_weight,
)
images = results['images']
return (convert_images_to_tensors(images),)
NODE_CLASS_MAPPINGS = {
"DiffusersPipelineLoader": DiffusersPipelineLoader,
"DiffusersSchedulerLoader": DiffusersSchedulerLoader,
"DiffusersModelMakeup": DiffusersModelMakeup,
"DiffusersClipTextEncode": DiffusersClipTextEncode,
"DiffusersSampler": DiffusersSampler,
"DiffusersCheckpointLoader": DiffusersCheckpointLoader,
"DiffusersVAELoader": DiffusersVAELoader,
"DiffusersCLIPLoader": DiffusersCLIPLoader,
"DiffusersControlNetLoader": DiffusersControlNetLoader,
"DiffusersLoraLoader": DiffusersLoraLoader
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DiffusersPipelineLoader": "HunYuan Pipeline Loader",
"DiffusersSchedulerLoader": "HunYuan Scheduler Loader",
"DiffusersModelMakeup": "HunYuan Model Makeup",
"DiffusersClipTextEncode": "HunYuan Clip Text Encode",
"DiffusersSampler": "HunYuan Sampler",
"DiffusersCheckpointLoader": "HunYuan Checkpoint Loader",
"DiffusersVAELoader": "HunYuan VAE Loader",
"DiffusersCLIPLoader": "HunYuan CLIP Loader",
"DiffusersControlNetLoader": "HunYuan ControlNet Loader",
"DiffusersLoraLoader": "HunYuan Lora Loader",
}