-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.py
216 lines (197 loc) · 6.62 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
204
205
206
207
208
209
210
211
212
213
214
215
216
import argparse
import os
import torch
from tgate import TgateSDXLLoader, TgateSDXLDeepCacheLoader, TgatePixArtAlphaLoader, TgatePixArtSigmaLoader, TgateSVDLoader
from diffusers import StableDiffusionXLPipeline, PixArtAlphaPipeline, PixArtSigmaPipeline, StableVideoDiffusionPipeline
from diffusers import UNet2DConditionModel, LCMScheduler
from diffusers import DPMSolverMultistepScheduler
from diffusers.utils import load_image, export_to_video
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of TGATE V2.")
parser.add_argument(
"--prompt",
type=str,
default=None,
help="the input prompts",
)
parser.add_argument(
"--image",
type=str,
default=None,
help="the dir of input image to generate video",
)
parser.add_argument(
"--saved_path",
type=str,
default=None,
required=True,
help="Path to save the generated results.",
)
parser.add_argument(
"--model",
type=str,
default='pixart',
help="[pixart_alpha,pixart_sigma,sdxl,lcm_sdxl,lcm_pixart_alpha,svd]",
)
parser.add_argument(
"--gate_step",
type=int,
default=10,
help="When re-using the cross-attention",
)
parser.add_argument(
'--sp_interval',
type=int,
default=5,
help="The time-step interval to cache self attention before gate_step (Semantics-Planning Phase).",
)
parser.add_argument(
'--fi_interval',
type=int,
default=1,
help="The time-step interval to cache self attention after gate_step (Fidelity-Improving Phase).",
)
parser.add_argument(
'--warm_up',
type=int,
default=2,
help="The time step to warm up the model inference",
)
parser.add_argument(
"--inference_step",
type=int,
default=25,
help="total inference steps",
)
parser.add_argument(
'--deepcache',
action='store_true',
default=False,
help='do deep cache',
)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
os.makedirs(args.saved_path, exist_ok=True)
if args.prompt:
saved_path = os.path.join(args.saved_path, 'test.png')
elif args.image:
saved_path = os.path.join(args.saved_path, 'test.mp4')
if args.model == 'sdxl':
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
)
if args.deepcache:
pipe = TgateSDXLDeepCacheLoader(
pipe,
cache_interval=3,
cache_branch_id=0
)
else:
pipe = TgateSDXLLoader(pipe)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
image = pipe.tgate(
prompt=args.prompt,
gate_step=args.gate_step,
sp_interval=args.sp_interval if not args.deepcache else 1,
fi_interval=args.fi_interval,
warm_up=args.warm_up if not args.deepcache else 0,
num_inference_steps=args.inference_step,
).images[0]
image.save(saved_path)
elif args.model == 'lcm_sdxl':
unet = UNet2DConditionModel.from_pretrained(
"latent-consistency/lcm-sdxl",
torch_dtype=torch.float16,
variant="fp16",
)
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
unet=unet,
torch_dtype=torch.float16,
variant="fp16",
)
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
pipe = TgateSDXLLoader(pipe).to("cuda")
image = pipe.tgate(
prompt=args.prompt,
gate_step=args.gate_step,
sp_interval=1,
fi_interval=args.fi_interval,
warm_up=0,
num_inference_steps=args.inference_step,
lcm=True,
).images[0]
image.save(saved_path)
elif args.model == 'pixart_alpha':
pipe = PixArtAlphaPipeline.from_pretrained(
"PixArt-alpha/PixArt-XL-2-1024-MS",
torch_dtype=torch.float16,
)
pipe = TgatePixArtAlphaLoader(pipe).to("cuda")
image = pipe.tgate(
prompt=args.prompt,
gate_step=args.gate_step,
sp_interval=args.sp_interval,
fi_interval=args.fi_interval,
warm_up=args.warm_up,
num_inference_steps=args.inference_step,
).images[0]
image.save(saved_path)
elif args.model == 'lcm_pixart':
pipe = PixArtAlphaPipeline.from_pretrained(
"PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
torch_dtype=torch.float16,
)
pipe = TgatePixArtAlphaLoader(pipe).to("cuda")
image = pipe.tgate(
args.prompt,
gate_step=args.gate_step,
sp_interval=1,
fi_interval=args.fi_interval,
warm_up=0,
num_inference_steps=args.inference_step,
lcm=True,
guidance_scale=0.,
).images[0]
image.save(saved_path)
elif args.model == 'pixart_sigma':
pipe = PixArtSigmaPipeline.from_pretrained(
"PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
torch_dtype=torch.float16,
)
pipe = TgatePixArtSigmaLoader(pipe).to("cuda")
image = pipe.tgate(
prompt=args.prompt,
gate_step=args.gate_step,
sp_interval=args.sp_interval,
fi_interval=args.fi_interval,
warm_up=args.warm_up,
num_inference_steps=args.inference_step,
).images[0]
image.save(saved_path)
elif args.model == 'svd':
pipe = StableVideoDiffusionPipeline.from_pretrained(
"stabilityai/stable-video-diffusion-img2vid-xt",
torch_dtype=torch.float16,
variant="fp16",
)
pipe = TgateSVDLoader(pipe).to("cuda")
image = load_image(args.image)
frames = pipe.tgate(
image,
gate_step=args.gate_step,
num_inference_steps=args.inference_step,
warm_up=args.warm_up,
sp_interval=args.sp_interval,
fi_interval=args.fi_interval,
num_frames=25,
decode_chunk_size=8,
).frames[0]
export_to_video(frames, saved_path, fps=7)
else:
raise Exception('Please sepcify the model name!')