-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.py
203 lines (176 loc) · 7.74 KB
/
main.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
import argparse
import time
import torch
from torchvision.utils import save_image
from DeepCache import DeepCacheSDHelper
def set_random_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_type", type=str, default = "sd1.5")
parser.add_argument("--prompt", type=str, default='a photo of an astronaut on a moon')
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--cache_interval", type=int, default=3)
parser.add_argument("--cache_branch_id", type=int, default=0)
args = parser.parse_args()
if args.model_type.lower() == 'sdxl':
from diffusers import StableDiffusionXLPipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
'stabilityai/stable-diffusion-xl-base-1.0', torch_dtype=torch.float16, variant="fp16", use_safetensors=True
).to("cuda:0")
elif args.model_type.lower() == 'sd1.5':
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained(
'runwayml/stable-diffusion-v1-5', torch_dtype=torch.float16
).to("cuda:0")
elif args.model_type.lower() == 'sd2.1':
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained(
'stabilityai/stable-diffusion-2-1-base', torch_dtype=torch.float16
).to("cuda:0")
elif args.model_type.lower() == 'svd':
from diffusers import StableVideoDiffusionPipeline
pipe = StableVideoDiffusionPipeline.from_pretrained(
"stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
)
pipe.enable_model_cpu_offload()
elif args.model_type.lower() == 'sd-inpaint':
from diffusers import StableDiffusionInpaintPipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained(
'runwayml/stable-diffusion-inpainting', torch_dtype=torch.float16
).to("cuda:0")
elif args.model_type.lower() == 'sdxl-inpaint':
from diffusers import StableDiffusionXLInpaintPipeline
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
'diffusers/stable-diffusion-xl-1.0-inpainting-0.1', torch_dtype=torch.float16
).to("cuda:0")
elif args.model_type.lower() == 'sd-img2img':
from diffusers import StableDiffusionImg2ImgPipeline
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipe.enable_model_cpu_offload()
else:
raise NotImplementedError
prompt = args.prompt
seed = args.seed
if args.model_type.lower() == 'svd':
import time
from diffusers.utils import load_image, export_to_video
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png?download=true")
print("Running Original Pipeline...")
set_random_seed(42)
start_time = time.time()
frames = pipe(
image,
decode_chunk_size=8,
).frames[0]
origin_time = time.time() - start_time
export_to_video(frames, "{}_origin.mp4".format('rocket'), fps=7)
print("Enable DeepCache...")
helper = DeepCacheSDHelper(pipe=pipe)
helper.set_params(
cache_interval=args.cache_interval,
cache_branch_id=args.cache_branch_id,
)
helper.enable()
print("Running Pipeline with DeepCache...")
set_random_seed(42)
start_time = time.time()
frames = pipe(
image,
decode_chunk_size=8,
).frames[0]
deepcache_time = time.time() - start_time
export_to_video(frames, "{}_deepcache.mp4".format('rocket'), fps=7)
helper.disable()
elif 'inpaint' in args.model_type.lower():
from diffusers.utils import load_image
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
image = load_image(img_url)
mask_image = load_image(mask_url)
prompt = "a tiger sitting on a park bench"
# warmup
_ = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
set_random_seed(seed)
start_time = time.time()
image = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]
origin_time = time.time() - start_time
image.save("inpaint_origin.png")
print("Enable DeepCache...")
helper = DeepCacheSDHelper(pipe=pipe)
helper.set_params(
cache_interval=args.cache_interval,
cache_branch_id=args.cache_branch_id,
)
helper.enable()
print("Running Pipeline with DeepCache...")
start_time = time.time()
set_random_seed(seed)
deepcache_image= pipe(
prompt=prompt,image=image, mask_image=mask_image
).images[0]
deepcache_time = time.time() - start_time
deepcache_image.save("inpaint_deepcache.png")
elif args.model_type.lower() == 'sd-img2img':
from diffusers.utils import make_image_grid, load_image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
init_image.save("img2img_init.png")
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# Warmup
image = pipe(prompt, image=init_image).images[0]
set_random_seed(seed)
start_time = time.time()
image = pipe(prompt, image=init_image).images[0]
origin_time = time.time() - start_time
image.save("img2img_ori.png")
print("Enable DeepCache...")
helper = DeepCacheSDHelper(pipe=pipe)
start_time = time.time()
helper.set_params(
cache_interval=args.cache_interval,
cache_branch_id=args.cache_branch_id,
)
helper.enable()
print("Running Pipeline with DeepCache...")
set_random_seed(seed)
start_time = time.time()
deepcache_img = pipe(prompt, image=init_image).images[0]
deepcache_time = time.time() - start_time
deepcache_img.save("img2img_deepcache.png")
else:
import time
print("Warmup GPU...")
for _ in range(1):
set_random_seed(seed)
_ = pipe(prompt)
print("Running Original Pipeline...")
set_random_seed(seed)
start_time = time.time()
pipeline_output = pipe(
prompt,
output_type='pt'
).images[0]
origin_time = time.time() - start_time
save_image([pipeline_output], 'text2img_origin.png')
print("Enable DeepCache...")
helper = DeepCacheSDHelper(pipe=pipe)
start_time = time.time()
helper.set_params(
cache_interval=args.cache_interval,
cache_branch_id=args.cache_branch_id,
)
helper.enable()
print("Running Pipeline with DeepCache...")
set_random_seed(seed)
deepcache_pipeline_output = pipe(
prompt,
output_type='pt'
).images[0]
deepcache_time = time.time() - start_time
save_image([deepcache_pipeline_output], 'text2img_deepcache.png')
helper.disable()
print("Done! Original Pipeline: {:.2f} seconds, DeepCache: {:.2f} seconds. Speedup Ratio = {:.2f}".format(origin_time, deepcache_time, origin_time/deepcache_time))