-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtext_to_image_sdxl_reuse_pipe.py
187 lines (164 loc) · 5.32 KB
/
text_to_image_sdxl_reuse_pipe.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
import argparse
import os
import torch
from diffusers import StableDiffusionXLPipeline
from onediff.infer_compiler import oneflow_compile
# import diffusers
# diffusers.logging.set_verbosity_info()
parser = argparse.ArgumentParser()
parser.add_argument(
"--base", type=str, default="stabilityai/stable-diffusion-xl-base-1.0"
)
parser.add_argument(
"--new_base",
type=str,
default="dataautogpt3/OpenDalleV1.1",
)
parser.add_argument("--variant", type=str, default="fp16")
parser.add_argument(
"--prompt",
type=str,
default="street style, detailed, raw photo, woman, face, shot on CineStill 800T",
)
parser.add_argument("--height", type=int, default=1024)
parser.add_argument("--width", type=int, default=1024)
parser.add_argument("--n_steps", type=int, default=30)
parser.add_argument("--guidance_scale", type=float, default=7.5)
parser.add_argument("--saved_image", type=str, required=False, default="sdxl-out.png")
parser.add_argument("--seed", type=int, default=1)
parser.add_argument(
"--compile_unet",
type=(lambda x: str(x).lower() in ["true", "1", "yes"]),
default=True,
)
parser.add_argument(
"--compile_vae",
type=(lambda x: str(x).lower() in ["true", "1", "yes"]),
default=True,
)
parser.add_argument(
"--run_multiple_resolutions",
type=(lambda x: str(x).lower() in ["true", "1", "yes"]),
default=True,
)
args = parser.parse_args()
# Normal SDXL pipeline init.
OUTPUT_TYPE = "pil"
# SDXL base: StableDiffusionXLPipeline
base = StableDiffusionXLPipeline.from_pretrained(
args.base,
torch_dtype=torch.float16,
variant=args.variant,
use_safetensors=True,
)
base.to("cuda")
# Compile unet with oneflow
if args.compile_unet:
print("Compiling unet with oneflow.")
compiled_unet = oneflow_compile(base.unet)
base.unet = compiled_unet
# Compile vae with oneflow
if args.compile_vae:
print("Compiling vae with oneflow.")
compiled_decoder = oneflow_compile(base.vae.decoder)
base.vae.decoder = compiled_decoder
# Warmup with run
# Will do compilatioin in the first run
print("Warmup with running graphs...")
torch.manual_seed(args.seed)
image = base(
prompt=args.prompt,
height=args.height,
width=args.width,
num_inference_steps=args.n_steps,
generator=torch.manual_seed(0),
output_type=OUTPUT_TYPE,
guidance_scale=args.guidance_scale,
).images
del base
torch.cuda.empty_cache()
print("loading new base")
if str(args.new_base).endswith(".safetensors"):
new_base = StableDiffusionXLPipeline.from_single_file(
args.new_base,
torch_dtype=torch.float16,
variant=args.variant,
use_safetensors=True,
)
else:
new_base = StableDiffusionXLPipeline.from_pretrained(
args.new_base,
torch_dtype=torch.float16,
variant=args.variant,
use_safetensors=True,
)
new_base.to("cuda")
print("New base running by torch backend")
torch.manual_seed(args.seed)
image = new_base(
prompt=args.prompt,
height=args.height,
width=args.width,
num_inference_steps=args.n_steps,
generator=torch.manual_seed(0),
output_type=OUTPUT_TYPE,
guidance_scale=args.guidance_scale,
).images
image[0].save(f"new_base_without_graph_h{args.height}-w{args.width}-{args.saved_image}")
image_eager = image[0]
# Update the unet and vae
# load_state_dict(state_dict, strict=True, assign=False), assign is False means copying them inplace into the module’s current parameters and buffers.
# Reference: https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.load_state_dict
print("Loading state_dict of new base into compiled graph")
compiled_unet._torch_module.load_state_dict(new_base.unet.state_dict())
compiled_decoder._torch_module.load_state_dict(new_base.vae.decoder.state_dict())
new_base.unet = compiled_unet
new_base.vae.decoder = compiled_decoder
torch.cuda.empty_cache()
# Normal SDXL run
print("Re-use the compiled graph")
torch.manual_seed(args.seed)
image = new_base(
prompt=args.prompt,
height=args.height,
width=args.width,
num_inference_steps=args.n_steps,
generator=torch.manual_seed(0),
output_type=OUTPUT_TYPE,
guidance_scale=args.guidance_scale,
).images
image[0].save(f"new_base_reuse_graph_h{args.height}-w{args.width}-{args.saved_image}")
image_graph = image[0]
import numpy as np
from skimage.metrics import structural_similarity
ssim = structural_similarity(
np.array(image_eager), np.array(image_graph), channel_axis=-1, data_range=255
)
print(f"ssim between naive torch and re-used graph is {ssim}")
# Should have no compilation for these new input shape
print("Test run with multiple resolutions...")
if args.run_multiple_resolutions:
sizes = [960, 720, 896, 768]
if "CI" in os.environ:
sizes = [360]
for h in sizes:
for w in sizes:
image = new_base(
prompt=args.prompt,
height=h,
width=w,
num_inference_steps=args.n_steps,
generator=torch.manual_seed(0),
output_type=OUTPUT_TYPE,
).images
# print("Test run with other another uncommon resolution...")
# if args.run_multiple_resolutions:
# h = 544
# w = 408
# image = base(
# prompt=args.prompt,
# height=h,
# width=w,
# num_inference_steps=args.n_steps,
# output_type=OUTPUT_TYPE,
# ).images