Skip to content

Commit b45f49c

Browse files
xuhuarenzml-ai
authored andcommitted
Update ComfyUI. Support HYDiT-1.1/1.2, Strandard/Diffusers Wrapper and model weights trained via KoHya or official training scripts.
1 parent 3a4dcef commit b45f49c

74 files changed

Lines changed: 7094 additions & 3841 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This repo contains PyTorch model definitions, pre-trained weights and inference/
2525
> [**DialogGen: Multi-modal Interactive Dialogue System for Multi-turn Text-to-Image Generation**](https://arxiv.org/abs/2403.08857) <br>
2626
2727
## 🔥🔥🔥 News!!
28+
* Jul 15, 2024: :tada: Update ComfyUI to support standardized workflows and compatibility with weights from t2i module and Lora training for versions 1.1/1.2, as well as those trained by Kohya or the official script. See [ComfyUI](./comfyui-hydit) for details.
2829
* Jul 08, 2024: :tada: HYDiT-v1.2 version is released. Please check [HunyuanDiT-v1.2](https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2) and [Distillation-v1.2](https://huggingface.co/Tencent-Hunyuan/Distillation-v1.2) for more details.
2930
* Jul 03, 2024: :tada: Kohya-hydit version now available for v1.1 and v1.2 models, with GUI for inference. Official Kohya version is under review. See [kohya](./kohya_ss-hydit) for details.
3031
* Jun 27, 2024: :art: Hunyuan-Captioner is released, providing fine-grained caption for training data. See [mllm](./mllm) for details.
@@ -648,43 +649,13 @@ We list some more useful configurations for easy usage:
648649

649650
### Using ComfyUI
650651

651-
We provide several commands to quick start:
652-
653-
```shell
654-
# Download comfyui code
655-
git clone https://github.com/comfyanonymous/ComfyUI.git
656-
657-
# Install torch, torchvision, torchaudio
658-
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu117
652+
- Support two workflows: Standard ComfyUI and Diffusers Wrapper, with the former being recommended.
653+
- Support HunyuanDiT-v1.1 and v1.2.
654+
- Support module, lora and clip lora models trained by Kohya.
655+
- Support module, lora models trained by HunyunDiT official training scripts.
656+
- ControlNet is coming soon.
659657

660-
# Install Comfyui essential python package.
661-
cd ComfyUI
662-
pip install -r requirements.txt
663-
664-
# ComfyUI has been successfully installed!
665-
666-
# Download model weight as before or link the existing model folder to ComfyUI.
667-
python -m pip install "huggingface_hub[cli]"
668-
mkdir models/hunyuan
669-
huggingface-cli download Tencent-Hunyuan/HunyuanDiT --local-dir ./models/hunyuan/ckpts
670-
671-
# Move to the ComfyUI custom_nodes folder and copy comfyui-hydit folder from HunyuanDiT Repo.
672-
cd custom_nodes
673-
cp -r ${HunyuanDiT}/comfyui-hydit ./
674-
cd comfyui-hydit
675-
676-
# Install some essential python Package.
677-
pip install -r requirements.txt
678-
679-
# Our tool has been successfully installed!
680-
681-
# Go to ComfyUI main folder
682-
cd ../..
683-
# Run the ComfyUI Lauch command
684-
python main.py --listen --port 80
685-
686-
# Running ComfyUI successfully!
687-
```
658+
![Workflow](comfyui-hydit/img/workflow_v1.2_lora.png)
688659
More details can be found in [./comfyui-hydit](comfyui-hydit/README.md)
689660

690661
### Using Kohya

comfyui-hydit/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

comfyui-hydit/README.md

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,186 @@
11
# comfyui-hydit
2+
The ComfyUI code is under review in the official repository. Meanwhile, a temporary version is available below for immediate community use.
3+
We welcome users to try our workflow and appreciate any inquiries or suggestions.
24

3-
This repository houses a tailored node and workflow designed specifically for HunYuan DIT. The official tests conducted on DDPM, DDIM, and DPMMS have consistently yielded results that align with those obtained through the Diffusers library. However, it's important to note that we cannot assure the consistency of results from other ComfyUI native samplers with the Diffusers inference. We cordially invite users to explore our workflow and are open to receiving any inquiries or suggestions you may have.
45

56
## Overview
7+
- Support two workflows: Standard ComfyUI and Diffusers Wrapper, with the former being recommended.
8+
- Support HunyuanDiT-v1.1 and v1.2.
9+
- Support module, lora and clip lora models trained by Kohya.
10+
- Support module, lora models trained by HunyunDiT official training scripts.
11+
- ControlNet is coming soon.
612

13+
### Standard Workflow (Recommended)
14+
- [HunyuanDiT-v1.2](workflow/workflow_v1.2_lora.json)
15+
![Workflow](img/workflow_v1.2_lora.png)
716

8-
### Workflow text2image
17+
- [HunyuanDiT-v1.1](workflow/workflow_v1.1_lora.json)
18+
![Workflow](img/workflow_v1.1_lora.png)
919

10-
![Workflow](img/work_diffusers.png)
11-
[workflow_diffusers](workflow/workflow_diffusers.json) file for HunyuanDiT txt2image with diffusers backend.
12-
![Workflow](img/workflow_ksampler.png)
13-
[workflow_ksampler](workflow/workflow_ksampler.json) file for HunyuanDiT txt2image with ksampler backend.
20+
### Diffusers Wrapper
21+
- [HunyuanDiT-v1.1-diffusers](workflow/workflow_lora_controlnet.json)
1422
![Workflow](img/workflow_lora_controlnet.png)
15-
[workflow_lora_controlnet_diffusers](workflow/workflow_lora_controlnet.json) file for HunyuanDiT lora and controlnet model with diffusers backend.
1623

1724

1825
## Usage
26+
### Dependencies
1927

20-
Make sure you run the following command inside [ComfyUI](https://github.com/comfyanonymous/ComfyUI) project with our [comfyui-hydit](.) and have correct conda environment.
21-
28+
1. Official ComfyUI Environment Setup
2229
```shell
2330
# Please use python 3.10 version with cuda 11.7
31+
conda create --name comfyui-hydit python=3.10
32+
conda activate comfyui-hydit
33+
2434
# Download comfyui code
2535
git clone https://github.com/comfyanonymous/ComfyUI.git
36+
cd ${ComfyUI}
37+
git reset --hard 90389b3b8a69c08c3ed0bcc9d87a92246578a8e3
2638

2739
# Install torch, torchvision, torchaudio
2840
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu117 --default-timeout=100 future
2941

3042
# Install Comfyui essential python package
31-
cd ComfyUI
3243
pip install -r requirements.txt
44+
```
3345

34-
# ComfyUI has been successfully installed!
35-
36-
46+
2. HunyuanDiT Environment Setup in ComfyUI
47+
```shell
3748
# Move to the ComfyUI custom_nodes folder and copy comfyui-hydit folder from HunyuanDiT Repo.
38-
cd custom_nodes
49+
cd ${ComfyUI}/custom_nodes
3950
git clone https://github.com/Tencent/HunyuanDiT.git
4051
cp -r HunyuanDiT/comfyui-hydit ./
4152
rm -rf HunyuanDiT
42-
cd comfyui-hydit
53+
cd ${ComfyUI}/custom_nodes/comfyui-hydit
4354

44-
# !!! If using windows system !!!
45-
cd custom_nodes
55+
# Install some essential python Package.
56+
pip install -r requirements.txt
57+
```
58+
59+
3. (Optional) Deployment on Windows environment
60+
```shell
61+
cd ${ComfyUI}/custom_nodes
4662
git clone https://github.com/Tencent/HunyuanDiT.git
4763
xcopy /E /I HunyuanDiT\comfyui-hydit comfyui-hydit
4864
rmdir /S /Q HunyuanDiT
49-
cd comfyui-hydit
50-
65+
cd ${ComfyUI}/custom_nodes/comfyui-hydit
5166
# Install some essential python Package.
5267
pip install -r requirements.txt
68+
```
5369

54-
# Our tool has been successfully installed!
55-
70+
4. Running ComfyUI successfully
71+
```shell
5672
# Go to ComfyUI main folder
57-
cd ../..
73+
cd ${ComfyUI}
5874
# Run the ComfyUI Lauch command
5975
python main.py --listen --port 80
76+
```
77+
78+
### Standard workflow (Recommended)
79+
80+
1. Preparing Model Weights
81+
82+
Use the command below to download the file to the specified folder. If already downloaded, create a symbolic link to that folder.
83+
```shell
84+
# clip encoder
85+
wget -O ${ComfyUI}/models/clip/pytorch_model.bin https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/blob/main/t2i/clip_text_encoder/pytorch_model.bin
86+
# mt5
87+
mkdir ${ComfyUI}/models/t5
88+
wget -O ${ComfyUI}/models/t5/pytorch_model.bin https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/blob/main/t2i/mt5/pytorch_model.bin
89+
# vae
90+
wget -O ${ComfyUI}/models/vae/diffusion_pytorch_model.bin https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/blob/main/t2i/sdxl-vae-fp16-fix/diffusion_pytorch_model.bin
91+
# base model
92+
wget -O ${ComfyUI}/models/checkpoints/pytorch_model_ema.pt https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/blob/main/t2i/model/pytorch_model_ema.pt
93+
```
94+
Put module weights trained through Kohya or the official script in `${ComfyUI}/models/checkpoints/` to switch model weights in ComfyUI.
95+
96+
2. Preparing LoRa Weights
6097

61-
# Running ComfyUI successfully!
98+
```shell
99+
# Put LoRa weights trained by Kohya in ComfyUI/models/loras
100+
cp ${HunyuanDiT}/kohya_ss/outputs/last-step{xxxx}.safetensors ${ComfyUI}/models/loras
101+
102+
# (Optional) Put LoRa weights trained by official scripts in ComfyUI/models/loras
103+
python convert_hunyuan_to_coimfyui_lora.py \
104+
--lora_path ${HunyuanDiT}/log_EXP/001-lora_porcelain_ema_rank64/checkpoints/0000100.pt/adapter_model.safetensors \
105+
--save_lora_path ${ComfyUI}/models/loras/adapter_model_convert.safetensors
106+
107+
# update the `lora.py` file
108+
cp ${ComfyUI}/custom_nodes/comfyui-hydit/lora.py ${ComfyUI}/comfy/lora.py
62109
```
63110

64-
## Download weights for diffusers mode
111+
### Diffusers Wrapper
112+
1. Preparing Model Weights
65113

66114
```shell
67115
python -m pip install "huggingface_hub[cli]"
68116
mkdir models/hunyuan
69-
huggingface-cli download Tencent-Hunyuan/HunyuanDiT-v1.1 --local-dir ./models/hunyuan/ckpts
70-
huggingface-cli download Tencent-Hunyuan/Distillation-v1.1 pytorch_model_distill.pt --local-dir ./models/hunyuan/ckpts/t2i/model
117+
huggingface-cli download Tencent-Hunyuan/HunyuanDiT-v1.2 --local-dir ./models/hunyuan/ckpts
118+
huggingface-cli download Tencent-Hunyuan/HunyuanDiT-v1.2 t2i/model/pytorch_model_ema.pt --local-dir ./models/hunyuan/ckpts/t2i/model
71119
```
72120

73-
## Download weights for ksampler mode
74-
Download the [clip encoder](https://huggingface.co/Tencent-Hunyuan/HunyuanDiT/blob/main/t2i/clip_text_encoder/pytorch_model.bin) and place it in `ComfyUI/models/clip`
75-
Download the [mt5](https://huggingface.co/Tencent-Hunyuan/HunyuanDiT/blob/main/t2i/mt5/pytorch_model.bin) and place it in `ComfyUI/models/t5`
76-
Download the [base model](https://huggingface.co/Tencent-Hunyuan/HunyuanDiT/blob/main/t2i/model/pytorch_model_ema.pt) and place it in `ComfyUI/models/checkpoints`
77-
Download the [sdxl vae](https://huggingface.co/Tencent-Hunyuan/HunyuanDiT/blob/main/t2i/sdxl-vae-fp16-fix/diffusion_pytorch_model.bin) and place it in `ComfyUI/models/vae`
121+
2. Preparing LoRa Weights
78122

123+
```shell
124+
# Put LoRa weights trained by Kohya in ComfyUI/models/loras
125+
cp ${HunyuanDiT}/kohya_ss/outputs/adapter_model.safetensors ${ComfyUI}/models/loras
126+
127+
# (Optional) Put LoRa weights trained by official scripts in ComfyUI/models/loras
128+
# The PEFT diffuser format needs to be converted into the standard ComfyUI format
129+
python convert_hunyuan_to_coimfyui_lora.py \
130+
--lora_path ${HunyuanDiT}/log_EXP/001-lora_porcelain_ema_rank64/checkpoints/0000100.pt/adapter_model.safetensors \
131+
--save_lora_path ${ComfyUI}/models/loras/adapter_model_convert.safetensors
132+
133+
# update the `lora.py` file
134+
cp ${ComfyUI}/custom_nodes/comfyui-hydit/lora.py ${ComfyUI}/comfy/lora.py
135+
```
79136

80137
## Custom Node
81138
Below I'm trying to document all the nodes, thanks for some good work[[1]](#1)[[2]](#2).
82-
#### HunYuan Pipeline Loader
139+
### HunYuan Pipeline Loader
83140
- Loads the full stack of models needed for HunYuanDiT.
84141
- **pipeline_folder_name** is the official weight folder path for hunyuan dit including clip_text_encoder, model, mt5, sdxl-vae-fp16-fix and tokenizer.
85142
- **lora** optional to load lora weight.
86143

87-
#### HunYuan Checkpoint Loader
144+
### HunYuan Checkpoint Loader
88145
- Loads the base model for HunYuanDiT in ksampler backend.
89146
- **model_name** is the weight list of comfyui checkpoint folder.
147+
- **version** two option, v1.1 and v1.2.
90148

91149

92-
#### HunYuan CLIP Loader
150+
### HunYuan CLIP Loader
93151
- Loads the clip and mt5 model for HunYuanDiT in ksampler backend.
94152
- **text_encoder_path** is the weight list of comfyui clip model folder.
95153
- **t5_text_encoder_path** is the weight list of comfyui t5 model folder.
96154

97-
#### HunYuan VAE Loader
155+
### HunYuan VAE Loader
98156
- Loads the vae model for HunYuanDiT in ksampler backend.
99157
- **model_name** is the weight list of comfyui vae model folder.
100158

101-
#### HunYuan Scheduler Loader
159+
### HunYuan Scheduler Loader
102160
- Loads the scheduler algorithm for HunYuanDiT.
103161
- **Input** is the algorithm name including ddpm, ddim and dpmms.
104162
- **Output** is the instance of diffusers.schedulers.
105163

106-
#### HunYuan Model Makeup
164+
### HunYuan Model Makeup
107165
- Assemble the models and scheduler module.
108166
- **Input** is the instance of StableDiffusionPipeline and diffusers.schedulers.
109167
- **Output** is the updated instance of StableDiffusionPipeline.
110168

111-
#### HunYuan Clip Text Encode
169+
### HunYuan Clip Text Encode
112170
- Assemble the models and scheduler module.
113171
- **Input** is the string of positive and negative prompts.
114172
- **Output** is the converted string for model.
115173

116-
#### HunYuan Sampler
174+
### HunYuan Sampler
117175
- Similar with KSampler in ComfyUI.
118176
- **Input** is the instance of StableDiffusionPipeline and some hyper-parameters for sampling.
119177
- **Output** is the generated image.
120178

121-
#### HunYuan Lora Loader
179+
### HunYuan Lora Loader
122180
- Loads the lora model for HunYuanDiT in diffusers backend.
123181
- **lora_name** is the weight list of comfyui lora folder.
124182

125-
#### HunYuan ControNet Loader
183+
### HunYuan ControNet Loader
126184
- Loads the controlnet model for HunYuanDiT in diffusers backend.
127185
- **controlnet_path** is the weight list of comfyui controlnet folder.
128186

comfyui-hydit/clip.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import comfy.model_patcher
44
import comfy.model_base
55
import comfy.utils
6-
from .hydit.modules.text_encoder import MT5Embedder
6+
from .hydit_v1_1.modules.text_encoder import MT5Embedder
77
from transformers import BertModel, BertTokenizer, AutoTokenizer
88
import torch
99
import os
1010
from transformers import T5Config, T5EncoderModel, BertConfig, BertModel
1111
from transformers import AutoTokenizer, modeling_utils
12+
from comfy import model_management
1213
#import pdb
1314

1415
class CLIP:
15-
def __init__(self, root, text_encoder_path = None, t5_text_encoder_path = None):
16+
def __init__(self,no_init=False, root = None, text_encoder_path = None, t5_text_encoder_path = None):
17+
if no_init:
18+
return
19+
1620
self.device = "cuda" if torch.cuda.is_available() else "cpu"
1721
if text_encoder_path == None:
1822
text_encoder_path = os.path.join(root,"clip_text_encoder")
@@ -53,7 +57,10 @@ def __init__(self, root, text_encoder_path = None, t5_text_encoder_path = None):
5357

5458

5559
self.cond_stage_model = clip_text_encoder
56-
60+
load_device = model_management.text_encoder_device()
61+
offload_device = model_management.text_encoder_offload_device()
62+
self.patcher = comfy.model_patcher.ModelPatcher(self.cond_stage_model, load_device=load_device, offload_device=offload_device)
63+
self.layer_idx = None
5764

5865
def tokenize(self, text):
5966
tokens = self.tokenizer.tokenize(text)
@@ -64,7 +71,9 @@ def tokenize(self, text):
6471
def tokenize_t5(self, text):
6572
return self.tokenizer_t5.tokenize(text)
6673

67-
def encode_from_tokens(self, tokens, return_pooled=False):
74+
def encode_from_tokens(self, tokens, return_pooled=False, return_dict=False):
75+
#self.cond_stage_model.reset_clip_options()
76+
self.load_model()
6877
attention_mask = tokens['attention_mask'].to(self.device)
6978
with torch.no_grad():
7079
prompt_embeds = self.cond_stage_model(
@@ -83,14 +92,44 @@ def encode_from_tokens(self, tokens, return_pooled=False):
8392
addit_embeds = {
8493
"t5_embeds": t5_embeds,
8594
"attention_mask": attention_mask.float(),
86-
"t5_attention_mask": t5_attention_mask.float()
95+
"t5_attention_mask": t5_attention_mask.float()
8796
}
8897
prompt_embeds.addit_embeds = addit_embeds
8998

9099
if return_pooled:
91100
return prompt_embeds, None
92101
else:
93102
return prompt_embeds
103+
104+
def clone(self):
105+
n = CLIP(no_init=True)
106+
n.patcher = self.patcher.clone()
107+
n.cond_stage_model = self.cond_stage_model
108+
n.tokenizer = self.tokenizer
109+
n.layer_idx = self.layer_idx
110+
n.tokenizer_t5 = self.tokenizer_t5
111+
n.embedder_t5 = self.embedder_t5
112+
n.device = self.device
113+
return n
114+
115+
def add_patches(self, patches, strength_patch=1.0, strength_model=1.0):
116+
#import pdb
117+
#pdb.set_trace()
118+
return self.patcher.add_patches(patches, strength_patch, strength_model)
119+
120+
def load_sd(self, sd):
121+
return self.cond_stage_model.load_sd(sd)
122+
123+
def get_sd(self):
124+
return self.cond_stage_model.state_dict()
125+
126+
def load_model(self):
127+
#if self.load_device != "cpu":
128+
model_management.load_model_gpu(self.patcher)
129+
return self.patcher
130+
131+
def get_key_patches(self):
132+
return self.patcher.get_key_patches()
94133

95134
class HyBertTokenizer:
96135
def __init__(self, tokenizer_path=None, max_length=77, truncation=True, return_attention_mask=True, device='cpu'):

0 commit comments

Comments
 (0)