-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsdxl_pipeline.py
More file actions
209 lines (173 loc) · 8.03 KB
/
sdxl_pipeline.py
File metadata and controls
209 lines (173 loc) · 8.03 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
import os
import time
import random
import argparse
from copy import deepcopy
import cv2
import numpy as np
from PIL import Image
import torch
import torch.distributed as dist
from lora_utils import patch_on_lora
from baselines.utils import process_prompt, read_prompts
from diffusers import ControlNetModel, AutoencoderKL, StableDiffusionXLControlNetPipelineKatz
import warnings
warnings.filterwarnings("ignore")
seed = 0
sd_generator = torch.manual_seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
num_gpus = torch.cuda.device_count()
print(f"Number of GPUs: {num_gpus}")
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
print(f"Available GPU {i}: {torch.cuda.get_device_properties(i)}")
# use generator to make the sampling deterministic
seed = 0
sd_generator = torch.manual_seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
parser = argparse.ArgumentParser()
parser.add_argument("--num-prompts", type=int, default=-1, help="Number of prompts to generate. -1 means all prompts")
parser.add_argument("--ref-image-path", type=str, default="/project/infattllm/slida/katz-ae-images/images_sdxl_t2i", help="the path to the reference image folder")
parser.add_argument("--output-image-path", type=str, default=None, help="the path to the output image folder")
parser.add_argument("--serve_mode", type=str, default="standard", choices=["standard", "zmq", "nvlink", "without"], help="which serve mode to use") # deprecated
# distributed
parser.add_argument("--world-size", type=int, default=2, help="world-size")
parser.add_argument("--gpu-id", type=int, default=0, help="which gpu to use")
parser.add_argument("--rank", type=int, default=0)
# ControlNet
parser.add_argument("--controlnet-parallel", action="store_true")
parser.add_argument("--controlnet-parallel-rank", type=int, nargs='+', default=[]) # Example: [2, 4, 6]
parser.add_argument("--num-controlnets", type=int, default=0)
# LoRA
parser.add_argument("--lora-mode", type=str, default="without", choices=["without", "full", "sync"])
parser.add_argument("--load-lora-mode", type=str, default="default", choices=["default", "async"])
parser.add_argument("--max-lora-num", type=int, default=0, choices=[0, 1, 2])
# Base Model
parser.add_argument("--latent-parallel", action="store_true")
# others
parser.add_argument("--verbose", "-v", action="store_true")
serve_args = parser.parse_args()
print(f"Args: {serve_args}")
if serve_args.latent_parallel or serve_args.controlnet_parallel:
assert serve_args.rank == 0, "This script should be run on GPU 0"
if serve_args.controlnet_parallel:
assert len(serve_args.controlnet_parallel_rank) == serve_args.num_controlnets, "controlnet_parallel_rank must have the same length as num_controlnets"
torch.cuda.set_device(serve_args.gpu_id)
dist.init_process_group(backend='nccl', init_method='tcp://127.0.0.1:8000', rank=serve_args.rank, world_size=serve_args.world_size)
print(f"World size: {torch.distributed.get_world_size()}, rank: {torch.distributed.get_rank()}")
enable_channels_last = os.getenv("ENABLE_CHANNELS_LAST", "0") == "1"
controlnet = ControlNetModel.from_pretrained(
"diffusers/controlnet-depth-sdxl-1.0",
variant="fp16",
use_safetensors=True,
torch_dtype=torch.float16,
)
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = StableDiffusionXLControlNetPipelineKatz.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
controlnet=controlnet,
vae=vae,
torch_dtype=torch.float16,
serve_args=serve_args,
)
pipe = pipe.to("cuda")
if enable_channels_last:
pipe.to(memory_format=torch.channels_last)
# copy state_dict to restore later
unet_state_dict_copy = deepcopy(pipe.unet.state_dict())
text_encoder_state_dict_copy = deepcopy(pipe.text_encoder.state_dict())
text_encoder_2_state_dict_copy = deepcopy(pipe.text_encoder_2.state_dict())
unet_state_dict_file = "default_unet_state_dict.pt"
if enable_channels_last:
unet_state_dict_file = "default_unet_state_dict_channels_last.pt"
default_unet_state_dict = torch.load(unet_state_dict_file)
for key in default_unet_state_dict:
default_unet_state_dict[key] = default_unet_state_dict[key].to("cuda")
print("default_unet_state_dict length", len(default_unet_state_dict.keys()))
pipe.unet.load_state_dict(deepcopy(default_unet_state_dict), strict=False)
ref_image_folder = serve_args.ref_image_path
assert os.path.isdir(ref_image_folder), "ref image folder not exists"
num_prompts = serve_args.num_prompts
prompts = read_prompts(num_prompts=num_prompts)
num_prompts = len(prompts)
prompt_prefix = ""
if serve_args.max_lora_num == 1:
prompt_prefix = "papercut -subject/scene-"
elif serve_args.max_lora_num == 2:
prompt_prefix = "by william eggleston, "
prompt_suffix = ", 4k, clean background"
negative_prompt = "low quality, bad quality, sketches, numbers, letters"
# output_image_folder = "./output_images"
output_image_folder = serve_args.output_image_path
if output_image_folder is not None:
if not os.path.exists(output_image_folder):
os.makedirs(output_image_folder)
for prompt_id, prompt in enumerate(prompts):
if prompt_id == 1:
e2e_serving_start = time.time()
unet_state_dict_copy = deepcopy(default_unet_state_dict)
# Process prompt
prompt = process_prompt(prompt_prefix, prompt, prompt_suffix)
ref_image_path = f"{ref_image_folder}/image_{prompt_id}_depth.png"
print(f"Prompt: {prompt}")
print(f"Reference image: {ref_image_path}")
ref_image = cv2.imread(ref_image_path)
ref_image = cv2.cvtColor(ref_image, cv2.COLOR_BGR2GRAY)
ref_image = ref_image[:, :, None]
ref_image = np.concatenate([ref_image, ref_image, ref_image], axis=2)
ref_image = Image.fromarray(ref_image)
inference_start = time.time()
# Load LoRAs
load_lora_start = time.time()
lora_model_repos = []
if serve_args.max_lora_num == 1:
lora_model_repos = ["TheLastBen/Papercut_SDXL"]
elif serve_args.max_lora_num == 2:
lora_model_repos = ["TheLastBen/William_Eggleston_Style_SDXL", "TheLastBen/Filmic"]
if serve_args.lora_mode == "full":
patch_on_lora(pipe, lora_model_repos)
load_lora_end = time.time()
images = pipe(
prompt, negative_prompt=negative_prompt,
image=ref_image,
controlnet_conditioning_scale=0.5, # recommended for good generalization
generater=sd_generator,
num_inference_steps=50,
lora_model_repos=lora_model_repos,
default_unet_state_dict=unet_state_dict_copy,
).images
inference_end = time.time()
print("Load LoRA latency: {:.2f}".format(load_lora_end - load_lora_start))
print("End2End inference latency: {:.2f}".format(inference_end - inference_start))
print("==============================")
# Save images
if output_image_folder is not None:
output_image_path = f"{output_image_folder}/image_{prompt_id}.png"
images[0].save(output_image_path)
# Unload LoRAs
if serve_args.load_lora_mode == "default":
pipe.unfuse_lora()
pipe.unload_lora_weights()
elif serve_args.load_lora_mode == "async":
# restore parameters and shm
pipe.clear_shm()
# pipe.unet.load_state_dict(unet_state_dict_copy)
pipe.unet.load_state_dict(deepcopy(default_unet_state_dict), strict=False)
if serve_args.max_lora_num == 2:
pipe.text_encoder.load_state_dict(text_encoder_state_dict_copy)
pipe.text_encoder_2.load_state_dict(text_encoder_2_state_dict_copy)
e2e_serving_end = time.time()
print(f"End2End inference latency: {e2e_serving_end - e2e_serving_start:.2f}")
print(f"Number of prompts: {num_prompts}")
print(f"End2End Throughput: {(e2e_serving_end - e2e_serving_start)/num_prompts:.2f}")
# stop all lora loaders
if serve_args.load_lora_mode == "async":
for i in range(serve_args.max_lora_num):
pipe.shm_dict["start_loading_flag_np"][i] = 100
print("Shut down all LoRA loaders", pipe.shm_dict["start_loading_flag_np"])